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