Skip to content

Commit e573acb

Browse files
byrnedjmeta-codesync[bot]
authored andcommitted
Add scalar BlockCache checksum offload
Summary: - Add opt-in DSA fused copy/CRC32C writes and checksum verification to `BlockCache`. - Support separate write/read size thresholds and a DSA copy cache hint. - Preserve CRC32C compatibility across CPU fallback and offload toggles for warm recovery. Source: D113115379 / #470. Reviewed By: rlyerly Differential Revision: D129540346 Pulled By: pbhandar2 fbshipit-source-id: be442a338c9a289a57165817826a91ee2862d05c
1 parent c13e1f1 commit e573acb

3 files changed

Lines changed: 336 additions & 78 deletions

File tree

‎cachelib/navy/block_cache/BlockCache.cpp‎

Lines changed: 122 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "cachelib/navy/block_cache/HitsReinsertionPolicy.h"
3333
#include "cachelib/navy/block_cache/PercentageReinsertionPolicy.h"
3434
#include "cachelib/navy/block_cache/SparseMapIndex.h"
35+
#include "cachelib/navy/common/ChecksumOffload.h"
3536
#include "cachelib/navy/common/Hash.h"
3637
#include "cachelib/navy/common/Types.h"
3738
#include "folly/Range.h"
@@ -108,6 +109,15 @@ BlockCache::Config& BlockCache::Config::validate() {
108109
}
109110
}
110111

112+
if (checksumOffload && !checksum) {
113+
throw std::invalid_argument(
114+
"checksum offload requires data checksum to be enabled");
115+
}
116+
if (checksumOffload && checksumType != ChecksumType::Crc32c) {
117+
throw std::invalid_argument(
118+
"checksum offload requires explicit CRC32C checksum selection");
119+
}
120+
111121
reinsertionConfig.validate();
112122
indexConfig.validate();
113123

