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