Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
39 changes: 39 additions & 0 deletions .github/workflows/build-cachelib-centos-long.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: build-cachelib-centos-latest
on:
schedule:
- cron: '0 7 * * *'

jobs:
build-cachelib-centos8-latest:
name: "CentOS/latest - Build CacheLib with all dependencies"
runs-on: ubuntu-latest
# Docker container image name
container: "centos:latest"
steps:
- name: "update packages"
run: dnf upgrade -y
- name: "install sudo,git"
run: dnf install -y sudo git cmake gcc
- name: "System Information"
run: |
echo === uname ===
uname -a
echo === /etc/os-release ===
cat /etc/os-release
echo === df -hl ===
df -hl
echo === free -h ===
free -h
echo === top ===
top -b -n1 -1 -Eg || timeout 1 top -b -n1
echo === env ===
env
echo === gcc -v ===
gcc -v
- name: "checkout sources"
uses: actions/checkout@v2
- name: "build CacheLib using build script"
run: ./contrib/build.sh -j -v -T
- name: "run tests"
timeout-minutes: 60
run: cd opt/cachelib/tests && ../../../run_tests.sh long
8 changes: 6 additions & 2 deletions .github/workflows/build-cachelib-centos.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: build-cachelib-centos-latest
on:
schedule:
- cron: '30 5 * * 1,4'
push:
pull_request:

jobs:
build-cachelib-centos8-latest:
name: "CentOS/latest - Build CacheLib with all dependencies"
Expand Down Expand Up @@ -33,3 +34,6 @@ jobs:
uses: actions/checkout@v2
- name: "build CacheLib using build script"
run: ./contrib/build.sh -j -v -T
- name: "run tests"
timeout-minutes: 60
run: cd opt/cachelib/tests && ../../../run_tests.sh
8 changes: 6 additions & 2 deletions .github/workflows/build-cachelib-debian.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: build-cachelib-debian-10
on:
schedule:
- cron: '30 5 * * 2,6'
push:
pull_request:

jobs:
build-cachelib-debian-10:
name: "Debian/Buster - Build CacheLib with all dependencies"
Expand Down Expand Up @@ -37,3 +38,6 @@ jobs:
uses: actions/checkout@v2
- name: "build CacheLib using build script"
run: ./contrib/build.sh -j -v -T
- name: "run tests"
timeout-minutes: 60
run: cd opt/cachelib/tests && ../../../run_tests.sh
2 changes: 2 additions & 0 deletions cachelib/allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ 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)
add_test (nvmcache/tests/NvmItemTests.cpp)
Expand Down
92 changes: 64 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 @@ -68,7 +78,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
AccessContainer::getRequiredSize(
config_.accessConfig.getNumBuckets()),
nullptr,
ShmSegmentOpts(config_.accessConfig.getPageSize()))
ShmSegmentOpts(config_.accessConfig.getPageSize(),
false, config_.isUsingPosixShm()))
.addr,
compressor_,
[this](Item* it) -> ItemHandle { return acquire(it); })),
Expand All @@ -79,7 +90,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
AccessContainer::getRequiredSize(
config_.chainedItemAccessConfig.getNumBuckets()),
nullptr,
ShmSegmentOpts(config_.accessConfig.getPageSize()))
ShmSegmentOpts(config_.accessConfig.getPageSize(),
false, config_.isUsingPosixShm()))
.addr,
compressor_,
[this](Item* it) -> ItemHandle { return acquire(it); })),
Expand All @@ -89,12 +101,14 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
config_.isNvmCacheTruncateAllocSizeEnabled()} {
initCommon(false);
shmManager_->removeShm(detail::kShmInfoName);
shmManager_->removeShm(detail::kShmInfoName,
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 @@ -107,13 +121,15 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
accessContainer_(std::make_unique<AccessContainer>(
deserializer_->deserialize<AccessSerializationType>(),
config_.accessConfig,
shmManager_->attachShm(detail::kShmHashTableName),
shmManager_->attachShm(detail::kShmHashTableName, nullptr,
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),
shmManager_->attachShm(detail::kShmChainedItemHashTableName, nullptr,
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
compressor_,
[this](Item* it) -> ItemHandle { return acquire(it); })),
chainedItemLocks_(config_.chainedItemsLockPower,
Expand All @@ -130,7 +146,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
// We will create a new info shm segment on shutDown(). If we don't remove
// 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);
shmManager_->removeShm(detail::kShmInfoName,
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
}

template <typename CacheTrait>
Expand All @@ -144,30 +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");
}

ShmSegmentOpts opts;
opts.alignment = sizeof(Slab);
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);
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 @@ -265,7 +291,8 @@ void CacheAllocator<CacheTrait>::initWorkers() {

template <typename CacheTrait>
std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
auto infoAddr = shmManager_->attachShm(detail::kShmInfoName);
auto infoAddr = shmManager_->attachShm(detail::kShmInfoName, nullptr,
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 @@ -2183,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 @@ -3041,8 +3068,11 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
std::unique_ptr<folly::IOBuf> ioBuf = serializedBuf.move();
ioBuf->coalesce();

void* infoAddr =
shmManager_->createShm(detail::kShmInfoName, ioBuf->length()).addr;
ShmSegmentOpts opts;
opts.typeOpts = PosixSysVSegmentOpts(config_.isUsingPosixShm());

void* infoAddr = shmManager_->createShm(detail::kShmInfoName, ioBuf->length(),
nullptr, opts).addr;
Serializer serializer(reinterpret_cast<uint8_t*>(infoAddr),
reinterpret_cast<uint8_t*>(infoAddr) + ioBuf->length());
serializer.writeToBuffer(std::move(ioBuf));
Expand Down Expand Up @@ -3386,7 +3416,7 @@ bool CacheAllocator<CacheTrait>::stopReaper(std::chrono::seconds timeout) {

template <typename CacheTrait>
bool CacheAllocator<CacheTrait>::cleanupStrayShmSegments(
const std::string& cacheDir, bool posix) {
const std::string& cacheDir, bool posix /*TODO(SHM_FILE): const std::vector<CacheMemoryTierConfig>& config */) {
if (util::getStatIfExists(cacheDir, nullptr) && util::isDir(cacheDir)) {
try {
// cache dir exists. clean up only if there are no other processes
Expand All @@ -3405,6 +3435,12 @@ bool CacheAllocator<CacheTrait>::cleanupStrayShmSegments(
ShmManager::removeByName(cacheDir, detail::kShmHashTableName, posix);
ShmManager::removeByName(cacheDir, detail::kShmChainedItemHashTableName,
posix);

// TODO(SHM_FILE): try to nuke segments of differente types (which require
// extra info)
// for (auto &tier : config) {
// ShmManager::removeByName(cacheDir, tierShmName, config_.memoryTiers[i].opts);
// }
}
return true;
}
Expand Down
7 changes: 6 additions & 1 deletion cachelib/allocator/CacheAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,8 @@ class CacheAllocator : public CacheBase {
// returns true if there was no error in trying to cleanup the segment
// because another process was attached. False if the user tried to clean up
// and the cache was actually attached.
static bool cleanupStrayShmSegments(const std::string& cacheDir, bool posix);
static bool cleanupStrayShmSegments(const std::string& cacheDir, bool posix
/*TODO: const std::vector<CacheMemoryTierConfig>& config = {} */);

// gives a relative offset to a pointer within the cache.
uint64_t getItemPtrAsOffset(const void* ptr);
Expand Down Expand Up @@ -1606,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 @@ -1713,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