@@ -395,6 +395,7 @@ class CacheAllocator : public CacheBase {
395395 using MMSerializationTypeContainer =
396396 typename MMType::SerializationTypeContainer;
397397 using AccessSerializationType = typename AccessType::SerializationType;
398+ using AllocatorsSerializationType = serialization::MemoryAllocatorCollection;
398399
399400 using ShmManager = facebook::cachelib::ShmManager;
400401
@@ -2153,7 +2154,7 @@ class CacheAllocator : public CacheBase {
21532154 PrivateSegmentOpts createPrivateSegmentOpts (TierId tid);
21542155 std::unique_ptr<MemoryAllocator> createPrivateAllocator (TierId tid);
21552156 std::unique_ptr<MemoryAllocator> createNewMemoryAllocator (TierId tid);
2156- std::unique_ptr<MemoryAllocator> restoreMemoryAllocator (TierId tid);
2157+ std::unique_ptr<MemoryAllocator> restoreMemoryAllocator (TierId tid, const serialization::MemoryAllocatorObject& sAllocator );
21572158 std::unique_ptr<CCacheManager> restoreCCacheManager (TierId tid);
21582159
21592160 PoolIds filterCompactCachePools (const PoolIds& poolIds) const ;
@@ -2698,9 +2699,10 @@ CacheAllocator<CacheTrait>::createNewMemoryAllocator(TierId tid) {
26982699
26992700template <typename CacheTrait>
27002701std::unique_ptr<MemoryAllocator>
2701- CacheAllocator<CacheTrait>::restoreMemoryAllocator(TierId tid) {
2702+ CacheAllocator<CacheTrait>::restoreMemoryAllocator(TierId tid,
2703+ const serialization::MemoryAllocatorObject& sAllocator ) {
27022704 return std::make_unique<MemoryAllocator>(
2703- deserializer_-> deserialize <MemoryAllocator::SerializationType>() ,
2705+ sAllocator ,
27042706 shmManager_
27052707 ->attachShm (detail::kShmCacheName + std::to_string (tid),
27062708 config_.slabMemoryBaseAddr , createShmCacheOpts (tid)).addr ,
@@ -2732,8 +2734,11 @@ template <typename CacheTrait>
27322734std::vector<std::unique_ptr<MemoryAllocator>>
27332735CacheAllocator<CacheTrait>::restoreAllocators() {
27342736 std::vector<std::unique_ptr<MemoryAllocator>> allocators;
2737+ const auto allocatorCollection =
2738+ deserializer_->deserialize <AllocatorsSerializationType>();
2739+ auto allocMap = *allocatorCollection.allocators ();
27352740 for (int tid = 0 ; tid < getNumTiers (); tid++) {
2736- allocators.emplace_back (restoreMemoryAllocator (tid));
2741+ allocators.emplace_back (restoreMemoryAllocator (tid,allocMap[tid] ));
27372742 }
27382743 return allocators;
27392744}
@@ -6410,26 +6415,43 @@ folly::IOBufQueue CacheAllocator<CacheTrait>::saveStateToIOBuf() {
64106415 *metadata_.numChainedChildItems () = stats_.numChainedChildItems .get ();
64116416 *metadata_.numAbortedSlabReleases () = stats_.numAbortedSlabReleases .get ();
64126417
6418+ const auto numTiers = getNumTiers ();
64136419 // TODO: implement serialization for multiple tiers
6414- auto serializeMMContainers = [](MMContainers& mmContainers) {
6415- MMSerializationTypeContainer state ;
6416- for (unsigned int i = 0 ; i < 1 /* TODO: */ ; ++i) {
6420+ auto serializeMMContainers = [numTiers ](MMContainers& mmContainers) {
6421+ std::map<serialization::MemoryDescriptorObject,MMSerializationType> containers ;
6422+ for (unsigned int i = 0 ; i < numTiers ; ++i) {
64176423 for (unsigned int j = 0 ; j < mmContainers[i].size (); ++j) {
64186424 for (unsigned int k = 0 ; k < mmContainers[i][j].size (); ++k) {
64196425 if (mmContainers[i][j][k]) {
6420- state.pools_ref ()[j][k] = mmContainers[i][j][k]->saveState ();
6426+ serialization::MemoryDescriptorObject md;
6427+ md.tid_ref () = i;
6428+ md.pid_ref () = j;
6429+ md.cid_ref () = k;
6430+ containers[md] = mmContainers[i][j][k]->saveState ();
64216431 }
64226432 }
64236433 }
64246434 }
6435+ MMSerializationTypeContainer state;
6436+ state.containers_ref () = containers;
64256437 return state;
64266438 };
64276439 MMSerializationTypeContainer mmContainersState =
64286440 serializeMMContainers (mmContainers_);
64296441
64306442 AccessSerializationType accessContainerState = accessContainer_->saveState ();
6431- // TODO: foreach allocator
6432- MemoryAllocator::SerializationType allocatorState = allocator_[0 ]->saveState ();
6443+
6444+ auto serializeAllocators = [numTiers,this ]() {
6445+ AllocatorsSerializationType state;
6446+ std::map<int ,MemoryAllocator::SerializationType> allocators;
6447+ for (int i = 0 ; i < numTiers; ++i) {
6448+ allocators[i] = allocator_[i]->saveState ();
6449+ }
6450+ state.allocators_ref () = allocators;
6451+ return state;
6452+ };
6453+ AllocatorsSerializationType allocatorsState = serializeAllocators ();
6454+
64336455 CCacheManager::SerializationType ccState = compactCacheManager_->saveState ();
64346456
64356457 AccessSerializationType chainedItemAccessContainerState =
@@ -6439,7 +6461,7 @@ folly::IOBufQueue CacheAllocator<CacheTrait>::saveStateToIOBuf() {
64396461 // results into a single buffer.
64406462 folly::IOBufQueue queue;
64416463 Serializer::serializeToIOBufQueue (queue, metadata_);
6442- Serializer::serializeToIOBufQueue (queue, allocatorState );
6464+ Serializer::serializeToIOBufQueue (queue, allocatorsState );
64436465 Serializer::serializeToIOBufQueue (queue, ccState);
64446466 Serializer::serializeToIOBufQueue (queue, mmContainersState);
64456467 Serializer::serializeToIOBufQueue (queue, accessContainerState);
@@ -6562,23 +6584,22 @@ CacheAllocator<CacheTrait>::deserializeMMContainers(
65626584 * only works for a single (topmost) tier. */
65636585 MMContainers mmContainers{getNumTiers ()};
65646586
6565- for (auto & kvPool : *container.pools_ref ()) {
6566- auto i = static_cast <PoolId>(kvPool.first );
6567- auto & pool = getPool (i);
6568- for (auto & kv : kvPool.second ) {
6569- auto j = static_cast <ClassId>(kv.first );
6570- for (TierId tid = 0 ; tid < getNumTiers (); tid++) {
6571- MMContainerPtr ptr =
6572- std::make_unique<typename MMContainerPtr::element_type>(kv.second ,
6573- compressor);
6574- auto config = ptr->getConfig ();
6575- config.addExtraConfig (config_.trackTailHits
6576- ? pool.getAllocationClass (j).getAllocsPerSlab ()
6577- : 0 );
6578- ptr->setConfig (config);
6579- mmContainers[tid][i][j] = std::move (ptr);
6580- }
6581- }
6587+ std::map<serialization::MemoryDescriptorObject,MMSerializationType> containerMap =
6588+ *container.containers ();
6589+ for (auto md : containerMap) {
6590+ uint32_t tid = *md.first .tid ();
6591+ uint32_t pid = *md.first .pid ();
6592+ uint32_t cid = *md.first .cid ();
6593+ auto & pool = getPoolByTid (pid,tid);
6594+ MMContainerPtr ptr =
6595+ std::make_unique<typename MMContainerPtr::element_type>(md.second ,
6596+ compressor);
6597+ auto config = ptr->getConfig ();
6598+ config.addExtraConfig (config_.trackTailHits
6599+ ? pool.getAllocationClass (cid).getAllocsPerSlab ()
6600+ : 0 );
6601+ ptr->setConfig (config);
6602+ mmContainers[tid][pid][cid] = std::move (ptr);
65826603 }
65836604 // We need to drop the unevictableMMContainer in the desierializer.
65846605 // TODO: remove this at version 17.
0 commit comments