@@ -24,7 +24,8 @@ 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
30
tempShm_ (isOnShm_ ? std::make_unique<TempShmMapping>(config_.size)
30
31
: nullptr ),
@@ -49,15 +50,21 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
49
50
cacheCreationTime_{util::getCurrentTimeSec ()},
50
51
nvmCacheState_{config_.cacheDir , config_.isNvmCacheEncryptionEnabled (),
51
52
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
+ }
52
58
initCommon (false );
53
59
}
54
60
55
61
template <typename CacheTrait>
56
62
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
57
- : isOnShm_{true },
63
+ : memoryTierConfigs(config.getMemoryTierConfigs()),
64
+ isOnShm_{true },
58
65
config_ (config.validate()),
59
66
shmManager_ (
60
- std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm )),
67
+ std::make_unique<ShmManager>(config_.cacheDir, config_.isUsingPosixShm() )),
61
68
allocator_ (createNewMemoryAllocator()),
62
69
compactCacheManager_ (std::make_unique<CCacheManager>(*allocator_)),
63
70
compressor_ (createPtrCompressor()),
@@ -69,7 +76,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
69
76
config_.accessConfig.getNumBuckets()),
70
77
nullptr,
71
78
ShmSegmentOpts(config_.accessConfig.getPageSize(),
72
- false, config_.usePosixShm ))
79
+ false, config_.isUsingPosixShm() ))
73
80
.addr,
74
81
compressor_,
75
82
[this](Item* it) -> ItemHandle { return acquire (it); })),
@@ -81,7 +88,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
81
88
config_.chainedItemAccessConfig.getNumBuckets()),
82
89
nullptr,
83
90
ShmSegmentOpts(config_.accessConfig.getPageSize(),
84
- false, config_.usePosixShm ))
91
+ false, config_.isUsingPosixShm() ))
85
92
.addr,
86
93
compressor_,
87
94
[this](Item* it) -> ItemHandle { return acquire (it); })),
@@ -92,12 +99,13 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
92
99
config_.isNvmCacheTruncateAllocSizeEnabled ()} {
93
100
initCommon (false );
94
101
shmManager_->removeShm (detail::kShmInfoName ,
95
- PosixSysVSegmentOpts (config_.usePosixShm ));
102
+ PosixSysVSegmentOpts (config_.isUsingPosixShm () ));
96
103
}
97
104
98
105
template <typename CacheTrait>
99
106
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
100
- : isOnShm_{true },
107
+ : memoryTierConfigs(config.getMemoryTierConfigs()),
108
+ isOnShm_{true },
101
109
config_ (config.validate()),
102
110
shmManager_(
103
111
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -111,14 +119,14 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
111
119
deserializer_->deserialize<AccessSerializationType>(),
112
120
config_.accessConfig,
113
121
shmManager_->attachShm(detail::kShmHashTableName , nullptr ,
114
- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm )),
122
+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm() )),
115
123
compressor_,
116
124
[this](Item* it) -> ItemHandle { return acquire (it); })),
117
125
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
118
126
deserializer_->deserialize<AccessSerializationType>(),
119
127
config_.chainedItemAccessConfig,
120
128
shmManager_->attachShm(detail::kShmChainedItemHashTableName , nullptr ,
121
- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm )),
129
+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm() )),
122
130
compressor_,
123
131
[this](Item* it) -> ItemHandle { return acquire (it); })),
124
132
chainedItemLocks_(config_.chainedItemsLockPower,
@@ -136,7 +144,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
136
144
// this info shm segment here and the new info shm segment's size is larger
137
145
// than this one, creating new one will fail.
138
146
shmManager_->removeShm (detail::kShmInfoName ,
139
- PosixSysVSegmentOpts (config_.usePosixShm ));
147
+ PosixSysVSegmentOpts (config_.isUsingPosixShm () ));
140
148
}
141
149
142
150
template <typename CacheTrait>
@@ -150,31 +158,47 @@ CacheAllocator<CacheTrait>::~CacheAllocator() {
150
158
}
151
159
152
160
template <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
+
155
166
ShmSegmentOpts opts;
156
167
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() {
158
185
return std::make_unique<MemoryAllocator>(
159
186
getAllocatorConfig (config_),
160
187
shmManager_
161
188
->createShm (detail::kShmCacheName , config_.size ,
162
- config_.slabMemoryBaseAddr , opts )
189
+ config_.slabMemoryBaseAddr , createShmCacheOpts () )
163
190
.addr ,
164
191
config_.size );
165
192
}
166
193
167
194
template <typename CacheTrait>
168
195
std::unique_ptr<MemoryAllocator>
169
196
CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
170
- ShmSegmentOpts opts;
171
- opts.alignment = sizeof (Slab);
172
- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
173
197
return std::make_unique<MemoryAllocator>(
174
198
deserializer_->deserialize <MemoryAllocator::SerializationType>(),
175
199
shmManager_
176
- ->attachShm (detail::kShmCacheName , config_.slabMemoryBaseAddr , opts)
177
- .addr ,
200
+ ->attachShm (detail::kShmCacheName , config_.slabMemoryBaseAddr ,
201
+ createShmCacheOpts ()) .addr ,
178
202
config_.size ,
179
203
config_.disableFullCoredump );
180
204
}
@@ -274,7 +298,7 @@ void CacheAllocator<CacheTrait>::initWorkers() {
274
298
template <typename CacheTrait>
275
299
std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
276
300
auto infoAddr = shmManager_->attachShm (detail::kShmInfoName , nullptr ,
277
- ShmSegmentOpts (PageSizeT::NORMAL, false , config_.usePosixShm ));
301
+ ShmSegmentOpts (PageSizeT::NORMAL, false , config_.isUsingPosixShm () ));
278
302
return std::make_unique<Deserializer>(
279
303
reinterpret_cast <uint8_t *>(infoAddr.addr ),
280
304
reinterpret_cast <uint8_t *>(infoAddr.addr ) + infoAddr.size );
@@ -3051,7 +3075,7 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
3051
3075
ioBuf->coalesce ();
3052
3076
3053
3077
ShmSegmentOpts opts;
3054
- opts.typeOpts = PosixSysVSegmentOpts (config_.usePosixShm );
3078
+ opts.typeOpts = PosixSysVSegmentOpts (config_.isUsingPosixShm () );
3055
3079
3056
3080
void * infoAddr = shmManager_->createShm (detail::kShmInfoName , ioBuf->length (),
3057
3081
nullptr , opts).addr ;
0 commit comments