Skip to content

Commit ee63ef3

Browse files
committed
Integrate Memory Tier config API with CacheAllocator.
1 parent aa758e8 commit ee63ef3

9 files changed

+138
-24
lines changed

cachelib/allocator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ 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()),
2930
tempShm_(isOnShm_ ? std::make_unique<TempShmMapping>(config_.size)
3031
: nullptr),
@@ -49,15 +50,21 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
4950
cacheCreationTime_{util::getCurrentTimeSec()},
5051
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
5152
config_.isNvmCacheTruncateAllocSizeEnabled()} {
53+
// TODO(MEMORY_TIER)
54+
if (memoryTierConfigs.size()) {
55+
throw std::runtime_error(
56+
"Using custom memory tier is only supported for Shared Memory.");
57+
}
5258
initCommon(false);
5359
}
5460

5561
template <typename CacheTrait>
5662
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
57-
: isOnShm_{true},
63+
: memoryTierConfigs(config.getMemoryTierConfigs()),
64+
isOnShm_{true},
5865
config_(config.validate()),
5966
shmManager_(
60-
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
67+
std::make_unique<ShmManager>(config_.cacheDir, config_.isUsingPosixShm())),
6168
allocator_(createNewMemoryAllocator()),
6269
compactCacheManager_(std::make_unique<CCacheManager>(*allocator_)),
6370
compressor_(createPtrCompressor()),
@@ -69,7 +76,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
6976
config_.accessConfig.getNumBuckets()),
7077
nullptr,
7178
ShmSegmentOpts(config_.accessConfig.getPageSize(),
72-
false, config_.usePosixShm))
79+
false, config_.isUsingPosixShm()))
7380
.addr,
7481
compressor_,
7582
[this](Item* it) -> ItemHandle { return acquire(it); })),
@@ -81,7 +88,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
8188
config_.chainedItemAccessConfig.getNumBuckets()),
8289
nullptr,
8390
ShmSegmentOpts(config_.accessConfig.getPageSize(),
84-
false, config_.usePosixShm))
91+
false, config_.isUsingPosixShm()))
8592
.addr,
8693
compressor_,
8794
[this](Item* it) -> ItemHandle { return acquire(it); })),
@@ -92,12 +99,13 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
9299
config_.isNvmCacheTruncateAllocSizeEnabled()} {
93100
initCommon(false);
94101
shmManager_->removeShm(detail::kShmInfoName,
95-
PosixSysVSegmentOpts(config_.usePosixShm));
102+
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
96103
}
97104

98105
template <typename CacheTrait>
99106
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
100-
: isOnShm_{true},
107+
: memoryTierConfigs(config.getMemoryTierConfigs()),
108+
isOnShm_{true},
101109
config_(config.validate()),
102110
shmManager_(
103111
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -111,14 +119,14 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
111119
deserializer_->deserialize<AccessSerializationType>(),
112120
config_.accessConfig,
113121
shmManager_->attachShm(detail::kShmHashTableName, nullptr,
114-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
122+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
115123
compressor_,
116124
[this](Item* it) -> ItemHandle { return acquire(it); })),
117125
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
118126
deserializer_->deserialize<AccessSerializationType>(),
119127
config_.chainedItemAccessConfig,
120128
shmManager_->attachShm(detail::kShmChainedItemHashTableName, nullptr,
121-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm)),
129+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
122130
compressor_,
123131
[this](Item* it) -> ItemHandle { return acquire(it); })),
124132
chainedItemLocks_(config_.chainedItemsLockPower,
@@ -136,7 +144,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
136144
// this info shm segment here and the new info shm segment's size is larger
137145
// than this one, creating new one will fail.
138146
shmManager_->removeShm(detail::kShmInfoName,
139-
PosixSysVSegmentOpts(config_.usePosixShm));
147+
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
140148
}
141149

142150
template <typename CacheTrait>
@@ -150,31 +158,47 @@ CacheAllocator<CacheTrait>::~CacheAllocator() {
150158
}
151159

