Skip to content

Commit 75c9acc

Browse files
byrnedjvinser52
authored andcommitted
This commit contains the additional memory tiers tests
for different pool sizes. We also use getPoolSize(pid), to get total size from all pools across allocators. It also fixes the tiering sizes (pulls changes from what was issue75 rebased commit that did not make it into upstream commits). Rebased to use ramCacheSize.
1 parent 3874c16 commit 75c9acc

File tree

5 files changed

+155
-13
lines changed

5 files changed

+155
-13
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts(TierId tid) {
119119
return opts;
120120
}
121121

122+
template <typename CacheTrait>
123+
size_t CacheAllocator<CacheTrait>::memoryTierSize(TierId tid) const {
124+
auto partitions = std::accumulate(memoryTierConfigs.begin(), memoryTierConfigs.end(), 0UL,
125+
[](const size_t i, const MemoryTierCacheConfig& config){
126+
return i + config.getRatio();
127+
});
128+
129+
return memoryTierConfigs[tid].calculateTierSize(config_.getCacheSize(), partitions);
130+
}
131+
122132
template <typename CacheTrait>
123133
std::vector<std::unique_ptr<MemoryAllocator>>
124134
CacheAllocator<CacheTrait>::createPrivateAllocator() {
@@ -140,14 +150,15 @@ CacheAllocator<CacheTrait>::createPrivateAllocator() {
140150
template <typename CacheTrait>
141151
std::unique_ptr<MemoryAllocator>
142152
CacheAllocator<CacheTrait>::createNewMemoryAllocator(TierId tid) {
153+
size_t tierSize = memoryTierSize(tid);
143154
return std::make_unique<MemoryAllocator>(
144155
getAllocatorConfig(config_),
145156
shmManager_
146157
->createShm(detail::kShmCacheName + std::to_string(tid),
147-
config_.getCacheSize(), config_.slabMemoryBaseAddr,
158+
tierSize, config_.slabMemoryBaseAddr,
148159
createShmCacheOpts(tid))
149160
.addr,
150-
config_.getCacheSize());
161+
tierSize);
151162
}
152163

153164
template <typename CacheTrait>
@@ -158,7 +169,7 @@ CacheAllocator<CacheTrait>::restoreMemoryAllocator(TierId tid) {
158169
shmManager_
159170
->attachShm(detail::kShmCacheName + std::to_string(tid),
160171
config_.slabMemoryBaseAddr, createShmCacheOpts(tid)).addr,
161-
config_.getCacheSize(),
172+
memoryTierSize(tid),
162173
config_.disableFullCoredump);
163174
}
164175

@@ -2391,6 +2402,16 @@ const std::string CacheAllocator<CacheTrait>::getCacheName() const {
23912402
return config_.cacheName;
23922403
}
23932404

2405+
template <typename CacheTrait>
2406+
size_t CacheAllocator<CacheTrait>::getPoolSize(PoolId poolId) const {
2407+
size_t poolSize = 0;
2408+
for (auto& allocator: allocator_) {
2409+
const auto& pool = allocator->getPool(poolId);
2410+
poolSize += pool.getPoolSize();
2411+
}
2412+
return poolSize;
2413+
}
2414+
23942415
template <typename CacheTrait>
23952416
PoolStats CacheAllocator<CacheTrait>::getPoolStats(PoolId poolId) const {
23962417
const auto& pool = allocator_[currentTier()]->getPool(poolId);
@@ -3443,9 +3464,12 @@ GlobalCacheStats CacheAllocator<CacheTrait>::getGlobalCacheStats() const {
34433464

34443465
template <typename CacheTrait>
34453466
CacheMemoryStats CacheAllocator<CacheTrait>::getCacheMemoryStats() const {
3446-
const auto configuredTotalCacheSize = allocator_[currentTier()]->getMemorySizeInclAdvised();
3447-
const auto totalCacheSize = allocator_[currentTier()]->getMemorySize();
3448-
3467+
size_t totalCacheSize = 0;
3468+
size_t configuredTotalCacheSize = 0;
3469+
for(auto& allocator: allocator_) {
3470+
totalCacheSize += allocator->getMemorySize();
3471+
configuredTotalCacheSize += allocator->getMemorySizeInclAdvised();
3472+
}
34493473
auto addSize = [this](size_t a, PoolId pid) {
34503474
return a + allocator_[currentTier()]->getPool(pid).getPoolSize();
34513475
};

cachelib/allocator/CacheAllocator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,8 @@ class CacheAllocator : public CacheBase {
20352035
return config_.memoryTierConfigs.size();
20362036
}
20372037

2038+
size_t memoryTierSize(TierId tid) const;
2039+
20382040
// Whether the memory allocator for this cache allocator was created on shared
20392041
// memory. The hash table, chained item hash table etc is also created on
20402042
// shared memory except for temporary shared memory mode when they're created

cachelib/allocator/tests/AllocatorMemoryTiersTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ namespace tests {
2323
using LruAllocatorMemoryTiersTest = AllocatorMemoryTiersTest<LruAllocator>;
2424

2525
// TODO(MEMORY_TIER): add more tests with different eviction policies
26-
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValid1) {
27-
this->testMultiTiersValid1();
28-
}
26+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersInvalid) { this->testMultiTiersInvalid(); }
27+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValid) { this->testMultiTiersValid(); }
28+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValidMixed) { this->testMultiTiersValidMixed(); }
2929

