@@ -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) {
172172TEST_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