@@ -6192,6 +6192,86 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
61926192 r2.wait ();
61936193 ASSERT_EQ (0 , alloc.getSlabReleaseStats ().numSlabReleaseStuck );
61946194 }
6195+
6196+ void testSingleTierMemoryAllocatorSize () {
6197+ typename AllocatorT::Config config;
6198+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
6199+ config.setCacheSize (cacheSize);
6200+ config.enableCachePersistence (folly::sformat (" /tmp/single-tier-test/{}" , ::getpid ()));
6201+
6202+ AllocatorT alloc (AllocatorT::SharedMemNew, config);
6203+
6204+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize);
6205+ }
6206+
6207+ void testSingleTierMemoryAllocatorSizeAnonymous () {
6208+ typename AllocatorT::Config config;
6209+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
6210+ config.setCacheSize (cacheSize);
6211+
6212+ AllocatorT alloc (config);
6213+
6214+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize);
6215+ }
6216+
6217+ void testBasicMultiTier () {
6218+ using Item = typename AllocatorT::Item;
6219+ const static std::string data = " data" ;
6220+
6221+ std::set<std::string> movedKeys;
6222+ auto moveCb = [&](const Item& oldItem, Item& newItem, Item* /* parentPtr */ ) {
6223+ std::memcpy (newItem.getMemory (), oldItem.getMemory (), oldItem.getSize ());
6224+ movedKeys.insert (oldItem.getKey ().str ());
6225+ };
6226+
6227+ typename AllocatorT::Config config;
6228+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
6229+ config.setCacheSize (100 * 1024 * 1024 ); /* 100 MB */
6230+ config.enableCachePersistence (folly::sformat (" /tmp/multi-tier-test/{}" , ::getpid ()));
6231+ config.configureMemoryTiers ({
6232+ MemoryTierCacheConfig::fromShm ().setRatio (1 )
6233+ .setMemBind (std::string (" 0" )),
6234+ MemoryTierCacheConfig::fromShm ().setRatio (1 )
6235+ .setMemBind (std::string (" 0" )),
6236+ });
6237+ config.enableMovingOnSlabRelease (moveCb);
6238+
6239+ AllocatorT alloc (AllocatorT::SharedMemNew, config);
6240+
6241+ EXPECT_EQ (alloc.allocator_ .size (), 2 );
6242+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize / 2 );
6243+ EXPECT_LE (alloc.allocator_ [1 ]->getMemorySize (), cacheSize / 2 );
6244+
6245+ const size_t numBytes = alloc.getCacheMemoryStats ().ramCacheSize ;
6246+ auto pid = alloc.addPool (" default" , numBytes);
6247+
6248+ static constexpr size_t numOps = cacheSize / 1024 ;
6249+ for (int i = 0 ; i < numOps; i++) {
6250+ std::string key = std::to_string (i);
6251+ auto h = alloc.allocate (pid, key, 1024 );
6252+ EXPECT_TRUE (h);
6253+
6254+ std::memcpy (h->getMemory (), data.data (), data.size ());
6255+
6256+ alloc.insertOrReplace (h);
6257+ }
6258+
6259+ EXPECT_TRUE (movedKeys.size () > 0 );
6260+
6261+ size_t movedButStillInMemory = 0 ;
6262+ for (const auto &k : movedKeys) {
6263+ auto h = alloc.find (k);
6264+
6265+ if (h) {
6266+ movedButStillInMemory++;
6267+ /* All moved elements should be in the second tier. */
6268+ EXPECT_TRUE (alloc.allocator_ [1 ]->isMemoryInAllocator (h->getMemory ()));
6269+ EXPECT_EQ (data, std::string ((char *)h->getMemory (), data.size ()));
6270+ }
6271+ }
6272+
6273+ EXPECT_TRUE (movedButStillInMemory > 0 );
6274+ }
61956275};
61966276} // namespace tests
61976277} // namespace cachelib
0 commit comments