@@ -24,16 +24,19 @@ namespace cachelib {
24
24
25
25
template <typename CacheTrait>
26
26
CacheAllocator<CacheTrait>::CacheAllocator(Config config)
27
- : isOnShm_{config.memMonitoringEnabled ()},
27
+ : memoryTierConfigs(config.getMemoryTierConfigs()),
28
+ isOnShm_{config.memMonitoringEnabled ()},
28
29
config_ (config.validate()),
29
- tempShm_ (isOnShm_ ? std::make_unique<TempShmMapping>(config_.size)
30
+ tempShm_ (isOnShm_ ? std::make_unique<TempShmMapping>(
31
+ config_.getCacheSize())
30
32
: nullptr ),
31
33
allocator_ (isOnShm_ ? std::make_unique<MemoryAllocator>(
32
34
getAllocatorConfig (config_),
33
35
tempShm_->getAddr(),
34
- config_.size )
36
+ config_.getCacheSize() )
35
37
: std::make_unique<MemoryAllocator>(
36
- getAllocatorConfig (config_), config_.size)),
38
+ getAllocatorConfig (config_),
39
+ config_.getCacheSize())),
37
40
compactCacheManager_(std::make_unique<CCacheManager>(*allocator_)),
38
41
compressor_(createPtrCompressor()),
39
42
accessContainer_(std::make_unique<AccessContainer>(
@@ -49,15 +52,22 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
49
52
cacheCreationTime_{util::getCurrentTimeSec ()},
50
53
nvmCacheState_{config_.cacheDir , config_.isNvmCacheEncryptionEnabled (),
51
54
config_.isNvmCacheTruncateAllocSizeEnabled ()} {
55
+ // TODO(MEMORY_TIER)
56
+ if (std::holds_alternative<FileShmSegmentOpts>(
57
+ memoryTierConfigs[0 ].getShmTypeOpts ())) {
58
+ throw std::runtime_error (
59
+ " Using custom memory tier is only supported for Shared Memory." );
60
+ }
52
61
initCommon (false );
53
62
}
54
63
55
64
template <typename CacheTrait>
56
65
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
57
- : isOnShm_{true },
66
+ : memoryTierConfigs(config.getMemoryTierConfigs()),
67
+ isOnShm_{true },
58
68
config_ (config.validate()),
59
69
shmManager_ (
60
- std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm )),
70
+ std::make_unique<ShmManager>(config_.cacheDir, config_.isUsingPosixShm() )),
61
71
allocator_ (createNewMemoryAllocator()),
62
72
compactCacheManager_ (std::make_unique<CCacheManager>(*allocator_)),
63
73
compressor_ (createPtrCompressor()),
@@ -69,7 +79,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
69
79
config_.accessConfig.getNumBuckets()),
70
80
nullptr,
71
81
ShmSegmentOpts(config_.accessConfig.getPageSize(),
72
- false, config_.usePosixShm ))
82
+ false, config_.isUsingPosixShm() ))
73
83
.addr,
74
84
compressor_,
75
85
[this](Item* it) -> ItemHandle { return acquire (it); })),
@@ -81,7 +91,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
81
91
config_.chainedItemAccessConfig.getNumBuckets()),
82
92
nullptr,
83
93
ShmSegmentOpts(config_.accessConfig.getPageSize(),
84
- false, config_.usePosixShm ))
94
+ false, config_.isUsingPosixShm() ))
85
95
.addr,
86
96
compressor_,
87
97
[this](Item* it) -> ItemHandle { return acquire (it); })),
@@ -92,12 +102,13 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
92
102
config_.isNvmCacheTruncateAllocSizeEnabled ()} {
93
103
initCommon (false );
94
104
shmManager_->removeShm (detail::kShmInfoName ,
95
- PosixSysVSegmentOpts (config_.usePosixShm ));
105
+ PosixSysVSegmentOpts (config_.isUsingPosixShm () ));
96
106
}
97
107
98
108
template <typename CacheTrait>
99
109
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
100
- : isOnShm_{true },
110
+ : memoryTierConfigs(config.getMemoryTierConfigs()),
111
+ isOnShm_{true },
101
112
config_ (config.validate()),
102
113
shmManager_(
103
114
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -111,14 +122,14 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
111
122
deserializer_->deserialize<AccessSerializationType>(),
112
123
config_.accessConfig,
113
124
shmManager_->attachShm(detail::kShmHashTableName , nullptr ,
114
- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm )),
125
+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm() )),
115
126
compressor_,
116
127
[this](Item* it) -> ItemHandle { return acquire (it); })),
117
128
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
118
129
deserializer_->deserialize<AccessSerializationType>(),
119
130
config_.chainedItemAccessConfig,
120
131
shmManager_->attachShm(detail::kShmChainedItemHashTableName , nullptr ,
121
- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm )),
132
+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm() )),
122
133
compressor_,
123
134
[this](Item* it) -> ItemHandle { return acquire (it); })),
124
135
chainedItemLocks_(config_.chainedItemsLockPower,
@@ -136,7 +147,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
136
147
// this info shm segment here and the new info shm segment's size is larger
137
148
// than this one, creating new one will fail.
138
149
shmManager_->removeShm (detail::kShmInfoName ,
139
- PosixSysVSegmentOpts (config_.usePosixShm ));
150
+ PosixSysVSegmentOpts (config_.isUsingPosixShm () ));
140
151
}
141
152
142
153
template <typename CacheTrait>
@@ -150,32 +161,39 @@ CacheAllocator<CacheTrait>::~CacheAllocator() {
150
161
}
151
162
152
163
template <typename CacheTrait>
153
- std::unique_ptr<MemoryAllocator>
154
- CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
164
+ ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts() {
165
+ if (memoryTierConfigs.size () > 1 ) {
166
+ throw std::invalid_argument (" CacheLib only supports a single memory tier" );
167
+ }
168
+
155
169
ShmSegmentOpts opts;
156
170
opts.alignment = sizeof (Slab);
157
- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
171
+ opts.typeOpts = memoryTierConfigs[0 ].getShmTypeOpts ();
172
+
173
+ return opts;
174
+ }
175
+
176
+ template <typename CacheTrait>
177
+ std::unique_ptr<MemoryAllocator>
178
+ CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
158
179
return std::make_unique<MemoryAllocator>(
159
180
getAllocatorConfig (config_),
160
181
shmManager_
161
- ->createShm (detail::kShmCacheName , config_.size ,
162
- config_.slabMemoryBaseAddr , opts )
182
+ ->createShm (detail::kShmCacheName , config_.getCacheSize () ,
183
+ config_.slabMemoryBaseAddr , createShmCacheOpts () )
163
184
.addr ,
164
- config_.size );
185
+ config_.getCacheSize () );
165
186
}
166
187
167
188
template <typename CacheTrait>
168
189
std::unique_ptr<MemoryAllocator>
169
190
CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
170
- ShmSegmentOpts opts;
171
- opts.alignment = sizeof (Slab);
172
- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
173
191
return std::make_unique<MemoryAllocator>(
174
192
deserializer_->deserialize <MemoryAllocator::SerializationType>(),
175
193
shmManager_
176
- ->attachShm (detail::kShmCacheName , config_.slabMemoryBaseAddr , opts)
177
- .addr ,
178
- config_.size ,
194
+ ->attachShm (detail::kShmCacheName , config_.slabMemoryBaseAddr ,
195
+ createShmCacheOpts ()) .addr ,
196
+ config_.getCacheSize () ,
179
197
config_.disableFullCoredump );
180
198
}
181
199
@@ -274,7 +292,7 @@ void CacheAllocator<CacheTrait>::initWorkers() {
274
292
template <typename CacheTrait>
275
293
std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
276
294
auto infoAddr = shmManager_->attachShm (detail::kShmInfoName , nullptr ,
277
- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm ));
295
+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm () ));
278
296
return std::make_unique<Deserializer>(
279
297
reinterpret_cast <uint8_t *>(infoAddr.addr ),
280
298
reinterpret_cast <uint8_t *>(infoAddr.addr ) + infoAddr.size );
@@ -2192,7 +2210,7 @@ PoolEvictionAgeStats CacheAllocator<CacheTrait>::getPoolEvictionAgeStats(
2192
2210
template <typename CacheTrait>
2193
2211
CacheMetadata CacheAllocator<CacheTrait>::getCacheMetadata() const noexcept {
2194
2212
return CacheMetadata{kCachelibVersion , kCacheRamFormatVersion ,
2195
- kCacheNvmFormatVersion , config_.size };
2213
+ kCacheNvmFormatVersion , config_.getCacheSize () };
2196
2214
}
2197
2215
2198
2216
template <typename CacheTrait>
@@ -3051,7 +3069,7 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
3051
3069
ioBuf->coalesce ();
3052
3070
3053
3071
ShmSegmentOpts opts;
3054
- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
3072
+ opts.typeOpts = PosixSysVSegmentOpts (config_.isUsingPosixShm () );
3055
3073
3056
3074
void * infoAddr = shmManager_->createShm (detail::kShmInfoName , ioBuf->length (),
3057
3075
nullptr , opts).addr ;
0 commit comments