Skip to content

Commit

Permalink
add read-only api to query hotness of hash
Browse files Browse the repository at this point in the history
Reviewed By: stuclar, therealgymmy

Differential Revision: D38539036

fbshipit-source-id: 1bb42e3604c41f629c7c2adb5f7e7ffe208546db
  • Loading branch information
Udip Pant authored and facebook-github-bot committed Dec 19, 2022
1 parent 913f788 commit 953f3c2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
21 changes: 21 additions & 0 deletions cachelib/common/hothash/HotHashDetector.cpp
Expand Up @@ -81,6 +81,27 @@ uint8_t HotHashDetector::bumpHash(uint64_t hash) {
return result;
}

bool HotHashDetector::isHotHash(uint64_t hash) const {
auto idx = l1HashFunction(hash);
auto l1count = l1Vector_[idx];
// Checking against threshold/2 since hot index should pass after one decay
if (LIKELY(l1count < (l1Threshold_ / 2))) {
return false;
}
auto idx2 = l2HashFunction(hash);
uint32_t l2count = l2Vector_[idx2].count;
if (l2count == 0) {
return false;
}
for (unsigned i = 0; i < kScanLen; ++i) {
auto& cell = l2Vector_[(idx2 + i) & bucketsMask_];
if (cell.hash == hash) {
return true;
}
}
return false;
}

void HotHashDetector::doMaintenance() {
bumpsSinceMaintenance_ = 0;

Expand Down
8 changes: 8 additions & 0 deletions cachelib/common/hothash/HotHashDetector.h
Expand Up @@ -106,6 +106,14 @@ class HotHashDetector {
// hotter, but any non-zero result here means it's very hot.
uint8_t bumpHash(uint64_t hash);

// Read-only method to check the hotness of a key-hash.
//
// @param hash hash value of an item. Zero is a valid value for hash, but
// may cause false positives.
//
// @return True if the hash is hot.
bool isHotHash(uint64_t hash) const;

// Manually trigger the data structure maintenance procedure.
void doMaintenance();

Expand Down
6 changes: 5 additions & 1 deletion cachelib/common/hothash/HotHashDetectorTest.cpp
Expand Up @@ -58,7 +58,11 @@ static void testHotHashes(uint32_t seed,
}
hotCount = 0;
for (size_t i = 0; i < numHashes; ++i) {
hotCount += detector.bumpHash(hashes[i]) ? 1 : 0;
bool isHot = detector.bumpHash(hashes[i]) > 0;
hotCount += isHot ? 1 : 0;
if (isHot) {
EXPECT_TRUE(detector.isHotHash(hashes[i]));
}
}
++iteration;
}
Expand Down

0 comments on commit 953f3c2

Please sign in to comment.