Skip to content

Commit 2eb9eac

Browse files
committed
cleaned up the interface. no need for use posix in some of the SHM APIs.
1 parent 4275baf commit 2eb9eac

File tree

9 files changed

+98
-99
lines changed

9 files changed

+98
-99
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,24 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
6868
accessContainer_(std::make_unique<AccessContainer>(
6969
config_.accessConfig,
7070
shmManager_
71-
->createShm(detail::kShmHashTableName,
72-
AccessContainer::getRequiredSize(
73-
config_.accessConfig.getNumBuckets()),
74-
nullptr,
75-
ShmSegmentOpts(config_.accessConfig.getPageSize(),
76-
false,
77-
config_.usePosixShm))
71+
->createShm(
72+
detail::kShmHashTableName,
73+
AccessContainer::getRequiredSize(
74+
config_.accessConfig.getNumBuckets()),
75+
nullptr,
76+
ShmSegmentOpts(config_.accessConfig.getPageSize(), false))
7877
.addr,
7978
compressor_,
8079
[this](Item* it) -> WriteHandle { return acquire(it); })),
8180
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
8281
config_.chainedItemAccessConfig,
8382
shmManager_
84-
->createShm(detail::kShmChainedItemHashTableName,
85-
AccessContainer::getRequiredSize(
86-
config_.chainedItemAccessConfig.getNumBuckets()),
87-
nullptr,
88-
ShmSegmentOpts(config_.accessConfig.getPageSize(),
89-
false,
90-
config_.usePosixShm))
83+
->createShm(
84+
detail::kShmChainedItemHashTableName,
85+
AccessContainer::getRequiredSize(
86+
config_.chainedItemAccessConfig.getNumBuckets()),
87+
nullptr,
88+
ShmSegmentOpts(config_.accessConfig.getPageSize(), false))
9189
.addr,
9290
compressor_,
9391
[this](Item* it) -> WriteHandle { return acquire(it); })),
@@ -103,8 +101,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
103101
config_.isNvmCacheEncryptionEnabled(),
104102
config_.isNvmCacheTruncateAllocSizeEnabled()} {
105103
initCommon(false);
106-
shmManager_->removeShm(detail::kShmInfoName,
107-
PosixSysVSegmentOpts(config_.usePosixShm));
104+
shmManager_->removeShm(detail::kShmInfoName);
108105
}
109106

110107
template <typename CacheTrait>
@@ -122,19 +119,17 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
122119
accessContainer_(std::make_unique<AccessContainer>(
123120
deserializer_->deserialize<AccessSerializationType>(),
124121
config_.accessConfig,
125-
shmManager_->attachShm(
126-
detail::kShmHashTableName,
127-
nullptr,
128-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
122+
shmManager_->attachShm(detail::kShmHashTableName,
123+
nullptr,
124+
ShmSegmentOpts(PageSizeT::NORMAL, false)),
129125
compressor_,
130126
[this](Item* it) -> WriteHandle { return acquire(it); })),
131127
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
132128
deserializer_->deserialize<AccessSerializationType>(),
133129
config_.chainedItemAccessConfig,
134-
shmManager_->attachShm(
135-
detail::kShmChainedItemHashTableName,
136-
nullptr,
137-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
130+
shmManager_->attachShm(detail::kShmChainedItemHashTableName,
131+
nullptr,
132+
ShmSegmentOpts(PageSizeT::NORMAL, false)),
138133
compressor_,
139134
[this](Item* it) -> WriteHandle { return acquire(it); })),
140135
chainedItemLocks_(config_.chainedItemsLockPower,
@@ -155,8 +150,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
155150
// We will create a new info shm segment on shutDown(). If we don't remove
156151
// this info shm segment here and the new info shm segment's size is larger
157152
// than this one, creating new one will fail.
158-
shmManager_->removeShm(detail::kShmInfoName,
159-
PosixSysVSegmentOpts(config_.usePosixShm));
153+
shmManager_->removeShm(detail::kShmInfoName);
160154
}
161155

