Skip to content

Commit 3b33452

Browse files
igchorfacebook-github-bot
authored andcommitted
Extend cachbench with value validation (#131)
Summary: The main purpose of this patch is to better simulate workloads in cachebench. Setting validateValue to true allows to see performance impact of using different mediums for memory cache. Pull Request resolved: #131 Reviewed By: haowu14 Differential Revision: D36361206 Pulled By: therealgymmy fbshipit-source-id: c5aae50529b0867d3f0fc1f75af54a976fa59c99
1 parent f113393 commit 3b33452

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ uint64_t Cache<Allocator>::fetchNandWrites() const {
5050
template <typename Allocator>
5151
Cache<Allocator>::Cache(const CacheConfig& config,
5252
ChainedItemMovingSync movingSync,
53-
std::string cacheDir)
53+
std::string cacheDir,
54+
bool touchValue)
5455
: config_(config),
56+
touchValue_(touchValue),
5557
nandBytesBegin_{fetchNandWrites()},
5658
itemRecords_(config_.enableItemDestructorCheck) {
5759
constexpr size_t MB = 1024ULL * 1024ULL;
@@ -396,6 +398,18 @@ typename Cache<Allocator>::WriteHandle Cache<Allocator>::insertOrReplace(
396398
return rv;
397399
}
398400

401+
template <typename Allocator>
402+
void Cache<Allocator>::touchValue(const ReadHandle& it) const {
403+
XDCHECK(touchValueEnabled());
404+
405+
auto ptr = reinterpret_cast<const uint8_t*>(getMemory(it));
406+
407+
/* The accumulate call is intended to access all bytes of the value
408+
* and nothing more. */
409+
auto sum = std::accumulate(ptr, ptr + getSize(it), 0ULL);
410+
folly::doNotOptimizeAway(sum);
411+
}
412+
399413
template <typename Allocator>
400414
typename Cache<Allocator>::ReadHandle Cache<Allocator>::find(Key key) {
401415
auto findFn = [&]() {
@@ -406,6 +420,11 @@ typename Cache<Allocator>::ReadHandle Cache<Allocator>::find(Key key) {
406420
// find from cache and wait for the result to be ready.
407421
auto it = cache_->find(key);
408422
it.wait();
423+
424+
if (touchValueEnabled()) {
425+
touchValue(it);
426+
}
427+
409428
return it;
410429
};
411430

@@ -431,6 +450,11 @@ typename Cache<Allocator>::WriteHandle Cache<Allocator>::findToWrite(Key key) {
431450
// find from cache and wait for the result to be ready.
432451
auto it = cache_->findToWrite(key);
433452
it.wait();
453+
454+
if (touchValueEnabled()) {
455+
touchValue(it);
456+
}
457+
434458
return it;
435459
};
436460

cachelib/cachebench/cache/Cache.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ class Cache {
6565
// cache.
6666
// @param cacheDir optional directory for the cache to enable
6767
// persistence across restarts.
68+
// @param touchValue read entire value on find
6869
explicit Cache(const CacheConfig& config,
6970
ChainedItemMovingSync movingSync = {},
70-
std::string cacheDir = "");
71+
std::string cacheDir = "",
72+
bool touchValue = false);
7173

7274
~Cache();
7375

@@ -179,6 +181,9 @@ class Cache {
179181
return getSize(item.get());
180182
}
181183

184+
// read entire value on find.
185+
void touchValue(const ReadHandle& it) const;
186+
182187
// returns the size of the item, taking into account ItemRecords could be
183188
// enabled.
184189
uint32_t getSize(const Item* item) const noexcept;
@@ -241,6 +246,9 @@ class Cache {
241246
// returns true if the consistency checking is enabled.
242247
bool consistencyCheckEnabled() const { return valueTracker_ != nullptr; }
243248

249+
// returns true if touching value is enabled.
250+
bool touchValueEnabled() const { return touchValue_; }
251+
244252
// return true if the key was previously detected to be inconsistent. This
245253
// is useful only when consistency checking is enabled by calling
246254
// enableConsistencyCheck()
@@ -363,6 +371,9 @@ class Cache {
363371
// tracker for consistency monitoring.
364372
std::unique_ptr<ValueTracker> valueTracker_;
365373

374+
// read entire value on find.
375+
bool touchValue_{false};
376+
366377
// reading of the nand bytes written for the benchmark if enabled.
367378
const uint64_t nandBytesBegin_{0};
368379

cachelib/cachebench/runner/CacheStressor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class CacheStressor : public Stressor {
9595
cacheConfig.ticker = ticker_;
9696
}
9797

98-
cache_ = std::make_unique<CacheT>(cacheConfig, movingSync);
98+
cache_ = std::make_unique<CacheT>(cacheConfig, movingSync, "",
99+
config_.touchValue);
99100
if (config_.opPoolDistribution.size() > cache_->numPools()) {
100101
throw std::invalid_argument(folly::sformat(
101102
"more pools specified in the test than in the cache. "

cachelib/cachebench/util/Config.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ StressorConfig::StressorConfig(const folly::dynamic& configJson) {
3434
JSONSetVal(configJson, samplingIntervalMs);
3535

3636
JSONSetVal(configJson, checkConsistency);
37+
JSONSetVal(configJson, touchValue);
3738

3839
JSONSetVal(configJson, numOps);
3940
JSONSetVal(configJson, numThreads);

cachelib/cachebench/util/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ struct StressorConfig : public JSONConfig {
194194
// output stats after warmup.
195195
bool checkNvmCacheWarmUp{false};
196196

197+
// If enabled, each value will be read on find. This is useful for measuring
198+
// performance of value access.
199+
bool touchValue{false};
200+
197201
uint64_t numOps{0}; // operation per thread
198202
uint64_t numThreads{0}; // number of threads that will run
199203
uint64_t numKeys{0}; // number of keys that will be used

0 commit comments

Comments
 (0)