Skip to content

Commit 67b0d0b

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 19c6828 commit 67b0d0b

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
@@ -121,6 +121,16 @@ ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts(TierId tid) {
121121
return opts;
122122
}
123123

124+
template <typename CacheTrait>
125+
size_t CacheAllocator<CacheTrait>::memoryTierSize(TierId tid) const {
126+
auto partitions = std::accumulate(memoryTierConfigs.begin(), memoryTierConfigs.end(), 0UL,
127+
[](const size_t i, const MemoryTierCacheConfig& config){
128+
return i + config.getRatio();
129+
});
130+
131+
return memoryTierConfigs[tid].calculateTierSize(config_.getCacheSize(), partitions);
132+
}
133+
124134
template <typename CacheTrait>
125135
std::vector<std::unique_ptr<MemoryAllocator>>
126136
CacheAllocator<CacheTrait>::createPrivateAllocator() {
@@ -142,14 +152,15 @@ CacheAllocator<CacheTrait>::createPrivateAllocator() {
142152
template <typename CacheTrait>
143153
std::unique_ptr<MemoryAllocator>
144154
CacheAllocator<CacheTrait>::createNewMemoryAllocator(TierId tid) {
155+
size_t tierSize = memoryTierSize(tid);
145156
return std::make_unique<MemoryAllocator>(
146157
getAllocatorConfig(config_),
147158
shmManager_
148159
->createShm(detail::kShmCacheName + std::to_string(tid),
149-
config_.getCacheSize(), config_.slabMemoryBaseAddr,
160+
tierSize, config_.slabMemoryBaseAddr,
150161
createShmCacheOpts(tid))
151162
.addr,
152-
config_.getCacheSize());
163+
tierSize);
153164
}
154165

155166
template <typename CacheTrait>
@@ -160,7 +171,7 @@ CacheAllocator<CacheTrait>::restoreMemoryAllocator(TierId tid) {
160171
shmManager_
161172
->attachShm(detail::kShmCacheName + std::to_string(tid),
162173
config_.slabMemoryBaseAddr, createShmCacheOpts(tid)).addr,
163-
config_.getCacheSize(),
174+
memoryTierSize(tid),
164175
config_.disableFullCoredump);
165176
}
166177

@@ -2454,6 +2465,16 @@ const std::string CacheAllocator<CacheTrait>::getCacheName() const {
24542465
return config_.cacheName;
24552466
}
24562467

2468+
template <typename CacheTrait>
2469+
size_t CacheAllocator<CacheTrait>::getPoolSize(PoolId poolId) const {
2470+
size_t poolSize = 0;
2471+
for (auto& allocator: allocator_) {
2472+
const auto& pool = allocator->getPool(poolId);
2473+
poolSize += pool.getPoolSize();
2474+
}
2475+
return poolSize;
2476+
}
2477+
24572478
template <typename CacheTrait>
24582479
PoolStats CacheAllocator<CacheTrait>::getPoolStats(PoolId poolId) const {
24592480
const auto& pool = allocator_[currentTier()]->getPool(poolId);
@@ -3396,9 +3417,12 @@ GlobalCacheStats CacheAllocator<CacheTrait>::getGlobalCacheStats() const {
33963417

33973418
template <typename CacheTrait>
33983419
CacheMemoryStats CacheAllocator<CacheTrait>::getCacheMemoryStats() const {
3399-
const auto configuredTotalCacheSize = allocator_[currentTier()]->getMemorySizeInclAdvised();
3400-
const auto totalCacheSize = allocator_[currentTier()]->getMemorySize();
3401-
3420+
size_t totalCacheSize = 0;
3421+
size_t configuredTotalCacheSize = 0;
3422+
for(auto& allocator: allocator_) {
3423+
totalCacheSize += allocator->getMemorySize();
3424+
configuredTotalCacheSize += allocator->getMemorySizeInclAdvised();
3425+
}
34023426
auto addSize = [this](size_t a, PoolId pid) {
34033427
return a + allocator_[currentTier()]->getPool(pid).getPoolSize();
34043428
};

cachelib/allocator/CacheAllocator.h

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

2214+
size_t memoryTierSize(TierId tid) const;
2215+
22142216
// Whether the memory allocator for this cache allocator was created on shared
22152217
// memory. The hash table, chained item hash table etc is also created on
22162218
// 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)