Skip to content

Commit c93b440

Browse files
victoria-mcgrathvinser52
authored andcommitted
Enabled memory tier config API for cachebench.
1 parent 3af7643 commit c93b440

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ Cache<Allocator>::Cache(const CacheConfig& config,
9494

9595
allocatorConfig_.setCacheSize(config_.cacheSizeMB * (MB));
9696

97+
if (!cacheDir.empty()) {
98+
allocatorConfig_.cacheDir = cacheDir;
99+
} else if (!config_.persistedCacheDir.empty()) {
100+
allocatorConfig_.enableCachePersistence(config_.persistedCacheDir);
101+
}
102+
103+
if (config_.usePosixShm) {
104+
allocatorConfig_.usePosixForShm();
105+
}
106+
107+
if (config_.memoryTierConfigs.size()) {
108+
allocatorConfig_.configureMemoryTiers(config_.memoryTierConfigs);
109+
}
110+
97111
auto cleanupGuard = folly::makeGuard([&] {
98112
if (!nvmCacheFilePath_.empty()) {
99113
util::removePath(nvmCacheFilePath_);
@@ -244,8 +258,7 @@ Cache<Allocator>::Cache(const CacheConfig& config,
244258

245259
allocatorConfig_.cacheName = "cachebench";
246260

247-
if (!cacheDir.empty()) {
248-
allocatorConfig_.cacheDir = cacheDir;
261+
if (!allocatorConfig_.cacheDir.empty()) {
249262
cache_ =
250263
std::make_unique<Allocator>(Allocator::SharedMemNew, allocatorConfig_);
251264
} else {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @nolint instantiates a small cache and runs a quick run of basic operations.
2+
{
3+
"cache_config" : {
4+
"cacheSizeMB" : 512,
5+
"usePosixShm" : true,
6+
"persistedCacheDir" : "/tmp/mem-tiers",
7+
"memoryTiers" : [
8+
{
9+
"ratio": 1,
10+
"file": "/tmp/mem-tiers/memory-mapped-tier"
11+
}
12+
],
13+
"poolRebalanceIntervalSec" : 1,
14+
"moveOnSlabRelease" : false,
15+
16+
"numPools" : 2,
17+
"poolSizes" : [0.3, 0.7]
18+
},
19+
"test_config" : {
20+
"numOps" : 100000,
21+
"numThreads" : 32,
22+
"numKeys" : 1000000,
23+
24+
"keySizeRange" : [1, 8, 64],
25+
"keySizeRangeProbability" : [0.3, 0.7],
26+
27+
"valSizeRange" : [1, 32, 10240, 409200],
28+
"valSizeRangeProbability" : [0.1, 0.2, 0.7],
29+
30+
"getRatio" : 0.15,
31+
"setRatio" : 0.8,
32+
"delRatio" : 0.05,
33+
"keyPoolDistribution": [0.4, 0.6],
34+
"opPoolDistribution" : [0.5, 0.5]
35+
}
36+
}

cachelib/cachebench/util/CacheConfig.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,18 @@ CacheConfig::CacheConfig(const folly::dynamic& configJson) {
9393
JSONSetVal(configJson, enableItemDestructorCheck);
9494
JSONSetVal(configJson, enableItemDestructor);
9595

96+
JSONSetVal(configJson, persistedCacheDir);
97+
JSONSetVal(configJson, usePosixShm);
98+
if (configJson.count("memoryTiers")) {
99+
for (auto& it : configJson["memoryTiers"]) {
100+
memoryTierConfigs.push_back(MemoryTierConfig(it).getMemoryTierCacheConfig());
101+
}
102+
}
103+
96104
// if you added new fields to the configuration, update the JSONSetVal
97105
// to make them available for the json configs and increment the size
98106
// below
99-
checkCorrectSize<CacheConfig, 688>();
107+
checkCorrectSize<CacheConfig, 752>();
100108

101109
if (numPools != poolSizes.size()) {
102110
throw std::invalid_argument(folly::sformat(
@@ -125,6 +133,16 @@ std::shared_ptr<RebalanceStrategy> CacheConfig::getRebalanceStrategy() const {
125133
RandomStrategy::Config{static_cast<unsigned int>(rebalanceMinSlabs)});
126134
}
127135
}
136+
137+
138+
MemoryTierConfig::MemoryTierConfig(const folly::dynamic& configJson) {
139+
JSONSetVal(configJson, file);
140+
JSONSetVal(configJson, ratio);
141+
JSONSetVal(configJson, size);
142+
143+
checkCorrectSize<MemoryTierConfig, 48>();
144+
}
145+
128146
} // namespace cachebench
129147
} // namespace cachelib
130148
} // namespace facebook

cachelib/cachebench/util/CacheConfig.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ class CacheMonitorFactory {
4141
virtual std::unique_ptr<CacheMonitor> create(Lru2QAllocator& cache) = 0;
4242
};
4343

44+
struct MemoryTierConfig : public JSONConfig {
45+
MemoryTierConfig() {}
46+
47+
explicit MemoryTierConfig(const folly::dynamic& configJson);
48+
MemoryTierCacheConfig getMemoryTierCacheConfig() {
49+
if (file.empty()) {
50+
throw std::invalid_argument("Please specify valid path to memory mapped file.");
51+
}
52+
MemoryTierCacheConfig config = MemoryTierCacheConfig::fromFile(file).setSize(size).setRatio(ratio);
53+
return config;
54+
}
55+
56+
std::string file{""};
57+
size_t ratio{0};
58+
size_t size{0};
59+
};
60+
4461
struct CacheConfig : public JSONConfig {
4562
// by defaullt, lru allocator. can be set to LRU-2Q.
4663
std::string allocator{"LRU"};
@@ -194,6 +211,13 @@ struct CacheConfig : public JSONConfig {
194211
// Not used when its value is 0. In seconds.
195212
uint32_t memoryOnlyTTL{0};
196213

214+
// Directory for the cache to enable persistence across restarts.
215+
std::string persistedCacheDir{""};
216+
217+
bool usePosixShm{false};
218+
219+
std::vector<MemoryTierCacheConfig> memoryTierConfigs{};
220+
197221
// If enabled, we will use nvm admission policy tuned for ML use cases
198222
std::string mlNvmAdmissionPolicy{""};
199223

0 commit comments

Comments
 (0)