@@ -5941,6 +5941,85 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
59415941 }
59425942 EXPECT_EQ (true , isRemoveCbTriggered);
59435943 }
5944+
5945+ void testSingleTierMemoryAllocatorSize () {
5946+ typename AllocatorT::Config config;
5947+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
5948+ config.setCacheSize (cacheSize);
5949+ config.enableCachePersistence (folly::sformat (" /tmp/single-tier-test/{}" , ::getpid ()));
5950+ config.usePosixForShm ();
5951+
5952+ AllocatorT alloc (AllocatorT::SharedMemNew, config);
5953+
5954+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize);
5955+ }
5956+
5957+ void testSingleTierMemoryAllocatorSizeAnonymous () {
5958+ typename AllocatorT::Config config;
5959+ static constexpr size_t cacheSize = 100 * 1024 * 1024 ; /* 100 MB */
5960+ config.setCacheSize (cacheSize);
5961+
5962+ AllocatorT alloc (config);
5963+
5964+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize);
5965+ }
5966+
5967+ void testBasicMultiTier () {
5968+ using Item = typename AllocatorT::Item;
5969+ const static std::string data = " data" ;
5970+
5971+ std::set<std::string> movedKeys;
5972+ auto moveCb = [&](const Item& oldItem, Item& newItem, Item* /* parentPtr */ ) {
5973+ std::memcpy (newItem.getWritableMemory (), oldItem.getMemory (), oldItem.getSize ());
5974+ movedKeys.insert (oldItem.getKey ().str ());
5975+ };
5976+
5977+ typename AllocatorT::Config config;
5978+ config.setCacheSize (100 * 1024 * 1024 ); /* 100 MB */
5979+ config.enableCachePersistence (folly::sformat (" /tmp/multi-tier-test/{}" , ::getpid ()));
5980+ config.usePosixForShm ();
5981+ config.configureMemoryTiers ({
5982+ MemoryTierCacheConfig::fromShm ().setRatio (1 ),
5983+ MemoryTierCacheConfig::fromShm ().setRatio (1 ),
5984+ });
5985+ config.enableMovingOnSlabRelease (moveCb);
5986+
5987+ AllocatorT alloc (AllocatorT::SharedMemNew, config);
5988+
5989+ EXPECT_EQ (alloc.allocator_ .size (), 2 );
5990+ EXPECT_LE (alloc.allocator_ [0 ]->getMemorySize (), cacheSize / 2 );
5991+ EXPECT_LE (alloc.allocator_ [1 ]->getMemorySize (), cacheSize / 2 );
5992+
5993+ const size_t numBytes = alloc.getCacheMemoryStats ().cacheSize ;
5994+ auto pid = alloc.addPool (" default" , numBytes);
5995+
5996+ static constexpr size_t numOps = cacheSize / 1024 ;
5997+ for (int i = 0 ; i < numOps; i++) {
5998+ std::string key = std::to_string (i);
5999+ auto h = alloc.allocate (pid, key, 1024 );
6000+ EXPECT_TRUE (h);
6001+
6002+ std::memcpy (h->getWritableMemory (), data.data (), data.size ());
6003+
6004+ alloc.insertOrReplace (h);
6005+ }
6006+
6007+ EXPECT_TRUE (movedKeys.size () > 0 );
6008+
6009+ size_t movedButStillInMemory = 0 ;
6010+ for (const auto &k : movedKeys) {
6011+ auto h = alloc.find (k);
6012+
6013+ if (h) {
6014+ movedButStillInMemory++;
6015+ /* All moved elements should be in the second tier. */
6016+ EXPECT_TRUE (alloc.allocator_ [1 ]->isMemoryInAllocator (h->getMemory ()));
6017+ EXPECT_EQ (data, std::string ((char *)h->getMemory (), data.size ()));
6018+ }
6019+ }
6020+
6021+ EXPECT_TRUE (movedButStillInMemory > 0 );
6022+ }
59446023};
59456024} // namespace tests
59466025} // namespace cachelib
0 commit comments