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.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "dataset/util/circular_pool.h"
  17. #include <algorithm>
  18. #include <limits>
  19. #include <utility>
  20. #include "./securec.h"
  21. #include "dataset/util/de_error.h"
  22. #include "dataset/util/system_pool.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. DS_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 we are asked to move forward the tail
  85. if (move_tail) {
  86. Arena *expected = cirIt.cur_tail_;
  87. (void)atomic_compare_exchange_weak(&tail_, &expected, ba);
  88. move_tail = false;
  89. }
  90. rc = ba->Allocate(n, &ptr);
  91. if (rc.IsOk()) {
  92. *p = ptr;
  93. break;
  94. } else if (rc.IsOutofMemory()) {
  95. // Make the next arena a new tail and continue.
  96. move_tail = true;
  97. } else {
  98. return rc;
  99. }
  100. }
  101. // Handle the case we have done one round robin search.
  102. if (ptr == nullptr) {
  103. // If we have room to expand.
  104. if (unlimited_ || cur_size_in_mb_ < max_size_in_mb_) {
  105. // lock in exclusively mode.
  106. lock_s.Upgrade();
  107. // Check again if someone has already expanded.
  108. if (cur_size_in_mb_ == prevSzInMB) {
  109. RETURN_IF_NOT_OK(AddOneArena());
  110. }
  111. // Re-acquire the shared lock and try again
  112. lock_s.Downgrade();
  113. } else {
  114. return Status(StatusCode::kOutOfMemory, __LINE__, __FILE__);
  115. }
  116. }
  117. } while (ptr == nullptr);
  118. return rc;
  119. }
  120. void CircularPool::Deallocate(void *p) {
  121. // Lock in the chain in shared mode and find out which
  122. // segment it comes from
  123. SharedLock lock(&rw_lock_);
  124. auto it = std::find_if(mem_segments_.begin(), mem_segments_.end(), [p](std::shared_ptr<Arena> &b) -> bool {
  125. char *q = reinterpret_cast<char *>(p);
  126. char *base = const_cast<char *>(reinterpret_cast<const char *>(b->get_base_addr()));
  127. return (q > base && q < base + b->get_max_size());
  128. });
  129. lock.Unlock();
  130. it->get()->Deallocate(p);
  131. }
  132. Status CircularPool::Reallocate(void **pp, size_t old_sz, size_t new_sz) {
  133. // Lock in the chain in shared mode and find out which
  134. // segment it comes from
  135. if (pp == nullptr) {
  136. RETURN_STATUS_UNEXPECTED("pp is null");
  137. }
  138. void *p = *pp;
  139. SharedLock lock(&rw_lock_);
  140. auto it = std::find_if(mem_segments_.begin(), mem_segments_.end(), [p](std::shared_ptr<Arena> &b) -> bool {
  141. char *q = reinterpret_cast<char *>(p);
  142. char *base = const_cast<char *>(reinterpret_cast<const char *>(b->get_base_addr()));
  143. return (q > base && q < base + b->get_max_size());
  144. });
  145. lock.Unlock();
  146. DS_ASSERT(it != mem_segments_.end());
  147. Arena *ba = it->get();
  148. Status rc = ba->Reallocate(pp, old_sz, new_sz);
  149. if (rc.IsOutofMemory()) {
  150. // The current arena has no room for the bigger size.
  151. // Allocate free space from another arena and copy
  152. // the content over.
  153. void *q = nullptr;
  154. rc = this->Allocate(new_sz, &q);
  155. RETURN_IF_NOT_OK(rc);
  156. errno_t err = memcpy_s(q, new_sz, p, old_sz);
  157. if (err) {
  158. this->Deallocate(q);
  159. RETURN_STATUS_UNEXPECTED(std::to_string(err));
  160. }
  161. *pp = q;
  162. ba->Deallocate(p);
  163. }
  164. return Status::OK();
  165. }
  166. uint64_t CircularPool::get_max_size() const { return mem_segments_.front()->get_max_size(); }
  167. int CircularPool::PercentFree() const {
  168. int percent_free = 0;
  169. int num_arena = 0;
  170. for (auto const &p : mem_segments_) {
  171. percent_free += p->PercentFree();
  172. num_arena++;
  173. }
  174. if (num_arena) {
  175. return percent_free / num_arena;
  176. } else {
  177. return 100;
  178. }
  179. }
  180. CircularPool::CircularPool(int max_size_in_gb, int arena_size)
  181. : unlimited_(max_size_in_gb <= 0),
  182. max_size_in_mb_(unlimited_ ? std::numeric_limits<int32_t>::max() : max_size_in_gb * 1024),
  183. arena_size_(arena_size),
  184. cur_size_in_mb_(0) {}
  185. Status CircularPool::CreateCircularPool(std::shared_ptr<MemoryPool> *out_pool, int max_size_in_gb, int arena_size,
  186. bool createOneArena) {
  187. Status rc;
  188. if (out_pool == nullptr) {
  189. RETURN_STATUS_UNEXPECTED("pPool is null");
  190. }
  191. auto pool = new (std::nothrow) CircularPool(max_size_in_gb, arena_size);
  192. if (pool == nullptr) {
  193. return Status(StatusCode::kOutOfMemory);
  194. }
  195. if (createOneArena) {
  196. rc = pool->AddOneArena();
  197. }
  198. if (rc.IsOk()) {
  199. (*out_pool).reset(pool);
  200. } else {
  201. delete pool;
  202. }
  203. return rc;
  204. }
  205. CircularPool::~CircularPool() = default;
  206. } // namespace dataset
  207. } // namespace mindspore