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