Skip to content

Commit abe622a

Browse files
guptaskvinser52
authored andcommitted
Track latency of per item eviction/promotion between memory tiers
1 parent d2c6b3b commit abe622a

File tree

9 files changed

+29
-9
lines changed

9 files changed

+29
-9
lines changed

cachelib/allocator/Cache.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ void CacheBase::updateGlobalCacheStats(const std::string& statPrefix) const {
477477

478478
visitEstimates(uploadStatsNanoToMicro, stats.allocateLatencyNs,
479479
statPrefix + "allocate.latency_us");
480+
visitEstimates(uploadStatsNanoToMicro, stats.bgEvictLatencyNs,
481+
statPrefix + "background.eviction.latency_us");
482+
visitEstimates(uploadStatsNanoToMicro, stats.bgPromoteLatencyNs,
483+
statPrefix + "background.promotion.latency_us");
480484
visitEstimates(uploadStatsNanoToMicro, stats.moveChainedLatencyNs,
481485
statPrefix + "move.chained.latency_us");
482486
visitEstimates(uploadStatsNanoToMicro, stats.moveRegularLatencyNs,

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,16 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
417417
uint32_t expiryTime,
418418
bool fromBgThread,
419419
bool evict) {
420-
util::LatencyTracker tracker{stats().allocateLatency_};
421-
420+
util::LatencyTracker tracker{stats().allocateLatency_, static_cast<size_t>(!fromBgThread)};
422421
SCOPE_FAIL { stats_.invalidAllocs.inc(); };
423422

424423
// number of bytes required for this item
425424
const auto requiredSize = Item::getRequiredSize(key, size);
426425

427426
// the allocation class in our memory allocator.
428427
const auto cid = allocator_[tid]->getAllocationClassId(pid, requiredSize);
429-
util::RollingLatencyTracker rollTracker{
430-
(*stats_.classAllocLatency)[tid][pid][cid]};
428+
429+
util::RollingLatencyTracker rollTracker{(*stats_.classAllocLatency)[tid][pid][cid]};
431430

432431
(*stats_.allocAttempts)[tid][pid][cid].inc();
433432

cachelib/allocator/CacheAllocator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,7 @@ class CacheAllocator : public CacheBase {
20202020
unsigned int pid,
20212021
unsigned int cid,
20222022
size_t batch) {
2023+
util::LatencyTracker tracker{stats().bgEvictLatency_, batch};
20232024
auto& mmContainer = getMMContainer(tid, pid, cid);
20242025
size_t evictions = 0;
20252026
size_t evictionCandidates = 0;
@@ -2096,6 +2097,7 @@ class CacheAllocator : public CacheBase {
20962097
unsigned int pid,
20972098
unsigned int cid,
20982099
size_t batch) {
2100+
util::LatencyTracker tracker{stats().bgPromoteLatency_, batch};
20992101
auto& mmContainer = getMMContainer(tid, pid, cid);
21002102
size_t promotions = 0;
21012103
std::vector<Item*> candidates;

cachelib/allocator/CacheStats.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ void Stats::populateGlobalCacheStats(GlobalCacheStats& ret) const {
105105
ret.numNvmItemDestructorAllocErrors = numNvmItemDestructorAllocErrors.get();
106106

107107
ret.allocateLatencyNs = this->allocateLatency_.estimate();
108+
ret.bgEvictLatencyNs = this->bgEvictLatency_.estimate();
109+
ret.bgPromoteLatencyNs = this->bgPromoteLatency_.estimate();
108110
ret.moveChainedLatencyNs = this->moveChainedLatency_.estimate();
109111
ret.moveRegularLatencyNs = this->moveRegularLatency_.estimate();
110112
ret.nvmLookupLatencyNs = this->nvmLookupLatency_.estimate();

cachelib/allocator/CacheStats.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ struct GlobalCacheStats {
512512

513513
// latency and percentile stats of various cachelib operations
514514
util::PercentileStats::Estimates allocateLatencyNs{};
515+
util::PercentileStats::Estimates bgEvictLatencyNs{};
516+
util::PercentileStats::Estimates bgPromoteLatencyNs{};
515517
util::PercentileStats::Estimates moveChainedLatencyNs{};
516518
util::PercentileStats::Estimates moveRegularLatencyNs{};
517519
util::PercentileStats::Estimates nvmLookupLatencyNs{};

cachelib/allocator/CacheStatsInternal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ struct Stats {
189189

190190
// latency stats of various cachelib operations
191191
mutable util::PercentileStats allocateLatency_;
192+
mutable util::PercentileStats bgEvictLatency_;
193+
mutable util::PercentileStats bgPromoteLatency_;
192194
mutable util::PercentileStats moveChainedLatency_;
193195
mutable util::PercentileStats moveRegularLatency_;
194196
mutable util::PercentileStats nvmLookupLatency_;

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ Stats Cache<Allocator>::getStats() const {
762762
static_cast<int64_t>(itemRecords_.count()) - totalDestructor_;
763763

764764
ret.cacheAllocateLatencyNs = cacheStats.allocateLatencyNs;
765+
ret.cacheBgEvictLatencyNs = cacheStats.bgEvictLatencyNs;
766+
ret.cacheBgPromoteLatencyNs = cacheStats.bgPromoteLatencyNs;
765767
ret.cacheFindLatencyNs = cacheFindLatency_.estimate();
766768

767769
// Populate counters.

cachelib/cachebench/cache/CacheStats.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ struct Stats {
9191
uint64_t numNvmItemRemovedSetSize{0};
9292

9393
util::PercentileStats::Estimates cacheAllocateLatencyNs;
94+
util::PercentileStats::Estimates cacheBgEvictLatencyNs;
95+
util::PercentileStats::Estimates cacheBgPromoteLatencyNs;
9496
util::PercentileStats::Estimates cacheFindLatencyNs;
9597

9698
double nvmReadLatencyMicrosP50{0};
@@ -282,6 +284,8 @@ struct Stats {
282284

283285
printLatencies("Cache Find API latency", cacheFindLatencyNs);
284286
printLatencies("Cache Allocate API latency", cacheAllocateLatencyNs);
287+
printLatencies("Cache Background Eviction API latency", cacheBgEvictLatencyNs);
288+
printLatencies("Cache Background Promotion API latency", cacheBgPromoteLatencyNs);
285289
}
286290
}
287291

@@ -528,6 +532,8 @@ struct Stats {
528532

529533
counters["find_latency_p99"] = cacheFindLatencyNs.p99;
530534
counters["alloc_latency_p99"] = cacheAllocateLatencyNs.p99;
535+
counters["bg_evict_latency_p99"] = cacheBgEvictLatencyNs.p99;
536+
counters["bg_promote_latency_p99"] = cacheBgPromoteLatencyNs.p99;
531537

532538
counters["ram_hit_rate"] = calcInvertPctFn(numCacheGetMiss, numCacheGets);
533539
counters["nvm_hit_rate"] = calcInvertPctFn(numCacheGetMiss, numCacheGets);

cachelib/common/PercentileStats.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,24 @@ class PercentileStats {
107107

108108
class LatencyTracker {
109109
public:
110-
explicit LatencyTracker(PercentileStats& stats)
111-
: stats_(&stats), begin_(std::chrono::steady_clock::now()) {}
110+
explicit LatencyTracker(PercentileStats& stats, size_t nSamples = 1)
111+
: stats_(&stats), nSamples_(nSamples), begin_(std::chrono::steady_clock::now()) {}
112112
LatencyTracker() {}
113113
~LatencyTracker() {
114-
if (stats_) {
114+
if (nSamples_ > 0 && stats_) {
115115
auto tp = std::chrono::steady_clock::now();
116116
auto diffNanos =
117117
std::chrono::duration_cast<std::chrono::nanoseconds>(tp - begin_)
118118
.count();
119-
stats_->trackValue(static_cast<double>(diffNanos), tp);
119+
stats_->trackValue(static_cast<double>(diffNanos/nSamples_), tp);
120120
}
121121
}
122122

123123
LatencyTracker(const LatencyTracker&) = delete;
124124
LatencyTracker& operator=(const LatencyTracker&) = delete;
125125

126126
LatencyTracker(LatencyTracker&& rhs) noexcept
127-
: stats_(rhs.stats_), begin_(rhs.begin_) {
127+
: stats_(rhs.stats_), nSamples_(rhs.nSamples_), begin_(rhs.begin_) {
128128
rhs.stats_ = nullptr;
129129
}
130130

@@ -138,6 +138,7 @@ class LatencyTracker {
138138

139139
private:
140140
PercentileStats* stats_{nullptr};
141+
size_t nSamples_{1};
141142
std::chrono::time_point<std::chrono::steady_clock> begin_;
142143
};
143144
} // namespace util

0 commit comments

Comments
 (0)