Skip to content

Commit 790c09f

Browse files
igchorvinser52
authored andcommitted
Introduce FileShmSegment for file-backed shared memory
It's implementation is mostly based on PosixShmSegment. Also, extend ShmManager and ShmSegmentOpts to support this new segment type.
1 parent 36eae66 commit 790c09f

File tree

12 files changed

+590
-55
lines changed

12 files changed

+590
-55
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
6565
AccessContainer::getRequiredSize(
6666
config_.accessConfig.getNumBuckets()),
6767
nullptr,
68-
ShmSegmentOpts(config_.accessConfig.getPageSize()))
68+
ShmSegmentOpts(config_.accessConfig.getPageSize(),
69+
false, config_.usePosixShm))
6970
.addr,
7071
compressor_,
7172
[this](Item* it) -> ItemHandle { return acquire(it); })),
@@ -76,7 +77,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
7677
AccessContainer::getRequiredSize(
7778
config_.chainedItemAccessConfig.getNumBuckets()),
7879
nullptr,
79-
ShmSegmentOpts(config_.accessConfig.getPageSize()))
80+
ShmSegmentOpts(config_.accessConfig.getPageSize(),
81+
false, config_.usePosixShm))
8082
.addr,
8183
compressor_,
8284
[this](Item* it) -> ItemHandle { return acquire(it); })),
@@ -86,7 +88,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
8688
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
8789
config_.isNvmCacheTruncateAllocSizeEnabled()} {
8890
initCommon(false);
89-
shmManager_->removeShm(detail::kShmInfoName);
91+
shmManager_->removeShm(detail::kShmInfoName,
92+
PosixSysVSegmentOpts(config_.usePosixShm));
9093
}
9194

9295
template <typename CacheTrait>
@@ -104,13 +107,15 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
104107
accessContainer_(std::make_unique<AccessContainer>(
105108
deserializer_->deserialize<AccessSerializationType>(),
106109
config_.accessConfig,
107-
shmManager_->attachShm(detail::kShmHashTableName),
110+
shmManager_->attachShm(detail::kShmHashTableName, nullptr,
111+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
108112
compressor_,
109113
[this](Item* it) -> ItemHandle { return acquire(it); })),
110114
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
111115
deserializer_->deserialize<AccessSerializationType>(),
112116
config_.chainedItemAccessConfig,
113-
shmManager_->attachShm(detail::kShmChainedItemHashTableName),
117+
shmManager_->attachShm(detail::kShmChainedItemHashTableName, nullptr,
118+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
114119
compressor_,
115120
[this](Item* it) -> ItemHandle { return acquire(it); })),
116121
chainedItemLocks_(config_.chainedItemsLockPower,
@@ -127,7 +132,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
127132
// We will create a new info shm segment on shutDown(). If we don't remove
128133
// this info shm segment here and the new info shm segment's size is larger
129134
// than this one, creating new one will fail.
130-
shmManager_->removeShm(detail::kShmInfoName);
135+
shmManager_->removeShm(detail::kShmInfoName,
136+
PosixSysVSegmentOpts(config_.usePosixShm));
131137
}
132138

