Skip to content

Commit 8d4f1a9

Browse files
committed
actually enable background workers + improved test
1 parent d8c58ff commit 8d4f1a9

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,18 @@ void CacheAllocator<CacheTrait>::initWorkers() {
302302
config_.poolOptimizeStrategy,
303303
config_.ccacheOptimizeStepSizePercent);
304304
}
305+
306+
if (config_.backgroundEvictorEnabled()) {
307+
startNewBackgroundEvictor(config_.backgroundEvictorInterval,
308+
config_.backgroundEvictorStrategy,
309+
config_.backgroundEvictorThreads);
310+
}
311+
312+
if (config_.backgroundPromoterEnabled()) {
313+
startNewBackgroundPromoter(config_.backgroundPromoterInterval,
314+
config_.backgroundPromoterStrategy,
315+
config_.backgroundPromoterThreads);
316+
}
305317
}
306318

307319
template <typename CacheTrait>
@@ -2424,6 +2436,16 @@ PoolId CacheAllocator<CacheTrait>::addPool(
24242436
setRebalanceStrategy(pid, std::move(rebalanceStrategy));
24252437
setResizeStrategy(pid, std::move(resizeStrategy));
24262438

2439+
if (backgroundEvictor_.size()) {
2440+
for (size_t id = 0; id < backgroundEvictor_.size(); id++)
2441+
backgroundEvictor_[id]->setAssignedMemory(getAssignedMemoryToBgWorker(id, backgroundEvictor_.size(), 0));
2442+
}
2443+
2444+
if (backgroundPromoter_.size()) {
2445+
for (size_t id = 0; id < backgroundPromoter_.size(); id++)
2446+
backgroundPromoter_[id]->setAssignedMemory(getAssignedMemoryToBgWorker(id, backgroundPromoter_.size(), 1));
2447+
}
2448+
24272449
return pid;
24282450
}
24292451

cachelib/allocator/CacheAllocatorConfig.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,17 @@ class CacheAllocatorConfig {
365365
poolOptimizeStrategy != nullptr;
366366
}
367367

368+
// @return whether background evictor thread is enabled
369+
bool backgroundEvictorEnabled() const noexcept {
370+
return backgroundEvictorInterval.count() > 0 &&
371+
backgroundEvictorStrategy != nullptr;
372+
}
373+
374+
bool backgroundPromoterEnabled() const noexcept {
375+
return backgroundPromoterInterval.count() > 0 &&
376+
backgroundPromoterStrategy != nullptr;
377+
}
378+
368379
// @return whether memory monitor is enabled
369380
bool memMonitoringEnabled() const noexcept {
370381
return memMonitorConfig.mode != MemoryMonitor::Disabled &&

cachelib/allocator/tests/AllocatorMemoryTiersTest.h

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
6767

6868
void testMultiTiersBackgroundMovers() {
6969
typename AllocatorT::Config config;
70-
config.setCacheSize(4 * Slab::kSize);
70+
config.setCacheSize(10 * Slab::kSize);
7171
config.enableCachePersistence("/tmp");
7272
config.usePosixForShm();
7373
config.configureMemoryTiers({
@@ -76,47 +76,42 @@ class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
7676
MemoryTierCacheConfig::fromFile("/tmp/b" + std::to_string(::getpid()))
7777
.setRatio(1)
7878
});
79-
config.enableBackgroundEvictor(std::make_shared<FreeThresholdStrategy>(10, 20, 4, 2),
79+
config.enableBackgroundEvictor(std::make_shared<FreeThresholdStrategy>(2, 10, 100, 40),
8080
std::chrono::milliseconds(10),1);
8181
config.enableBackgroundPromoter(std::make_shared<PromotionStrategy>(5, 4, 2),
8282
std::chrono::milliseconds(10),1);
8383

8484
auto allocator = std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config);
8585
ASSERT(allocator != nullptr);
86-
8786
const size_t numBytes = allocator->getCacheMemoryStats().cacheSize;
88-
const size_t kItemSize = 100;
89-
auto poolId = allocator->addPool("default", numBytes);
90-
91-
const int numItems = 10000;
92-
93-
int numAllocatedItems = 0;
94-
for (unsigned int i = 0; i < numItems; i++) {
95-
auto handle = util::allocateAccessible(
96-
*allocator, poolId, folly::to<std::string>(i), kItemSize, 0);
97-
++numAllocatedItems;
98-
}
9987

100-
ASSERT_GT(numAllocatedItems, 0);
88+
auto poolId = allocator->addPool("default", numBytes);
10189

10290
const unsigned int keyLen = 100;
103-
const unsigned int nSizes = 10;
104-
const auto sizes =
105-
this->getValidAllocSizes(*allocator, poolId, nSizes, keyLen);
91+
std::vector<uint32_t> sizes = {100};
10692
this->fillUpPoolUntilEvictions(*allocator, poolId, sizes, keyLen);
93+
94+
const auto key = this->getRandomNewKey(*allocator, keyLen);
95+
auto handle = util::allocateAccessible(*allocator, poolId, key, sizes[0]);
96+
ASSERT_NE(nullptr, handle);
97+
const uint8_t cid = allocator->getAllocInfo(handle->getMemory()).classId;
98+
99+
//wait for bg movers
100+
std::this_thread::sleep_for(std::chrono::seconds(1));
107101

108102
auto stats = allocator->getGlobalCacheStats();
109103
auto perclassEstats = allocator->getBackgroundMoverClassStats(MoverDir::Evict);
110104
auto perclassPstats = allocator->getBackgroundMoverClassStats(MoverDir::Promote);
111105

112-
EXPECT_GT(1, stats.evictionStats.numMovedItems);
113-
EXPECT_GT(1, stats.promotionStats.numMovedItems);
106+
EXPECT_GT(stats.evictionStats.numMovedItems,1);
107+
EXPECT_GT(stats.evictionStats.runCount,1);
108+
EXPECT_GT(stats.promotionStats.numMovedItems,1);
114109

115-
auto cid = 2;
116-
EXPECT_GT(1, perclassEstats[0][0][cid]);
117-
EXPECT_GT(1, perclassPstats[1][0][cid]);
110+
EXPECT_GT(perclassEstats[0][0][cid], 1);
111+
EXPECT_GT(perclassPstats[1][0][cid], 1);
118112

119113
auto slabStats = allocator->getAllocationClassStats(0,0,cid);
114+
120115
ASSERT_GE(slabStats.approxFreePercent,10);
121116
}
122117

0 commit comments

Comments
 (0)