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.

circular_pool.cc 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #include "minddata/dataset/util/circular_pool.h"
  17. #include <algorithm>
  18. #include <limits>
  19. #include <utility>
  20. #include "./securec.h"
  21. #include "minddata/dataset/util/system_pool.h"
  22. #include "utils/log_adapter.h"
  23. namespace mindspore {
  24. namespace dataset {
  25. Status CircularPool::AddOneArena() {
  26. Status rc;
  27. std::shared_ptr<Arena> b;
  28. RETURN_IF_NOT_OK(Arena::CreateArena(&b, arena_size_));
  29. tail_ = b.get();
  30. cur_size_in_mb_ += arena_size_;
  31. mem_segments_.push_back(std::move(b));
  32. return Status::OK();
  33. }
  34. ListOfArenas::iterator CircularPool::CircularIterator::Next() {
  35. ListOfArenas::iterator it = dp_->mem_segments_.begin();
  36. uint32_t size = dp_->mem_segments_.size();
  37. // This is what we return
  38. it += cur_;
  39. // Prepare for the next round
  40. cur_++;
  41. if (cur_ == size) {
  42. if (start_ == 0) {
  43. has_next_ = false;
  44. } else {
  45. wrap_ = true;
  46. cur_ = 0;
  47. }
  48. } else if (cur_ == start_) {
  49. has_next_ = false;
  50. }
  51. return it;
  52. }
  53. bool CircularPool::CircularIterator::has_next() const { return has_next_; }
  54. void CircularPool::CircularIterator::Reset() {
  55. wrap_ = false;
  56. has_next_ = false;
  57. if (!dp_->mem_segments_.empty()) {
  58. // Find the buddy arena that corresponds to the tail.
  59. cur_tail_ = dp_->tail_;
  60. auto list_end = dp_->mem_segments_.end();
  61. auto it = std::find_if(dp_->mem_segments_.begin(), list_end,
  62. [this](const std::shared_ptr<Arena> &b) { return b.get() == cur_tail_; });
  63. MS_ASSERT(it != list_end);
  64. start_ = std::distance(dp_->mem_segments_.begin(), it);
  65. cur_ = start_;
  66. has_next_ = true;
  67. }
  68. }
  69. CircularPool::CircularIterator::CircularIterator(CircularPool *dp) : dp_(dp) { Reset(); }
  70. Status CircularPool::Allocate(size_t n, void **p) {
  71. if (p == nullptr) {
  72. RETURN_STATUS_UNEXPECTED("p is null");
  73. }
  74. Status rc;
  75. void *ptr = nullptr;
  76. do {
  77. SharedLock lock_s(&rw_lock_);
  78. int prevSzInMB = cur_size_in_mb_;
  79. bool move_tail = false;
  80. CircularIterator cirIt(this);
  81. while (cirIt.has_next()) {
  82. auto it = cirIt.Next();
  83. Arena *ba = it->get();
  84. if (ba->get_max_size() < n) {
  85. return Status(StatusCode::kOutOfMemory);
  86. }
  87. // If we are asked to move forward the tail
  88. if (move_tail) {
  89. Arena *expected = cirIt.cur_tail_;
  90. (void)atomic_compare_exchange_weak(&tail_, &expected, ba);
  91. move_tail = false;
  92. }
  93. rc = ba->Allocate(n, &ptr);
  94. if (rc.IsOk()) {
  95. *p = ptr;
  96. break;
  97. } else if (rc.IsOutofMemory()) {
  98. // Make the next arena a new tail and continue.
  99. move_tail = true;
  100. } else {
  101. return rc;
  102. }
  103. }
  104. // Handle the case we have done one round robin search.
  105. if (ptr == nullptr) {
  106. // If we have room to expand.
  107. if (unlimited_ || cur_size_in_mb_ < max_size_in_mb_) {
  108. // lock in exclusively mode.
  109. lock_s.Upgrade();
  110. // Check again if someone has already expanded.
  111. if (cur_size_in_mb_ == prevSzInMB) {
  112. RETURN_IF_NOT_OK(AddOneArena());
  113. }
  114. // Re-acquire the shared lock and try again
  115. lock_s.Downgrade();
  116. } else {
  117. return Status(StatusCode::kOutOfMemory, __LINE__, __FILE__);
  118. }
  119. }
  120. } while (ptr == nullptr);
  121. return rc;
  122. }
  123. void CircularPool::Deallocate(void *p) {
  124. // Lock in the chain in shared mode and find out which
  125. // segment it comes from
  126. SharedLock lock(&rw_lock_);
  127. auto it = std::find_if(mem_segments_.begin(), mem_segments_.end(), [p](std::shared_ptr<Arena> &b) -> bool {
  128. char *q = reinterpret_cast<char *>(p);
  129. char *base = const_cast<char *>(reinterpret_cast<const char *>(b->get_base_addr()));
  130. return (q > base && q < base + b->get_max_size());
  131. });
  132. lock.Unlock();
  133. it->get()->Deallocate(p);
  134. }
  135. Status CircularPool::Reallocate(void **pp, size_t old_sz, size_t new_sz) {
  136. // Lock in the chain in shared mode and find out which
  137. // segment it comes from
  138. if (pp == nullptr) {
  139. RETURN_STATUS_UNEXPECTED("pp is null");
  140. }
  141. void *p = *pp;
  142. SharedLock lock(&rw_lock_);
  143. auto it = std::find_if(mem_segments_.begin(), mem_segments_.end(), [p](std::shared_ptr<Arena> &b) -> bool {
  144. char *q = reinterpret_cast<char *>(p);
  145. char *base = const_cast<char *>(reinterpret_cast<const char *>(b->get_base_addr()));
  146. return (q > base && q < base + b->get_max_size());
  147. });
  148. lock.Unlock();
  149. MS_ASSERT(it != mem_segments_.end());
  150. Arena *ba = it->get();
  151. Status rc = ba->Reallocate(pp, old_sz, new_sz);
  152. if (rc.IsOutofMemory()) {
  153. // The current arena has no room for the bigger size.
  154. // Allocate free space from another arena and copy
  155. // the content over.
  156. void *q = nullptr;
  157. rc = this->Allocate(new_sz, &q);
  158. RETURN_IF_NOT_OK(rc);
  159. errno_t err = memcpy_s(q, new_sz, p, old_sz);
  160. if (err) {
  161. this->Deallocate(q);
  162. RETURN_STATUS_UNEXPECTED(std::to_string(err));
  163. }
  164. *pp = q;
  165. ba->Deallocate(p);
  166. }
  167. return Status::OK();
  168. }
  169. uint64_t CircularPool::get_max_size() const { return mem_segments_.front()->get_max_size(); }
  170. int CircularPool::PercentFree() const {
  171. int percent_free = 0;
  172. int num_arena = 0;
  173. for (auto const &p : mem_segments_) {
  174. percent_free += p->PercentFree();
  175. num_arena++;
  176. }
  177. if (num_arena) {
  178. return percent_free / num_arena;
  179. } else {
  180. return 100;
  181. }
  182. }
  183. CircularPool::CircularPool(int max_size_in_gb, int arena_size)
  184. : unlimited_(max_size_in_gb <= 0),
  185. max_size_in_mb_(unlimited_ ? std::numeric_limits<int32_t>::max() : max_size_in_gb * 1024),
  186. arena_size_(arena_size),
  187. cur_size_in_mb_(0) {}
  188. Status CircularPool::CreateCircularPool(std::shared_ptr<MemoryPool> *out_pool, int max_size_in_gb, int arena_size,
  189. bool createOneArena) {
  190. Status rc;
  191. if (out_pool == nullptr) {
  192. RETURN_STATUS_UNEXPECTED("pPool is null");
  193. }
  194. auto pool = new (std::nothrow) CircularPool(max_size_in_gb, arena_size);
  195. if (pool == nullptr) {
  196. return Status(StatusCode::kOutOfMemory);
  197. }
  198. if (createOneArena) {
  199. rc = pool->AddOneArena();
  200. }
  201. if (rc.IsOk()) {
  202. (*out_pool).reset(pool);
  203. } else {
  204. delete pool;
  205. }
  206. return rc;
  207. }
  208. CircularPool::~CircularPool() = default;
  209. } // namespace dataset
  210. } // namespace mindspore