3030
} // end of namespace tests
3131
} // end of namespace cachelib

cachelib/allocator/tests/AllocatorMemoryTiersTest.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace tests {
2727
template <typename AllocatorT>
2828
class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
2929
public:
30-
void testMultiTiersValid1() {
30+
void testMultiTiersInvalid() {
3131
typename AllocatorT::Config config;
3232
config.setCacheSize(100 * Slab::kSize);
3333
ASSERT_NO_THROW(config.configureMemoryTiers(
@@ -36,6 +36,44 @@ class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
3636
MemoryTierCacheConfig::fromShm().setRatio(1).setMemBind(
3737
std::string("0"))}));
3838
}
39+
40+
void testMultiTiersValid() {
41+
typename AllocatorT::Config config;
42+
config.setCacheSize(100 * Slab::kSize);
43+
config.enableCachePersistence("/tmp");
44+
ASSERT_NO_THROW(config.configureMemoryTiers(
45+
{MemoryTierCacheConfig::fromShm().setRatio(1).setMemBind(
46+
std::string("0")),
47+
MemoryTierCacheConfig::fromShm().setRatio(1).setMemBind(
48+
std::string("0"))}));
49+
50+
auto alloc = std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config);
51+
ASSERT(alloc != nullptr);
52+
53+
auto pool = alloc->addPool("default", alloc->getCacheMemoryStats().ramCacheSize);
54+
auto handle = alloc->allocate(pool, "key", std::string("value").size());
55+
ASSERT(handle != nullptr);
56+
ASSERT_NO_THROW(alloc->insertOrReplace(handle));
57+
}
58+
59+
void testMultiTiersValidMixed() {
60+
typename AllocatorT::Config config;
61+
config.setCacheSize(100 * Slab::kSize);
62+
config.enableCachePersistence("/tmp");
63+
ASSERT_NO_THROW(config.configureMemoryTiers(
64+
{MemoryTierCacheConfig::fromShm().setRatio(1).setMemBind(
65+
std::string("0")),
66+
MemoryTierCacheConfig::fromShm().setRatio(1).setMemBind(
67+
std::string("0"))}));
68+
69+
auto alloc = std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config);
70+
ASSERT(alloc != nullptr);
71+
72+
auto pool = alloc->addPool("default", alloc->getCacheMemoryStats().ramCacheSize);
73+
auto handle = alloc->allocate(pool, "key", std::string("value").size());
74+
ASSERT(handle != nullptr);
75+
ASSERT_NO_THROW(alloc->insertOrReplace(handle));
76+
}
3977
};
4078
} // namespace tests
4179
} // namespace cachelib