162156
template <typename CacheTrait>
@@ -174,7 +168,8 @@ std::unique_ptr<MemoryAllocator>
174168
CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
175169
ShmSegmentOpts opts;
176170
opts.alignment = sizeof(Slab);
177-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
171+
opts.typeOpts =
172+
config_.usePosixShm ? PosixShmSegmentOpts{} : SysVShmSegmentOpts{};
178173
return std::make_unique<MemoryAllocator>(
179174
getAllocatorConfig(config_),
180175
shmManager_
@@ -189,7 +184,8 @@ std::unique_ptr<MemoryAllocator>
189184
CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
190185
ShmSegmentOpts opts;
191186
opts.alignment = sizeof(Slab);
192-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
187+
opts.typeOpts =
188+
config_.usePosixShm ? PosixShmSegmentOpts{} : SysVShmSegmentOpts{};
193189
return std::make_unique<MemoryAllocator>(
194190
deserializer_->deserialize<MemoryAllocator::SerializationType>(),
195191
shmManager_
@@ -298,8 +294,7 @@ void CacheAllocator<CacheTrait>::initWorkers() {
298294
template <typename CacheTrait>
299295
std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
300296
auto infoAddr = shmManager_->attachShm(
301-
detail::kShmInfoName, nullptr,
302-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm));
297+
detail::kShmInfoName, nullptr, ShmSegmentOpts(PageSizeT::NORMAL, false));
303298
return std::make_unique<Deserializer>(
304299
reinterpret_cast<uint8_t*>(infoAddr.addr),
305300
reinterpret_cast<uint8_t*>(infoAddr.addr) + infoAddr.size);
@@ -3162,7 +3157,8 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
31623157
ioBuf->coalesce();
31633158

31643159
ShmSegmentOpts opts;
3165-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
3160+
opts.typeOpts =
3161+
config_.usePosixShm ? PosixShmSegmentOpts{} : SysVShmSegmentOpts{};
31663162

31673163
void* infoAddr =
31683164
shmManager_

cachelib/allocator/TempShmMapping.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ TempShmMapping::TempShmMapping(size_t size)
3434
TempShmMapping::~TempShmMapping() {
3535
try {
3636
if (addr_) {
37-
shmManager_->removeShm(detail::kTempShmCacheName.str(),
38-
PosixSysVSegmentOpts(false /* posix */));
37+
shmManager_->removeShm(detail::kTempShmCacheName.str());
3938
}
4039
if (shmManager_) {
4140
shmManager_.reset();
@@ -78,8 +77,7 @@ void* TempShmMapping::createShmMapping(ShmManager& shmManager,
7877
return shmAddr;
7978
} catch (...) {
8079
if (shmAddr) {
81-
shmManager.removeShm(detail::kTempShmCacheName.str(),
82-
PosixSysVSegmentOpts(false /* posix */));
80+
shmManager.removeShm(detail::kTempShmCacheName.str());
8381
} else {
8482
munmap(addr, size);
8583
}

cachelib/allocator/memory/tests/SlabAllocatorTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ TEST_F(SlabAllocatorTest, AdviseRelease) {
584584
shmName += std::to_string(::getpid());
585585
shmManager.createShm(shmName, allocSize, memory);
586586

587-
SCOPE_EXIT { shmManager.removeShm(shmName, PosixSysVSegmentOpts(false)); };
587+
SCOPE_EXIT { shmManager.removeShm(shmName); };
588588

589589
memory = util::align(Slab::kSize, size, memory, allocSize);
590590

@@ -714,7 +714,7 @@ TEST_F(SlabAllocatorTest, AdviseSaveRestore) {
714714
ShmManager shmManager(cacheDir, false /* posix */);
715715
shmManager.createShm(shmName, allocSize, memory);
716716

717-
SCOPE_EXIT { shmManager.removeShm(shmName, PosixSysVSegmentOpts(false)); };
717+
SCOPE_EXIT { shmManager.removeShm(shmName); };
718718

719719
{
720720
SlabAllocator s(memory, size, config);

cachelib/shm/Shm.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ class ShmSegment {
5252
if (auto* v = std::get_if<FileShmSegmentOpts>(&opts.typeOpts)) {
5353
segment_ =
5454
std::make_unique<FileShmSegment>(ShmNew, std::move(name), size, opts);
55-
} else if (auto* v = std::get_if<PosixSysVSegmentOpts>(&opts.typeOpts)) {
56-
if (v->usePosix)
57-
segment_ = std::make_unique<PosixShmSegment>(ShmNew, std::move(name),
58-
size, opts);
59-
else
60-
segment_ = std::make_unique<SysVShmSegment>(ShmNew, std::move(name),
61-
size, opts);
55+
} else if (auto* v = std::get_if<PosixShmSegmentOpts>(&opts.typeOpts)) {
56+
segment_ = std::make_unique<PosixShmSegment>(ShmNew, std::move(name),
57+
size, opts);
58+
} else { // SysV segment
59+
segment_ =
60+
std::make_unique<SysVShmSegment>(ShmNew, std::move(name), size, opts);
6261
}
6362
}
6463

@@ -69,13 +68,12 @@ class ShmSegment {
6968
if (std::get_if<FileShmSegmentOpts>(&opts.typeOpts)) {
7069
segment_ =
7170
std::make_unique<FileShmSegment>(ShmAttach, std::move(name), opts);
72-
} else if (auto* v = std::get_if<PosixSysVSegmentOpts>(&opts.typeOpts)) {
73-
if (v->usePosix)
74-
segment_ =
75-
std::make_unique<PosixShmSegment>(ShmAttach, std::move(name), opts);
76-
else
77-
segment_ =
78-
std::make_unique<SysVShmSegment>(ShmAttach, std::move(name), opts);
71+
} else if (auto* v = std::get_if<PosixShmSegmentOpts>(&opts.typeOpts)) {
72+
segment_ =
73+
std::make_unique<PosixShmSegment>(ShmAttach, std::move(name), opts);
74+
} else { // SysV segment
75+
segment_ =
76+
std::make_unique<SysVShmSegment>(ShmAttach, std::move(name), opts);
7977
}
8078
}
8179

cachelib/shm/ShmCommon.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ struct FileShmSegmentOpts {
8282
std::string path;
8383
};
8484

85-
struct PosixSysVSegmentOpts {
86-
PosixSysVSegmentOpts(bool usePosix = false) : usePosix(usePosix) {}
87-
bool usePosix;
88-
};
85+
struct PosixShmSegmentOpts {};
86+
87+
struct SysVShmSegmentOpts {};
8988

90-
using ShmTypeOpts = std::variant<FileShmSegmentOpts, PosixSysVSegmentOpts>;
89+
using ShmTypeOpts =
90+
std::variant<FileShmSegmentOpts, PosixShmSegmentOpts, SysVShmSegmentOpts>;
9191

9292
struct ShmSegmentOpts {
9393
PageSizeT pageSize{PageSizeT::NORMAL};
@@ -96,11 +96,10 @@ struct ShmSegmentOpts {
9696
ShmTypeOpts typeOpts{}; // opts specific to segment type
9797

9898
explicit ShmSegmentOpts(PageSizeT p) : pageSize(p) {}
99-
explicit ShmSegmentOpts(PageSizeT p, bool ro) : pageSize(p), readOnly(ro) {}
10099
explicit ShmSegmentOpts(PageSizeT p, bool ro, const std::string& path)
101100
: pageSize(p), readOnly(ro), typeOpts(path) {}
102-
explicit ShmSegmentOpts(PageSizeT p, bool ro, bool posix)
103-
: pageSize(p), readOnly(ro), typeOpts(posix) {}
101+
explicit ShmSegmentOpts(PageSizeT p, bool ro)
102+
: pageSize(p), readOnly(ro), typeOpts() {}
104103
ShmSegmentOpts() : pageSize(PageSizeT::NORMAL) {}
105104
};
106105

cachelib/shm/ShmManager.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,13 @@ bool ShmManager::initFromFile() {
141141

142142
for (const auto& kv : *object.nameToKeyMap_ref()) {
143143
if (kv.second.get_path() == "") {
144-
PosixSysVSegmentOpts type;
145-
type.usePosix = kv.second.get_usePosix();
146-
nameToOpts_.insert({kv.first, type});
144+
if (kv.second.get_usePosix()) {
145+
PosixShmSegmentOpts type;
146+
nameToOpts_.insert({kv.first, type});
147+
} else {
148+
SysVShmSegmentOpts type;
149+
nameToOpts_.insert({kv.first, type});
150+
}
147151
} else {
148152
FileShmSegmentOpts type;
149153
type.path = kv.second.get_path();
@@ -178,15 +182,15 @@ typename ShmManager::ShutDownRes ShmManager::writeActiveSegmentsToFile() {
178182
serialization::ShmTypeObject key;
179183
if (const auto* opts = std::get_if<FileShmSegmentOpts>(&kv.second)) {
180184
key.path() = opts->path;
181-
} else {
182-
try {
183-
const auto& v = std::get<PosixSysVSegmentOpts>(kv.second);
184-
key.usePosix() = v.usePosix;
185-
key.path() = "";
186-
} catch (std::bad_variant_access&) {
187-
throw std::invalid_argument(folly::sformat("Not a valid segment"));
188-
}
185+
} else if (const auto* opts =
186+
std::get_if<PosixShmSegmentOpts>(&kv.second)) {
187+
key.usePosix() = true;
188+
key.path() = "";
189+
} else { // SysV
190+
key.usePosix() = false;
191+
key.path() = "";
189192
}
193+
190194
const auto it = segments_.find(name);
191195
// segment exists and is active.
192196
if (it != segments_.end() && it->second->isActive()) {
@@ -227,10 +231,7 @@ namespace {
227231
bool removeSegByName(ShmTypeOpts typeOpts, const std::string& uniqueName) {
228232
if (const auto* v = std::get_if<FileShmSegmentOpts>(&typeOpts)) {
229233
return FileShmSegment::removeByPath(v->path);
230-
}
231-
232-
bool usePosix = std::get<PosixSysVSegmentOpts>(typeOpts).usePosix;
233-
if (usePosix) {
234+
} else if (std::get_if<PosixShmSegmentOpts>(&typeOpts)) {
234235
return PosixShmSegment::removeByName(uniqueName);
235236
} else {
236237
return SysVShmSegment::removeByName(uniqueName);
@@ -310,13 +311,13 @@ ShmAddr ShmManager::createShm(const std::string& shmName,
310311
// attached or mapped
311312
// TODO(SHM_FILE): should we try to remove the segment using all possible
312313
// segment types?
313-
removeShm(shmName, opts.typeOpts);
314+
removeShm(shmName);
314315

315316
DCHECK(segments_.find(shmName) == segments_.end());
316317
DCHECK(nameToOpts_.find(shmName) == nameToOpts_.end());
317318

318-
const auto* v = std::get_if<PosixSysVSegmentOpts>(&opts.typeOpts);
319-
if (v && usePosix_ != v->usePosix) {
319+
const auto* v = std::get_if<PosixShmSegmentOpts>(&opts.typeOpts);
320+
if (v && !usePosix_) {
320321
throw std::invalid_argument(folly::sformat("Expected {} but got {} segment",
321322
usePosix_ ? "posix" : "SysV",
322323
usePosix_ ? "SysV" : "posix"));
@@ -347,9 +348,11 @@ ShmAddr ShmManager::createShm(const std::string& shmName,
347348
}
348349

349350
auto ret = newSeg->getCurrentMapping();
350-
if (v) {
351-
PosixSysVSegmentOpts opts;
352-
opts.usePosix = v->usePosix;
351+
if (std::get_if<PosixShmSegmentOpts>(&opts.typeOpts)) {
352+
PosixShmSegmentOpts opts;
353+
nameToOpts_.emplace(shmName, opts);
354+
} else if (std::get_if<SysVShmSegmentOpts>(&opts.typeOpts)) {
355+
SysVShmSegmentOpts opts;
353356
nameToOpts_.emplace(shmName, opts);
354357
} else {
355358
FileShmSegmentOpts opts;
@@ -368,8 +371,8 @@ void ShmManager::attachNewShm(const std::string& shmName, ShmSegmentOpts opts) {
368371
folly::sformat("Unable to find any segment with name {}", shmName));
369372
}
370373

371-
const auto* v = std::get_if<PosixSysVSegmentOpts>(&opts.typeOpts);
372-
if (v && usePosix_ != v->usePosix) {
374+
const auto* v = std::get_if<PosixShmSegmentOpts>(&opts.typeOpts);
375+
if (v && !usePosix_) {
373376
throw std::invalid_argument(folly::sformat("Expected {} but got {} segment",
374377
usePosix_ ? "posix" : "SysV",
375378
usePosix_ ? "SysV" : "posix"));
@@ -423,7 +426,7 @@ ShmAddr ShmManager::attachShm(const std::string& shmName,
423426
return shm.getCurrentMapping();
424427
}
425428

426-
bool ShmManager::removeShm(const std::string& shmName, ShmTypeOpts typeOpts) {
429+
bool ShmManager::removeShm(const std::string& shmName) {
427430
try {
428431
auto& shm = getShmByName(shmName);
429432
shm.detachCurrentMapping();
@@ -437,7 +440,11 @@ bool ShmManager::removeShm(const std::string& shmName, ShmTypeOpts typeOpts) {
437440
DCHECK(shm.isInvalid());
438441
} catch (const std::invalid_argument&) {
439442
// shm by this name is not attached.
440-
const bool wasPresent = removeSegByName(typeOpts, uniqueIdForName(shmName));
443+
if (nameToOpts_.find(shmName) == nameToOpts_.end()) {
444+
return false;
445+
}
446+
const bool wasPresent =
447+
removeSegByName(nameToOpts_[shmName], uniqueIdForName(shmName));
441448
if (!wasPresent) {
442449
DCHECK(segments_.end() == segments_.find(shmName));
443450
DCHECK(nameToOpts_.end() == nameToOpts_.find(shmName));

cachelib/shm/ShmManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class ShmManager {
9999
// @param shmName name of the segment
100100
// @return true if such a segment existed and we removed it.
101101
// false if segment never existed
102-
bool removeShm(const std::string& segName, ShmTypeOpts opts);
102+
bool removeShm(const std::string& segName);
103103

104104
// gets a current segment by the name that is managed by this
105105
// instance. The lifetime of the returned object is same as the

cachelib/shm/tests/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class ShmTest : public ShmTestBase {
9898

9999
class ShmTestPosix : public ShmTest {
100100
public:
101-
ShmTestPosix() { opts.typeOpts = PosixSysVSegmentOpts(true); }
101+
ShmTestPosix() { opts.typeOpts = PosixShmSegmentOpts(); }
102102

103103
private:
104104
void clearSegment() override {
@@ -114,7 +114,7 @@ class ShmTestPosix : public ShmTest {
114114

115115
class ShmTestSysV : public ShmTest {
116116
public:
117-
ShmTestSysV() { opts.typeOpts = PosixSysVSegmentOpts(false); }
117+
ShmTestSysV() { opts.typeOpts = SysVShmSegmentOpts(); }
118118

119119
private:
120120
void clearSegment() override {

0 commit comments

Comments
 (0)