Skip to content

Commit 1fee456

Browse files
committed
Added per pool per class used size metrics
(cherry picked from commit ea5b3a3)
1 parent 7531144 commit 1fee456

File tree

10 files changed

+48
-1
lines changed

10 files changed

+48
-1
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ CacheAllocator<CacheTrait>::allocateInternal(PoolId pid,
324324

325325
handle = acquire(new (memory) Item(key, size, creationTime, expiryTime));
326326
if (handle) {
327+
(*stats_.usedSize)[pid][cid].set(allocator_->getPoolUsedSize(pid));
327328
handle.markNascent();
328329
(*stats_.fragmentationSize)[pid][cid].add(
329330
util::getFragmentation(*this, *handle));
@@ -2239,7 +2240,7 @@ PoolStats CacheAllocator<CacheTrait>::getPoolStats(PoolId poolId) const {
22392240
(*stats_.fragmentationSize)[poolId][cid].get(), classHits,
22402241
(*stats_.chainedItemEvictions)[poolId][cid].get(),
22412242
(*stats_.regularItemEvictions)[poolId][cid].get(),
2242-
container.getStats()}});
2243+
(*stats_.usedSize)[poolId][cid].get(), container.getStats()}});
22432244
totalHits += classHits;
22442245
}
22452246
}