133139
template <typename CacheTrait>
@@ -145,6 +151,7 @@ std::unique_ptr<MemoryAllocator>
145151
CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
146152
ShmSegmentOpts opts;
147153
opts.alignment = sizeof(Slab);
154+
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
148155
return std::make_unique<MemoryAllocator>(
149156
getAllocatorConfig(config_),
150157
shmManager_
@@ -159,6 +166,7 @@ std::unique_ptr<MemoryAllocator>
159166
CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
160167
ShmSegmentOpts opts;
161168
opts.alignment = sizeof(Slab);
169+
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
162170
return std::make_unique<MemoryAllocator>(
163171
deserializer_->deserialize<MemoryAllocator::SerializationType>(),
164172
shmManager_
@@ -263,7 +271,8 @@ void CacheAllocator<CacheTrait>::initWorkers() {
263271

264272
template <typename CacheTrait>
265273
std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
266-
auto infoAddr = shmManager_->attachShm(detail::kShmInfoName);
274+
auto infoAddr = shmManager_->attachShm(detail::kShmInfoName, nullptr,
275+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm));
267276
return std::make_unique<Deserializer>(
268277
reinterpret_cast<uint8_t*>(infoAddr.addr),
269278
reinterpret_cast<uint8_t*>(infoAddr.addr) + infoAddr.size);
@@ -3097,8 +3106,11 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
30973106
std::unique_ptr<folly::IOBuf> ioBuf = serializedBuf.move();
30983107
ioBuf->coalesce();
30993108

3100-
void* infoAddr =
3101-
shmManager_->createShm(detail::kShmInfoName, ioBuf->length()).addr;
3109+
ShmSegmentOpts opts;
3110+
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
3111+
3112+
void* infoAddr = shmManager_->createShm(detail::kShmInfoName, ioBuf->length(),
3113+
nullptr, opts).addr;
31023114
Serializer serializer(reinterpret_cast<uint8_t*>(infoAddr),
31033115
reinterpret_cast<uint8_t*>(infoAddr) + ioBuf->length());
31043116
serializer.writeToBuffer(std::move(ioBuf));
@@ -3444,7 +3456,7 @@ bool CacheAllocator<CacheTrait>::stopReaper(std::chrono::seconds timeout) {
34443456

34453457
template <typename CacheTrait>
34463458
bool CacheAllocator<CacheTrait>::cleanupStrayShmSegments(
3447-
const std::string& cacheDir, bool posix) {
3459+
const std::string& cacheDir, bool posix /*TODO(SHM_FILE): const std::vector<CacheMemoryTierConfig>& config */) {
34483460
if (util::getStatIfExists(cacheDir, nullptr) && util::isDir(cacheDir)) {
34493461
try {
34503462
// cache dir exists. clean up only if there are no other processes
@@ -3463,6 +3475,12 @@ bool CacheAllocator<CacheTrait>::cleanupStrayShmSegments(
34633475
ShmManager::removeByName(cacheDir, detail::kShmHashTableName, posix);
34643476
ShmManager::removeByName(cacheDir, detail::kShmChainedItemHashTableName,
34653477
posix);
3478+
3479+
// TODO(SHM_FILE): try to nuke segments of differente types (which require
3480+
// extra info)
3481+
// for (auto &tier : config) {
3482+
// ShmManager::removeByName(cacheDir, tierShmName, config_.memoryTiers[i].opts);
3483+
// }
34663484
}
34673485
return true;
34683486
}

cachelib/allocator/CacheAllocator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,8 @@ class CacheAllocator : public CacheBase {
11531153
// returns true if there was no error in trying to cleanup the segment
11541154
// because another process was attached. False if the user tried to clean up
11551155
// and the cache was actually attached.
1156-
static bool cleanupStrayShmSegments(const std::string& cacheDir, bool posix);
1156+
static bool cleanupStrayShmSegments(const std::string& cacheDir, bool posix
1157+
/*TODO: const std::vector<CacheMemoryTierConfig>& config = {} */);
11571158

11581159
// gives a relative offset to a pointer within the cache.
11591160
uint64_t getItemPtrAsOffset(const void* ptr);

cachelib/allocator/TempShmMapping.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ TempShmMapping::TempShmMapping(size_t size)
3434
TempShmMapping::~TempShmMapping() {
3535
try {
3636
if (addr_) {
37-
shmManager_->removeShm(detail::kTempShmCacheName.str());
37+
shmManager_->removeShm(detail::kTempShmCacheName.str(),
38+
PosixSysVSegmentOpts(false /* posix */));
3839
}
3940
if (shmManager_) {
4041
shmManager_.reset();
@@ -77,7 +78,8 @@ void* TempShmMapping::createShmMapping(ShmManager& shmManager,
7778
return shmAddr;
7879
} catch (...) {
7980
if (shmAddr) {
80-
shmManager.removeShm(detail::kTempShmCacheName.str());
81+
shmManager.removeShm(detail::kTempShmCacheName.str(),
82+
PosixSysVSegmentOpts(false /* posix */));
8183
} else {
8284
munmap(addr, size);
8385
}

cachelib/shm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_thrift_file(SHM shm.thrift frozen2)
1616

1717
add_library (cachelib_shm
1818
${SHM_THRIFT_FILES}
19+
FileShmSegment.cpp
1920
PosixShmSegment.cpp
2021
ShmCommon.cpp
2122
ShmManager.cpp

0 commit comments

Comments
 (0)