Skip to content

Commit 6d9985d

Browse files
igchorbyrnedj
authored andcommitted
Add memory usage statistics for allocation classes
This includes printing: - allocSize - allocated memory size - memory usage fraction
1 parent caf3d60 commit 6d9985d

File tree

6 files changed

+37
-10
lines changed

6 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.h

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

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

@@ -4689,6 +4692,14 @@ PoolStats CacheAllocator<CacheTrait>::getPoolStats(PoolId poolId) const {
46894692
return ret;
46904693
}
46914694

4695+
template <typename CacheTrait>
4696+
ACStats CacheAllocator<CacheTrait>::getACStats(PoolId poolId,
4697+
ClassId classId) const {
4698+
const auto& pool = allocator_->getPool(poolId);
4699+
const auto& ac = pool.getAllocationClass(classId);
4700+
return ac.getStats();
4701+
}
4702+
46924703
template <typename CacheTrait>
46934704
PoolEvictionAgeStats CacheAllocator<CacheTrait>::getPoolEvictionAgeStats(
46944705
PoolId pid, unsigned int slabProjectionLength) const {

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
@@ -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
@@ -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)