Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cachelib/allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ if (BUILD_TESTS)
add_test (tests/ChainedHashTest.cpp)
add_test (tests/AllocatorResizeTypeTest.cpp)
add_test (tests/AllocatorHitStatsTypeTest.cpp)
add_test (tests/AllocatorMemoryTiersTest.cpp)
add_test (tests/MemoryTiersTest.cpp)
add_test (tests/MultiAllocatorTest.cpp)
add_test (tests/NvmAdmissionPolicyTest.cpp)
Expand Down
74 changes: 46 additions & 28 deletions cachelib/allocator/CacheAllocator-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ namespace cachelib {

template <typename CacheTrait>
CacheAllocator<CacheTrait>::CacheAllocator(Config config)
: isOnShm_{config.memMonitoringEnabled()},
: memoryTierConfigs(config.getMemoryTierConfigs()),
isOnShm_{config.memMonitoringEnabled()},
config_(config.validate()),
tempShm_(isOnShm_ ? std::make_unique<TempShmMapping>(config_.size)
tempShm_(isOnShm_ ? std::make_unique<TempShmMapping>(
config_.getCacheSize())
: nullptr),
allocator_(isOnShm_ ? std::make_unique<MemoryAllocator>(
getAllocatorConfig(config_),
tempShm_->getAddr(),
config_.size)
config_.getCacheSize())
: std::make_unique<MemoryAllocator>(
getAllocatorConfig(config_), config_.size)),
getAllocatorConfig(config_),
config_.getCacheSize())),
compactCacheManager_(std::make_unique<CCacheManager>(*allocator_)),
compressor_(createPtrCompressor()),
accessContainer_(std::make_unique<AccessContainer>(
Expand All @@ -49,15 +52,22 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
cacheCreationTime_{util::getCurrentTimeSec()},
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
config_.isNvmCacheTruncateAllocSizeEnabled()} {
// TODO(MEMORY_TIER)
if (std::holds_alternative<FileShmSegmentOpts>(
memoryTierConfigs[0].getShmTypeOpts())) {
throw std::runtime_error(
"Using custom memory tier is only supported for Shared Memory.");
}
initCommon(false);
}

template <typename CacheTrait>
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
: isOnShm_{true},
: memoryTierConfigs(config.getMemoryTierConfigs()),
isOnShm_{true},
config_(config.validate()),
shmManager_(
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
std::make_unique<ShmManager>(config_.cacheDir, config_.isUsingPosixShm())),
allocator_(createNewMemoryAllocator()),
compactCacheManager_(std::make_unique<CCacheManager>(*allocator_)),
compressor_(createPtrCompressor()),
Expand All @@ -69,7 +79,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
config_.accessConfig.getNumBuckets()),
nullptr,
ShmSegmentOpts(config_.accessConfig.getPageSize(),
false, config_.usePosixShm))
false, config_.isUsingPosixShm()))
.addr,
compressor_,
[this](Item* it) -> ItemHandle { return acquire(it); })),
Expand All @@ -81,7 +91,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
config_.chainedItemAccessConfig.getNumBuckets()),
nullptr,
ShmSegmentOpts(config_.accessConfig.getPageSize(),
false, config_.usePosixShm))
false, config_.isUsingPosixShm()))
.addr,
compressor_,
[this](Item* it) -> ItemHandle { return acquire(it); })),
Expand All @@ -92,12 +102,13 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
config_.isNvmCacheTruncateAllocSizeEnabled()} {
initCommon(false);
shmManager_->removeShm(detail::kShmInfoName,
PosixSysVSegmentOpts(config_.usePosixShm));
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
}

template <typename CacheTrait>
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
: isOnShm_{true},
: memoryTierConfigs(config.getMemoryTierConfigs()),
isOnShm_{true},
config_(config.validate()),
shmManager_(
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
Expand All @@ -111,14 +122,14 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
deserializer_->deserialize<AccessSerializationType>(),
config_.accessConfig,
shmManager_->attachShm(detail::kShmHashTableName, nullptr,
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
compressor_,
[this](Item* it) -> ItemHandle { return acquire(it); })),
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
deserializer_->deserialize<AccessSerializationType>(),
config_.chainedItemAccessConfig,
shmManager_->attachShm(detail::kShmChainedItemHashTableName, nullptr,
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
compressor_,
[this](Item* it) -> ItemHandle { return acquire(it); })),
chainedItemLocks_(config_.chainedItemsLockPower,
Expand All @@ -136,7 +147,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
// this info shm segment here and the new info shm segment's size is larger
// than this one, creating new one will fail.
shmManager_->removeShm(detail::kShmInfoName,
PosixSysVSegmentOpts(config_.usePosixShm));
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
}

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

