@@ -24,7 +24,8 @@ namespace cachelib {
2424
2525template <typename CacheTrait>
2626CacheAllocator<CacheTrait>::CacheAllocator(Config config)
27- : isOnShm_{config.memMonitoringEnabled ()},
27+ : memoryTierConfigs(config.getMemoryTierConfigs()),
28+ isOnShm_{config.memMonitoringEnabled ()},
2829 config_ (config.validate()),
2930 tempShm_ (isOnShm_ ? std::make_unique<TempShmMapping>(config_.size)
3031 : nullptr ),
@@ -49,15 +50,21 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
4950 cacheCreationTime_{util::getCurrentTimeSec ()},
5051 nvmCacheState_{config_.cacheDir , config_.isNvmCacheEncryptionEnabled (),
5152 config_.isNvmCacheTruncateAllocSizeEnabled ()} {
53+ // TODO(MEMORY_TIER)
54+ if (memoryTierConfigs.size ()) {
55+ throw std::runtime_error (
56+ " Using custom memory tier is only supported for Shared Memory." );
57+ }
5258 initCommon (false );
5359}
5460
5561template <typename CacheTrait>
5662CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
57- : isOnShm_{true },
63+ : memoryTierConfigs(config.getMemoryTierConfigs()),
64+ isOnShm_{true },
5865 config_ (config.validate()),
5966 shmManager_ (
60- std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm )),
67+ std::make_unique<ShmManager>(config_.cacheDir, config_.isUsingPosixShm() )),
6168 allocator_ (createNewMemoryAllocator()),
6269 compactCacheManager_ (std::make_unique<CCacheManager>(*allocator_)),
6370 compressor_ (createPtrCompressor()),
@@ -69,7 +76,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
6976 config_.accessConfig.getNumBuckets()),
7077 nullptr,
7178 ShmSegmentOpts(config_.accessConfig.getPageSize(),
72- false, config_.usePosixShm ))
79+ false, config_.isUsingPosixShm() ))
7380 .addr,
7481 compressor_,
7582 [this](Item* it) -> ItemHandle { return acquire (it); })),
@@ -81,7 +88,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
8188 config_.chainedItemAccessConfig.getNumBuckets()),
8289 nullptr,
8390 ShmSegmentOpts(config_.accessConfig.getPageSize(),
84- false, config_.usePosixShm ))
91+ false, config_.isUsingPosixShm() ))
8592 .addr,
8693 compressor_,
8794 [this](Item* it) -> ItemHandle { return acquire (it); })),
@@ -92,12 +99,13 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
9299 config_.isNvmCacheTruncateAllocSizeEnabled ()} {
93100 initCommon (false );
94101 shmManager_->removeShm (detail::kShmInfoName ,
95- PosixSysVSegmentOpts (config_.usePosixShm ));
102+ PosixSysVSegmentOpts (config_.isUsingPosixShm () ));
96103}
97104
98105template <typename CacheTrait>
99106CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
100- : isOnShm_{true },
107+ : memoryTierConfigs(config.getMemoryTierConfigs()),
108+ isOnShm_{true },
101109 config_ (config.validate()),
102110 shmManager_(
103111 std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -111,14 +119,14 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
111119 deserializer_->deserialize<AccessSerializationType>(),
112120 config_.accessConfig,
113121 shmManager_->attachShm(detail::kShmHashTableName , nullptr ,
114- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm )),
122+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm() )),
115123 compressor_,
116124 [this](Item* it) -> ItemHandle { return acquire (it); })),
117125 chainedItemAccessContainer_(std::make_unique<AccessContainer>(
118126 deserializer_->deserialize<AccessSerializationType>(),
119127 config_.chainedItemAccessConfig,
120128 shmManager_->attachShm(detail::kShmChainedItemHashTableName , nullptr ,
121- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm )),
129+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm() )),
122130 compressor_,
123131 [this](Item* it) -> ItemHandle { return acquire (it); })),
124132 chainedItemLocks_(config_.chainedItemsLockPower,
@@ -136,7 +144,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
136144 // this info shm segment here and the new info shm segment's size is larger
137145 // than this one, creating new one will fail.
138146 shmManager_->removeShm (detail::kShmInfoName ,
139- PosixSysVSegmentOpts (config_.usePosixShm ));
147+ PosixSysVSegmentOpts (config_.isUsingPosixShm () ));
140148}
141149
142150template <typename CacheTrait>
@@ -150,31 +158,47 @@ CacheAllocator<CacheTrait>::~CacheAllocator() {
150158}
151159
152160template <typename CacheTrait>
153- std::unique_ptr<MemoryAllocator>
154- CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
161+ ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts() {
162+ if (memoryTierConfigs.size () > 1 ) {
163+ throw std::invalid_argument (" CacheLib only supports a single memory tier" );
164+ }
165+
155166 ShmSegmentOpts opts;
156167 opts.alignment = sizeof (Slab);
157- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
168+
169+ // If memoryTierConfigs is empty, Fallback to Posix/SysV segment
170+ // to keep legacy bahavior
171+ // TODO(MEMORY_TIER) - guarantee there is always at least one mem
172+ // layer inside Config
173+ if (memoryTierConfigs.size ()) {
174+ opts.typeOpts = FileShmSegmentOpts (memoryTierConfigs[0 ].path );
175+ } else {
176+ opts.typeOpts = PosixSysVSegmentOpts (config_.isUsingPosixShm ());
177+ }
178+
179+ return opts;
180+ }
181+
182+ template <typename CacheTrait>
183+ std::unique_ptr<MemoryAllocator>
184+ CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
158185 return std::make_unique<MemoryAllocator>(
159186 getAllocatorConfig (config_),
160187 shmManager_
161188 ->createShm (detail::kShmCacheName , config_.size ,
162- config_.slabMemoryBaseAddr , opts )
189+ config_.slabMemoryBaseAddr , createShmCacheOpts () )
163190 .addr ,
164191 config_.size );
165192}
166193
167194template <typename CacheTrait>
168195std::unique_ptr<MemoryAllocator>
169196CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
170- ShmSegmentOpts opts;
171- opts.alignment = sizeof (Slab);
172- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
173197 return std::make_unique<MemoryAllocator>(
174198 deserializer_->deserialize <MemoryAllocator::SerializationType>(),
175199 shmManager_
176- ->attachShm (detail::kShmCacheName , config_.slabMemoryBaseAddr , opts)
177- .addr ,
200+ ->attachShm (detail::kShmCacheName , config_.slabMemoryBaseAddr ,
201+ createShmCacheOpts ()) .addr ,
178202 config_.size ,
179203 config_.disableFullCoredump );
180204}
@@ -274,7 +298,7 @@ void CacheAllocator<CacheTrait>::initWorkers() {
274298template <typename CacheTrait>
275299std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
276300 auto infoAddr = shmManager_->attachShm (detail::kShmInfoName , nullptr ,
277- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm ));
301+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm () ));
278302 return std::make_unique<Deserializer>(
279303 reinterpret_cast <uint8_t *>(infoAddr.addr ),
280304 reinterpret_cast <uint8_t *>(infoAddr.addr ) + infoAddr.size );
@@ -3051,7 +3075,7 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
30513075 ioBuf->coalesce ();
30523076
30533077 ShmSegmentOpts opts;
3054- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
3078+ opts.typeOpts = PosixSysVSegmentOpts (config_.isUsingPosixShm () );
30553079
30563080 void * infoAddr = shmManager_->createShm (detail::kShmInfoName , ioBuf->length (),
30573081 nullptr , opts).addr ;
0 commit comments