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