@@ -6044,6 +6044,85 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
60446044 }
60456045 EXPECT_EQ (true , isRemoveCbTriggered);
60466046 }
6047+
6048+ void testSingleTierMemoryAllocatorSize () {
6049+ typename AllocatorT::Config config;
6050+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
6051+ config.setCacheSize (cacheSize);
6052+ config.enableCachePersistence (folly::sformat (" /tmp/single-tier-test/{}" , ::getpid ()));
6053+ config.usePosixForShm ();
6054+
6055+ AllocatorT alloc (AllocatorT::SharedMemNew, config);
6056+
6057+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize);
6058+ }
6059+
6060+ void testSingleTierMemoryAllocatorSizeAnonymous () {
6061+ typename AllocatorT::Config config;
6062+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
6063+ config.setCacheSize (cacheSize);
6064+
6065+ AllocatorT alloc (config);
6066+
6067+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize);
6068+ }
6069+
6070+ void testBasicMultiTier () {
6071+ using Item = typename AllocatorT::Item;
6072+ const static std::string data = " data" ;
6073+
6074+ std::set<std::string> movedKeys;
6075+ auto moveCb = [&](const Item& oldItem, Item& newItem, Item* /* parentPtr */ ) {
6076+ std::memcpy (newItem.getWritableMemory (), oldItem.getMemory (), oldItem.getSize ());
6077+ movedKeys.insert (oldItem.getKey ().str ());
6078+ };
6079+
6080+ typename AllocatorT::Config config;
6081+ config.setCacheSize (100 * 1024 * 1024 ); /* 100 MB */
6082+ config.enableCachePersistence (folly::sformat (" /tmp/multi-tier-test/{}" , ::getpid ()));
6083+ config.usePosixForShm ();
6084+ config.configureMemoryTiers ({
6085+ MemoryTierCacheConfig::fromShm ().setRatio (1 ),
6086+ MemoryTierCacheConfig::fromShm ().setRatio (1 ),
6087+ });
6088+ config.enableMovingOnSlabRelease (moveCb);
6089+
6090+ AllocatorT alloc (AllocatorT::SharedMemNew, config);
6091+
6092+ EXPECT_EQ (alloc.allocator_ .size (), 2 );
6093+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize / 2 );
6094+ EXPECT_LE (alloc.allocator_ [1 ]->getMemorySize (), cacheSize / 2 );
6095+
6096+ const size_t numBytes = alloc.getCacheMemoryStats ().cacheSize ;
6097+ auto pid = alloc.addPool (" default" , numBytes);
6098+
6099+ static constexpr size_t numOps = cacheSize / 1024 ;
6100+ for (int i = 0 ; i < numOps; i++) {
6101+ std::string key = std::to_string (i);
6102+ auto h = alloc.allocate (pid, key, 1024 );
6103+ EXPECT_TRUE (h);
6104+
6105+ std::memcpy (h->getWritableMemory (), data.data (), data.size ());
6106+
6107+ alloc.insertOrReplace (h);
6108+ }
6109+
6110+ EXPECT_TRUE (movedKeys.size () > 0 );
6111+
6112+ size_t movedButStillInMemory = 0 ;
6113+ for (const auto &k : movedKeys) {
6114+ auto h = alloc.find (k);
6115+
6116+ if (h) {
6117+ movedButStillInMemory++;
6118+ /* All moved elements should be in the second tier. */
6119+ EXPECT_TRUE (alloc.allocator_ [1 ]->isMemoryInAllocator (h->getMemory ()));
6120+ EXPECT_EQ (data, std::string ((char *)h->getMemory (), data.size ()));
6121+ }
6122+ }
6123+
6124+ EXPECT_TRUE (movedButStillInMemory > 0 );
6125+ }
60476126};
60486127} // namespace tests
60496128} // namespace cachelib
0 commit comments