@@ -226,6 +236,12 @@ BlockCache::BlockCache(Config&& config, ValidConfigTag)
226236
destructorCb_{std::move(config.destructorCb)},
227237
checksumData_{config.checksum},
228238
checksumType_{config.checksumType},
239+
checksumOffload_{config.checksumOffload},
240+
checksumOffloadMinSize_{config.checksumOffloadMinSize},
241+
checksumOffloadReadMinSize_{config.checksumOffloadReadMinSize
242+
? config.checksumOffloadReadMinSize
243+
: config.checksumOffloadMinSize},
244+
checksumOffloadCacheControl_{config.checksumOffloadCacheControl},
229245
device_{*config.device},
230246
allocAlignSize_{calcAllocAlignSize()},
231247
readBufferSize_{config.readBufferSize < kDefReadBufferSize
@@ -279,6 +295,13 @@ std::shared_ptr<BlockCacheReinsertionPolicy> BlockCache::makeReinsertionPolicy(
279295
return reinsertionConfig.getCustomPolicy(*index_);
280296
}
281297

298+
uint32_t BlockCache::valueChecksum(BufferView value) const {
299+
if (checksumOffload_ && value.size() >= checksumOffloadReadMinSize_) {
300+
return checksumWithOverlap(value, [] {});
301+
}
302+
return checksum(value, checksumType_);
303+
}
304+
282305
uint32_t BlockCache::serializedSize(uint32_t keySize,
283306
uint32_t valueSize) const {
284307
uint32_t size = sizeof(EntryDesc) + keySize + valueSize;
@@ -562,21 +585,24 @@ std::pair<Status, std::string> BlockCache::getRandomAlloc(Buffer& value) {
562585
}
563586

564587
BufferView valueView{desc.valueSize, entryEnd - entrySize};
565-
if (checksumData_ && desc.cs != checksum(valueView, checksumType_)) {
566-
XLOGF(ERR,
567-
"Item value checksum mismatch in getRandomAlloc(). Region {} is "
568-
"likely corrupted. Expected: {}, Actual: {}, Offset: {}, "
569-
"Physical-offset: {}, Value-size: {}, Payload (hex): {}",
570-
rid.index(),
571-
desc.cs,
572-
checksum(valueView, checksumType_),
573-
addrEnd.offset() - entrySize,
574-
regionManager_.physicalOffset(addrEnd) - entrySize,
575-
desc.valueSize,
576-
// call folly::unhexlify to convert it back to binary data
577-
folly::hexlify(
578-
folly::ByteRange(valueView.data(), valueView.dataEnd())));
579-
break;
588+
if (checksumData_) {
589+
const uint32_t valueCs = valueChecksum(valueView);
590+
if (desc.cs != valueCs) {
591+
XLOGF(ERR,
592+
"Item value checksum mismatch in getRandomAlloc(). Region {} is "
593+
"likely corrupted. Expected: {}, Actual: {}, Offset: {}, "
594+
"Physical-offset: {}, Value-size: {}, Payload (hex): {}",
595+
rid.index(),
596+
desc.cs,
597+
valueCs,
598+
addrEnd.offset() - entrySize,
599+
regionManager_.physicalOffset(addrEnd) - entrySize,
600+
desc.valueSize,
601+
// call folly::unhexlify to convert it back to binary data
602+
folly::hexlify(
603+
folly::ByteRange(valueView.data(), valueView.dataEnd())));
604+
break;
605+
}
580606
}
581607

582608
// confirm that the chosen NvmItem is still being mapped with the key
@@ -775,7 +801,7 @@ void BlockCache::onRegionCleanup(RegionId rid, BufferView buffer) {
775801
HashedKey hk =
776802
makeHK(entryEnd - sizeof(EntryDesc) - desc.keySize, desc.keySize);
777803
BufferView value{desc.valueSize, entryEnd - entrySize};
778-
if (checksumData_ && desc.cs != checksum(value, checksumType_)) {
804+
if (checksumData_ && desc.cs != valueChecksum(value)) {
779805
// We do not need to abort here since the EntryDesc checksum was good, so
780806
// we can safely proceed to read the next entry.
781807
cleanupValueChecksumErrorCount_.inc();
@@ -1027,22 +1053,22 @@ Status BlockCache::onReadCombinedEntryBlock(uint32_t address,
10271053
}
10281054

10291055
// Check the checksum for the value
1030-
if (checksumData_ &&
1031-
entryDesc.cs != checksum(BufferView(entryDesc.valueSize, buffer.data()),
1032-
checksumType_)) {
1033-
XLOGF(
1034-
ERR,
1035-
"Item value checksum mismatch in onReadCombinedEntryBlock(). "
1036-
"Region {} is likely corrupted. Expected: {}, Actual: {}, Offset: "
1037-
"{}, Physical-offset: {}, "
1038-
"Value-size: {} Payload (hex): {}",
1039-
addrEnd.rid().index(), entryDesc.cs,
1040-
checksum(BufferView(entryDesc.valueSize, buffer.data()), checksumType_),
1041-
addrEnd.offset() - size, regionManager_.physicalOffset(addrEnd) - size,
1042-
entryDesc.valueSize,
1043-
folly::hexlify(folly::ByteRange(buffer.data(),
1044-
buffer.data() + entryDesc.valueSize)));
1045-
return Status::ChecksumError;
1056+
if (checksumData_) {
1057+
const uint32_t cebValueCs =
1058+
valueChecksum(BufferView(entryDesc.valueSize, buffer.data()));
1059+
if (entryDesc.cs != cebValueCs) {
1060+
XLOGF(ERR,
1061+
"Item value checksum mismatch in onReadCombinedEntryBlock(). "
1062+
"Region {} is likely corrupted. Expected: {}, Actual: {}, Offset: "
1063+
"{}, Physical-offset: {}, "
1064+
"Value-size: {} Payload (hex): {}",
1065+
addrEnd.rid().index(), entryDesc.cs, cebValueCs,
1066+
addrEnd.offset() - size,
1067+
regionManager_.physicalOffset(addrEnd) - size, entryDesc.valueSize,
1068+
folly::hexlify(folly::ByteRange(
1069+
buffer.data(), buffer.data() + entryDesc.valueSize)));
1070+
return Status::ChecksumError;
1071+
}
10461072
}
10471073

10481074
// shrink and set it as CEB's buffer
@@ -1194,23 +1220,26 @@ AllocatorApiResult BlockCache::reinsertOrRemoveItem(
11941220
}
11951221

11961222
// Validate checksum as we want to reinsert the item
1197-
if (checksumData_ && entryDesc.cs != checksum(value, checksumType_)) {
1198-
// We do not need to abort here since the EntryDesc checksum was good, so
1199-
// we can safely proceed to read the next entry.
1200-
XLOGF(ERR,
1201-
"Item value checksum mismatch in reinsertOrRemoveItem(). "
1202-
"Item is likely corrupted. Item will not be reinserted. "
1203-
"We will continue evaluating remaining items in the region."
1204-
"Expected: {}, Actual: {}, Value-size: {}, Payload (hex): {}",
1205-
entryDesc.cs,
1206-
checksum(value, checksumType_),
1207-
entryDesc.valueSize,
1208-
folly::hexlify(folly::ByteRange(value.data(), value.dataEnd())));
1209-
reclaimValueChecksumErrorCount_.inc();
1210-
removeItem(false);
1211-
recordEvent(hk.key(), AllocatorApiEvent::NVM_REINSERT,
1212-
AllocatorApiResult::CORRUPTED, entrySize);
1213-
return AllocatorApiResult::CORRUPTED;
1223+
if (checksumData_) {
1224+
const uint32_t reinsertValueCs = valueChecksum(value);
1225+
if (entryDesc.cs != reinsertValueCs) {
1226+
// We do not need to abort here since the EntryDesc checksum was good, so
1227+
// we can safely proceed to read the next entry.
1228+
XLOGF(ERR,
1229+
"Item value checksum mismatch in reinsertOrRemoveItem(). "
1230+
"Item is likely corrupted. Item will not be reinserted. "
1231+
"We will continue evaluating remaining items in the region."
1232+
"Expected: {}, Actual: {}, Value-size: {}, Payload (hex): {}",
1233+
entryDesc.cs,
1234+
reinsertValueCs,
1235+
entryDesc.valueSize,
1236+
folly::hexlify(folly::ByteRange(value.data(), value.dataEnd())));
1237+
reclaimValueChecksumErrorCount_.inc();
1238+
removeItem(false);
1239+
recordEvent(hk.key(), AllocatorApiEvent::NVM_REINSERT,
1240+
AllocatorApiResult::CORRUPTED, entrySize);
1241+
return AllocatorApiResult::CORRUPTED;
1242+
}
12141243
}
12151244

12161245
// Priority of an re-inserted item is determined by its past accesses
@@ -1344,21 +1373,36 @@ void BlockCache::writeEntry(MutableBufferView buffer,
13441373

13451374
// Copy descriptor and the key to the end
13461375
uint8_t* dest = buffer.data() + buffer.size() - sizeof(EntryDesc);
1347-
auto desc = new (dest) EntryDesc(static_cast<uint32_t>(keySize),
1348-
static_cast<uint32_t>(value.size()), keyHash,
1349-
lastAccessTimeSecs, checksumType_);
1350-
1351-
if (checksumData_) {
1352-
desc->cs = precomputedChecksum.has_value() ? *precomputedChecksum
1353-
: checksum(value, checksumType_);
1354-
}
1376+
EntryDesc* desc = nullptr;
1377+
1378+
auto writeDescAndKey = [&] {
1379+
desc = new (dest) EntryDesc(static_cast<uint32_t>(keySize),
1380+
static_cast<uint32_t>(value.size()), keyHash,
1381+
lastAccessTimeSecs, checksumType_);
1382+
if (!combinedEntry) {
1383+
// Key info will be inside the value itself for combined entry block
1384+
std::memcpy(dest - keySize, hk->key().data(), keySize);
1385+
logicalWriteSize = keySize + value.size();
1386+
}
1387+
};
13551388

1356-
if (!combinedEntry) {
1357-
// Key info will be inside the value itself for combined entry block
1358-
std::memcpy(dest - keySize, hk->key().data(), keySize);
1359-
logicalWriteSize = keySize + value.size();
1389+
if (checksumData_ && !precomputedChecksum.has_value() && checksumOffload_ &&
1390+
value.size() >= checksumOffloadMinSize_) {
1391+
// Finish metadata writes before DSA submission to avoid false sharing.
1392+
// cs is excluded from csSelf, so it can be filled in after completion.
1393+
writeDescAndKey();
1394+
const uint32_t cs = copyAndChecksum(
1395+
buffer.data(), value, [] {}, checksumOffloadCacheControl_);
1396+
desc->cs = cs;
1397+
} else {
1398+
writeDescAndKey();
1399+
if (checksumData_) {
1400+
desc->cs = precomputedChecksum.has_value()
1401+
? *precomputedChecksum
1402+
: checksum(value, checksumType_);
1403+
}
1404+
std::memcpy(buffer.data(), value.data(), value.size());
13601405
}
1361-
std::memcpy(buffer.data(), value.data(), value.size());
13621406

13631407
logicalWrittenCount_.add(logicalWriteSize);
13641408
serializedWrittenCount_.add(buffer.size());
@@ -1444,20 +1488,22 @@ void BlockCache::readEntry(RegionDescriptor& regionDesc,
14441488
ld.valueSize_ = desc.valueSize;
14451489
ld.lastAccessTimeSecs_ = desc.lastAccessTimeSecs;
14461490
auto slice = ld.buffer_.view().slice(0, desc.valueSize);
1447-
if (checksumData_ && desc.cs != checksum(slice, checksumType_)) {
1448-
XLOG_N_PER_MS(ERR, 10, 10'000) << fmt::format(
1449-
"Item value checksum mismatch in readEntry() looking up key {} in "
1450-
"Region {}. Expected: {}, Actual: {}, Offset: {}, Physical-offset: {}, "
1451-
"Value-size: {} Payload (hex): {}",
1452-
key, addrEnd.rid().index(), desc.cs, checksum(slice, checksumType_),
1453-
addrEnd.offset() - size, regionManager_.physicalOffset(addrEnd) - size,
1454-
slice.size(),
1455-
folly::hexlify(
1456-
folly::ByteRange(slice.data(), slice.data() + slice.size())));
1457-
ld.buffer_.reset();
1458-
lookupValueChecksumErrorCount_.inc();
1459-
ld.status_ = Status::ChecksumError;
1460-
return;
1491+
if (checksumData_) {
1492+
const uint32_t sliceCs = valueChecksum(slice);
1493+
if (desc.cs != sliceCs) {
1494+
XLOG_N_PER_MS(ERR, 10, 10'000) << fmt::format(
1495+
"Item value checksum mismatch in readEntry() looking up key {} in "
1496+
"Region {}. Expected: {}, Actual: {}, Offset: {}, Physical-offset: "
1497+
"{}, Value-size: {} Payload (hex): {}",
1498+
key, addrEnd.rid().index(), desc.cs, sliceCs, addrEnd.offset() - size,
1499+
regionManager_.physicalOffset(addrEnd) - size, slice.size(),
1500+
folly::hexlify(
1501+
folly::ByteRange(slice.data(), slice.data() + slice.size())));
1502+
ld.buffer_.reset();
1503+
lookupValueChecksumErrorCount_.inc();
1504+
ld.status_ = Status::ChecksumError;
1505+
return;
1506+
}
14611507
}
14621508
ld.status_ = Status::Ok;
14631509
}

‎cachelib/navy/block_cache/BlockCache.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ class BlockCache final : public Engine {
6060
// CRC-32 preserves existing persisted caches. Selecting CRC-32C changes
6161
// the persisted format and requires a cold cache when changing modes.
6262
ChecksumType checksumType{ChecksumType::Crc32};
63+
// Offload value CRC32C and write copies to DSA, with CRC32C CPU fallback.
64+
// Requires checksum=true and checksumType=Crc32c.
65+
bool checksumOffload{false};
66+
// Minimum value size in bytes for fused copy/checksum offload.
67+
uint32_t checksumOffloadMinSize{16384};
68+
// Minimum value size in bytes for verification offload.
69+
// 0 uses checksumOffloadMinSize; UINT32_MAX disables verification offload.
70+
uint32_t checksumOffloadReadMinSize{0};
71+
// DSA copy cache hint: enable when CPU reuse is expected.
72+
bool checksumOffloadCacheControl{true};
6373
// Base offset and size (in bytes) of cache on the device
6474
uint64_t cacheBaseOffset{};
6575
uint64_t cacheSize{};
@@ -556,6 +566,14 @@ class BlockCache final : public Engine {
556566
const DestructorCallback destructorCb_;
557567
const bool checksumData_{};
558568
const ChecksumType checksumType_;
569+
570+
const bool checksumOffload_{};
571+
const uint32_t checksumOffloadMinSize_{};
572+
const uint32_t checksumOffloadReadMinSize_{};
573+
const bool checksumOffloadCacheControl_{};
574+
575+
// Computes a value checksum; may yield while waiting for DSA.
576+
uint32_t valueChecksum(BufferView value) const;
559577
// reference to the under-lying device.
560578
const Device& device_;
561579
// alloc alignment size indicates the granularity of entry sizes on device.

0 commit comments

Comments
 (0)