Skip to content

Commit 35cb682

Browse files
authored
Merge pull request #2 from igchor/config_api_integration
Config api integration
2 parents aa758e8 + f401f17 commit 35cb682

12 files changed

+285
-107
lines changed

cachelib/allocator/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ if (BUILD_TESTS)
109109
add_test (tests/ChainedHashTest.cpp)
110110
add_test (tests/AllocatorResizeTypeTest.cpp)
111111
add_test (tests/AllocatorHitStatsTypeTest.cpp)
112+
add_test (tests/AllocatorMemoryTiersTest.cpp)
112113
add_test (tests/MemoryTiersTest.cpp)
113114
add_test (tests/MultiAllocatorTest.cpp)
114115
add_test (tests/NvmAdmissionPolicyTest.cpp)

cachelib/allocator/CacheAllocator-inl.h

+46-28
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@ namespace cachelib {
2424

2525
template <typename CacheTrait>
2626
CacheAllocator<CacheTrait>::CacheAllocator(Config config)
27-
: isOnShm_{config.memMonitoringEnabled()},
27+
: memoryTierConfigs(config.getMemoryTierConfigs()),
28+
isOnShm_{config.memMonitoringEnabled()},
2829
config_(config.validate()),
29-
tempShm_(isOnShm_ ? std::make_unique<TempShmMapping>(config_.size)
30+
tempShm_(isOnShm_ ? std::make_unique<TempShmMapping>(
31+
config_.getCacheSize())
3032
: nullptr),
3133
allocator_(isOnShm_ ? std::make_unique<MemoryAllocator>(
3234
getAllocatorConfig(config_),
3335
tempShm_->getAddr(),
34-
config_.size)
36+
config_.getCacheSize())
3537
: std::make_unique<MemoryAllocator>(
36-
getAllocatorConfig(config_), config_.size)),
38+
getAllocatorConfig(config_),
39+
config_.getCacheSize())),
3740
compactCacheManager_(std::make_unique<CCacheManager>(*allocator_)),
3841
compressor_(createPtrCompressor()),
3942
accessContainer_(std::make_unique<AccessContainer>(
@@ -49,15 +52,22 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
4952
cacheCreationTime_{util::getCurrentTimeSec()},
5053
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
5154
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+
}
5261
initCommon(false);
5362
}
5463

5564
template <typename CacheTrait>
5665
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
57-
: isOnShm_{true},
66+
: memoryTierConfigs(config.getMemoryTierConfigs()),
67+
isOnShm_{true},
5868
config_(config.validate()),
5969
shmManager_(
60-
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
70+
std::make_unique<ShmManager>(config_.cacheDir, config_.isUsingPosixShm())),
6171
allocator_(createNewMemoryAllocator()),
6272
compactCacheManager_(std::make_unique<CCacheManager>(*allocator_)),
6373
compressor_(createPtrCompressor()),
@@ -69,7 +79,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
6979
config_.accessConfig.getNumBuckets()),
7080
nullptr,
7181
ShmSegmentOpts(config_.accessConfig.getPageSize(),
72-
false, config_.usePosixShm))
82+
false, config_.isUsingPosixShm()))
7383
.addr,
7484
compressor_,
7585
[this](Item* it) -> ItemHandle { return acquire(it); })),
@@ -81,7 +91,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
8191
config_.chainedItemAccessConfig.getNumBuckets()),
8292
nullptr,
8393
ShmSegmentOpts(config_.accessConfig.getPageSize(),
84-
false, config_.usePosixShm))
94+
false, config_.isUsingPosixShm()))
8595
.addr,
8696
compressor_,
8797
[this](Item* it) -> ItemHandle { return acquire(it); })),
@@ -92,12 +102,13 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
92102
config_.isNvmCacheTruncateAllocSizeEnabled()} {
93103
initCommon(false);
94104
shmManager_->removeShm(detail::kShmInfoName,
95-
PosixSysVSegmentOpts(config_.usePosixShm));
105+
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
96106
}
97107