cachelib/allocator/CacheStats.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void Stats::init() {
2929
allocFailures = std::make_unique<PerPoolClassAtomicCounters>();
3030
chainedItemEvictions = std::make_unique<PerPoolClassAtomicCounters>();
3131
regularItemEvictions = std::make_unique<PerPoolClassAtomicCounters>();
32+
usedSize = std::make_unique<PerPoolClassAtomicCounters>();
3233
auto initToZero = [](auto& a) {
3334
for (auto& s : a) {
3435
for (auto& c : s) {
@@ -42,6 +43,7 @@ void Stats::init() {
4243
initToZero(*fragmentationSize);
4344
initToZero(*chainedItemEvictions);
4445
initToZero(*regularItemEvictions);
46+
initToZero(*usedSize);
4547
}
4648

4749
template <int>
@@ -125,6 +127,14 @@ void Stats::populateGlobalCacheStats(GlobalCacheStats& ret) const {
125127
ret.numEvictions = accum(*chainedItemEvictions);
126128
ret.numEvictions += accum(*regularItemEvictions);
127129

130+
for (const auto& x : *usedSize) {
131+
uint64_t sum{0};
132+
for (const auto& v : x) {
133+
sum += v.get();
134+
}
135+
ret.poolUsedSize.emplace_back(sum);
136+
}
137+
128138
ret.invalidAllocs = invalidAllocs.get();
129139
ret.numRefcountOverflow = numRefcountOverflow.get();
130140

@@ -176,6 +186,7 @@ PoolStats& PoolStats::operator+=(const PoolStats& other) {
176186
d.numHits += s.numHits;
177187
d.chainedItemEvictions += s.chainedItemEvictions;
178188
d.regularItemEvictions += s.regularItemEvictions;
189+
d.usedSize += s.usedSize;
179190
}
180191

181192
// aggregate container stats within CacheStat

cachelib/allocator/CacheStats.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ struct CacheStat {
121121
// number of regular items that were evicted from this classId
122122
uint64_t regularItemEvictions;
123123

124+
// used size of this classId
125+
uint64_t usedSize;
126+
124127
// the stats from the mm container
125128
MMContainerStat containerStat;
126129

@@ -416,6 +419,9 @@ struct GlobalCacheStats {
416419
// number of evictions across all the pools in the cache.
417420
uint64_t numEvictions{0};
418421

422+
// Used size of all the pools
423+
std::vector<uint64_t> poolUsedSize;
424+
419425
// number of allocation attempts with invalid input params.
420426
uint64_t invalidAllocs{0};
421427

cachelib/allocator/CacheStatsInternal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ struct Stats {
220220
std::unique_ptr<PerPoolClassAtomicCounters> fragmentationSize{};
221221
std::unique_ptr<PerPoolClassAtomicCounters> chainedItemEvictions{};
222222
std::unique_ptr<PerPoolClassAtomicCounters> regularItemEvictions{};
223+
std::unique_ptr<PerPoolClassAtomicCounters> usedSize{};
223224

224225
// Eviction failures due to parent cannot be removed from access container
225226
AtomicCounter evictFailParentAC{0};

cachelib/allocator/memory/MemoryAllocator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,12 @@ class MemoryAllocator {
633633
memoryPoolManager_.updateNumSlabsToAdvise(numSlabs);
634634
}
635635

636+
// fetch used size of a particular pool
637+
// @return used size of the pool
638+
size_t getPoolUsedSize(PoolId id) {
639+
return slabAllocator_.getPoolUsedSize(id);
640+
}
641+
636642
private:
637643
// @param memory pointer to the memory.
638644
// @return the MemoryPool corresponding to the memory.

cachelib/allocator/memory/SlabAllocator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ SlabAllocator::~SlabAllocator() {
9191
}
9292
}
9393

94+
size_t SlabAllocator::getPoolUsedSize(PoolId id) {
95+
if (id >= memoryPoolSize_.size()) {
96+
throw std::invalid_argument(folly::sformat("Invalid pool id {}.", id));
97+
}
98+
return memoryPoolSize_[id];
99+
}
100+
94101
void SlabAllocator::stopMemoryLocker() {
95102
if (memoryLocker_.joinable()) {
96103
stopLocking_ = true;

cachelib/allocator/memory/SlabAllocator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ class SlabAllocator {
316316
return PtrCompressor<PtrType, SlabAllocator>(*this);
317317
}
318318

319+
// Retrive used size of a pool
320+
// @return used size of Pool
321+
size_t getPoolUsedSize(PoolId id);
322+
319323
private:
320324
// null Slab* presenttation. With 4M Slab size, a valid slab index would never
321325
// reach 2^16 - 1;

cachelib/allocator/tests/AllocatorHitStatsTest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ class AllocatorHitStatsTest : public SlabAllocatorTestBase {
393393
ASSERT_EQ(nThreads * numHits, poolStats.numPoolGetHits);
394394
ASSERT_EQ(nThreads * (numHits + numMiss), globalCacheStats.numCacheGets);
395395
ASSERT_EQ(nThreads * numMiss, globalCacheStats.numCacheGetMiss);
396+
ASSERT_EQ(globalCacheStats.poolUsedSize.size(), MemoryPoolManager::kMaxPools);
397+
ASSERT_EQ(4521459712, globalCacheStats.poolUsedSize[0]);
398+
ASSERT_EQ(4521459712, globalCacheStats.poolUsedSize[1]);
396399

397400
uint64_t totalHits = 0;
398401
for (const auto& classId : poolStats.getClassIds()) {

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ Stats Cache<Allocator>::getStats() const {
461461
ret.allocAttempts = cacheStats.allocAttempts;
462462
ret.allocFailures = cacheStats.allocFailures;
463463

464+
ret.poolUsedSize = cacheStats.poolUsedSize;
464465
ret.numCacheGets = cacheStats.numCacheGets;
465466
ret.numCacheGetMiss = cacheStats.numCacheGetMiss;
466467
ret.numRamDestructorCalls = cacheStats.numRamDestructorCalls;

cachelib/cachebench/cache/CacheStats.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct Stats {
3232
uint64_t allocAttempts{0};
3333
uint64_t allocFailures{0};
3434

35+
std::vector<uint64_t> poolUsedSize;
3536
uint64_t numCacheGets{0};
3637
uint64_t numCacheGetMiss{0};
3738
uint64_t numRamDestructorCalls{0};
@@ -115,6 +116,12 @@ struct Stats {
115116
<< std::endl;
116117
out << folly::sformat("RAM Evictions : {:,}", numEvictions) << std::endl;
117118

119+
for (auto pid = 0U; pid < poolUsedSize.size(); pid++) {
120+
out << folly::sformat("Pool {:,} Used size : {:,} Bytes", pid,
121+
poolUsedSize[pid])
122+
<< std::endl;
123+
}
124+
118125
if (numCacheGets > 0) {
119126
out << folly::sformat("Cache Gets : {:,}", numCacheGets) << std::endl;
120127
out << folly::sformat("Hit Ratio : {:6.2f}%", overallHitRatio)

0 commit comments

Comments
 (0)