Skip to content

Commit daf1793

Browse files
committed
updated the background eviction stats to use AtomicCounters lib
1 parent 4fa3901 commit daf1793

File tree

7 files changed

+77
-36
lines changed

7 files changed

+77
-36
lines changed

cachelib/allocator/BackgroundEvictor-inl.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ void BackgroundEvictor<CacheT>::work() {
3939
while (auto entry = tasks_.try_dequeue()) {
4040
auto [pid, cid] = entry.value();
4141
auto batch = strategy_->calculateBatchSize(cache_, tid_, pid, cid);
42+
stats.evictionSize.add(batch);
4243
auto evicted = BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems(cache_,
4344
tid_,pid,cid,batch);
44-
numEvictedItemsFromSchedule_.fetch_add(1, std::memory_order_relaxed);
45-
runCount_.fetch_add(1, std::memory_order_relaxed);
45+
stats.numEvictedItemsFromSchedule.inc();
46+
stats.numTraversals.inc();
4647
}
4748
} else {
4849
for (const auto pid : cache_.getRegularPoolIds()) {
@@ -67,22 +68,25 @@ void BackgroundEvictor<CacheT>::checkAndRun(PoolId pid) {
6768
if (!batch)
6869
continue;
6970

71+
stats.evictionSize.add(batch);
7072
//try evicting BATCH items from the class in order to reach free target
7173
auto evicted =
7274
BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems(cache_,
7375
tid_,pid,cid,batch);
74-
numEvictedItems_.fetch_add(evicted, std::memory_order_relaxed);
76+
stats.numEvictedItems.add(evicted);
7577
}
76-
runCount_.fetch_add(1, std::memory_order_relaxed);
78+
stats.numTraversals.inc();
7779
}
7880

7981
template <typename CacheT>
80-
BackgroundEvictorStats BackgroundEvictor<CacheT>::getStats() const noexcept {
81-
BackgroundEvictorStats stats;
82-
stats.numEvictedItems = numEvictedItems_.load(std::memory_order_relaxed);
83-
stats.numTraversals = runCount_.load(std::memory_order_relaxed);
84-
stats.numEvictedItemsFromSchedule = numEvictedItemsFromSchedule_.load(std::memory_order_relaxed);
85-
return stats;
82+
BackgroundEvictionStats BackgroundEvictor<CacheT>::getStats() const noexcept {
83+
BackgroundEvictionStats evicStats;
84+
evicStats.numEvictedItems = stats.numEvictedItems.get();
85+
evicStats.numEvictedItemsFromSchedule = stats.numEvictedItemsFromSchedule.get();
86+
evicStats.numTraversals = stats.numTraversals.get();
87+
evicStats.evictionSize = stats.evictionSize.get();
88+
89+
return evicStats;
8690
}
8791

8892
} // namespace cachelib

cachelib/allocator/BackgroundEvictor.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "cachelib/allocator/CacheStats.h"
2323
#include "cachelib/common/PeriodicWorker.h"
2424
#include "cachelib/allocator/BackgroundEvictorStrategy.h"
25+
#include "cachelib/common/AtomicCounter.h"
2526

2627

2728
namespace facebook {
@@ -38,6 +39,20 @@ struct BackgroundEvictorAPIWrapper {
3839
}
3940
};
4041

42+
struct BackgroundEvictorStats {
43+
// items evicted
44+
AtomicCounter numEvictedItems{0};
45+
46+
// items evicted from schedule
47+
AtomicCounter numEvictedItemsFromSchedule;
48+
49+
// traversals
50+
AtomicCounter numTraversals{0};
51+
52+
// item eviction size
53+
AtomicCounter evictionSize{0};
54+
};
55+
4156
// Periodic worker that evicts items from tiers in batches
4257
// The primary aim is to reduce insertion times for new items in the
4358
// cache
@@ -58,7 +73,8 @@ class BackgroundEvictor : public PeriodicWorker {
5873
void schedule(size_t pid, size_t cid) {
5974
tasks_.enqueue(std::make_pair(pid,cid));
6075
}
61-
BackgroundEvictorStats getStats() const noexcept;
76+
77+
BackgroundEvictionStats getStats() const noexcept;
6278

6379
private:
6480
// cache allocator's interface for evicting
@@ -73,10 +89,8 @@ class BackgroundEvictor : public PeriodicWorker {
7389
// implements the actual logic of running the background evictor
7490
void work() override final;
7591
void checkAndRun(PoolId pid);
76-
77-
std::atomic<uint64_t> numEvictedItems_{0};
78-
std::atomic<uint64_t> numEvictedItemsFromSchedule_{0};
79-
std::atomic<uint64_t> runCount_{0};
92+
93+
BackgroundEvictorStats stats;
8094
};
8195
} // namespace cachelib
8296
} // namespace facebook

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3676,7 +3676,7 @@ GlobalCacheStats CacheAllocator<CacheTrait>::getGlobalCacheStats() const {
36763676
ret.nvmCacheEnabled = nvmCache_ ? nvmCache_->isEnabled() : false;
36773677
ret.nvmUpTime = currTime - getNVMCacheCreationTime();
36783678
ret.reaperStats = getReaperStats();
3679-
ret.backgroundEvictorStats = getBackgroundEvictorStats();
3679+
ret.evictionStats = getBackgroundEvictorStats();
36803680
ret.numActiveHandles = getNumActiveHandles();
36813681

36823682
return ret;

cachelib/allocator/CacheAllocator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,8 @@ class CacheAllocator : public CacheBase {
10441044
}
10451045

10461046
// returns the background evictor
1047-
BackgroundEvictorStats getBackgroundEvictorStats() const {
1048-
auto stats = backgroundEvictor_ ? backgroundEvictor_->getStats() : BackgroundEvictorStats{};
1047+
BackgroundEvictionStats getBackgroundEvictorStats() const {
1048+
auto stats = backgroundEvictor_ ? backgroundEvictor_->getStats() : BackgroundEvictionStats{};
10491049
return stats;
10501050
}
10511051

cachelib/allocator/CacheStats.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ struct ReaperStats {
285285
uint64_t avgTraversalTimeMs{0};
286286
};
287287

288-
// Stats for background evictor
289-
struct BackgroundEvictorStats {
288+
// Eviction Stats
289+
struct BackgroundEvictionStats {
290290
// the number of items this worker evicted by looking at pools/classes stats
291291
uint64_t numEvictedItems{0};
292292

@@ -296,6 +296,8 @@ struct BackgroundEvictorStats {
296296
// number of times we went executed the thread //TODO: is this def correct?
297297
uint64_t numTraversals{0};
298298

299+
// eviction size
300+
uint64_t evictionSize{0};
299301
};
300302

301303
// CacheMetadata type to export
@@ -318,6 +320,9 @@ struct Stats;
318320
// Stats that apply globally in cache and
319321
// the ones that are aggregated over all pools
320322
struct GlobalCacheStats {
323+
// background eviction stats
324+
BackgroundEvictionStats evictionStats;
325+
321326
// number of calls to CacheAllocator::find
322327
uint64_t numCacheGets{0};
323328

@@ -482,9 +487,6 @@ struct GlobalCacheStats {
482487

483488
// stats related to the reaper
484489
ReaperStats reaperStats;
485-
486-
// stats related to the background evictor
487-
BackgroundEvictorStats backgroundEvictorStats;
488490

489491
uint64_t numNvmRejectsByExpiry{};
490492
uint64_t numNvmRejectsByClean{};

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,19 @@ Stats Cache<Allocator>::getStats() const {
522522
const auto navyStats = cache_->getNvmCacheStatsMap();
523523

524524
Stats ret;
525+
ret.backgndEvicStats.nEvictedItems =
526+
cacheStats.evictionStats.numEvictedItems;
527+
ret.backgndEvicStats.nEvictedItemsFromSchedule =
528+
cacheStats.evictionStats.numEvictedItemsFromSchedule;
529+
ret.backgndEvicStats.nTraversals =
530+
cacheStats.evictionStats.numTraversals;
531+
ret.backgndEvicStats.evictionSize =
532+
cacheStats.evictionStats.evictionSize;
533+
525534
ret.numEvictions = aggregate.numEvictions();
526535
ret.numItems = aggregate.numItems();
527536
ret.allocAttempts = cacheStats.allocAttempts;
528537
ret.allocFailures = cacheStats.allocFailures;
529-
530-
ret.numBackgroundEvictions = cacheStats.backgroundEvictorStats.numEvictedItems;
531-
ret.numBackgroundEvictionsFromSchedule = cacheStats.backgroundEvictorStats.numEvictedItemsFromSchedule;
532-
ret.numBackgroundEvictorRuns = cacheStats.backgroundEvictorStats.numTraversals;
533538

534539
ret.numCacheGets = cacheStats.numCacheGets;
535540
ret.numCacheGetMiss = cacheStats.numCacheGetMiss;

cachelib/cachebench/cache/CacheStats.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,30 @@ DECLARE_bool(report_api_latency);
2525
namespace facebook {
2626
namespace cachelib {
2727
namespace cachebench {
28+
29+
struct BackgroundEvictionStats {
30+
// the number of items this worker evicted by looking at pools/classes stats
31+
uint64_t nEvictedItems{0};
32+
33+
// the number of items this worker evicted for pools/classes requested by schedule call
34+
uint64_t nEvictedItemsFromSchedule{0};
35+
36+
// number of times we went executed the thread //TODO: is this def correct?
37+
uint64_t nTraversals{0};
38+
39+
// size of evicted items
40+
uint64_t evictionSize;
41+
};
42+
2843
struct Stats {
44+
BackgroundEvictionStats backgndEvicStats;
45+
2946
uint64_t numEvictions{0};
3047
uint64_t numItems{0};
3148

3249
uint64_t allocAttempts{0};
3350
uint64_t allocFailures{0};
3451

35-
uint64_t numBackgroundEvictions{0};
36-
uint64_t numBackgroundEvictionsFromSchedule{0};
37-
uint64_t numBackgroundEvictorRuns{0};
38-
3952
uint64_t numCacheGets{0};
4053
uint64_t numCacheGetMiss{0};
4154
uint64_t numRamDestructorCalls{0};
@@ -118,12 +131,15 @@ struct Stats {
118131
invertPctFn(allocFailures, allocAttempts))
119132
<< std::endl;
120133
out << folly::sformat("RAM Evictions : {:,}", numEvictions) << std::endl;
121-
122134

123-
out << folly::sformat("Background Tier 0 Evictions : {:,}", numBackgroundEvictions) << std::endl;
124-
out << folly::sformat("Background Tier 0 Evictions from schedule() : {:,}", numBackgroundEvictionsFromSchedule) << std::endl;
125-
126-
out << folly::sformat("Background Tier 0 Eviction Runs : {:,}", numBackgroundEvictorRuns) << std::endl;
135+
out << folly::sformat("Tier 0 Background Evicted items : {:,}",
136+
backgndEvicStats.nEvictedItems) << std::endl;
137+
out << folly::sformat("Tier 0 Background Evicted items from schedule : {:,}",
138+
backgndEvicStats.nEvictedItemsFromSchedule) << std::endl;
139+
out << folly::sformat("Tier 0 Background Traversals : {:,}",
140+
backgndEvicStats.nTraversals) << std::endl;
141+
out << folly::sformat("Tier 0 Background Evicted Size : {:,}",
142+
backgndEvicStats.evictionSize) << std::endl;
127143

128144
if (numCacheGets > 0) {
129145
out << folly::sformat("Cache Gets : {:,}", numCacheGets) << std::endl;

0 commit comments

Comments
 (0)