Skip to content

Commit b3942e0

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

9 files changed

+123
-15
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: 30 additions & 12 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,12 +50,16 @@ 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("Using custom memory tier is only supported for Shared Memory.");
5256
initCommon(false);
5357
}
5458

5559
template <typename CacheTrait>
5660
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
57-
: isOnShm_{true},
61+
: memoryTierConfigs(config.getMemoryTierConfigs()),
62+
isOnShm_{true},
5863
config_(config.validate()),
5964
shmManager_(
6065
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -97,7 +102,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
97102

98103
template <typename CacheTrait>
99104
CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
100-
: isOnShm_{true},
105+
: memoryTierConfigs(config.getMemoryTierConfigs()),
106+
isOnShm_{true},
101107
config_(config.validate()),
102108
shmManager_(
103109
std::make_unique<ShmManager>(config_.cacheDir, config_.usePosixShm)),
@@ -150,31 +156,43 @@ CacheAllocator<CacheTrait>::~CacheAllocator() {
150156
}
151157

152158
template <typename CacheTrait>
153-
std::unique_ptr<MemoryAllocator>
154-
CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
159+
ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts() {
160+
if (memoryTierConfigs.size() > 1)
161+
throw std::invalid_argument("CacheLib only supports a single memory tier");
162+
155163
ShmSegmentOpts opts;
156164
opts.alignment = sizeof(Slab);
157-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
165+
166+
if (memoryTierConfigs.size()) {
167+
opts.typeOpts = FileShmSegmentOpts(memoryTierConfigs[0].path);
168+
} else {
169+
// Fallback to Posix/SysV segment
170+
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
171+
}
172+
173+
return opts;
174+
}
175+
176+
template <typename CacheTrait>
177+
std::unique_ptr<MemoryAllocator>
178+
CacheAllocator<CacheTrait>::createNewMemoryAllocator() {
158179
return std::make_unique<MemoryAllocator>(
159180
getAllocatorConfig(config_),
160181
shmManager_
161182
->createShm(detail::kShmCacheName, config_.size,
162-
config_.slabMemoryBaseAddr, opts)
183+
config_.slabMemoryBaseAddr, createShmCacheOpts())
163184
.addr,
164185
config_.size);
165186
}
166187

167188
template <typename CacheTrait>
168189
std::unique_ptr<MemoryAllocator>
169190
CacheAllocator<CacheTrait>::restoreMemoryAllocator() {
170-
ShmSegmentOpts opts;
171-
opts.alignment = sizeof(Slab);
172-
opts.typeOpts = PosixSysVSegmentOpts(config_.usePosixShm);
173191
return std::make_unique<MemoryAllocator>(
174192
deserializer_->deserialize<MemoryAllocator::SerializationType>(),
175193
shmManager_
176-
->attachShm(detail::kShmCacheName, config_.slabMemoryBaseAddr, opts)
177-
.addr,
194+
->attachShm(detail::kShmCacheName, config_.slabMemoryBaseAddr,
195+
createShmCacheOpts()).addr,
178196
config_.size,
179197
config_.disableFullCoredump);
180198
}

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)