Skip to content

Commit 682cc63

Browse files
committed
Enabled memory tier config API in cachebench
1 parent 8564a25 commit 682cc63

File tree

4 files changed

+137
-3
lines changed

4 files changed

+137
-3
lines changed

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ Cache<Allocator>::Cache(const CacheConfig& config,
8080

8181
allocatorConfig_.setCacheSize(config_.cacheSizeMB * (MB));
8282

83+
if (!cacheDir.empty()) {
84+
allocatorConfig_.cacheDir = cacheDir;
85+
}
86+
87+
if (config_.usePosixShm) {
88+
allocatorConfig_.usePosixForShm();
89+
}
90+
91+
if (!config_.memoryTierConfigs.empty()) {
92+
allocatorConfig_.configureMemoryTiers(config_.memoryTierConfigs);
93+
}
94+
8395
auto cleanupGuard = folly::makeGuard([&] {
8496
if (!nvmCacheFilePath_.empty()) {
8597
util::removePath(nvmCacheFilePath_);
@@ -222,8 +234,7 @@ Cache<Allocator>::Cache(const CacheConfig& config,
222234
allocatorConfig_.cacheName = "cachebench";
223235

224236
bool isRecovered = false;
225-
if (!cacheDir.empty()) {
226-
allocatorConfig_.cacheDir = cacheDir;
237+
if (!allocatorConfig_.cacheDir.empty()) {
227238
try {
228239
cache_ = std::make_unique<Allocator>(Allocator::SharedMemAttach,
229240
allocatorConfig_);
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" : false,
6+
"cacheDir" : "/tmp/mem-tiers",
7+
"memoryTiers" : [
8+
{
9+
"ratio": 1,
10+
"memBindNodes": 0
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: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ CacheConfig::CacheConfig(const folly::dynamic& configJson) {
8383
JSONSetVal(configJson, deviceMaxWriteSize);
8484

8585
JSONSetVal(configJson, memoryOnlyTTL);
86+
87+
JSONSetVal(configJson, usePosixShm);
88+
if (configJson.count("memoryTiers")) {
89+
for (auto& it : configJson["memoryTiers"]) {
90+
memoryTierConfigs.push_back(MemoryTierConfig(it).getMemoryTierCacheConfig());
91+
}
92+
}
8693

8794
JSONSetVal(configJson, useTraceTimeStamp);
8895
JSONSetVal(configJson, printNvmCounters);
@@ -95,7 +102,7 @@ CacheConfig::CacheConfig(const folly::dynamic& configJson) {
95102
// if you added new fields to the configuration, update the JSONSetVal
96103
// to make them available for the json configs and increment the size
97104
// below
98-
checkCorrectSize<CacheConfig, 664>();
105+
checkCorrectSize<CacheConfig, 696>();
99106

100107
if (numPools != poolSizes.size()) {
101108
throw std::invalid_argument(folly::sformat(
@@ -124,6 +131,55 @@ std::shared_ptr<RebalanceStrategy> CacheConfig::getRebalanceStrategy() const {
124131
RandomStrategy::Config{static_cast<unsigned int>(rebalanceMinSlabs)});
125132
}
126133
}
134+
135+
MemoryTierConfig::MemoryTierConfig(const folly::dynamic& configJson) {
136+
JSONSetVal(configJson, ratio);
137+
JSONSetVal(configJson, memBindNodes);
138+
139+
checkCorrectSize<MemoryTierConfig, 40>();
140+
}
141+
142+
std::vector<size_t> MemoryTierConfig::parseNumaNodes() {
143+
std::vector<size_t> numaNodes;
144+
145+
std::vector<folly::StringPiece> tokens;
146+
folly::split(",", memBindNodes, tokens, true /*ignore empty*/);
147+
for (const auto &token : tokens) {
148+
if (token.startsWith("!")) {
149+
throw std::invalid_argument(folly::sformat(
150+
"invalid NUMA nodes binding in memory tier config: {} "
151+
"inverse !N or !N-N is not supported "
152+
"nodes may be specified as N,N,N or N-N or N,N-N or N-N,N-N and so forth.",
153+
token));
154+
}
155+
else if (token.startsWith("+")) {
156+
throw std::invalid_argument(folly::sformat(
157+
"invalid NUMA nodes binding in memory tier config: {} "
158+
"relative nodes are not supported. "
159+
"nodes may be specified as N,N,N or N-N or N,N-N or N-N,N-N and so forth.",
160+
token));
161+
}
162+
else if (token.contains("-")) {
163+
size_t begin, end;
164+
if(folly::split("-", token, begin, end) && begin < end) {
165+
while(begin <=end) {
166+
numaNodes.push_back(begin++);
167+
}
168+
} else {
169+
throw std::invalid_argument(folly::sformat(
170+
"invalid NUMA nodes binding in memory tier config: {} "
171+
"Invalid range format. "
172+
"nodes may be specified as N,N,N or N-N or N,N-N or N-N,N-N and so forth.",
173+
token));
174+
}
175+
}
176+
else {
177+
numaNodes.push_back(folly::to<size_t>(token));
178+
}
179+
}
180+
181+
return numaNodes;
182+
}
127183
} // namespace cachebench
128184
} // namespace cachelib
129185
} // namespace facebook

cachelib/cachebench/util/CacheConfig.h

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

44+
// Parse memory tiers configuration from JSON config
45+
struct MemoryTierConfig : public JSONConfig {
46+
MemoryTierConfig() {}
47+
48+
explicit MemoryTierConfig(const folly::dynamic& configJson);
49+
MemoryTierCacheConfig getMemoryTierCacheConfig() {
50+
MemoryTierCacheConfig config = memoryTierCacheConfigFromSource();
51+
config.setRatio(ratio);
52+
config.setMemBind(parseNumaNodes());
53+
return config;
54+
}
55+
56+
// Specifies ratio of this memory tier to other tiers
57+
size_t ratio{0};
58+
// Allocate memory only from specified NUMA nodes
59+
std::string memBindNodes{""};
60+
61+
private:
62+
MemoryTierCacheConfig memoryTierCacheConfigFromSource() {
63+
return MemoryTierCacheConfig::fromShm();
64+
}
65+
66+
std::vector<size_t> parseNumaNodes();
67+
};
68+
4469
struct CacheConfig : public JSONConfig {
4570
// by defaullt, lru allocator. can be set to LRU-2Q.
4671
std::string allocator{"LRU"};
@@ -194,6 +219,12 @@ struct CacheConfig : public JSONConfig {
194219
// Not used when its value is 0. In seconds.
195220
uint32_t memoryOnlyTTL{0};
196221

222+
// Use Posix Shm instead of SysVShm
223+
bool usePosixShm{false};
224+
225+
// Memory tiers configs
226+
std::vector<MemoryTierCacheConfig> memoryTierConfigs{};
227+
197228
// If enabled, we will use the timestamps from the trace file in the ticker
198229
// so that the cachebench will observe time based on timestamps from the trace
199230
// instead of the system time.

0 commit comments

Comments
 (0)