Skip to content

Commit 4e04c43

Browse files
Jiayue Baofacebook-github-bot
authored andcommitted
Add counter to track global evictions across RAM and NVM cache
Summary: When HybridCache is enabled, we don't have a global eviction counter (i.e. when an item is leaving the cache entirely due to eviction). - `ram.evictions`: item evicted from ram cache but it can be inserted into nvm cache - `nvm.evictions`: item evicted from nvm cache but it can be still in ram cache - `ram.destructors`: item leaves the cache entirely either because of removal or eviction from ram; must enable itemDestructor - `nvm.destructors` : item leaves the cache entirely either because of removal or eviction from nvm; must enable itemDestructor In this diff, we add `cache.evictions`: item leaves the cache entirely because of evictions from ram or nvm. No need to enable itemDestructor. Reviewed By: therealgymmy, jaesoo-fb Differential Revision: D40909473 fbshipit-source-id: 4d61d1434f073c95ea59da3abfd4152b17b0f67c
1 parent dd70021 commit 4e04c43

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -761,17 +761,27 @@ CacheAllocator<CacheTrait>::releaseBackToAllocator(Item& it,
761761

762762
// only skip destructor for evicted items that are either in the queue to put
763763
// into nvm or already in nvm
764-
if (!nascent && config_.itemDestructor &&
765-
(ctx != RemoveContext::kEviction || !it.isNvmClean() ||
766-
it.isNvmEvicted())) {
767-
try {
768-
config_.itemDestructor(DestructorData{
769-
ctx, it, viewAsChainedAllocsRange(it), allocInfo.poolId});
770-
stats().numRamDestructorCalls.inc();
771-
} catch (const std::exception& e) {
772-
stats().numDestructorExceptions.inc();
773-
XLOG_EVERY_N(INFO, 100)
774-
<< "Catch exception from user's item destructor: " << e.what();
764+
bool skipDestructor =
765+
nascent || (ctx == RemoveContext::kEviction &&
766+
// When this item is queued for NvmCache, it will be marked
767+
// as clean and the NvmEvicted bit will also be set to false.
768+
// Refer to NvmCache::put()
769+
it.isNvmClean() && !it.isNvmEvicted());
770+
if (!skipDestructor) {
771+
if (ctx == RemoveContext::kEviction) {
772+
stats().numCacheEvictions.inc();
773+
}
774+
// execute ItemDestructor
775+
if (config_.itemDestructor) {
776+
try {
777+
config_.itemDestructor(DestructorData{
778+
ctx, it, viewAsChainedAllocsRange(it), allocInfo.poolId});
779+
stats().numRamDestructorCalls.inc();
780+
} catch (const std::exception& e) {
781+
stats().numDestructorExceptions.inc();
782+
XLOG_EVERY_N(INFO, 100)
783+
<< "Catch exception from user's item destructor: " << e.what();
784+
}
775785
}
776786
}
777787

cachelib/allocator/CacheStats.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ struct SizeVerify {};
5151

5252
void Stats::populateGlobalCacheStats(GlobalCacheStats& ret) const {
5353
#ifndef SKIP_SIZE_VERIFY
54-
SizeVerify<sizeof(Stats)> a = SizeVerify<16160>{};
54+
SizeVerify<sizeof(Stats)> a = SizeVerify<16176>{};
5555
std::ignore = a;
5656
#endif
5757
ret.numCacheGets = numCacheGets.get();
5858
ret.numCacheGetMiss = numCacheGetMiss.get();
5959
ret.numCacheGetExpiries = numCacheGetExpiries.get();
6060
ret.numCacheRemoves = numCacheRemoves.get();
6161
ret.numCacheRemoveRamHits = numCacheRemoveRamHits.get();
62+
ret.numCacheEvictions = numCacheEvictions.get();
6263
ret.numRamDestructorCalls = numRamDestructorCalls.get();
6364
ret.numDestructorExceptions = numDestructorExceptions.get();
6465

cachelib/allocator/CacheStats.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ struct GlobalCacheStats {
377377
// number of evictions from NvmCache
378378
uint64_t numNvmEvictions{0};
379379

380+
// number of evictions where items leave both RAM and NvmCache entirely
381+
uint64_t numCacheEvictions{0};
382+
380383
// number of evictions from nvm that found an inconsistent state in RAM
381384
uint64_t numNvmUncleanEvict{0};
382385

cachelib/allocator/CacheStatsInternal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ struct Stats {
5151
// number of remove calls that resulted in a ram hit
5252
TLCounter numCacheRemoveRamHits{0};
5353

54+
// number of evictions where items leave both RAM and NvmCache entirely
55+
AtomicCounter numCacheEvictions{0};
56+
5457
// number of item destructor calls from ram
5558
TLCounter numRamDestructorCalls{0};
5659

cachelib/allocator/nvmcache/NvmCache-inl.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,15 @@ void NvmCache<C>::evictCB(HashedKey hk,
381381
}
382382
}
383383

384+
if (!needDestructor) {
385+
return;
386+
}
387+
388+
if (event != cachelib::navy::DestructorEvent::Removed) {
389+
stats().numCacheEvictions.inc();
390+
}
384391
// ItemDestructor
385-
if (itemDestructor_ && needDestructor) {
392+
if (itemDestructor_) {
386393
// create the item on heap instead of memory pool to avoid allocation
387394
// failure and evictions from cache for a temporary item.
388395
auto iobuf = createItemAsIOBuf(hk.key(), nvmItem);

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ Stats Cache<Allocator>::getStats() const {
609609

610610
ret.numCacheGets = cacheStats.numCacheGets;
611611
ret.numCacheGetMiss = cacheStats.numCacheGetMiss;
612+
ret.numCacheEvictions = cacheStats.numCacheEvictions;
612613
ret.numRamDestructorCalls = cacheStats.numRamDestructorCalls;
613614
ret.numNvmGets = cacheStats.numNvmGets;
614615
ret.numNvmGetMiss = cacheStats.numNvmGetMiss;

cachelib/cachebench/cache/CacheStats.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct Stats {
3737

3838
uint64_t numCacheGets{0};
3939
uint64_t numCacheGetMiss{0};
40+
uint64_t numCacheEvictions{0};
4041
uint64_t numRamDestructorCalls{0};
4142
uint64_t numNvmGets{0};
4243
uint64_t numNvmGetMiss{0};
@@ -293,6 +294,11 @@ struct Stats {
293294
numRamDestructorCalls, numNvmDestructorCalls)
294295
<< std::endl;
295296
}
297+
298+
if (numCacheEvictions > 0) {
299+
out << folly::sformat("Total eviction executed {}", numCacheEvictions)
300+
<< std::endl;
301+
}
296302
}
297303

298304
uint64_t getTotalMisses() const {

0 commit comments

Comments
 (0)