|
| 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 | +BackgroundEvictor<CacheT>::BackgroundEvictor(Cache& cache, |
| 25 | + std::shared_ptr<BackgroundEvictorStrategy> strategy, |
| 26 | + unsigned int tid) |
| 27 | + : cache_(cache), |
| 28 | + strategy_(strategy), |
| 29 | + tid_(tid) { |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +template <typename CacheT> |
| 34 | +BackgroundEvictor<CacheT>::~BackgroundEvictor() { stop(std::chrono::seconds(0)); } |
| 35 | + |
| 36 | +template <typename CacheT> |
| 37 | +void BackgroundEvictor<CacheT>::work() { |
| 38 | + try { |
| 39 | + for (const auto pid : cache_.getRegularPoolIds()) { |
| 40 | + //check if we exceed threshold (per class basis static right now) |
| 41 | + checkAndRun(pid); |
| 42 | + } |
| 43 | + } catch (const std::exception& ex) { |
| 44 | + XLOGF(ERR, "BackgroundEvictor interrupted due to exception: {}", ex.what()); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +// Look for classes that exceed the target memory capacity |
| 50 | +// and return those for eviction |
| 51 | +template <typename CacheT> |
| 52 | +void BackgroundEvictor<CacheT>::checkAndRun(PoolId pid) { |
| 53 | + |
| 54 | + const auto& mpStats = cache_.getPoolByTid(pid,tid_).getStats(); |
| 55 | + for (auto& cid : mpStats.classIds) { |
| 56 | + if (strategy_->shouldEvict(cache_,tid_,pid,cid)) { |
| 57 | + unsigned int batch = strategy_->calculateBatchSize(cache_,tid_,pid,cid); |
| 58 | + //try evicting BATCH items from the class in order to reach free target |
| 59 | + unsigned int evicted = |
| 60 | + BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems(cache_, |
| 61 | + tid_,pid,cid,batch); |
| 62 | + numEvictedItems_ += evicted; |
| 63 | + } |
| 64 | + } |
| 65 | + runCount_ = runCount_ + 1; |
| 66 | +} |
| 67 | + |
| 68 | +template <typename CacheT> |
| 69 | +BackgroundEvictorStats BackgroundEvictor<CacheT>::getStats() const noexcept { |
| 70 | + BackgroundEvictorStats stats; |
| 71 | + stats.numEvictedItems = numEvictedItems_.load(std::memory_order_relaxed); |
| 72 | + stats.numTraversals = runCount_.load(std::memory_order_relaxed); |
| 73 | + return stats; |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace cachelib |
| 77 | +} // namespace facebook |
0 commit comments