Skip to content

Commit 64ce2ed

Browse files
IvanTopolcicmeta-codesync[bot]
authored andcommitted
Add chain-aware getTotalAllocSize
Summary: Adds getTotalAllocSize which walks the chained item linked list and totals the allocation size of all chained items. Reviewed By: rlyerly Differential Revision: D110933811 fbshipit-source-id: 41a504a41e8fc16ceeab25de5f94079f714d4988
1 parent ea001e8 commit 64ce2ed

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

cachelib/allocator/CacheAllocator.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,16 @@ class CacheAllocator : public CacheBase {
11701170
return allocator_->getAllocInfo(memory);
11711171
}
11721172

1173+
// total allocated size of the item and all of its chained items.
1174+
uint64_t getTotalAllocSize(const Item& item,
1175+
folly::Range<ChainedItemIter> chainedItems) const {
1176+
uint64_t size = getAllocInfo(item.getMemory()).allocSize;
1177+
for (const auto& c : chainedItems) {
1178+
size += getAllocInfo(c.getMemory()).allocSize;
1179+
}
1180+
return size;
1181+
}
1182+
11731183
// return the ids for the set of existing pools in this cache.
11741184
std::set<PoolId> getPoolIds() const override final {
11751185
return allocator_->getPoolIds();

cachelib/allocator/tests/AllocatorTypeTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ TYPED_TEST(BaseAllocatorTest, ChainedAllocsIteration) {
342342
this->testChainedAllocsIteration();
343343
}
344344

345+
TYPED_TEST(BaseAllocatorTest, GetTotalAllocSize) {
346+
this->testGetTotalAllocSize();
347+
}
348+
345349
TYPED_TEST(BaseAllocatorTest, ReplaceChainedItem) {
346350
this->testReplaceChainedItem();
347351
}

cachelib/allocator/tests/BaseAllocatorTest.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,38 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
25012501
}
25022502
}
25032503

2504+
void testGetTotalAllocSize() {
2505+
typename AllocatorT::Config config;
2506+
config.configureChainedItems();
2507+
config.setCacheSize(10 * Slab::kSize);
2508+
AllocatorT alloc(config);
2509+
const size_t numBytes = alloc.getCacheMemoryStats().ramCacheSize;
2510+
auto poolId = alloc.addPool("foobar", numBytes);
2511+
2512+
auto parent = util::allocateAccessible(alloc, poolId, "parent", 100);
2513+
ASSERT_NE(nullptr, parent);
2514+
const int nChainedAllocs = 3;
2515+
for (int i = 0; i < nChainedAllocs; ++i) {
2516+
auto chained = alloc.allocateChainedItem(parent, 100 * (i + 1));
2517+
ASSERT_NE(nullptr, chained);
2518+
alloc.addChainedItem(parent, std::move(chained));
2519+
}
2520+
2521+
auto handle = alloc.find("parent");
2522+
auto chainedAllocs = alloc.viewAsChainedAllocs(handle);
2523+
auto chain = chainedAllocs.getChain();
2524+
2525+
// Independently sum the allocation-class size across the whole chain.
2526+
uint64_t expected = alloc.getAllocInfo(handle->getMemory()).allocSize;
2527+
for (const auto& c : chain) {
2528+
expected += alloc.getAllocInfo(c.getMemory()).allocSize;
2529+
}
2530+
// The chained parts must contribute beyond the parent's own block.
2531+
ASSERT_GT(expected, alloc.getAllocInfo(handle->getMemory()).allocSize);
2532+
2533+
ASSERT_EQ(expected, alloc.getTotalAllocSize(*handle, chain));
2534+
}
2535+
25042536
// create a chain of allocations, replace the allocation and ensure that the
25052537
// order is preserved (multithreaded version).
25062538
void testChainedAllocsReplaceInChainMultithread() {

0 commit comments

Comments
 (0)