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.

arena.h 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright 2019 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_ARENA_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_ARENA_H_
  18. #include <memory>
  19. #include <mutex>
  20. #include <utility>
  21. #include "minddata/dataset/util/allocator.h"
  22. #include "minddata/dataset/util/memory_pool.h"
  23. #include "minddata/dataset/util/treap.h"
  24. #define ARENA_LOG_BLK_SZ (6u)
  25. #define ARENA_BLK_SZ (static_cast<uint16_t>(1u << ARENA_LOG_BLK_SZ))
  26. #define ARENA_WALL_OVERHEAD_SZ 32
  27. namespace mindspore {
  28. namespace dataset {
  29. /// This is a memory arena based on a treap data structure.
  30. /// The constructor of the Arena takes the size of the initial memory size (in MB).
  31. /// Internally we divide the memory into multiple blocks. Each block is 64 bytes.
  32. /// The treap contains all the free blocks with the relative memory address as key
  33. /// and the size of the block as priority.
  34. ///
  35. /// Initially the treap has only one root which is the whole memory piece.
  36. ///
  37. /// For memory suballocation, we pop the root node of the treap which contains the largest free block.
  38. /// We allocate what we need and return the rest back to the treap. We search for the first fit instead
  39. /// of the best fit so to give us a constant time in memory allocation.
  40. ///
  41. /// When a block of memory is freed. It is joined with the blocks before and after (if they are available) to
  42. /// form a bigger block.
  43. /// At the lowest level, we don't really care where the memory is coming from.
  44. /// This allows other class to make use of Arena method and override the origin of the
  45. /// memory, say from some unix shared memory instead.
  46. /// \note Implementation class is not thread safe. Caller needs to ensure proper serialization
  47. class ArenaImpl {
  48. public:
  49. /// Constructor
  50. /// \param ptr The start of the memory address
  51. /// \param sz Size of the memory block we manage
  52. ArenaImpl(void *ptr, size_t sz);
  53. ~ArenaImpl() { ptr_ = nullptr; }
  54. /// \brief Allocate a sub block
  55. /// \param n Size requested
  56. /// \param p pointer to where the result is stored
  57. /// \return Status object.
  58. Status Allocate(size_t n, void **p);
  59. /// \brief Enlarge or shrink a sub block
  60. /// \param old_sz Original size
  61. /// \param new_sz New size
  62. /// \return Status object
  63. Status Reallocate(void **, size_t old_sz, size_t new_sz);
  64. /// \brief Free a sub block
  65. /// \param Address of the block to be freed.
  66. void Deallocate(void *);
  67. /// \brief Calculate % free of the memory
  68. /// \return Percent free
  69. int PercentFree() const;
  70. /// \brief What is the maximum we can support in allocate.
  71. /// \return Max value
  72. uint64_t get_max_size() const { return (size_in_bytes_ - ARENA_WALL_OVERHEAD_SZ); }
  73. /// \brief Get the start of the address. Read only
  74. /// \return Start of the address block
  75. const void *get_base_addr() const { return ptr_; }
  76. static uint64_t SizeToBlk(uint64_t sz);
  77. friend std::ostream &operator<<(std::ostream &os, const ArenaImpl &s);
  78. private:
  79. size_t size_in_bytes_;
  80. Treap<uint64_t, uint64_t> tr_;
  81. void *ptr_;
  82. void *get_user_addr(void *base_addr) const { return reinterpret_cast<char *>(base_addr) + ARENA_WALL_OVERHEAD_SZ; }
  83. void *get_base_addr(void *user_addr) const { return reinterpret_cast<char *>(user_addr) - ARENA_WALL_OVERHEAD_SZ; }
  84. std::pair<std::pair<uint64_t, uint64_t>, bool> FindPrevBlk(uint64_t addr);
  85. bool BlockEnlarge(uint64_t *addr, uint64_t old_sz, uint64_t new_sz);
  86. Status FreeAndAlloc(void **pp, size_t old_sz, size_t new_sz);
  87. };
  88. /// \brief This version of Arena allocates from private memory
  89. class Arena : public MemoryPool {
  90. public:
  91. // Disable copy and assignment constructor
  92. Arena(const Arena &) = delete;
  93. Arena &operator=(const Arena &) = delete;
  94. ~Arena() override {
  95. if (ptr_ != nullptr) {
  96. free(ptr_);
  97. }
  98. ptr_ = nullptr;
  99. }
  100. /// As a derived class of MemoryPool, we have to implement the following.
  101. /// But we simply transfer the call to the implementation class
  102. Status Allocate(size_t size, void **pVoid) override {
  103. std::unique_lock<std::mutex> lock(mux_);
  104. return impl_->Allocate(size, pVoid);
  105. }
  106. Status Reallocate(void **pVoid, size_t old_sz, size_t new_sz) override {
  107. std::unique_lock<std::mutex> lock(mux_);
  108. return impl_->Reallocate(pVoid, old_sz, new_sz);
  109. }
  110. void Deallocate(void *pVoid) override {
  111. std::unique_lock<std::mutex> lock(mux_);
  112. impl_->Deallocate(pVoid);
  113. }
  114. uint64_t get_max_size() const override { return impl_->get_max_size(); }
  115. int PercentFree() const override {
  116. std::unique_lock<std::mutex> lock(mux_);
  117. return impl_->PercentFree();
  118. }
  119. /// \return Return the start of the memory block
  120. const void *get_base_addr() const { return impl_->get_base_addr(); }
  121. /// \brief Dump the memory allocation block.
  122. friend std::ostream &operator<<(std::ostream &os, const Arena &s) {
  123. os << *(s.impl_);
  124. return os;
  125. }
  126. /// The only method to create an arena.
  127. static Status CreateArena(std::shared_ptr<Arena> *p_ba, size_t val_in_MB = 4096);
  128. protected:
  129. mutable std::mutex mux_;
  130. std::unique_ptr<ArenaImpl> impl_;
  131. void *ptr_;
  132. size_t size_in_MB_;
  133. explicit Arena(size_t val_in_MB = 4096);
  134. Status Init();
  135. };
  136. } // namespace dataset
  137. } // namespace mindspore
  138. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_ARENA_H_