98108
template <typename CacheTrait>
99109
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
100-
: isOnShm_{true},
110+
: memoryTierConfigs(config.getMemoryTierConfigs()),
111+
isOnShm_{true},
101112
config_(config.validate()),
102113
shmManager_(
103114
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -111,14 +122,14 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
111122
deserializer_->deserialize<AccessSerializationType>(),
112123
config_.accessConfig,
113124
shmManager_->attachShm(detail::kShmHashTableName, nullptr,
114-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
125+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
115126
compressor_,
116127
[this](Item* it) -> ItemHandle { return acquire(it); })),
117128
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
118129
deserializer_->deserialize<AccessSerializationType>(),
119130
config_.chainedItemAccessConfig,
120131
shmManager_->attachShm(detail::kShmChainedItemHashTableName, nullptr,
121-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
132+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
122133
compressor_,
123134
[this](Item* it) -> ItemHandle { return acquire(it); })),
124135
chainedItemLocks_(config_.chainedItemsLockPower,
@@ -136,7 +147,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
136147
// this info shm segment here and the new info shm segment's size is larger
137148
// than this one, creating new one will fail.
138149
shmManager_->removeShm(detail::kShmInfoName,
139-
PosixSysVSegmentOpts(config_.usePosixShm));
150+
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
140151
}
141152

142153
template <typename CacheTrait>
@@ -150,32 +161,39 @@ CacheAllocator<CacheTrait>::~CacheAllocator() {
150161
}
151162

152163
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+
155169
ShmSegmentOpts opts;
156170
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() {
158179
return std::make_unique<MemoryAllocator>(
159180
getAllocatorConfig(config_),
160181
shmManager_
161-
->createShm(detail::kShmCacheName, config_.size,
162-
config_.slabMemoryBaseAddr, opts)
182+
->createShm(detail::kShmCacheName, config_.getCacheSize(),
183+
config_.slabMemoryBaseAddr, createShmCacheOpts())
163184
.addr,
164-
config_.size);
185+
config_.getCacheSize());
165186
}
166187

167188
template <typename CacheTrait>
168189
std::unique_ptr<MemoryAllocator>
169190
CacheAllocator<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,
178-
config_.size,
194+
->attachShm(detail::kShmCacheName, config_.slabMemoryBaseAddr,
195+
createShmCacheOpts()).addr,
196+
config_.getCacheSize(),
179197
config_.disableFullCoredump);
180198
}
181199

@@ -274,7 +292,7 @@ void CacheAllocator<CacheTrait>::initWorkers() {
274292
template <typename CacheTrait>
275293
std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
276294
auto infoAddr = shmManager_->attachShm(detail::kShmInfoName, nullptr,
277-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm));
295+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm()));
278296
return std::make_unique<Deserializer>(
279297
reinterpret_cast<uint8_t*>(infoAddr.addr),
280298
reinterpret_cast<uint8_t*>(infoAddr.addr) + infoAddr.size);
@@ -2192,7 +2210,7 @@ PoolEvictionAgeStats CacheAllocator<CacheTrait>::getPoolEvictionAgeStats(
21922210
template <typename CacheTrait>
21932211
CacheMetadata CacheAllocator<CacheTrait>::getCacheMetadata() const noexcept {
21942212
return CacheMetadata{kCachelibVersion, kCacheRamFormatVersion,
2195-
kCacheNvmFormatVersion, config_.size};
2213+
kCacheNvmFormatVersion, config_.getCacheSize()};
21962214
}
21972215

21982216
template <typename CacheTrait>
@@ -3051,7 +3069,7 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
30513069
ioBuf->coalesce();
30523070

30533071
ShmSegmentOpts opts;
3054-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
3072+
opts.typeOpts = PosixSysVSegmentOpts(config_.isUsingPosixShm());
30553073

30563074
void* infoAddr = shmManager_->createShm(detail::kShmInfoName, ioBuf->length(),
30573075
nullptr, opts).addr;

cachelib/allocator/CacheAllocator.h

+4
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,8 @@ class CacheAllocator : public CacheBase {
16071607
std::unique_ptr<T>& worker,
16081608
std::chrono::seconds timeout = std::chrono::seconds{0});
16091609

1610+
ShmSegmentOpts createShmCacheOpts();
1611+
16101612
std::unique_ptr<MemoryAllocator> createNewMemoryAllocator();
16111613
std::unique_ptr<MemoryAllocator> restoreMemoryAllocator();
16121614
std::unique_ptr<CCacheManager> restoreCCacheManager();
@@ -1714,6 +1716,8 @@ class CacheAllocator : public CacheBase {
17141716

17151717
const Config config_{};
17161718

1719+
const typename Config::MemoryTierConfigs memoryTierConfigs;
1720+
17171721
// Manages the temporary shared memory segment for memory allocator that
17181722
// is not persisted when cache process exits.
17191723
std::unique_ptr<TempShmMapping> tempShm_;

0 commit comments

Comments
 (0)