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