Skip to content

Commit 6a832fb

Browse files
Stuart Clarkfacebook-github-bot
Stuart Clark
authored andcommitted
Make kShards configurable in CacheAllocator
Summary: During T2 testing, to avoid lock contention in CacheAllocator, we had to increase this from 8K to 64-128K. Make it configurable and default to 8K Reviewed By: therealgymmy Differential Revision: D69193533 fbshipit-source-id: bfc0a292a9d00cf50bfbf7f5f8269cf08cd47f51
1 parent fc40516 commit 6a832fb

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

cachelib/allocator/CacheAllocator.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -2185,8 +2185,8 @@ class CacheAllocator : public CacheBase {
21852185
std::unique_ptr<MoveCtx>,
21862186
folly::HeterogeneousAccessHash<folly::StringPiece>>;
21872187

2188-
static size_t getShardForKey(folly::StringPiece key) {
2189-
return folly::Hash()(key) % kShards;
2188+
size_t getShardForKey(folly::StringPiece key) {
2189+
return folly::Hash()(key) % shards_;
21902190
}
21912191

21922192
MoveMap& getMoveMapForShard(size_t shard) {
@@ -2300,7 +2300,7 @@ class CacheAllocator : public CacheBase {
23002300
// poolResizer_, poolOptimizer_, memMonitor_, reaper_
23012301
mutable std::mutex workersMutex_;
23022302

2303-
static constexpr size_t kShards = 8192; // TODO: need to define right value
2303+
const size_t shards_;
23042304

23052305
struct MovesMapShard {
23062306
alignas(folly::hardware_destructive_interference_size) MoveMap movesMap_;
@@ -2455,8 +2455,9 @@ CacheAllocator<CacheTrait>::CacheAllocator(
24552455
config.chainedItemAccessConfig)),
24562456
chainedItemLocks_(config_.chainedItemsLockPower,
24572457
std::make_shared<MurmurHash2>()),
2458-
movesMap_(kShards),
2459-
moveLock_(kShards),
2458+
shards_{config_.numShards},
2459+
movesMap_(shards_),
2460+
moveLock_(shards_),
24602461
cacheCreationTime_{
24612462
type != InitMemType::kMemAttach
24622463
? util::getCurrentTimeSec()

cachelib/allocator/CacheAllocatorConfig.h

+12
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ class CacheAllocatorConfig {
345345
// CacheAllocator::startCacheWorkers()
346346
CacheAllocatorConfig& setDelayCacheWorkersStart();
347347

348+
// Set numShards to use for CacheAllocator locks
349+
CacheAllocatorConfig& setNumShards(size_t shards);
350+
348351
// skip promote children items in chained when parent fail to promote
349352
bool isSkipPromoteChildrenWhenParentFailed() const noexcept {
350353
return skipPromoteChildrenWhenParentFailed;
@@ -655,6 +658,8 @@ class CacheAllocatorConfig {
655658
// CacheAllocator::startCacheWorkers()
656659
bool delayCacheWorkersStart{false};
657660

661+
size_t numShards{8192};
662+
658663
friend CacheT;
659664

660665
private:
@@ -1123,6 +1128,12 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setDelayCacheWorkersStart() {
11231128
return *this;
11241129
}
11251130

1131+
template <typename T>
1132+
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setNumShards(size_t shards) {
1133+
numShards = shards;
1134+
return *this;
1135+
}
1136+
11261137
template <typename T>
11271138
const CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::validate() const {
11281139
// we can track tail hits only if MMType is MM2Q
@@ -1274,6 +1285,7 @@ std::map<std::string, std::string> CacheAllocatorConfig<T>::serialize() const {
12741285
configMap["nvmAdmissionMinTTL"] = std::to_string(nvmAdmissionMinTTL);
12751286
configMap["delayCacheWorkersStart"] =
12761287
delayCacheWorkersStart ? "true" : "false";
1288+
configMap["numShards"] = std::to_string(numShards);
12771289
mergeWithPrefix(configMap, throttleConfig.serialize(), "throttleConfig");
12781290
mergeWithPrefix(configMap,
12791291
chainedItemAccessConfig.serialize(),

0 commit comments

Comments
 (0)