cachelib/allocator/tests/MemoryTiersTest.cpp

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class MemoryTiersTest : public AllocatorTest<Allocator> {
109109
void validatePoolSize(PoolId poolId,
110110
std::unique_ptr<LruAllocator>& allocator,
111111
size_t expectedSize) {
112-
size_t actualSize = allocator->getPool(poolId).getPoolSize();
112+
size_t actualSize = allocator->getPoolSize(poolId);
113113
EXPECT_EQ(actualSize, expectedSize);
114114
}
115115

@@ -119,9 +119,9 @@ class MemoryTiersTest : public AllocatorTest<Allocator> {
119119
size_t numTiers = 2) {
120120
if (isSizeValid) {
121121
auto pool = alloc->addPool("validPoolSize", poolSize);
122-
EXPECT_LE(alloc->getPool(pool).getPoolSize(), poolSize);
122+
EXPECT_LE(alloc->getPoolSize(pool), poolSize);
123123
if (poolSize >= numTiers * Slab::kSize)
124-
EXPECT_GE(alloc->getPool(pool).getPoolSize(),
124+
EXPECT_GE(alloc->getPoolSize(pool),
125125
poolSize - numTiers * Slab::kSize);
126126
} else {
127127
EXPECT_THROW(alloc->addPool("invalidPoolSize", poolSize),
@@ -172,6 +172,84 @@ TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigRatioNotSet) {
172172
TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigSizesNeCacheSize) {
173173
EXPECT_THROW(createTestCacheConfig({0, 0}), std::invalid_argument);
174174
}
175+
176+
TEST_F(LruMemoryTiersTest, TestPoolAllocations) {
177+
std::vector<size_t> totalCacheSizes = {8 * GB, 2 * GB};
178+
179+
static const size_t numExtraSizes = 4;
180+
static const size_t numExtraSlabs = 20;
181+
182+
for (size_t i = 0; i < numExtraSizes; i++) {
183+
totalCacheSizes.push_back(totalCacheSizes.back() +
184+
(folly::Random::rand64() % numExtraSlabs) *
185+
Slab::kSize);
186+
}
187+
188+
size_t min_ratio = 1;
189+
size_t max_ratio = 111;
190+
191+
static const size_t numCombinations = 10;
192+
193+
for (auto totalCacheSize : totalCacheSizes) {
194+
for (size_t k = 0; k < numCombinations; k++) {
195+
const size_t i = folly::Random::rand32() % max_ratio + min_ratio;
196+
const size_t j = folly::Random::rand32() % max_ratio + min_ratio;
197+
LruAllocatorConfig cfg =
198+
createTestCacheConfig({i, j},
199+
/* usePoisx */ true, totalCacheSize);
200+
basicCheck(cfg, totalCacheSize);
201+
202+
std::unique_ptr<LruAllocator> alloc = std::unique_ptr<LruAllocator>(
203+
new LruAllocator(LruAllocator::SharedMemNew, cfg));
204+
205+
size_t size = (folly::Random::rand64() %
206+
(alloc->getCacheMemoryStats().ramCacheSize - Slab::kSize)) +
207+
Slab::kSize;
208+
testAddPool(alloc, size, true);
209+
}
210+
}
211+
}
212+
213+
TEST_F(LruMemoryTiersTest, TestPoolInvalidAllocations) {
214+
std::vector<size_t> totalCacheSizes = {48 * MB, 51 * MB, 256 * MB,
215+
1 * GB, 5 * GB, 8 * GB};
216+
size_t min_ratio = 1;
217+
size_t max_ratio = 111;
218+
219+
static const size_t numCombinations = 10;
220+
221+
for (auto totalCacheSize : totalCacheSizes) {
222+
for (size_t k = 0; k < numCombinations; k++) {
223+
const size_t i = folly::Random::rand32() % max_ratio + min_ratio;
224+
const size_t j = folly::Random::rand32() % max_ratio + min_ratio;
225+
LruAllocatorConfig cfg =
226+
createTestCacheConfig({i, j},
227+
/* usePoisx */ true, totalCacheSize);
228+
229+
std::unique_ptr<LruAllocator> alloc = nullptr;
230+
try {
231+
alloc = std::unique_ptr<LruAllocator>(
232+
new LruAllocator(LruAllocator::SharedMemNew, cfg));
233+
} catch(...) {
234+
// expection only if cache too small
235+
size_t sum_ratios = std::accumulate(
236+
cfg.getMemoryTierConfigs().begin(), cfg.getMemoryTierConfigs().end(), 0UL,
237+
[](const size_t i, const MemoryTierCacheConfig& config) {
238+
return i + config.getRatio();
239+
});
240+
auto tier1slabs = cfg.getMemoryTierConfigs()[0].calculateTierSize(cfg.getCacheSize(), sum_ratios) / Slab::kSize;
241+
auto tier2slabs = cfg.getMemoryTierConfigs()[1].calculateTierSize(cfg.getCacheSize(), sum_ratios) / Slab::kSize;
242+
EXPECT_TRUE(tier1slabs <= 2 || tier2slabs <= 2);
243+
244+
continue;
245+
}
246+
247+
size_t size = (folly::Random::rand64() % (100 * GB)) +
248+
alloc->getCacheMemoryStats().ramCacheSize;
249+
testAddPool(alloc, size, false);
250+
}
251+
}
252+
}
175253
} // namespace tests
176254
} // namespace cachelib
177255
} // namespace facebook

0 commit comments

Comments
 (0)