Skip to content

Commit 8ed0c95

Browse files
igchorvinser52
authored andcommitted
Add memory usage statistics for allocation classes
This includes printing: - allocSize - allocated memory size - memory usage fraction
1 parent 5d46690 commit 8ed0c95

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
@@ -2312,6 +2312,14 @@ PoolStats CacheAllocator<CacheTrait>::getPoolStats(PoolId poolId) const {
23122312
return ret;
23132313
}
23142314

2315+
template <typename CacheTrait>
2316+
ACStats CacheAllocator<CacheTrait>::getACStats(PoolId poolId,
2317+
ClassId classId) const {
2318+
const auto& pool = allocator_->getPool(poolId);
2319+
const auto& ac = pool.getAllocationClass(classId);
2320+
return ac.getStats();
2321+
}
2322+
23152323
template <typename CacheTrait>
23162324
PoolEvictionAgeStats CacheAllocator<CacheTrait>::getPoolEvictionAgeStats(
23172325
PoolId pid, unsigned int slabProjectionLength) const {

cachelib/allocator/CacheAllocator.h

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

1188+
// return stats for Allocation Class
1189+
ACStats getACStats(PoolId pid, ClassId cid) const override final;
1190+
11881191
// return the nvm cache stats map
11891192
util::StatsMap getNvmCacheStatsMap() const override final;
11901193

cachelib/allocator/memory/MemoryAllocatorStats.h

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

5970
// 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
@@ -325,6 +325,10 @@ class Cache {
325325
// return the stats for the pool.
326326
PoolStats getPoolStats(PoolId pid) const { return cache_->getPoolStats(pid); }
327327

328+
ACStats getACStats(PoolId pid, ClassId cid) const {
329+
return cache_->getACStats(pid, cid);
330+
}
331+
328332
// return the total number of inconsistent operations detected since start.
329333
unsigned int getInconsistencyCount() const {
330334
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
@@ -165,7 +165,7 @@ struct Stats {
165165
foreachAC([&](auto pid, auto cid, auto stats) {
166166
auto [allocSizeSuffix, allocSize] = formatMemory(stats.allocSize);
167167
auto [memorySizeSuffix, memorySize] =
168-
formatMemory(stats.activeAllocs * stats.allocSize);
168+
formatMemory(stats.totalAllocatedSize());
169169
out << folly::sformat("pid{:2} cid{:4} {:8.2f}{} memorySize: {:8.2f}{}",
170170
pid, cid, allocSize, allocSizeSuffix, memorySize,
171171
memorySizeSuffix)
@@ -177,15 +177,9 @@ struct Stats {
177177

178178
// If the pool is not full, extrapolate usageFraction for AC assuming it
179179
// will grow at the same rate. This value will be the same for all ACs.
180-
double acUsageFraction;
181-
if (poolUsageFraction[pid] < 1.0) {
182-
acUsageFraction = poolUsageFraction[pid];
183-
} else if (stats.usedSlabs == 0) {
184-
acUsageFraction = 0.0;
185-
} else {
186-
acUsageFraction =
187-
stats.activeAllocs / (stats.usedSlabs * stats.allocsPerSlab);
188-
}
180+
auto acUsageFraction = (poolUsageFraction[pid] < 1.0)
181+
? poolUsageFraction[pid]
182+
: stats.usageFraction();
189183

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

0 commit comments

Comments
 (0)