Skip to content

Commit 6085f23

Browse files
authored
Merge pull request pmem#4 from guptask/reduced_cs_bkgrnd_evictor
Updated stats structure
2 parents b0ab826 + bf18c85 commit 6085f23

File tree

7 files changed

+95
-45
lines changed

7 files changed

+95
-45
lines changed

cachelib/allocator/BackgroundEvictor-inl.h

Lines changed: 16 additions & 12 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()) {
@@ -71,6 +72,7 @@ void BackgroundEvictor<CacheT>::checkAndRun(PoolId pid) {
7172
continue;
7273
}
7374

75+
stats.evictionSize.add(batch);
7476
//try evicting BATCH items from the class in order to reach free target
7577
auto evicted =
7678
BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems(cache_,
@@ -84,19 +86,21 @@ void BackgroundEvictor<CacheT>::checkAndRun(PoolId pid) {
8486
evictions_per_class_[cid_id] = 0;
8587
}
8688
}
87-
runCount_.fetch_add(1, std::memory_order_relaxed);
88-
numEvictedItems_.fetch_add(evictions, std::memory_order_relaxed);
89-
totalClasses_.fetch_add(classes, std::memory_order_relaxed);
89+
stats.numTraversals.inc();
90+
stats.numEvictedItems.add(evictions);
91+
stats.totalClasses.add(classes);
9092
}
9193

9294
template <typename CacheT>
93-
BackgroundEvictorStats BackgroundEvictor<CacheT>::getStats() const noexcept {
94-
BackgroundEvictorStats stats;
95-
stats.numEvictedItems = numEvictedItems_.load(std::memory_order_relaxed);
96-
stats.runCount = runCount_.load(std::memory_order_relaxed);
97-
stats.numEvictedItemsFromSchedule = numEvictedItemsFromSchedule_.load(std::memory_order_relaxed);
98-
stats.totalClasses = totalClasses_.load(std::memory_order_relaxed);
99-
return stats;
95+
BackgroundEvictionStats BackgroundEvictor<CacheT>::getStats() const noexcept {
96+
BackgroundEvictionStats evicStats;
97+
evicStats.numEvictedItems = stats.numEvictedItems.get();
98+
evicStats.numEvictedItemsFromSchedule = stats.numEvictedItemsFromSchedule.get();
99+
evicStats.runCount = stats.numTraversals.get();
100+
evicStats.evictionSize = stats.evictionSize.get();
101+
evicStats.totalClasses = stats.totalClasses.get();
102+
103+
return evicStats;
100104
}
101105

102106
template <typename CacheT>

cachelib/allocator/BackgroundEvictor.h

Lines changed: 23 additions & 7 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,23 @@ struct BackgroundEvictorAPIWrapper {
3839
}
3940
};
4041

