Skip to content

Commit 36347e2

Browse files
igchorvinser52
authored andcommitted
Add memory usage statistics for allocation classes
This includes printing: - allocSize - allocated memory size - memory usage fraction
1 parent 7b01549 commit 36347e2

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed

cachelib/allocator/Cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ class CacheBase {
102102
// @param poolId the pool id
103103
virtual PoolStats getPoolStats(PoolId poolId) const = 0;
104104

105+
// Get Allocation Class specific stats.
106+
//
107+
// @param poolId the pool id
108+
// @param classId the class id
109+
virtual ACStats getACStats(PoolId poolId, ClassId classId) const = 0;
110+
105111
// @param poolId the pool id
106112
virtual AllSlabReleaseEvents getAllSlabReleaseEvents(PoolId poolId) const = 0;
107113

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,14 @@ PoolStats CacheAllocator<CacheTrait>::getPoolStats(PoolId poolId) const {
23722372
return ret;
23732373
}
23742374

2375+
template <typename CacheTrait>
2376+
ACStats CacheAllocator<CacheTrait>::getACStats(PoolId poolId,
2377+
ClassId classId) const {
2378+
const auto& pool = allocator_->getPool(poolId);
2379+
const auto& ac = pool.getAllocationClass(classId);
2380+
return ac.getStats();
2381+
}
2382+
23752383
template <typename CacheTrait>
23762384
PoolEvictionAgeStats CacheAllocator<CacheTrait>::getPoolEvictionAgeStats(
23772385
PoolId pid, unsigned int slabProjectionLength) const {

cachelib/allocator/CacheAllocator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,9 @@ class CacheAllocator : public CacheBase {
12141214
// return cache's memory usage stats
12151215
CacheMemoryStats getCacheMemoryStats() const override final;
12161216

1217+
// return stats for Allocation Class
1218+
ACStats getACStats(PoolId pid, ClassId cid) const override final;
1219+
12171220
// return the nvm cache stats map
12181221
util::StatsMap getNvmCacheStatsMap() const override final;
12191222

cachelib/allocator/memory/MemoryAllocatorStats.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ struct ACStats {
5656
constexpr size_t getTotalFreeMemory() const noexcept {
5757
return Slab::kSize * freeSlabs + freeAllocs * allocSize;
5858
}
59+
60+
constexpr double usageFraction() const noexcept {
61+
if (usedSlabs == 0)
62+
return 0.0;
63+
64+
return activeAllocs / (usedSlabs * allocsPerSlab);
65+
}
66+
67+
constexpr size_t totalAllocatedSize() const noexcept {
68+
return activeAllocs * allocSize;
69+
}
5970
};
6071

6172
// structure to query stats corresponding to a MemoryPool

cachelib/allocator/tests/CacheBaseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CacheBaseTest : public CacheBase, public SlabAllocatorTestBase {
3434
bool isObjectCache() const override { return false; }
3535
const MemoryPool& getPool(PoolId) const override { return memoryPool_; }
3636
PoolStats getPoolStats(PoolId) const override { return PoolStats(); }
37+
ACStats getACStats(PoolId, ClassId) const { return ACStats(); };
3738
AllSlabReleaseEvents getAllSlabReleaseEvents(PoolId) const override {
3839
return AllSlabReleaseEvents{};
3940
}

cachelib/cachebench/cache/Cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ class Cache {
327327
// return the stats for the pool.
328328
PoolStats getPoolStats(PoolId pid) const { return cache_->getPoolStats(pid); }
329329

330+
ACStats getACStats(PoolId pid, ClassId cid) const {
331+
return cache_->getACStats(pid, cid);
332+
}
333+
330334
// return the total number of inconsistent operations detected since start.
331335
unsigned int getInconsistencyCount() const {
332336
return inconsistencyCount_.load(std::memory_order_relaxed);

cachelib/cachebench/cache/CacheStats.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ struct Stats {
194194
foreachAC(allocationClassStats, [&](auto pid, auto cid, auto stats) {
195195
auto [allocSizeSuffix, allocSize] = formatMemory(stats.allocSize);
196196
auto [memorySizeSuffix, memorySize] =
197-
formatMemory(stats.activeAllocs * stats.allocSize);
197+
formatMemory(stats.totalAllocatedSize());
198198
out << folly::sformat("pid{:2} cid{:4} {:8.2f}{} memorySize: {:8.2f}{}",
199199
pid, cid, allocSize, allocSizeSuffix, memorySize,
200200
memorySizeSuffix)
@@ -206,15 +206,9 @@ struct Stats {
206206

207207
// If the pool is not full, extrapolate usageFraction for AC assuming it
208208
// will grow at the same rate. This value will be the same for all ACs.
209-
double acUsageFraction;
210-
if (poolUsageFraction[pid] < 1.0) {
211-
acUsageFraction = poolUsageFraction[pid];
212-
} else if (stats.usedSlabs == 0) {
213-
acUsageFraction = 0.0;
214-
} else {
215-
acUsageFraction =
216-
stats.activeAllocs / (stats.usedSlabs * stats.allocsPerSlab);
217-
}
209+
auto acUsageFraction = (poolUsageFraction[pid] < 1.0)
210+
? poolUsageFraction[pid]
211+
: stats.usageFraction();
218212

219213
out << folly::sformat(
220214
"pid{:2} cid{:4} {:8.2f}{} usageFraction: {:4.2f}", pid, cid,

0 commit comments

Comments
 (0)