From 2a38e80719ed082f614b84eb9f9c1e513620c28b Mon Sep 17 00:00:00 2001 From: Daniel Byrne Date: Tue, 16 Aug 2022 23:23:10 -0400 Subject: [PATCH 1/2] combined promoter and evictor classes --- ...undEvictor-inl.h => BackgroundMover-inl.h} | 66 ++++++----- ...{BackgroundEvictor.h => BackgroundMover.h} | 60 +++++----- ...orStrategy.h => BackgroundMoverStrategy.h} | 2 +- cachelib/allocator/BackgroundPromoter-inl.h | 109 ------------------ cachelib/allocator/BackgroundPromoter.h | 98 ---------------- cachelib/allocator/CacheAllocator-inl.h | 12 +- cachelib/allocator/CacheAllocator.h | 79 ++++++------- cachelib/allocator/CacheAllocatorConfig.h | 12 +- cachelib/allocator/CacheStats.h | 36 ++---- cachelib/allocator/FreeThresholdStrategy.h | 6 +- cachelib/allocator/PromotionStrategy.h | 4 +- cachelib/cachebench/cache/Cache-inl.h | 10 +- cachelib/cachebench/util/CacheConfig.cpp | 4 +- cachelib/cachebench/util/CacheConfig.h | 6 +- 14 files changed, 143 insertions(+), 361 deletions(-) rename cachelib/allocator/{BackgroundEvictor-inl.h => BackgroundMover-inl.h} (53%) rename cachelib/allocator/{BackgroundEvictor.h => BackgroundMover.h} (65%) rename cachelib/allocator/{BackgroundEvictorStrategy.h => BackgroundMoverStrategy.h} (96%) delete mode 100644 cachelib/allocator/BackgroundPromoter-inl.h delete mode 100644 cachelib/allocator/BackgroundPromoter.h diff --git a/cachelib/allocator/BackgroundEvictor-inl.h b/cachelib/allocator/BackgroundMover-inl.h similarity index 53% rename from cachelib/allocator/BackgroundEvictor-inl.h rename to cachelib/allocator/BackgroundMover-inl.h index 9cec5d393..04adf4c92 100644 --- a/cachelib/allocator/BackgroundEvictor-inl.h +++ b/cachelib/allocator/BackgroundMover-inl.h @@ -19,27 +19,37 @@ namespace cachelib { template -BackgroundEvictor::BackgroundEvictor(Cache& cache, - std::shared_ptr strategy) +BackgroundMover::BackgroundMover(Cache& cache, + std::shared_ptr strategy, + MoverDir direction) : cache_(cache), - strategy_(strategy) + strategy_(strategy), + direction_(direction) { + if (direction_ == MoverDir::Evict) { + moverFunc = + BackgroundMoverAPIWrapper::traverseAndEvictItems; + + } else if (direction_ == MoverDir::Promote) { + moverFunc = + BackgroundMoverAPIWrapper::traverseAndPromoteItems; + } } template -BackgroundEvictor::~BackgroundEvictor() { stop(std::chrono::seconds(0)); } +BackgroundMover::~BackgroundMover() { stop(std::chrono::seconds(0)); } template -void BackgroundEvictor::work() { +void BackgroundMover::work() { try { checkAndRun(); } catch (const std::exception& ex) { - XLOGF(ERR, "BackgroundEvictor interrupted due to exception: {}", ex.what()); + XLOGF(ERR, "BackgroundMover interrupted due to exception: {}", ex.what()); } } template -void BackgroundEvictor::setAssignedMemory(std::vector> &&assignedMemory) +void BackgroundMover::setAssignedMemory(std::vector> &&assignedMemory) { XLOG(INFO, "Class assigned to background worker:"); for (auto [tid, pid, cid] : assignedMemory) { @@ -54,12 +64,12 @@ void BackgroundEvictor::setAssignedMemory(std::vector -void BackgroundEvictor::checkAndRun() { +void BackgroundMover::checkAndRun() { auto assignedMemory = mutex.lock_combine([this]{ return assignedMemory_; }); - unsigned int evictions = 0; + unsigned int moves = 0; std::set classes{}; auto batches = strategy_->calculateBatchSizes(cache_,assignedMemory); @@ -74,36 +84,34 @@ void BackgroundEvictor::checkAndRun() { continue; } - stats.evictionSize.add(batch * mpStats.acStats.at(cid).allocSize); + totalBytesMoved.add(batch * mpStats.acStats.at(cid).allocSize); - //try evicting BATCH items from the class in order to reach free target - auto evicted = - BackgroundEvictorAPIWrapper::traverseAndEvictItems(cache_, - tid,pid,cid,batch); - evictions += evicted; - evictions_per_class_[tid][pid][cid] += evicted; + //try moving BATCH items from the class in order to reach free target + auto moved = moverFunc(cache_,tid,pid,cid,batch); + moves += moved; + moves_per_class_[tid][pid][cid] += moved; } - stats.numTraversals.inc(); - stats.numEvictedItems.add(evictions); - stats.totalClasses.add(classes.size()); + numTraversals.inc(); + numMovedItems.add(moves); + totalClasses.add(classes.size()); } template -BackgroundEvictionStats BackgroundEvictor::getStats() const noexcept { - BackgroundEvictionStats evicStats; - evicStats.numEvictedItems = stats.numEvictedItems.get(); - evicStats.runCount = stats.numTraversals.get(); - evicStats.evictionSize = stats.evictionSize.get(); - evicStats.totalClasses = stats.totalClasses.get(); - - return evicStats; +BackgroundMoverStats BackgroundMover::getStats() const noexcept { + BackgroundMoverStats stats; + stats.numMovedItems = numMovedItems.get(); + stats.runCount = numTraversals.get(); + stats.totalBytesMoved = totalBytesMoved.get(); + stats.totalClasses = totalClasses.get(); + + return stats; } template std::map>> -BackgroundEvictor::getClassStats() const noexcept { - return evictions_per_class_; +BackgroundMover::getClassStats() const noexcept { + return moves_per_class_; } } // namespace cachelib diff --git a/cachelib/allocator/BackgroundEvictor.h b/cachelib/allocator/BackgroundMover.h similarity index 65% rename from cachelib/allocator/BackgroundEvictor.h rename to cachelib/allocator/BackgroundMover.h index 758373212..45593b0f3 100644 --- a/cachelib/allocator/BackgroundEvictor.h +++ b/cachelib/allocator/BackgroundMover.h @@ -21,7 +21,7 @@ #include "cachelib/allocator/CacheStats.h" #include "cachelib/common/PeriodicWorker.h" -#include "cachelib/allocator/BackgroundEvictorStrategy.h" +#include "cachelib/allocator/BackgroundMoverStrategy.h" #include "cachelib/common/AtomicCounter.h" @@ -29,66 +29,68 @@ namespace facebook { namespace cachelib { // wrapper that exposes the private APIs of CacheType that are specifically -// needed for the eviction. +// needed for the cache api template -struct BackgroundEvictorAPIWrapper { +struct BackgroundMoverAPIWrapper { static size_t traverseAndEvictItems(C& cache, unsigned int tid, unsigned int pid, unsigned int cid, size_t batch) { return cache.traverseAndEvictItems(tid,pid,cid,batch); } + + static size_t traverseAndPromoteItems(C& cache, + unsigned int tid, unsigned int pid, unsigned int cid, size_t batch) { + return cache.traverseAndPromoteItems(tid,pid,cid,batch); + } + }; -struct BackgroundEvictorStats { - // items evicted - AtomicCounter numEvictedItems{0}; - - // traversals - AtomicCounter numTraversals{0}; - - // total class size - AtomicCounter totalClasses{0}; - - // item eviction size - AtomicCounter evictionSize{0}; +enum class MoverDir { + Evict = 0, + Promote }; // Periodic worker that evicts items from tiers in batches // The primary aim is to reduce insertion times for new items in the // cache template -class BackgroundEvictor : public PeriodicWorker { +class BackgroundMover : public PeriodicWorker { public: using Cache = CacheT; // @param cache the cache interface - // @param target_free the target amount of memory to keep free in - // this tier - // @param tier id memory tier to perform eviction on - BackgroundEvictor(Cache& cache, - std::shared_ptr strategy); + // @param strategy the stragey class that defines how objects are moved, + // (promoted vs. evicted and how much) + BackgroundMover(Cache& cache, + std::shared_ptr strategy, + MoverDir direction_); - ~BackgroundEvictor() override; + ~BackgroundMover() override; - BackgroundEvictionStats getStats() const noexcept; + BackgroundMoverStats getStats() const noexcept; std::map>> getClassStats() const noexcept; void setAssignedMemory(std::vector> &&assignedMemory); private: - std::map>> evictions_per_class_; - + std::map>> moves_per_class_; // cache allocator's interface for evicting - using Item = typename Cache::Item; Cache& cache_; - std::shared_ptr strategy_; + std::shared_ptr strategy_; + MoverDir direction_; + + std::function moverFunc; // implements the actual logic of running the background evictor void work() override final; void checkAndRun(); - BackgroundEvictorStats stats; + + AtomicCounter numMovedItems{0}; + AtomicCounter numTraversals{0}; + AtomicCounter totalClasses{0}; + AtomicCounter totalBytesMoved{0}; std::vector> assignedMemory_; folly::DistributedMutex mutex; @@ -96,4 +98,4 @@ class BackgroundEvictor : public PeriodicWorker { } // namespace cachelib } // namespace facebook -#include "cachelib/allocator/BackgroundEvictor-inl.h" +#include "cachelib/allocator/BackgroundMover-inl.h" diff --git a/cachelib/allocator/BackgroundEvictorStrategy.h b/cachelib/allocator/BackgroundMoverStrategy.h similarity index 96% rename from cachelib/allocator/BackgroundEvictorStrategy.h rename to cachelib/allocator/BackgroundMoverStrategy.h index 1d05a801b..9a37e8a41 100644 --- a/cachelib/allocator/BackgroundEvictorStrategy.h +++ b/cachelib/allocator/BackgroundMoverStrategy.h @@ -22,7 +22,7 @@ namespace facebook { namespace cachelib { // Base class for background eviction strategy. -class BackgroundEvictorStrategy { +class BackgroundMoverStrategy { public: virtual std::vector calculateBatchSizes(const CacheBase& cache, diff --git a/cachelib/allocator/BackgroundPromoter-inl.h b/cachelib/allocator/BackgroundPromoter-inl.h deleted file mode 100644 index daa6ae0a9..000000000 --- a/cachelib/allocator/BackgroundPromoter-inl.h +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) Intel and its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -namespace facebook { -namespace cachelib { - - -template -BackgroundPromoter::BackgroundPromoter(Cache& cache, - std::shared_ptr strategy) - : cache_(cache), - strategy_(strategy) -{ -} - -template -BackgroundPromoter::~BackgroundPromoter() { stop(std::chrono::seconds(0)); } - -template -void BackgroundPromoter::work() { - try { - checkAndRun(); - } catch (const std::exception& ex) { - XLOGF(ERR, "BackgroundPromoter interrupted due to exception: {}", ex.what()); - } -} - -template -void BackgroundPromoter::setAssignedMemory(std::vector> &&assignedMemory) -{ - XLOG(INFO, "Class assigned to background worker:"); - for (auto [tid, pid, cid] : assignedMemory) { - XLOGF(INFO, "Tid: {}, Pid: {}, Cid: {}", tid, pid, cid); - } - - mutex.lock_combine([this, &assignedMemory]{ - this->assignedMemory_ = std::move(assignedMemory); - }); -} - -// Look for classes that exceed the target memory capacity -// and return those for eviction -template -void BackgroundPromoter::checkAndRun() { - auto assignedMemory = mutex.lock_combine([this]{ - return assignedMemory_; - }); - - unsigned int promotions = 0; - std::set classes{}; - - auto batches = strategy_->calculateBatchSizes(cache_,assignedMemory); - - for (size_t i = 0; i < batches.size(); i++) { - const auto [tid, pid, cid] = assignedMemory[i]; - const auto batch = batches[i]; - - - classes.insert(cid); - const auto& mpStats = cache_.getPoolByTid(pid,tid).getStats(); - if (!batch) { - continue; - } - - // stats.promotionsize.add(batch * mpStats.acStats.at(cid).allocSize); - - //try evicting BATCH items from the class in order to reach free target - auto promoted = - BackgroundPromoterAPIWrapper::traverseAndPromoteItems(cache_, - tid,pid,cid,batch); - promotions += promoted; - promotions_per_class_[tid][pid][cid] += promoted; - } - - stats.numTraversals.inc(); - stats.numPromotedItems.add(promotions); - // stats.totalClasses.add(classes.size()); -} - -template -BackgroundPromotionStats BackgroundPromoter::getStats() const noexcept { - BackgroundPromotionStats promoStats; - promoStats.numPromotedItems = stats.numPromotedItems.get(); - promoStats.runCount = stats.numTraversals.get(); - - return promoStats; -} - -template -std::map>> -BackgroundPromoter::getClassStats() const noexcept { - return promotions_per_class_; -} - -} // namespace cachelib -} // namespace facebook diff --git a/cachelib/allocator/BackgroundPromoter.h b/cachelib/allocator/BackgroundPromoter.h deleted file mode 100644 index 04e0e7d18..000000000 --- a/cachelib/allocator/BackgroundPromoter.h +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) Intel and its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include - -#include "cachelib/allocator/CacheStats.h" -#include "cachelib/common/PeriodicWorker.h" -#include "cachelib/allocator/BackgroundEvictorStrategy.h" -#include "cachelib/common/AtomicCounter.h" - - -namespace facebook { -namespace cachelib { - -// wrapper that exposes the private APIs of CacheType that are specifically -// needed for the promotion. -template -struct BackgroundPromoterAPIWrapper { - - static size_t traverseAndPromoteItems(C& cache, - unsigned int tid, unsigned int pid, unsigned int cid, size_t batch) { - return cache.traverseAndPromoteItems(tid,pid,cid,batch); - } -}; - -struct BackgroundPromoterStats { - // items evicted - AtomicCounter numPromotedItems{0}; - - // traversals - AtomicCounter numTraversals{0}; - - // total class size - AtomicCounter totalClasses{0}; - - // item eviction size - AtomicCounter promotionSize{0}; -}; - -template -class BackgroundPromoter : public PeriodicWorker { - public: - using Cache = CacheT; - // @param cache the cache interface - // @param target_free the target amount of memory to keep free in - // this tier - // @param tier id memory tier to perform promotin from - BackgroundPromoter(Cache& cache, - std::shared_ptr strategy); - // TODO: use separate strategy for eviction and promotion - - ~BackgroundPromoter() override; - - // TODO - BackgroundPromotionStats getStats() const noexcept; - std::map>> getClassStats() const noexcept; - - void setAssignedMemory(std::vector> &&assignedMemory); - - private: - std::map>> promotions_per_class_; - - // cache allocator's interface for evicting - - using Item = typename Cache::Item; - - Cache& cache_; - std::shared_ptr strategy_; - - // implements the actual logic of running the background evictor - void work() override final; - void checkAndRun(); - - BackgroundPromoterStats stats; - - std::vector> assignedMemory_; - folly::DistributedMutex mutex; -}; -} // namespace cachelib -} // namespace facebook - -#include "cachelib/allocator/BackgroundPromoter-inl.h" diff --git a/cachelib/allocator/CacheAllocator-inl.h b/cachelib/allocator/CacheAllocator-inl.h index fa29ca47a..504e170af 100644 --- a/cachelib/allocator/CacheAllocator-inl.h +++ b/cachelib/allocator/CacheAllocator-inl.h @@ -3803,8 +3803,8 @@ GlobalCacheStats CacheAllocator::getGlobalCacheStats() const { ret.nvmCacheEnabled = nvmCache_ ? nvmCache_->isEnabled() : false; ret.nvmUpTime = currTime - getNVMCacheCreationTime(); ret.reaperStats = getReaperStats(); - ret.evictionStats = getBackgroundEvictorStats(); - ret.promotionStats = getBackgroundPromoterStats(); + ret.evictionStats = getBackgroundMoverStats(MoverDir::Evict); + ret.promotionStats = getBackgroundMoverStats(MoverDir::Promote); ret.numActiveHandles = getNumActiveHandles(); return ret; @@ -3966,14 +3966,14 @@ auto CacheAllocator::getAssignedMemoryToBgWorker(size_t evictorId, s template bool CacheAllocator::startNewBackgroundEvictor( std::chrono::milliseconds interval, - std::shared_ptr strategy, + std::shared_ptr strategy, size_t threads) { XDCHECK(threads > 0); backgroundEvictor_.resize(threads); bool result = true; for (size_t i = 0; i < threads; i++) { - auto ret = startNewWorker("BackgroundEvictor" + std::to_string(i), backgroundEvictor_[i], interval, strategy); + auto ret = startNewWorker("BackgroundEvictor" + std::to_string(i), backgroundEvictor_[i], interval, strategy, MoverDir::Evict); result = result && ret; if (result) { @@ -3986,7 +3986,7 @@ bool CacheAllocator::startNewBackgroundEvictor( template bool CacheAllocator::startNewBackgroundPromoter( std::chrono::milliseconds interval, - std::shared_ptr strategy, + std::shared_ptr strategy, size_t threads) { XDCHECK(threads > 0); XDCHECK(numTiers_ > 1); @@ -3994,7 +3994,7 @@ bool CacheAllocator::startNewBackgroundPromoter( bool result = true; for (size_t i = 0; i < threads; i++) { - auto ret = startNewWorker("BackgroundPromoter" + std::to_string(i), backgroundPromoter_[i], interval, strategy); + auto ret = startNewWorker("BackgroundPromoter" + std::to_string(i), backgroundPromoter_[i], interval, strategy, MoverDir::Promote); result = result && ret; if (result) { diff --git a/cachelib/allocator/CacheAllocator.h b/cachelib/allocator/CacheAllocator.h index 4f61d1408..4785d7dbb 100644 --- a/cachelib/allocator/CacheAllocator.h +++ b/cachelib/allocator/CacheAllocator.h @@ -36,8 +36,7 @@ #include #include #pragma GCC diagnostic pop -#include "cachelib/allocator/BackgroundEvictor.h" -#include "cachelib/allocator/BackgroundPromoter.h" +#include "cachelib/allocator/BackgroundMover.h" #include "cachelib/allocator/CCacheManager.h" #include "cachelib/allocator/Cache.h" #include "cachelib/allocator/CacheAllocatorConfig.h" @@ -949,10 +948,10 @@ class CacheAllocator : public CacheBase { bool startNewReaper(std::chrono::milliseconds interval, util::Throttler::Config reaperThrottleConfig); - bool startNewBackgroundEvictor(std::chrono::milliseconds interval, - std::shared_ptr strategy, size_t threads); bool startNewBackgroundPromoter(std::chrono::milliseconds interval, - std::shared_ptr strategy, size_t threads); + std::shared_ptr strategy, size_t threads); + bool startNewBackgroundEvictor(std::chrono::milliseconds interval, + std::shared_ptr strategy, size_t threads); // Stop existing workers with a timeout bool stopPoolRebalancer(std::chrono::seconds timeout = std::chrono::seconds{ @@ -1049,54 +1048,51 @@ class CacheAllocator : public CacheBase { return stats; } - // returns the background evictor - BackgroundEvictionStats getBackgroundEvictorStats() const { - auto stats = BackgroundEvictionStats{}; - for (auto &bg : backgroundEvictor_) - stats += bg->getStats(); + // returns the background mover stats + BackgroundMoverStats getBackgroundMoverStats(MoverDir direction) const { + + auto stats = BackgroundMoverStats{}; + if (direction == MoverDir::Evict) { + for (auto &bg : backgroundEvictor_) + stats += bg->getStats(); + } else if (direction == MoverDir::Promote) { + for (auto &bg : backgroundPromoter_) + stats += bg->getStats(); + } return stats; + } - BackgroundPromotionStats getBackgroundPromoterStats() const { - auto stats = BackgroundPromotionStats{}; - for (auto &bg : backgroundPromoter_) - stats += bg->getStats(); - return stats; - } std::map>> - getBackgroundEvictorClassStats() const { + getBackgroundMoverClassStats(MoverDir direction) const { std::map>> stats; - for (auto &bg : backgroundEvictor_) { - for (auto &tid : bg->getClassStats()) { - for (auto &pid : tid.second) { - for (auto &cid : pid.second) { - stats[tid.first][pid.first][cid.first] += cid.second; + if (direction == MoverDir::Evict) { + for (auto &bg : backgroundEvictor_) { + for (auto &tid : bg->getClassStats()) { + for (auto &pid : tid.second) { + for (auto &cid : pid.second) { + stats[tid.first][pid.first][cid.first] += cid.second; + } + } } } - } - } - - return stats; - } - - std::map>> - getBackgroundPromoterClassStats() const { - std::map>> stats; - - for (auto &bg : backgroundPromoter_) { - for (auto &tid : bg->getClassStats()) { - for (auto &pid : tid.second) { - for (auto &cid : pid.second) { - stats[tid.first][pid.first][cid.first] += cid.second; + } else if (direction == MoverDir::Promote) { + for (auto &bg : backgroundPromoter_) { + for (auto &tid : bg->getClassStats()) { + for (auto &pid : tid.second) { + for (auto &cid : pid.second) { + stats[tid.first][pid.first][cid.first] += cid.second; + } + } } } - } } return stats; } + // return the LruType of an item typename MMType::LruType getItemLruType(const Item& item) const; @@ -2258,8 +2254,8 @@ class CacheAllocator : public CacheBase { std::unique_ptr memMonitor_; // background evictor - std::vector>> backgroundEvictor_; - std::vector>> backgroundPromoter_; + std::vector>> backgroundEvictor_; + std::vector>> backgroundPromoter_; // check whether a pool is a slabs pool std::array isCompactCachePool_{}; @@ -2315,8 +2311,7 @@ class CacheAllocator : public CacheBase { // Make this friend to give access to acquire and release friend ReadHandle; friend ReaperAPIWrapper; - friend BackgroundEvictorAPIWrapper; - friend BackgroundPromoterAPIWrapper; + friend BackgroundMoverAPIWrapper; friend class CacheAPIWrapperForNvm; friend class FbInternalRuntimeUpdateWrapper; diff --git a/cachelib/allocator/CacheAllocatorConfig.h b/cachelib/allocator/CacheAllocatorConfig.h index aa8ff039e..7b454084d 100644 --- a/cachelib/allocator/CacheAllocatorConfig.h +++ b/cachelib/allocator/CacheAllocatorConfig.h @@ -271,11 +271,11 @@ class CacheAllocatorConfig { // Enable the background evictor - scans a tier to look for objects // to evict to the next tier CacheAllocatorConfig& enableBackgroundEvictor( - std::shared_ptr backgroundEvictorStrategy, + std::shared_ptr backgroundMoverStrategy, std::chrono::milliseconds regularInterval, size_t threads); CacheAllocatorConfig& enableBackgroundPromoter( - std::shared_ptr backgroundEvictorStrategy, + std::shared_ptr backgroundMoverStrategy, std::chrono::milliseconds regularInterval, size_t threads); // This enables an optimization for Pool rebalancing and resizing. @@ -475,8 +475,8 @@ class CacheAllocatorConfig { new RebalanceStrategy{}}; // rebalance to avoid alloc fialures. - std::shared_ptr backgroundEvictorStrategy; - std::shared_ptr backgroundPromoterStrategy; + std::shared_ptr backgroundEvictorStrategy; + std::shared_ptr backgroundPromoterStrategy; // time interval to sleep between iterations of pool size optimization, // for regular pools and compact caches @@ -996,7 +996,7 @@ CacheAllocatorConfig& CacheAllocatorConfig::enablePoolRebalancing( template CacheAllocatorConfig& CacheAllocatorConfig::enableBackgroundEvictor( - std::shared_ptr strategy, + std::shared_ptr strategy, std::chrono::milliseconds interval, size_t evictorThreads) { backgroundEvictorStrategy = strategy; backgroundEvictorInterval = interval; @@ -1006,7 +1006,7 @@ CacheAllocatorConfig& CacheAllocatorConfig::enableBackgroundEvictor( template CacheAllocatorConfig& CacheAllocatorConfig::enableBackgroundPromoter( - std::shared_ptr strategy, + std::shared_ptr strategy, std::chrono::milliseconds interval, size_t promoterThreads) { backgroundPromoterStrategy = strategy; backgroundPromoterInterval = interval; diff --git a/cachelib/allocator/CacheStats.h b/cachelib/allocator/CacheStats.h index c8af1a2a9..a480b5db2 100644 --- a/cachelib/allocator/CacheStats.h +++ b/cachelib/allocator/CacheStats.h @@ -300,42 +300,26 @@ struct ReaperStats { uint64_t avgTraversalTimeMs{0}; }; -// Eviction Stats -struct BackgroundEvictionStats { - // the number of items this worker evicted by looking at pools/classes stats - uint64_t numEvictedItems{0}; - +// Mover Stats +struct BackgroundMoverStats { + // the number of items this worker moved by looking at pools/classes stats + uint64_t numMovedItems{0}; // number of times we went executed the thread //TODO: is this def correct? uint64_t runCount{0}; - // total number of classes uint64_t totalClasses{0}; - // eviction size - uint64_t evictionSize{0}; + uint64_t totalBytesMoved{0}; - BackgroundEvictionStats& operator+=(const BackgroundEvictionStats& rhs) { - numEvictedItems += rhs.numEvictedItems; + BackgroundMoverStats& operator+=(const BackgroundMoverStats& rhs) { + numMovedItems += rhs.numMovedItems; runCount += rhs.runCount; totalClasses += rhs.totalClasses; - evictionSize += rhs.evictionSize; + totalBytesMoved += rhs.totalBytesMoved; return *this; } }; -struct BackgroundPromotionStats { - // the number of items this worker evicted by looking at pools/classes stats - uint64_t numPromotedItems{0}; - - // number of times we went executed the thread //TODO: is this def correct? - uint64_t runCount{0}; - - BackgroundPromotionStats& operator+=(const BackgroundPromotionStats& rhs) { - numPromotedItems += rhs.numPromotedItems; - runCount += rhs.runCount; - return *this; - } -}; // CacheMetadata type to export struct CacheMetadata { @@ -358,9 +342,9 @@ struct Stats; // the ones that are aggregated over all pools struct GlobalCacheStats { // background eviction stats - BackgroundEvictionStats evictionStats; + BackgroundMoverStats evictionStats; - BackgroundPromotionStats promotionStats; + BackgroundMoverStats promotionStats; // number of calls to CacheAllocator::find uint64_t numCacheGets{0}; diff --git a/cachelib/allocator/FreeThresholdStrategy.h b/cachelib/allocator/FreeThresholdStrategy.h index 6a6b0c895..babd8935c 100644 --- a/cachelib/allocator/FreeThresholdStrategy.h +++ b/cachelib/allocator/FreeThresholdStrategy.h @@ -17,14 +17,14 @@ #pragma once #include "cachelib/allocator/Cache.h" -#include "cachelib/allocator/BackgroundEvictorStrategy.h" +#include "cachelib/allocator/BackgroundMoverStrategy.h" namespace facebook { namespace cachelib { -// Base class for background eviction strategy. -class FreeThresholdStrategy : public BackgroundEvictorStrategy { +// Base class for background mover strategy. +class FreeThresholdStrategy : public BackgroundMoverStrategy { public: FreeThresholdStrategy(double lowEvictionAcWatermark, double highEvictionAcWatermark, uint64_t maxEvictionBatch, uint64_t minEvictionBatch); diff --git a/cachelib/allocator/PromotionStrategy.h b/cachelib/allocator/PromotionStrategy.h index 8479c54dc..ad9145282 100644 --- a/cachelib/allocator/PromotionStrategy.h +++ b/cachelib/allocator/PromotionStrategy.h @@ -17,14 +17,14 @@ #pragma once #include "cachelib/allocator/Cache.h" -#include "cachelib/allocator/BackgroundEvictorStrategy.h" +#include "cachelib/allocator/BackgroundMoverStrategy.h" namespace facebook { namespace cachelib { // Base class for background eviction strategy. -class PromotionStrategy : public BackgroundEvictorStrategy { +class PromotionStrategy : public BackgroundMoverStrategy { public: PromotionStrategy(uint64_t promotionAcWatermark, uint64_t maxPromotionBatch, uint64_t minPromotionBatch): diff --git a/cachelib/cachebench/cache/Cache-inl.h b/cachelib/cachebench/cache/Cache-inl.h index 0f555cc0d..a5c38e2fb 100644 --- a/cachelib/cachebench/cache/Cache-inl.h +++ b/cachelib/cachebench/cache/Cache-inl.h @@ -551,16 +551,16 @@ Stats Cache::getStats() const { ret.allocationClassStats = allocationClassStats; ret.backgndEvicStats.nEvictedItems = - cacheStats.evictionStats.numEvictedItems; + cacheStats.evictionStats.numMovedItems; ret.backgndEvicStats.nTraversals = cacheStats.evictionStats.runCount; ret.backgndEvicStats.nClasses = cacheStats.evictionStats.totalClasses; ret.backgndEvicStats.evictionSize = - cacheStats.evictionStats.evictionSize; + cacheStats.evictionStats.totalBytesMoved; ret.backgndPromoStats.nPromotedItems = - cacheStats.promotionStats.numPromotedItems; + cacheStats.promotionStats.numMovedItems; ret.backgndPromoStats.nTraversals = cacheStats.promotionStats.runCount; @@ -614,8 +614,8 @@ Stats Cache::getStats() const { ret.nvmCounters = cache_->getNvmCacheStatsMap(); } - ret.backgroundEvictionClasses = cache_->getBackgroundEvictorClassStats(); - ret.backgroundPromotionClasses = cache_->getBackgroundPromoterClassStats(); + ret.backgroundEvictionClasses = cache_->getBackgroundMoverClassStats(MoverDir::Evict); + ret.backgroundPromotionClasses = cache_->getBackgroundMoverClassStats(MoverDir::Promote); // nvm stats from navy if (!isRamOnly() && !navyStats.empty()) { diff --git a/cachelib/cachebench/util/CacheConfig.cpp b/cachelib/cachebench/util/CacheConfig.cpp index 1a92eef97..17b89b608 100644 --- a/cachelib/cachebench/util/CacheConfig.cpp +++ b/cachelib/cachebench/util/CacheConfig.cpp @@ -162,7 +162,7 @@ std::shared_ptr CacheConfig::getRebalanceStrategy() const { } } -std::shared_ptr CacheConfig::getBackgroundEvictorStrategy() const { +std::shared_ptr CacheConfig::getBackgroundEvictorStrategy() const { if (backgroundEvictorIntervalMilSec == 0) { return nullptr; } @@ -170,7 +170,7 @@ std::shared_ptr CacheConfig::getBackgroundEvictorStra return std::make_shared(lowEvictionAcWatermark, highEvictionAcWatermark, maxEvictionBatch, minEvictionBatch); } -std::shared_ptr CacheConfig::getBackgroundPromoterStrategy() const { +std::shared_ptr CacheConfig::getBackgroundPromoterStrategy() const { if (backgroundPromoterIntervalMilSec == 0) { return nullptr; } diff --git a/cachelib/cachebench/util/CacheConfig.h b/cachelib/cachebench/util/CacheConfig.h index cf5846d5f..53d5ec029 100644 --- a/cachelib/cachebench/util/CacheConfig.h +++ b/cachelib/cachebench/util/CacheConfig.h @@ -20,7 +20,7 @@ #include "cachelib/allocator/CacheAllocator.h" #include "cachelib/allocator/RebalanceStrategy.h" -#include "cachelib/allocator/BackgroundEvictorStrategy.h" +#include "cachelib/allocator/BackgroundMoverStrategy.h" #include "cachelib/cachebench/util/JSONConfig.h" #include "cachelib/common/Ticker.h" #include "cachelib/navy/common/Device.h" @@ -324,8 +324,8 @@ struct CacheConfig : public JSONConfig { CacheConfig() {} std::shared_ptr getRebalanceStrategy() const; - std::shared_ptr getBackgroundEvictorStrategy() const; - std::shared_ptr getBackgroundPromoterStrategy() const; + std::shared_ptr getBackgroundEvictorStrategy() const; + std::shared_ptr getBackgroundPromoterStrategy() const; }; } // namespace cachebench } // namespace cachelib From b158c67d30243f92d4f660e58ba35a98afe761f8 Mon Sep 17 00:00:00 2001 From: Daniel Byrne Date: Wed, 17 Aug 2022 06:35:29 -0400 Subject: [PATCH 2/2] fix header --- cachelib/allocator/CacheAllocatorConfig.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachelib/allocator/CacheAllocatorConfig.h b/cachelib/allocator/CacheAllocatorConfig.h index 7b454084d..9897d6f2d 100644 --- a/cachelib/allocator/CacheAllocatorConfig.h +++ b/cachelib/allocator/CacheAllocatorConfig.h @@ -32,7 +32,7 @@ #include "cachelib/allocator/NvmAdmissionPolicy.h" #include "cachelib/allocator/PoolOptimizeStrategy.h" #include "cachelib/allocator/RebalanceStrategy.h" -#include "cachelib/allocator/BackgroundEvictorStrategy.h" +#include "cachelib/allocator/BackgroundMoverStrategy.h" #include "cachelib/allocator/Util.h" #include "cachelib/common/EventInterface.h" #include "cachelib/common/Throttler.h"