Skip to content

Commit dcce19d

Browse files
byrnedjmeta-codesync[bot]
authored andcommitted
Add BigHash checksum offload
Summary: - Add opt-in DSA CRC32C generation and verification for `BigHash` buckets. - Offload only buckets of at least `checksumOffloadMinSize` (default 16KB, matching `BlockCache`); smaller buckets use software CRC32C. - Overlap write checksums with bloom-filter updates before writing to the device. - Preserve CRC32C compatibility across CPU fallback and offload toggles for warm recovery. Source: D113115379 / #470. Reviewed By: rlyerly Differential Revision: D129547056 Pulled By: pbhandar2 fbshipit-source-id: f946777d6287cf9dc52500503ae8e1a9190ee06c
1 parent e573acb commit dcce19d

5 files changed

Lines changed: 161 additions & 30 deletions

File tree

‎cachelib/navy/bighash/BigHash.cpp‎

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ namespace facebook::cachelib::navy {
3232
constexpr uint32_t kBigHashValidBucketCheckerBucketsPerBit = 100;
3333

3434
BigHash::Config& BigHash::Config::validate() {
35+
if (checksumOffload && checksumType != ChecksumType::Crc32c) {
36+
throw std::invalid_argument("BigHash checksum offload requires CRC32C");
37+
}
38+
3539
if (cacheSize < bucketSize) {
3640
throw std::invalid_argument(
3741
fmt::format("cache size: {} cannot be smaller than bucket size: {}",
@@ -87,6 +91,7 @@ BigHash::BigHash(Config&& config, ValidConfigTag)
8791
}
8892
}},
8993
bucketSize_{config.bucketSize},
94+
checksumOffload_{config.usesChecksumOffload()},
9095
cacheBaseOffset_{config.cacheBaseOffset},
9196
numBuckets_{config.numBuckets()},
9297
bloomFilter_{std::move(config.bloomFilter)},
@@ -96,11 +101,12 @@ BigHash::BigHash(Config&& config, ValidConfigTag)
96101
bfLock_(numMutexes_) {
97102
XLOGF(INFO,
98103
"BigHash created: buckets: {}, bucket size: {}, base offset: {}, num "
99-
"mutexes: {}",
104+
"mutexes: {}, checksum offload: {}",
100105
numBuckets_,
101106
bucketSize_,
102107
cacheBaseOffset_,
103-
numMutexes_);
108+
numMutexes_,
109+
checksumOffload_);
104110
reset();
105111
}
106112

@@ -367,16 +373,17 @@ Status BigHash::insert(HashedKey hk,
367373
bucket->insert(hk, value, checkExpired_, cb);
368374
newRemainingBytes = bucket->remainingBytes();
369375

370-
// rebuild / fix the bloom filter before we move the buffer to do the
371-
// actual write
372-
if (removed + evicted == 0) {
373-
// In case nothing was removed or evicted, we can just add
374-
bfSet(bid, hk.keyHash());
375-
} else {
376-
bfRebuild(bid, bucket);
377-
}
378-
379-
const auto res = writeBucket(bid, std::move(buffer));
376+
// The bloom filter update runs as overlap work: concurrently with the
377+
// bucket checksum when offloaded to DSA, right before it otherwise. In
378+
// both cases it completes before the bucket is written to the device.
379+
const auto res = writeBucket(bid, std::move(buffer), [&] {
380+
if (removed + evicted == 0) {
381+
// In case nothing was removed or evicted, we can just add
382+
bfSet(bid, hk.keyHash());
383+
} else {
384+
bfRebuild(bid, bucket);
385+
}
386+
});
380387
if (!res) {
381388
bfClear(bid);
382389
ioErrorCount_.inc();
@@ -513,11 +520,11 @@ Status BigHash::remove(HashedKey hk) {
513520
}
514521
newRemainingBytes = bucket->remainingBytes();
515522

516-
// We compute bloom filter before writing the bucket because when encryption
517-
// is enabled, we will "move" the bucket content into writeBucket().
518-
bfRebuild(bid, bucket);
519-
520-
const auto res = writeBucket(bid, std::move(buffer));
523+
// The bloom filter rebuild runs as overlap work: concurrently with the
524+
// bucket checksum when offloaded to DSA, right before it otherwise. In
525+
// both cases it completes before the bucket is written to the device.
526+
const auto res =
527+
writeBucket(bid, std::move(buffer), [&] { bfRebuild(bid, bucket); });
521528
if (!res) {
522529
bfClear(bid);
523530
ioErrorCount_.inc();
@@ -608,8 +615,10 @@ Buffer BigHash::readBucket(BucketId bid) {
608615
auto* bucket = reinterpret_cast<Bucket*>(buffer.data());
609616

610617
auto checksumCheck = [this](auto* b, auto bufferView) {
611-
const bool checksumSuccess =
612-
Bucket::computeChecksum(bufferView, checksumType_) == b->getChecksum();
618+
const uint32_t computed =
619+
checksumOffload_ ? Bucket::computeChecksumOffloaded(bufferView, [] {})
620+
: Bucket::computeChecksum(bufferView, checksumType_);
621+
const bool checksumSuccess = computed == b->getChecksum();
613622
// TODO (T93631284) we only read a bucket if the bloom filter indicates that
614623
// the bucket could have the element. Hence, if check sum errors out and
615624
// bloom filter is enabled, we could record the checksum error. However,
@@ -633,9 +642,17 @@ Buffer BigHash::readBucket(BucketId bid) {
633642
return buffer;
634643
}
635644

636-
bool BigHash::writeBucket(BucketId bid, Buffer buffer) {
645+
bool BigHash::writeBucket(BucketId bid,
646+
Buffer buffer,
647+
folly::FunctionRef<void()> overlap) {
637648
auto* bucket = reinterpret_cast<Bucket*>(buffer.data());
638-
bucket->setChecksum(Bucket::computeChecksum(buffer.view(), checksumType_));
649+
if (checksumOffload_) {
650+
bucket->setChecksum(
651+
Bucket::computeChecksumOffloaded(buffer.view(), overlap));
652+
} else {
653+
overlap();
654+
bucket->setChecksum(Bucket::computeChecksum(buffer.view(), checksumType_));
655+
}
639656
const bool res =
640657
device_.write(getBucketOffset(bid), std::move(buffer), placementHandle_);
641658
if (!res) {

‎cachelib/navy/bighash/BigHash.h‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#pragma once
1818

19+
#include <folly/Function.h>
1920
#include <folly/Utility.h>
2021
#include <folly/fibers/TimedMutex.h>
2122

@@ -67,6 +68,16 @@ class BigHash final : public Engine, folly::NonCopyableNonMovable {
6768
uint32_t bucketSize{4 * 1024};
6869
ChecksumType checksumType{ChecksumType::Crc32};
6970

71+
// Offload bucket checksumming to Intel DSA via the DTO library when
72+
// available. Most beneficial with large buckets (16KB+), where the bloom
73+
// filter rebuild overlaps with the accelerator computing the checksum.
74+
// Requires checksumType == ChecksumType::Crc32c. Uses software
75+
// CRC32C when built without DTO support or DSA is unavailable.
76+
bool checksumOffload{false};
77+
// Minimum bucketSize in bytes for checksum offload. Smaller buckets use
78+
// software CRC32C.
79+
uint32_t checksumOffloadMinSize{16384};
80+
7081
// The range of device that BigHash will access is guaranteed to be
7182
// within [baseOffset, baseOffset + cacheSize)
7283
uint64_t cacheBaseOffset{};
@@ -83,6 +94,10 @@ class BigHash final : public Engine, folly::NonCopyableNonMovable {
8394

8495
uint64_t numBuckets() const { return cacheSize / bucketSize; }
8596

97+
bool usesChecksumOffload() const {
98+
return checksumOffload && bucketSize >= checksumOffloadMinSize;
99+
}
100+
86101
Config& validate();
87102
};
88103

@@ -188,7 +203,13 @@ class BigHash final : public Engine, folly::NonCopyableNonMovable {
188203
BigHash(Config&& config, ValidConfigTag);
189204

190205
Buffer readBucket(BucketId bid);
191-
bool writeBucket(BucketId bid, Buffer buffer);
206+
207+
// Checksums the bucket and writes it to the device. @overlap is CPU work
208+
// (e.g. bloom filter maintenance) run while the checksum is computed by
209+
// DSA when checksum offload is enabled, or immediately before the software
210+
// checksum otherwise. It may read the bucket but must not modify it.
211+
bool writeBucket(
212+
BucketId bid, Buffer buffer, folly::FunctionRef<void()> overlap = [] {});
192213

193214
// The corresponding r/w bucket lock must be held during the entire
194215
// duration of the read and write operations. For example, during write,
@@ -237,6 +258,9 @@ class BigHash final : public Engine, folly::NonCopyableNonMovable {
237258
const ExpiredCheck checkExpired_{};
238259
const DestructorCallback destructorCb_{};
239260
const uint64_t bucketSize_{};
261+
// Whether to offload bucket checksumming to DSA. See
262+
// Config::usesChecksumOffload.
263+
const bool checksumOffload_{};
240264
const uint64_t cacheBaseOffset_{};
241265
const uint64_t numBuckets_{};
242266
std::unique_ptr<BloomFilter> bloomFilter_;

‎cachelib/navy/bighash/Bucket.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <folly/Random.h>
2020

21+
#include "cachelib/navy/common/ChecksumOffload.h"
2122
#include "cachelib/navy/common/Hash.h"
2223

2324
namespace facebook::cachelib::navy {
@@ -53,6 +54,13 @@ uint32_t Bucket::computeChecksum(BufferView view, ChecksumType checksumType) {
5354
return navy::checksum(data, checksumType);
5455
}
5556

57+
uint32_t Bucket::computeChecksumOffloaded(BufferView view,
58+
folly::FunctionRef<void()> overlap) {
59+
constexpr auto kChecksumStart = sizeof(checksum_);
60+
auto data = view.slice(kChecksumStart, view.size() - kChecksumStart);
61+
return checksumWithOverlap(data, overlap);
62+
}
63+
5664
Bucket& Bucket::initNew(MutableBufferView view, uint64_t generationTime) {
5765
return *new (view.data())
5866
Bucket(generationTime, view.size() - sizeof(Bucket));

‎cachelib/navy/bighash/Bucket.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#pragma once
1818

19+
#include <folly/Function.h>
1920
#include <folly/Portability.h>
2021

2122
#include "cachelib/navy/bighash/BucketStorage.h"
@@ -71,6 +72,12 @@ class FOLLY_PACK_ATTR Bucket {
7172
static uint32_t computeChecksum(
7273
BufferView view, ChecksumType checksumType = ChecksumType::Crc32);
7374

75+
// Computes CRC32C by DSA (when built with DTO support), or software
76+
// CRC32C if offload is unavailable. @overlap CPU work runs while the
77+
// accelerator operates; it may read the bucket but must not modify it.
78+
static uint32_t computeChecksumOffloaded(BufferView view,
79+
folly::FunctionRef<void()> overlap);
80+
7481
// Initialize a brand new Bucket given a piece of memory in the case
7582
// that the existing bucket is invalid. (I.e. checksum or generation
7683
// and generation time for.

‎cachelib/navy/bighash/tests/BigHashTest.cpp‎

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,68 @@ TEST(BigHash, InsertAndRemove) {
8080
EXPECT_EQ(Status::NotFound, bh.remove(makeHK("key")));
8181
}
8282

83+
TEST(BigHash, RejectsChecksumOffloadWithCrc32) {
84+
BigHash::Config config;
85+
setLayout(config, 4096, 1);
86+
config.checksumOffload = true;
87+
auto device = createMemoryDevice(config.cacheSize, nullptr /* encryption */);
88+
config.device = device.get();
89+
90+
EXPECT_THROW(BigHash{std::move(config)}, std::invalid_argument);
91+
}
92+
93+
TEST(BigHash, ChecksumOffloadMinSize) {
94+
BigHash::Config config;
95+
config.checksumOffload = true;
96+
// Default 4KB buckets are below the default threshold.
97+
EXPECT_FALSE(config.usesChecksumOffload());
98+
99+
config.bucketSize = config.checksumOffloadMinSize;
100+
EXPECT_TRUE(config.usesChecksumOffload());
101+
102+
config.bucketSize = 128;
103+
config.checksumOffloadMinSize = 0;
104+
EXPECT_TRUE(config.usesChecksumOffload());
105+
106+
config.checksumOffload = false;
107+
EXPECT_FALSE(config.usesChecksumOffload());
108+
}
109+
110+
// Exercises the offloaded bucket-checksum path (DSA when built with DTO
111+
// support, software otherwise), including the bloom-filter-rebuild overlap
112+
// in insert/remove and offloaded verification in readBucket.
113+
TEST(BigHash, InsertRemoveChecksumOffload) {
114+
BigHash::Config config;
115+
setLayout(config, 128, 2);
116+
config.checksumType = ChecksumType::Crc32c;
117+
config.checksumOffload = true;
118+
config.checksumOffloadMinSize = 0;
119+
config.bloomFilter = std::make_unique<BloomFilter>(config.numBuckets(), 2, 4);
120+
auto device = std::make_unique<NiceMock<MockDevice>>(config.cacheSize, 128);
121+
config.device = device.get();
122+
123+
BigHash bh(std::move(config));
124+
125+
Buffer value;
126+
uint32_t lat = 0;
127+
EXPECT_FALSE(bh.couldExist(makeHK("key")));
128+
ASSERT_EQ(Status::Ok,
129+
bh.insert(makeHK("key"), makeView("12345"), 0 /* poolId */,
130+
0 /* expiryTime */));
131+
EXPECT_TRUE(bh.couldExist(makeHK("key")));
132+
ASSERT_EQ(Status::Ok, bh.lookup(makeHK("key"), value, lat));
133+
EXPECT_EQ(makeView("12345"), value.view());
134+
135+
ASSERT_EQ(Status::Ok, bh.insert(makeHK("key"), makeView("67890")));
136+
EXPECT_TRUE(bh.couldExist(makeHK("key")));
137+
ASSERT_EQ(Status::Ok, bh.lookup(makeHK("key"), value, lat));
138+
EXPECT_EQ(makeView("67890"), value.view());
139+
140+
ASSERT_EQ(Status::Ok, bh.remove(makeHK("key")));
141+
EXPECT_FALSE(bh.couldExist(makeHK("key")));
142+
EXPECT_EQ(Status::NotFound, bh.lookup(makeHK("key"), value, lat));
143+
}
144+
83145
// without bloom filters, could exist always returns true.
84146
TEST(BigHash, CouldExistWithoutBF) {
85147
BigHash::Config config;
@@ -512,25 +574,38 @@ TEST(BigHash, Recovery) {
512574

513575
TEST(BigHash, ChecksumFormatCompatibility) {
514576
constexpr uint32_t kBucketSize = 4096;
515-
for (auto writeType : {ChecksumType::Crc32, ChecksumType::Crc32c}) {
516-
for (auto recoverType : {ChecksumType::Crc32, ChecksumType::Crc32c}) {
577+
struct ChecksumMode {
578+
ChecksumType type;
579+
bool offload;
580+
};
581+
const ChecksumMode modes[]{{ChecksumType::Crc32, false},
582+
{ChecksumType::Crc32c, false},
583+
{ChecksumType::Crc32c, true}};
584+
for (const auto writeMode : modes) {
585+
for (const auto recoverMode : modes) {
586+
const auto writeType = writeMode.type;
587+
const auto recoverType = recoverMode.type;
517588
SCOPED_TRACE(testing::Message()
518589
<< "write=" << static_cast<int>(writeType)
519-
<< " recover=" << static_cast<int>(recoverType));
590+
<< " offload=" << writeMode.offload
591+
<< " recover=" << static_cast<int>(recoverType)
592+
<< " offload=" << recoverMode.offload);
520593
auto device = createMemoryDevice(kBucketSize, nullptr /* encryption */);
521-
auto makeConfig = [&](ChecksumType checksumType) {
594+
auto makeConfig = [&](ChecksumMode mode) {
522595
BigHash::Config config;
523596
setLayout(config, kBucketSize, 1);
524597
config.device = device.get();
525-
if (checksumType == ChecksumType::Crc32c) {
526-
config.checksumType = checksumType;
598+
if (mode.type == ChecksumType::Crc32c) {
599+
config.checksumType = mode.type;
527600
}
601+
config.checksumOffload = mode.offload;
602+
config.checksumOffloadMinSize = 0;
528603
return config;
529604
};
530605

531606
folly::IOBufQueue queue;
532607
{
533-
BigHash writer(makeConfig(writeType));
608+
BigHash writer(makeConfig(writeMode));
534609
ASSERT_EQ(Status::Ok, writer.insert(makeHK("key"), makeView("value")));
535610

536611
auto bucketData = device->makeIOBuffer(kBucketSize);
@@ -555,7 +630,7 @@ TEST(BigHash, ChecksumFormatCompatibility) {
555630
writer.persist(*rw);
556631
}
557632

558-
BigHash recovered(makeConfig(recoverType));
633+
BigHash recovered(makeConfig(recoverMode));
559634
auto rr = createMemoryRecordReader(queue);
560635
const bool sameFormat = writeType == recoverType;
561636
ASSERT_EQ(sameFormat, recovered.recover(*rr));

0 commit comments

Comments
 (0)