@@ -395,6 +395,7 @@ class CacheAllocator : public CacheBase {
395
395
using MMSerializationTypeContainer =
396
396
typename MMType::SerializationTypeContainer;
397
397
using AccessSerializationType = typename AccessType::SerializationType;
398
+ using AllocatorsSerializationType = serialization::MemoryAllocatorCollection;
398
399
399
400
using ShmManager = facebook::cachelib::ShmManager;
400
401
@@ -2153,7 +2154,7 @@ class CacheAllocator : public CacheBase {
2153
2154
PrivateSegmentOpts createPrivateSegmentOpts (TierId tid);
2154
2155
std::unique_ptr<MemoryAllocator> createPrivateAllocator (TierId tid);
2155
2156
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 );
2157
2158
std::unique_ptr<CCacheManager> restoreCCacheManager (TierId tid);
2158
2159
2159
2160
PoolIds filterCompactCachePools (const PoolIds& poolIds) const ;
@@ -2698,9 +2699,10 @@ CacheAllocator<CacheTrait>::createNewMemoryAllocator(TierId tid) {
2698
2699
2699
2700
template <typename CacheTrait>
2700
2701
std::unique_ptr<MemoryAllocator>
2701
- CacheAllocator<CacheTrait>::restoreMemoryAllocator(TierId tid) {
2702
+ CacheAllocator<CacheTrait>::restoreMemoryAllocator(TierId tid,
2703
+ const serialization::MemoryAllocatorObject& sAllocator ) {
2702
2704
return std::make_unique<MemoryAllocator>(
2703
- deserializer_-> deserialize <MemoryAllocator::SerializationType>() ,
2705
+ sAllocator ,
2704
2706
shmManager_
2705
2707
->attachShm (detail::kShmCacheName + std::to_string (tid),
2706
2708
config_.slabMemoryBaseAddr , createShmCacheOpts (tid)).addr ,
@@ -2732,8 +2734,11 @@ template <typename CacheTrait>
2732
2734
std::vector<std::unique_ptr<MemoryAllocator>>
2733
2735
CacheAllocator<CacheTrait>::restoreAllocators() {
2734
2736
std::vector<std::unique_ptr<MemoryAllocator>> allocators;
2737
+ const auto allocatorCollection =
2738
+ deserializer_->deserialize <AllocatorsSerializationType>();
2739
+ auto allocMap = *allocatorCollection.allocators ();
2735
2740
for (int tid = 0 ; tid < getNumTiers (); tid++) {
2736
- allocators.emplace_back (restoreMemoryAllocator (tid));
2741
+ allocators.emplace_back (restoreMemoryAllocator (tid,allocMap[tid] ));
2737
2742
}
2738
2743
return allocators;
2739
2744
}
@@ -6410,26 +6415,43 @@ folly::IOBufQueue CacheAllocator<CacheTrait>::saveStateToIOBuf() {
6410
6415
*metadata_.numChainedChildItems () = stats_.numChainedChildItems .get ();
6411
6416
*metadata_.numAbortedSlabReleases () = stats_.numAbortedSlabReleases .get ();
6412
6417
6418
+ const auto numTiers = getNumTiers ();
6413
6419
// 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) {
6417
6423
for (unsigned int j = 0 ; j < mmContainers[i].size (); ++j) {
6418
6424
for (unsigned int k = 0 ; k < mmContainers[i][j].size (); ++k) {
6419
6425
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 ();
6421
6431
}
6422
6432
}
6423
6433
}
6424
6434
}
6435
+ MMSerializationTypeContainer state;
6436
+ state.containers_ref () = containers;
6425
6437
return state;
6426
6438
};
6427
6439
MMSerializationTypeContainer mmContainersState =
6428
6440
serializeMMContainers (mmContainers_);
6429
6441
6430
6442
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
+
6433
6455
CCacheManager::SerializationType ccState = compactCacheManager_->saveState ();
6434
6456
6435
6457
AccessSerializationType chainedItemAccessContainerState =
@@ -6439,7 +6461,7 @@ folly::IOBufQueue CacheAllocator<CacheTrait>::saveStateToIOBuf() {
6439
6461
// results into a single buffer.
6440
6462
folly::IOBufQueue queue;
6441
6463
Serializer::serializeToIOBufQueue (queue, metadata_);
6442
- Serializer::serializeToIOBufQueue (queue, allocatorState );
6464
+ Serializer::serializeToIOBufQueue (queue, allocatorsState );
6443
6465
Serializer::serializeToIOBufQueue (queue, ccState);
6444
6466
Serializer::serializeToIOBufQueue (queue, mmContainersState);
6445
6467
Serializer::serializeToIOBufQueue (queue, accessContainerState);
@@ -6562,23 +6584,22 @@ CacheAllocator<CacheTrait>::deserializeMMContainers(
6562
6584
* only works for a single (topmost) tier. */
6563
6585
MMContainers mmContainers{getNumTiers ()};
6564
6586
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);
6582
6603
}
6583
6604
// We need to drop the unevictableMMContainer in the desierializer.
6584
6605
// TODO: remove this at version 17.
0 commit comments