42+
struct BackgroundEvictorStats {
43+
// items evicted
44+
AtomicCounter numEvictedItems{0};
45+
46+
// items evicted from schedule
47+
AtomicCounter numEvictedItemsFromSchedule{0};
48+
49+
// traversals
50+
AtomicCounter numTraversals{0};
51+
52+
// total class size
53+
AtomicCounter totalClasses{0};
54+
55+
// item eviction size
56+
AtomicCounter evictionSize{0};
57+
};
58+
4159
// Periodic worker that evicts items from tiers in batches
4260
// The primary aim is to reduce insertion times for new items in the
4361
// cache
@@ -58,10 +76,12 @@ class BackgroundEvictor : public PeriodicWorker {
5876
void schedule(size_t pid, size_t cid) {
5977
tasks_.enqueue(std::make_pair(pid,cid));
6078
}
61-
BackgroundEvictorStats getStats() const noexcept;
79+
BackgroundEvictionStats getStats() const noexcept;
6280
std::map<uint32_t,uint64_t> getClassStats() const noexcept;
6381

6482
private:
83+
std::map<uint32_t,uint64_t> evictions_per_class_;
84+
6585
// cache allocator's interface for evicting
6686

6787
using Item = typename Cache::Item;
@@ -74,12 +94,8 @@ class BackgroundEvictor : public PeriodicWorker {
7494
// implements the actual logic of running the background evictor
7595
void work() override final;
7696
void checkAndRun(PoolId pid);
77-
78-
std::map<uint32_t,uint64_t> evictions_per_class_;
79-
std::atomic<uint64_t> numEvictedItems_{0};
80-
std::atomic<uint64_t> numEvictedItemsFromSchedule_{0};
81-
std::atomic<uint64_t> runCount_{0};
82-
std::atomic<uint64_t> totalClasses_{0};
97+
98+
BackgroundEvictorStats stats;
8399
};
84100
} // namespace cachelib
85101
} // namespace facebook

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3642,7 +3642,7 @@ GlobalCacheStats CacheAllocator<CacheTrait>::getGlobalCacheStats() const {
36423642
ret.nvmCacheEnabled = nvmCache_ ? nvmCache_->isEnabled() : false;
36433643
ret.nvmUpTime = currTime - getNVMCacheCreationTime();
36443644
ret.reaperStats = getReaperStats();
3645-
ret.backgroundEvictorStats = getBackgroundEvictorStats();
3645+
ret.evictionStats = getBackgroundEvictorStats();
36463646
ret.numActiveHandles = getNumActiveHandles();
36473647

36483648
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: 8 additions & 7 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,10 +296,11 @@ struct BackgroundEvictorStats {
296296
// number of times we went executed the thread //TODO: is this def correct?
297297
uint64_t runCount{0};
298298

299+
// total number of classes
299300
uint64_t totalClasses{0};
300301

301-
302-
302+
// eviction size
303+
uint64_t evictionSize{0};
303304
};
304305

305306
// CacheMetadata type to export
@@ -322,6 +323,9 @@ struct Stats;
322323
// Stats that apply globally in cache and
323324
// the ones that are aggregated over all pools
324325
struct GlobalCacheStats {
326+
// background eviction stats
327+
BackgroundEvictionStats evictionStats;
328+
325329
// number of calls to CacheAllocator::find
326330
uint64_t numCacheGets{0};
327331

@@ -486,9 +490,6 @@ struct GlobalCacheStats {
486490

487491
// stats related to the reaper
488492
ReaperStats reaperStats;
489-
490-
// stats related to the background evictor
491-
BackgroundEvictorStats backgroundEvictorStats;
492493

493494
uint64_t numNvmRejectsByExpiry{};
494495
uint64_t numNvmRejectsByClean{};

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,25 @@ Stats Cache<Allocator>::getStats() const {
527527
const auto navyStats = cache_->getNvmCacheStatsMap();
528528

529529
Stats ret;
530+
ret.backgndEvicStats.nEvictedItems =
531+
cacheStats.evictionStats.numEvictedItems;
532+
ret.backgndEvicStats.nEvictedItemsFromSchedule =
533+
cacheStats.evictionStats.numEvictedItemsFromSchedule;
534+
ret.backgndEvicStats.nTraversals =
535+
cacheStats.evictionStats.runCount;
536+
ret.backgndEvicStats.nClasses =
537+
cacheStats.evictionStats.totalClasses;
538+
ret.backgndEvicStats.evictionSize =
539+
cacheStats.evictionStats.evictionSize;
540+
530541
ret.numEvictions = aggregate.numEvictions();
531542
ret.numItems = aggregate.numItems();
532543
ret.allocAttempts = cacheStats.allocAttempts;
533544
ret.allocFailures = cacheStats.allocFailures;
534-
535-
ret.numBackgroundEvictions = cacheStats.backgroundEvictorStats.numEvictedItems;
536-
ret.numBackgroundEvictionsFromSchedule = cacheStats.backgroundEvictorStats.numEvictedItemsFromSchedule;
537-
ret.numBackgroundEvictorRuns = cacheStats.backgroundEvictorStats.runCount;
545+
546+
ret.numCacheGets = cacheStats.numCacheGets;
547+
ret.numCacheGetMiss = cacheStats.numCacheGetMiss;
548+
ret.numRamDestructorCalls = cacheStats.numRamDestructorCalls;
538549
ret.numNvmGets = cacheStats.numNvmGets;
539550
ret.numNvmGetMiss = cacheStats.numNvmGetMiss;
540551
ret.numNvmGetCoalesced = cacheStats.numNvmGetCoalesced;

cachelib/cachebench/cache/CacheStats.h

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,33 @@ 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+
// number of classes
40+
uint64_t nClasses{0};
41+
42+
// size of evicted items
43+
uint64_t evictionSize{0};
44+
};
45+
2846
struct Stats {
47+
BackgroundEvictionStats backgndEvicStats;
48+
2949
uint64_t numEvictions{0};
3050
uint64_t numItems{0};
3151

3252
uint64_t allocAttempts{0};
3353
uint64_t allocFailures{0};
3454

35-
uint64_t numBackgroundEvictions{0};
36-
uint64_t numBackgroundEvictionsFromSchedule{0};
37-
uint64_t numBackgroundEvictorRuns{0};
38-
uint64_t numBackgroundEvictorClasses{0};
39-
4055
uint64_t numCacheGets{0};
4156
uint64_t numCacheGetMiss{0};
4257
uint64_t numRamDestructorCalls{0};
@@ -121,14 +136,17 @@ struct Stats {
121136
invertPctFn(allocFailures, allocAttempts))
122137
<< std::endl;
123138
out << folly::sformat("RAM Evictions : {:,}", numEvictions) << std::endl;
124-
125139

126-
out << folly::sformat("Background Tier 0 Evictions : {:,}", numBackgroundEvictions) << std::endl;
127-
out << folly::sformat("Background Tier 0 Evictions from schedule() : {:,}", numBackgroundEvictionsFromSchedule) << std::endl;
128-
129-
out << folly::sformat("Background Tier 0 Eviction Runs : {:,}", numBackgroundEvictorRuns) << std::endl;
130-
131-
out << folly::sformat("Background Tier 0 Total Classes Searched : {:,}", numBackgroundEvictorClasses) << std::endl;
140+
out << folly::sformat("Tier 0 Background Evicted items : {:,}",
141+
backgndEvicStats.nEvictedItems) << std::endl;
142+
out << folly::sformat("Tier 0 Background Evicted items from schedule : {:,}",
143+
backgndEvicStats.nEvictedItemsFromSchedule) << std::endl;
144+
out << folly::sformat("Tier 0 Background Traversals : {:,}",
145+
backgndEvicStats.nTraversals) << std::endl;
146+
out << folly::sformat("Tier 0 Total Classes : {:,}",
147+
backgndEvicStats.nClasses) << std::endl;
148+
out << folly::sformat("Tier 0 Background Evicted Size : {:,}",
149+
backgndEvicStats.evictionSize) << std::endl;
132150

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

0 commit comments

Comments
 (0)