152160
template <typename CacheTrait>
153-
std::unique_ptr<MemoryAllocator>
154-
CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
161+
ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts() {
162+
if (memoryTierConfigs.size() > 1) {
163+
throw std::invalid_argument("CacheLib only supports a single memory tier");
164+
}
165+
155166
ShmSegmentOpts opts;
156167
opts.alignment = sizeof(Slab);
157-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
168+
169+
// If memoryTierConfigs is empty, Fallback to Posix/SysV segment
170+
// to keep legacy bahavior
171+
// TODO(MEMORY_TIER) - guarantee there is always at least one mem
172+
// layer inside Config
173+
if (memoryTierConfigs.size()) {
174+
opts.typeOpts = FileShmSegmentOpts(memoryTierConfigs[0].path);
175+
} else {
176+
opts.typeOpts = PosixSysVSegmentOpts(config_.isUsingPosixShm());
177+
}
178+
179+
return opts;
180+
}
181+
182+
template <typename CacheTrait>
183+
std::unique_ptr<MemoryAllocator>
184+
CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
158185
return std::make_unique<MemoryAllocator>(
159186
getAllocatorConfig(config_),
160187
shmManager_
161188
->createShm(detail::kShmCacheName, config_.size,
162-
config_.slabMemoryBaseAddr, opts)
189+
config_.slabMemoryBaseAddr, createShmCacheOpts())
163190
.addr,
164191
config_.size);
165192
}
166193

167194
template <typename CacheTrait>
168195
std::unique_ptr<MemoryAllocator>
169196
CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
170-
ShmSegmentOpts opts;
171-
opts.alignment = sizeof(Slab);
172-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
173197
return std::make_unique<MemoryAllocator>(
174198
deserializer_->deserialize<MemoryAllocator::SerializationType>(),
175199
shmManager_
176-
->attachShm(detail::kShmCacheName, config_.slabMemoryBaseAddr, opts)
177-
.addr,
200+
->attachShm(detail::kShmCacheName, config_.slabMemoryBaseAddr,
201+
createShmCacheOpts()).addr,
178202
config_.size,
179203
config_.disableFullCoredump);
180204
}
@@ -274,7 +298,7 @@ void CacheAllocator<CacheTrait>::initWorkers() {
274298
template <typename CacheTrait>
275299
std::unique_ptr<Deserializer> CacheAllocator<CacheTrait>::createDeserializer() {
276300
auto infoAddr = shmManager_->attachShm(detail::kShmInfoName, nullptr,
277-
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.usePosixShm));
301+
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm()));
278302
return std::make_unique<Deserializer>(
279303
reinterpret_cast<uint8_t*>(infoAddr.addr),
280304
reinterpret_cast<uint8_t*>(infoAddr.addr) + infoAddr.size);
@@ -3051,7 +3075,7 @@ void CacheAllocator<CacheTrait>::saveRamCache() {
30513075
ioBuf->coalesce();
30523076

30533077
ShmSegmentOpts opts;
3054-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
3078+
opts.typeOpts = PosixSysVSegmentOpts(config_.isUsingPosixShm());
30553079

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

cachelib/allocator/CacheAllocator.h

Lines changed: 4 additions & 0 deletions
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_;

cachelib/allocator/CacheAllocatorConfig.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,6 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::configureMemoryTiers(
877877
return *this;
878878
}
879879

880-
//const std::vector<MemoryTierCacheConfig>& CacheAllocatorConfig<T>::getMemoryTierConfigs() {
881880
template <typename T>
882881
const typename CacheAllocatorConfig<T>::MemoryTierConfigs& CacheAllocatorConfig<T>::getMemoryTierConfigs() {
883882
return memoryTierConfigs;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) Intel Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cachelib/allocator/tests/AllocatorMemoryTiersTest.h"
18+
19+
namespace facebook {
20+
namespace cachelib {
21+
namespace tests {
22+
23+
using LruAllocatorMemoryTiersTest = AllocatorMemoryTiersTest<LruAllocator>;
24+
25+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiers) { this->testMultiTiers(); }
26+
27+
} // end of namespace tests
28+
} // end of namespace cachelib
29+
} // end of namespace facebook
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include "cachelib/allocator/CacheAllocatorConfig.h"
20+
#include "cachelib/allocator/MemoryTierCacheConfig.h"
21+
#include "cachelib/allocator/tests/TestBase.h"
22+
23+
namespace facebook {
24+
namespace cachelib {
25+
namespace tests {
26+
27+
template <typename AllocatorT>
28+
class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
29+
public:
30+
void testMultiTiers() {
31+
typename AllocatorT::Config config;
32+
config.setCacheSize(100 * Slab::kSize);
33+
config.configureMemoryTiers({
34+
MemoryTierCacheConfig::fromFile("/tmp/a" + std::to_string(::getpid()))
35+
.setRatio(1),
36+
MemoryTierCacheConfig::fromFile("/tmp/b" + std::to_string(::getpid()))
37+
.setRatio(1)
38+
});
39+
40+
// More than one tier is not supported
41+
ASSERT_THROW(std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config),
42+
std::invalid_argument);
43+
}
44+
};
45+
} // namespace tests
46+
} // namespace cachelib
47+
} // namespace facebook

