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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <algorithm>
  17. #include "common/utils.h"
  18. #include "dataset/util/cache_pool.h"
  19. #include "dataset/util/services.h"
  20. namespace mindspore {
  21. namespace dataset {
  22. CachePool::CachePool(const value_allocator &alloc, const std::string &root)
  23. : alloc_(alloc), root_(root), subfolder_(Services::GetUniqueID()), sm_(nullptr), tree_(nullptr) {}
  24. Status CachePool::DoServiceStart() {
  25. tree_ = std::make_shared<data_index>();
  26. // If we are given a disk path, set up the StorageManager
  27. if (!root_.toString().empty()) {
  28. Path spill = GetSpillPath();
  29. RETURN_IF_NOT_OK(spill.CreateDirectories());
  30. sm_ = std::make_shared<StorageManager>(spill);
  31. RETURN_IF_NOT_OK(sm_->ServiceStart());
  32. MS_LOG(INFO) << "CachePool will use disk folder: " << common::SafeCStr(spill.toString());
  33. }
  34. return Status::OK();
  35. }
  36. Status CachePool::DoServiceStop() {
  37. Status rc;
  38. Status rc2;
  39. if (sm_ != nullptr) {
  40. rc = sm_->ServiceStop();
  41. if (rc.IsError()) {
  42. rc2 = rc;
  43. }
  44. }
  45. sm_.reset();
  46. for (auto &bl : *tree_) {
  47. if (bl.ptr != nullptr) {
  48. alloc_.deallocate(bl.ptr, bl.sz);
  49. }
  50. }
  51. tree_.reset();
  52. if (!root_.toString().empty()) {
  53. Path spill = GetSpillPath();
  54. auto it = Path::DirIterator::OpenDirectory(&spill);
  55. while (it->hasNext()) {
  56. rc = it->next().Remove();
  57. if (rc.IsError() && rc2.IsOk()) {
  58. rc2 = rc;
  59. }
  60. }
  61. rc = spill.Remove();
  62. if (rc.IsError() && rc2.IsOk()) {
  63. rc2 = rc;
  64. }
  65. }
  66. return rc2;
  67. }
  68. CachePool::~CachePool() noexcept { (void)ServiceStop(); }
  69. Status CachePool::Insert(const std::vector<ReadableSlice> &buf, CachePool::key_type *key) {
  70. DataLocator bl;
  71. Status rc;
  72. size_t sz = 0;
  73. // We will consolidate all the slices into one piece.
  74. for (auto &v : buf) {
  75. sz += v.GetSize();
  76. }
  77. bl.sz = sz;
  78. try {
  79. bl.ptr = alloc_.allocate(sz);
  80. // We will do a piecewise copy.
  81. WritableSlice dest(bl.ptr, bl.sz);
  82. size_t pos = 0;
  83. for (auto &v : buf) {
  84. WritableSlice out(dest, pos);
  85. rc = WritableSlice::Copy(&out, v);
  86. if (rc.IsError()) {
  87. break;
  88. }
  89. pos += v.GetSize();
  90. }
  91. if (rc.IsError()) {
  92. alloc_.deallocate(bl.ptr, sz);
  93. bl.ptr = nullptr;
  94. return rc;
  95. }
  96. } catch (std::bad_alloc &e) {
  97. if (sm_ != nullptr) {
  98. RETURN_IF_NOT_OK(sm_->Write(&bl.storage_key, buf));
  99. // We have an assumption 0 is not a valid key from the design of AutoIndexObj.
  100. // Make sure it is not 0.
  101. if (bl.storage_key == 0) {
  102. RETURN_STATUS_UNEXPECTED("Key 0 is returned which is unexpected");
  103. }
  104. } else {
  105. return Status(StatusCode::kOutOfMemory, __LINE__, __FILE__);
  106. }
  107. }
  108. rc = tree_->insert(bl, key);
  109. if (rc.IsError() && bl.ptr != nullptr) {
  110. alloc_.deallocate(bl.ptr, sz);
  111. }
  112. return rc;
  113. }
  114. Status CachePool::Read(CachePool::key_type key, WritableSlice *dest, size_t *bytesRead) const {
  115. RETURN_UNEXPECTED_IF_NULL(dest);
  116. auto r = tree_->Search(key);
  117. if (r.second) {
  118. auto &it = r.first;
  119. if (it->ptr != nullptr) {
  120. ReadableSlice src(it->ptr, it->sz);
  121. RETURN_IF_NOT_OK(WritableSlice::Copy(dest, src));
  122. } else if (sm_ != nullptr) {
  123. size_t expectedLength = 0;
  124. RETURN_IF_NOT_OK(sm_->Read(it->storage_key, dest, &expectedLength));
  125. if (expectedLength != it->sz) {
  126. MS_LOG(ERROR) << "Unexpected length. Read " << expectedLength << ". Expected " << it->sz << "."
  127. << " Internal key: " << key << "\n";
  128. RETURN_STATUS_UNEXPECTED("Length mismatch. See log file for details.");
  129. }
  130. }
  131. if (bytesRead != nullptr) {
  132. *bytesRead = it->sz;
  133. }
  134. } else {
  135. RETURN_STATUS_UNEXPECTED("Key not found");
  136. }
  137. return Status::OK();
  138. }
  139. const CachePool::value_allocator &CachePool::get_allocator() const { return alloc_; }
  140. Path CachePool::GetSpillPath() const {
  141. auto spill = Path(root_) / subfolder_;
  142. return spill;
  143. }
  144. CachePool::CacheStat CachePool::GetStat() const {
  145. CacheStat cs{0};
  146. for (auto &it : *tree_) {
  147. if (it.ptr != nullptr) {
  148. ++cs.num_mem_cached;
  149. } else {
  150. ++cs.num_disk_cached;
  151. }
  152. }
  153. return cs;
  154. }
  155. Status CachePool::Spill(CachePool::DataLocator *dl) {
  156. if (sm_ == nullptr) {
  157. RETURN_STATUS_UNEXPECTED("No disk storage to spill");
  158. }
  159. RETURN_UNEXPECTED_IF_NULL(dl);
  160. RETURN_UNEXPECTED_IF_NULL(dl->ptr);
  161. if (dl->storage_key == 0) {
  162. ReadableSlice data(dl->ptr, dl->sz);
  163. RETURN_IF_NOT_OK(sm_->Write(&dl->storage_key, {data}));
  164. }
  165. alloc_.deallocate(dl->ptr, dl->sz);
  166. dl->ptr = nullptr;
  167. return Status::OK();
  168. }
  169. Status CachePool::Locate(CachePool::DataLocator *dl) {
  170. RETURN_UNEXPECTED_IF_NULL(dl);
  171. if (dl->ptr == nullptr) {
  172. if (sm_ == nullptr) {
  173. RETURN_STATUS_UNEXPECTED("No disk storage to locate the data");
  174. }
  175. try {
  176. dl->ptr = alloc_.allocate(dl->sz);
  177. WritableSlice dest(dl->ptr, dl->sz);
  178. Status rc = Read(dl->storage_key, &dest);
  179. if (rc.IsError()) {
  180. alloc_.deallocate(dl->ptr, dl->sz);
  181. dl->ptr = nullptr;
  182. return rc;
  183. }
  184. } catch (const std::bad_alloc &e) {
  185. return Status(StatusCode::kOutOfMemory, __LINE__, __FILE__);
  186. }
  187. }
  188. return Status::OK();
  189. }
  190. size_t CachePool::GetSize(CachePool::key_type key) const {
  191. auto r = tree_->Search(key);
  192. if (r.second) {
  193. auto &it = r.first;
  194. return it->sz;
  195. } else {
  196. return 0;
  197. }
  198. }
  199. } // namespace dataset
  200. } // namespace mindspore