@@ -27,67 +27,61 @@ BackgroundEvictor<CacheT>::BackgroundEvictor(Cache& cache,
2727 : cache_(cache),
2828 strategy_ (strategy),
2929 tid_(tid) {
30-
3130}
3231
3332template <typename CacheT>
3433BackgroundEvictor<CacheT>::~BackgroundEvictor () { stop (std::chrono::seconds (0 )); }
3534
3635template <typename CacheT>
3736void BackgroundEvictor<CacheT>::work() {
38- if (strategy_->poll_ ) {
39- try {
37+ try {
38+ if (!tasks_.empty ()) {
39+ while (!tasks_.empty ()) {
40+ auto [pid, cid] = tasks_.dequeue ();
41+ auto batch = strategy_->calculateBatchSize (cache_, tid_, pid, cid);
42+ auto evicted = BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems (cache_,
43+ tid_,pid,cid,batch);
44+ numEvictedItemsFromSchedule_.fetch_add (1 , std::memory_order_relaxed);
45+ runCount_.fetch_add (1 , std::memory_order_relaxed);
46+ }
47+ } else {
4048 for (const auto pid : cache_.getRegularPoolIds ()) {
4149 // check if the pool is full - probably should be if tier is full
4250 if (cache_.getPoolByTid (pid,tid_).allSlabsAllocated ()) {
4351 checkAndRun (pid);
4452 }
4553 }
46- } catch (const std::exception& ex) {
47- XLOGF (ERR, " BackgroundEvictor interrupted due to exception: {}" , ex.what ());
4854 }
49- } else {
50- // when an eviction for a given pid,cid at tier 0 is triggered this will be run
51- while (1 ) {
52- std::pair p = tasks_.dequeue ();
53- unsigned int pid = p.first ;
54- unsigned int cid = p.second ;
55- unsigned int batch = strategy_->calculateBatchSize (cache_,tid_,pid,cid);
56- // try evicting BATCH items from the class in order to reach free target
57- unsigned int evicted =
58- BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems (cache_,
59- tid_,pid,cid,batch);
60- runCount_ = runCount_ + 1 ;
61- }
62-
55+ } catch (const std::exception& ex) {
56+ XLOGF (ERR, " BackgroundEvictor interrupted due to exception: {}" , ex.what ());
6357 }
6458}
6559
66-
6760// Look for classes that exceed the target memory capacity
6861// and return those for eviction
6962template <typename CacheT>
70- void BackgroundEvictor<CacheT>::checkAndRun(PoolId pid) {
71-
63+ void BackgroundEvictor<CacheT>::checkAndRun(PoolId pid) {
7264 const auto & mpStats = cache_.getPoolByTid (pid,tid_).getStats ();
7365 for (auto & cid : mpStats.classIds ) {
74- if (strategy_->shouldEvict (cache_,tid_,pid,cid)) {
75- unsigned int batch = strategy_->calculateBatchSize (cache_,tid_,pid,cid);
76- // try evicting BATCH items from the class in order to reach free target
77- unsigned int evicted =
78- BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems (cache_,
79- tid_,pid,cid,batch);
80- numEvictedItems_ += evicted;
81- }
66+ auto batch = strategy_->calculateBatchSize (cache_,tid_,pid,cid);
67+ if (!batch)
68+ continue ;
69+
70+ // try evicting BATCH items from the class in order to reach free target
71+ auto evicted =
72+ BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems (cache_,
73+ tid_,pid,cid,batch);
74+ numEvictedItems_.fetch_add (evicted, std::memory_order_relaxed);
8275 }
83- runCount_ = runCount_ + 1 ;
76+ runCount_. fetch_add ( 1 , std::memory_order_relaxed) ;
8477}
8578
8679template <typename CacheT>
8780BackgroundEvictorStats BackgroundEvictor<CacheT>::getStats() const noexcept {
8881 BackgroundEvictorStats stats;
8982 stats.numEvictedItems = numEvictedItems_.load (std::memory_order_relaxed);
9083 stats.numTraversals = runCount_.load (std::memory_order_relaxed);
84+ stats.numEvictedItemsFromSchedule = numEvictedItemsFromSchedule_.load (std::memory_order_relaxed);
9185 return stats;
9286}
9387
0 commit comments