cachelib/allocator/tests/AllocatorTypeTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "cachelib/allocator/tests/BaseAllocatorTest.h"
1818
#include "cachelib/allocator/tests/TestBase.h"
19+
#include "cachelib/allocator/MemoryTierCacheConfig.h"
1920

2021
namespace facebook {
2122
namespace cachelib {
@@ -215,6 +216,12 @@ TYPED_TEST(BaseAllocatorTest, ReaperOutOfBound) {
215216
}
216217

217218
TYPED_TEST(BaseAllocatorTest, ReaperShutDown) { this->testReaperShutDown(); }
219+
TYPED_TEST(BaseAllocatorTest, ReaperShutDownFile) {
220+
this->testReaperShutDown({
221+
MemoryTierCacheConfig::fromFile("/tmp/a" + std::to_string(::getpid()))
222+
.setRatio(1)
223+
});
224+
}
218225

219226
TYPED_TEST(BaseAllocatorTest, ShutDownWithActiveHandles) {
220227
this->testShutDownWithActiveHandles();

cachelib/allocator/tests/BaseAllocatorTest.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
11401140
this->testLruLength(alloc, poolId, sizes, keyLen, evictedKeys);
11411141
}
11421142

1143-
void testReaperShutDown() {
1143+
void testReaperShutDown(typename AllocatorT::Config::MemoryTierConfigs cfgs = {}) {
11441144
const size_t nSlabs = 20;
11451145
const size_t size = nSlabs * Slab::kSize;
11461146

@@ -1150,6 +1150,8 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
11501150
config.setAccessConfig({8, 8});
11511151
config.enableCachePersistence(this->cacheDir_);
11521152
config.enableItemReaperInBackground(std::chrono::seconds(1), {});
1153+
if (cfgs.size())
1154+
config.configureMemoryTiers(cfgs);
11531155
std::vector<typename AllocatorT::Key> keys;
11541156
{
11551157
AllocatorT alloc(AllocatorT::SharedMemNew, config);

cachelib/shm/ShmCommon.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ struct ShmSegmentOpts {
5757
PageSizeT pageSize{PageSizeT::NORMAL};
5858
bool readOnly{false};
5959
size_t alignment{1}; // alignment for mapping.
60-
ShmTypeOpts typeOpts{}; // opts specific to segment type
60+
// opts specific to segment type
61+
ShmTypeOpts typeOpts{PosixSysVSegmentOpts(false)};
6162

6263
explicit ShmSegmentOpts(PageSizeT p) : pageSize(p) {}
6364
explicit ShmSegmentOpts(PageSizeT p, bool ro) : pageSize(p), readOnly(ro) {}

0 commit comments

Comments
 (0)