|
| 1 | +/* |
| 2 | + * Copyright (c) Intel and its affiliates. |
| 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 | + |
| 17 | +namespace facebook { |
| 18 | +namespace cachelib { |
| 19 | + |
| 20 | +template <typename CacheT> |
| 21 | +BackgroundMover<CacheT>::BackgroundMover( |
| 22 | + Cache& cache, |
| 23 | + std::shared_ptr<BackgroundMoverStrategy> strategy, |
| 24 | + MoverDir direction) |
| 25 | + : cache_(cache), strategy_(strategy), direction_(direction) { |
| 26 | + if (direction_ == MoverDir::Evict) { |
| 27 | + moverFunc = BackgroundMoverAPIWrapper<CacheT>::traverseAndEvictItems; |
| 28 | + |
| 29 | + } else if (direction_ == MoverDir::Promote) { |
| 30 | + moverFunc = BackgroundMoverAPIWrapper<CacheT>::traverseAndPromoteItems; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +template <typename CacheT> |
| 35 | +BackgroundMover<CacheT>::~BackgroundMover() { |
| 36 | + stop(std::chrono::seconds(0)); |
| 37 | +} |
| 38 | + |
| 39 | +template <typename CacheT> |
| 40 | +void BackgroundMover<CacheT>::work() { |
| 41 | + try { |
| 42 | + checkAndRun(); |
| 43 | + } catch (const std::exception& ex) { |
| 44 | + XLOGF(ERR, "BackgroundMover interrupted due to exception: {}", ex.what()); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +template <typename CacheT> |
| 49 | +void BackgroundMover<CacheT>::setAssignedMemory( |
| 50 | + std::vector<MemoryDescriptorType>&& assignedMemory) { |
| 51 | + XLOG(INFO, "Class assigned to background worker:"); |
| 52 | + for (auto [tid, pid, cid] : assignedMemory) { |
| 53 | + XLOGF(INFO, "Tid: {}, Pid: {}, Cid: {}", tid, pid, cid); |
| 54 | + } |
| 55 | + |
| 56 | + mutex.lock_combine([this, &assignedMemory] { |
| 57 | + this->assignedMemory_ = std::move(assignedMemory); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +// Look for classes that exceed the target memory capacity |
| 62 | +// and return those for eviction |
| 63 | +template <typename CacheT> |
| 64 | +void BackgroundMover<CacheT>::checkAndRun() { |
| 65 | + auto assignedMemory = mutex.lock_combine([this] { return assignedMemory_; }); |
| 66 | + |
| 67 | + unsigned int moves = 0; |
| 68 | + std::set<ClassId> classes{}; |
| 69 | + auto batches = strategy_->calculateBatchSizes(cache_, assignedMemory); |
| 70 | + |
| 71 | + for (size_t i = 0; i < batches.size(); i++) { |
| 72 | + const auto [tid, pid, cid] = assignedMemory[i]; |
| 73 | + const auto batch = batches[i]; |
| 74 | + |
| 75 | + classes.insert(cid); |
| 76 | + const auto& mpStats = cache_.getPoolByTid(pid, tid).getStats(); |
| 77 | + |
| 78 | + if (!batch) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + // try moving BATCH items from the class in order to reach free target |
| 83 | + auto moved = moverFunc(cache_, tid, pid, cid, batch); |
| 84 | + moves += moved; |
| 85 | + moves_per_class_[tid][pid][cid] += moved; |
| 86 | + totalBytesMoved.add(moved * mpStats.acStats.at(cid).allocSize); |
| 87 | + } |
| 88 | + |
| 89 | + numTraversals.inc(); |
| 90 | + numMovedItems.add(moves); |
| 91 | + totalClasses.add(classes.size()); |
| 92 | +} |
| 93 | + |
| 94 | +template <typename CacheT> |
| 95 | +BackgroundMoverStats BackgroundMover<CacheT>::getStats() const noexcept { |
| 96 | + BackgroundMoverStats stats; |
| 97 | + stats.numMovedItems = numMovedItems.get(); |
| 98 | + stats.runCount = numTraversals.get(); |
| 99 | + stats.totalBytesMoved = totalBytesMoved.get(); |
| 100 | + stats.totalClasses = totalClasses.get(); |
| 101 | + |
| 102 | + return stats; |
| 103 | +} |
| 104 | + |
| 105 | +template <typename CacheT> |
| 106 | +std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>> |
| 107 | +BackgroundMover<CacheT>::getClassStats() const noexcept { |
| 108 | + return moves_per_class_; |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace cachelib |
| 112 | +} // namespace facebook |
0 commit comments