@@ -6295,6 +6295,86 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
62956295 });
62966296 EXPECT_EQ (intervalNameExists, 4 );
62976297 }
6298+
6299+ void testSingleTierMemoryAllocatorSize () {
6300+ typename AllocatorT::Config config;
6301+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
6302+ config.setCacheSize (cacheSize);
6303+ config.enableCachePersistence (folly::sformat (" /tmp/single-tier-test/{}" , ::getpid ()));
6304+
6305+ AllocatorT alloc (AllocatorT::SharedMemNew, config);
6306+
6307+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize);
6308+ }
6309+
6310+ void testSingleTierMemoryAllocatorSizeAnonymous () {
6311+ typename AllocatorT::Config config;
6312+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
6313+ config.setCacheSize (cacheSize);
6314+
6315+ AllocatorT alloc (config);
6316+
6317+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize);
6318+ }
6319+
6320+ void testBasicMultiTier () {
6321+ using Item = typename AllocatorT::Item;
6322+ const static std::string data = " data" ;
6323+
6324+ std::set<std::string> movedKeys;
6325+ auto moveCb = [&](const Item& oldItem, Item& newItem, Item* /* parentPtr */ ) {
6326+ std::memcpy (newItem.getMemory (), oldItem.getMemory (), oldItem.getSize ());
6327+ movedKeys.insert (oldItem.getKey ().str ());
6328+ };
6329+
6330+ typename AllocatorT::Config config;
6331+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
6332+ config.setCacheSize (100 * 1024 * 1024 ); /* 100 MB */
6333+ config.enableCachePersistence (folly::sformat (" /tmp/multi-tier-test/{}" , ::getpid ()));
6334+ config.configureMemoryTiers ({
6335+ MemoryTierCacheConfig::fromShm ().setRatio (1 )
6336+ .setMemBind (std::string (" 0" )),
6337+ MemoryTierCacheConfig::fromShm ().setRatio (1 )
6338+ .setMemBind (std::string (" 0" )),
6339+ });
6340+ config.enableMovingOnSlabRelease (moveCb);
6341+
6342+ AllocatorT alloc (AllocatorT::SharedMemNew, config);
6343+
6344+ EXPECT_EQ (alloc.allocator_ .size (), 2 );
6345+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize / 2 );
6346+ EXPECT_LE (alloc.allocator_ [1 ]->getMemorySize (), cacheSize / 2 );
6347+
6348+ const size_t numBytes = alloc.getCacheMemoryStats ().ramCacheSize ;
6349+ auto pid = alloc.addPool (" default" , numBytes);
6350+
6351+ static constexpr size_t numOps = cacheSize / 1024 ;
6352+ for (int i = 0 ; i < numOps; i++) {
6353+ std::string key = std::to_string (i);
6354+ auto h = alloc.allocate (pid, key, 1024 );
6355+ EXPECT_TRUE (h);
6356+
6357+ std::memcpy (h->getMemory (), data.data (), data.size ());
6358+
6359+ alloc.insertOrReplace (h);
6360+ }
6361+
6362+ EXPECT_TRUE (movedKeys.size () > 0 );
6363+
6364+ size_t movedButStillInMemory = 0 ;
6365+ for (const auto &k : movedKeys) {
6366+ auto h = alloc.find (k);
6367+
6368+ if (h) {
6369+ movedButStillInMemory++;
6370+ /* All moved elements should be in the second tier. */
6371+ EXPECT_TRUE (alloc.allocator_ [1 ]->isMemoryInAllocator (h->getMemory ()));
6372+ EXPECT_EQ (data, std::string ((char *)h->getMemory (), data.size ()));
6373+ }
6374+ }
6375+
6376+ EXPECT_TRUE (movedButStillInMemory > 0 );
6377+ }
62986378};
62996379} // namespace tests
63006380} // namespace cachelib
0 commit comments