template <typename CacheTrait>
std::unique_ptr<MemoryAllocator>
CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts() {
if (memoryTierConfigs.size() > 1) {
throw std::invalid_argument("CacheLib only supports a single memory tier");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{} coding standard compliance needed

}

ShmSegmentOpts opts;
opts.alignment = sizeof(Slab);
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
opts.typeOpts = memoryTierConfigs[0].getShmTypeOpts();

return opts;
}

template <typename CacheTrait>
std::unique_ptr<MemoryAllocator>
CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
return std::make_unique<MemoryAllocator>(
getAllocatorConfig(config_),
shmManager_
->createShm(detail::kShmCacheName, config_.size,
config_.slabMemoryBaseAddr, opts)
->createShm(detail::kShmCacheName, config_.getCacheSize(),
config_.slabMemoryBaseAddr, createShmCacheOpts())
.addr,
config_.size);
config_.getCacheSize());
}

template <typename CacheTrait>
std::unique_ptr<MemoryAllocator>
CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
ShmSegmentOpts opts;
opts.alignment = sizeof(Slab);
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
return std::make_unique<MemoryAllocator>(
deserializer_->deserialize<MemoryAllocator::SerializationType>(),
shmManager_
->attachShm(detail::kShmCacheName, config_.slabMemoryBaseAddr, opts)
.addr,
config_.size,
->attachShm(detail::kShmCacheName, config_.slabMemoryBaseAddr,
createShmCacheOpts()).addr,
config_.getCacheSize(),
config_.disableFullCoredump);
}

Expand Down Expand Up @@ -274,7 +292,7 @@ void CacheAllocator<CacheTrait>::initWorkers() {
template <typename CacheTrait>
std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
auto infoAddr = shmManager_->attachShm(detail::kShmInfoName, nullptr,
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm));
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm()));
return std::make_unique<Deserializer>(
reinterpret_cast<uint8_t*>(infoAddr.addr),
reinterpret_cast<uint8_t*>(infoAddr.addr) + infoAddr.size);
Expand Down Expand Up @@ -2192,7 +2210,7 @@ PoolEvictionAgeStats CacheAllocator<CacheTrait>::getPoolEvictionAgeStats(
template <typename CacheTrait>
CacheMetadata CacheAllocator<CacheTrait>::getCacheMetadata() const noexcept {
return CacheMetadata{kCachelibVersion, kCacheRamFormatVersion,
kCacheNvmFormatVersion, config_.size};
kCacheNvmFormatVersion, config_.getCacheSize()};
}

template <typename CacheTrait>
Expand Down Expand Up @@ -3051,7 +3069,7 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
ioBuf->coalesce();

ShmSegmentOpts opts;
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
opts.typeOpts = PosixSysVSegmentOpts(config_.isUsingPosixShm());

void* infoAddr = shmManager_->createShm(detail::kShmInfoName, ioBuf->length(),
nullptr, opts).addr;
Expand Down
4 changes: 4 additions & 0 deletions cachelib/allocator/CacheAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,8 @@ class CacheAllocator : public CacheBase {
std::unique_ptr<T>& worker,
std::chrono::seconds timeout = std::chrono::seconds{0});

ShmSegmentOpts createShmCacheOpts();

std::unique_ptr<MemoryAllocator> createNewMemoryAllocator();
std::unique_ptr<MemoryAllocator> restoreMemoryAllocator();
std::unique_ptr<CCacheManager> restoreCCacheManager();
Expand Down Expand Up @@ -1714,6 +1716,8 @@ class CacheAllocator : public CacheBase {

const Config config_{};

const typename Config::MemoryTierConfigs memoryTierConfigs;

// Manages the temporary shared memory segment for memory allocator that
// is not persisted when cache process exits.
std::unique_ptr<TempShmMapping> tempShm_;
Expand Down
Loading