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