@@ -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,12 +50,16 @@ 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 (" Using custom memory tier is only supported for Shared Memory." );
5256 initCommon (false );
5357}
5458
5559template <typename CacheTrait>
5660CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
57- : isOnShm_{true },
61+ : memoryTierConfigs(config.getMemoryTierConfigs()),
62+ isOnShm_{true },
5863 config_ (config.validate()),
5964 shmManager_ (
6065 std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -97,7 +102,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
97102
98103template <typename CacheTrait>
99104CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
100- : isOnShm_{true },
105+ : memoryTierConfigs(config.getMemoryTierConfigs()),
106+ isOnShm_{true },
101107 config_ (config.validate()),
102108 shmManager_(
103109 std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -150,31 +156,43 @@ CacheAllocator<CacheTrait>::~CacheAllocator() {
150156}
151157
152158template <typename CacheTrait>
153- std::unique_ptr<MemoryAllocator>
154- CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
159+ ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts() {
160+ if (memoryTierConfigs.size () > 1 )
161+ throw std::invalid_argument (" CacheLib only supports a single memory tier" );
162+
155163 ShmSegmentOpts opts;
156164 opts.alignment = sizeof (Slab);
157- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
165+
166+ if (memoryTierConfigs.size ()) {
167+ opts.typeOpts = FileShmSegmentOpts (memoryTierConfigs[0 ].path );
168+ } else {
169+ // Fallback to Posix/SysV segment
170+ opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
171+ }
172+
173+ return opts;
174+ }
175+
176+ template <typename CacheTrait>
177+ std::unique_ptr<MemoryAllocator>
178+ CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
158179 return std::make_unique<MemoryAllocator>(
159180 getAllocatorConfig (config_),
160181 shmManager_
161182 ->createShm (detail::kShmCacheName , config_.size ,
162- config_.slabMemoryBaseAddr , opts )
183+ config_.slabMemoryBaseAddr , createShmCacheOpts () )
163184 .addr ,
164185 config_.size );
165186}
166187
167188template <typename CacheTrait>
168189std::unique_ptr<MemoryAllocator>
169190CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
170- ShmSegmentOpts opts;
171- opts.alignment = sizeof (Slab);
172- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
173191 return std::make_unique<MemoryAllocator>(
174192 deserializer_->deserialize <MemoryAllocator::SerializationType>(),
175193 shmManager_
176- ->attachShm (detail::kShmCacheName , config_.slabMemoryBaseAddr , opts)
177- .addr ,
194+ ->attachShm (detail::kShmCacheName , config_.slabMemoryBaseAddr ,
195+ createShmCacheOpts ()) .addr ,
178196 config_.size ,
179197 config_.disableFullCoredump );
180198}
0 commit comments