You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache_pool.h 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_CACHE_POOL_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_CACHE_POOL_H_
  18. #include <memory>
  19. #include <mutex>
  20. #include <string>
  21. #include <vector>
  22. #include "minddata/dataset/util/allocator.h"
  23. #include "minddata/dataset/util/service.h"
  24. #include "minddata/dataset/util/slice.h"
  25. #include "minddata/dataset/util/storage_manager.h"
  26. #include "minddata/dataset/util/auto_index.h"
  27. namespace mindspore {
  28. namespace dataset {
  29. /// \brief A CachePool provides service for backup/restore a buffer. A buffer can be represented in a form of vector of
  30. /// ReadableSlice where all memory blocks will be copied to one contiguous block which can be in memory or spilled to
  31. /// disk (if a disk directory is provided). Every buffer insert will return a generated key which can be used to
  32. /// restore the buffer.
  33. /// \see ReadableSlice
  34. class CachePool : public Service {
  35. public:
  36. using base_type = uint8_t;
  37. using pointer = base_type *;
  38. using const_pointer = const base_type *;
  39. using reference = base_type &;
  40. using const_reference = const base_type &;
  41. using value_allocator = Allocator<base_type>;
  42. // An internal class to locate the whereabouts of a backed up buffer which can be either in
  43. class DataLocator {
  44. public:
  45. DataLocator() : ptr(nullptr), sz(0), storage_key(0) {}
  46. ~DataLocator() = default;
  47. DataLocator(const DataLocator &other) = default;
  48. DataLocator &operator=(const DataLocator &other) = default;
  49. DataLocator(DataLocator &&other) noexcept {
  50. ptr = other.ptr;
  51. sz = other.sz;
  52. storage_key = other.storage_key;
  53. other.ptr = nullptr;
  54. other.sz = 0;
  55. other.storage_key = 0;
  56. }
  57. DataLocator &operator=(DataLocator &&other) noexcept {
  58. if (&other != this) {
  59. ptr = other.ptr;
  60. sz = other.sz;
  61. storage_key = other.storage_key;
  62. other.ptr = nullptr;
  63. other.sz = 0;
  64. other.storage_key = 0;
  65. }
  66. return *this;
  67. }
  68. pointer ptr;
  69. size_t sz;
  70. StorageManager::key_type storage_key;
  71. };
  72. using data_index = AutoIndexObj<DataLocator>;
  73. using key_type = data_index::key_type;
  74. using bl_alloc_type = typename value_allocator::template rebind<DataLocator>::other;
  75. /// \brief Simple statistics returned from CachePool like how many elements are cached in memory and
  76. /// how many elements are spilled to disk.
  77. struct CacheStat {
  78. int64_t num_mem_cached;
  79. int64_t num_disk_cached;
  80. };
  81. /// \brief Constructor
  82. /// \param alloc Allocator to allocate memory from
  83. /// \param root Optional disk folder to spill
  84. explicit CachePool(const value_allocator &alloc, const std::string &root = "");
  85. CachePool(const CachePool &) = delete;
  86. CachePool(CachePool &&) = delete;
  87. CachePool &operator=(const CachePool &) = delete;
  88. CachePool &operator=(CachePool &&) = delete;
  89. ~CachePool() noexcept;
  90. Status DoServiceStart() override;
  91. Status DoServiceStop() override;
  92. Path GetSpillPath() const;
  93. /// \brief Insert a sequence of ReadableSlice objects into the pool.
  94. /// All memory blocks will be consolidated into one contiguous block and be cached in either memory or on disk.
  95. /// \param[in] buf A sequence of ReadableSlice objects.
  96. /// \param[out] key Generated key
  97. /// \return Error code
  98. Status Insert(const std::vector<ReadableSlice> &buf, key_type *key);
  99. /// \brief Restore a cached buffer (from memory or disk)
  100. /// \param[in] key A previous key returned from Insert
  101. /// \param[out] dest The cached buffer will be copied to this destination represented by a WritableSlice
  102. /// \param[out] bytesRead Optional. Number of bytes read.
  103. /// \return Error code
  104. Status Read(key_type key, WritableSlice *dest, size_t *bytesRead = nullptr) const;
  105. Status Spill(DataLocator *dl);
  106. Status Locate(DataLocator *dl);
  107. size_t GetSize(key_type key) const;
  108. /// \brief Get statistics.
  109. /// \return CacheStat object
  110. CacheStat GetStat() const;
  111. const value_allocator &get_allocator() const;
  112. std::string MyName() const { return subfolder_; }
  113. private:
  114. value_allocator alloc_;
  115. Path root_;
  116. const std::string subfolder_;
  117. std::shared_ptr<StorageManager> sm_;
  118. std::shared_ptr<data_index> tree_;
  119. };
  120. } // namespace dataset
  121. } // namespace mindspore
  122. #endif