|
32 | 32 | #include "cachelib/navy/block_cache/HitsReinsertionPolicy.h" |
33 | 33 | #include "cachelib/navy/block_cache/PercentageReinsertionPolicy.h" |
34 | 34 | #include "cachelib/navy/block_cache/SparseMapIndex.h" |
| 35 | +#include "cachelib/navy/common/ChecksumOffload.h" |
35 | 36 | #include "cachelib/navy/common/Hash.h" |
36 | 37 | #include "cachelib/navy/common/Types.h" |
37 | 38 | #include "folly/Range.h" |
@@ -108,6 +109,15 @@ BlockCache::Config& BlockCache::Config::validate() { |
108 | 109 | } |
109 | 110 | } |
110 | 111 |
|
| 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 | + |
111 | 121 | reinsertionConfig.validate(); |
112 | 122 | indexConfig.validate(); |
113 | 123 |
|
@@ -226,6 +236,12 @@ BlockCache::BlockCache(Config&& config, ValidConfigTag) |
226 | 236 | destructorCb_{std::move(config.destructorCb)}, |
227 | 237 | checksumData_{config.checksum}, |
228 | 238 | 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}, |
229 | 245 | device_{*config.device}, |
230 | 246 | allocAlignSize_{calcAllocAlignSize()}, |
231 | 247 | readBufferSize_{config.readBufferSize < kDefReadBufferSize |
@@ -279,6 +295,13 @@ std::shared_ptr<BlockCacheReinsertionPolicy> BlockCache::makeReinsertionPolicy( |
279 | 295 | return reinsertionConfig.getCustomPolicy(*index_); |
280 | 296 | } |
281 | 297 |
|
| 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 | + |
282 | 305 | uint32_t BlockCache::serializedSize(uint32_t keySize, |
283 | 306 | uint32_t valueSize) const { |
284 | 307 | uint32_t size = sizeof(EntryDesc) + keySize + valueSize; |
@@ -562,21 +585,24 @@ std::pair<Status, std::string> BlockCache::getRandomAlloc(Buffer& value) { |
562 | 585 | } |
563 | 586 |
|
564 | 587 | 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 | + } |
580 | 606 | } |
581 | 607 |
|
582 | 608 | // confirm that the chosen NvmItem is still being mapped with the key |
@@ -775,7 +801,7 @@ void BlockCache::onRegionCleanup(RegionId rid, BufferView buffer) { |
775 | 801 | HashedKey hk = |
776 | 802 | makeHK(entryEnd - sizeof(EntryDesc) - desc.keySize, desc.keySize); |
777 | 803 | BufferView value{desc.valueSize, entryEnd - entrySize}; |
778 | | - if (checksumData_ && desc.cs != checksum(value, checksumType_)) { |
| 804 | + if (checksumData_ && desc.cs != valueChecksum(value)) { |
779 | 805 | // We do not need to abort here since the EntryDesc checksum was good, so |
780 | 806 | // we can safely proceed to read the next entry. |
781 | 807 | cleanupValueChecksumErrorCount_.inc(); |
@@ -1027,22 +1053,22 @@ Status BlockCache::onReadCombinedEntryBlock(uint32_t address, |
1027 | 1053 | } |
1028 | 1054 |
|
1029 | 1055 | // 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 | + } |
1046 | 1072 | } |
1047 | 1073 |
|
1048 | 1074 | // shrink and set it as CEB's buffer |
@@ -1194,23 +1220,26 @@ AllocatorApiResult BlockCache::reinsertOrRemoveItem( |
1194 | 1220 | } |
1195 | 1221 |
|
1196 | 1222 | // 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 | + } |
1214 | 1243 | } |
1215 | 1244 |
|
1216 | 1245 | // Priority of an re-inserted item is determined by its past accesses |
@@ -1344,21 +1373,36 @@ void BlockCache::writeEntry(MutableBufferView buffer, |
1344 | 1373 |
|
1345 | 1374 | // Copy descriptor and the key to the end |
1346 | 1375 | 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 | + }; |
1355 | 1388 |
|
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()); |
1360 | 1405 | } |
1361 | | - std::memcpy(buffer.data(), value.data(), value.size()); |
1362 | 1406 |
|
1363 | 1407 | logicalWrittenCount_.add(logicalWriteSize); |
1364 | 1408 | serializedWrittenCount_.add(buffer.size()); |
@@ -1444,20 +1488,22 @@ void BlockCache::readEntry(RegionDescriptor& regionDesc, |
1444 | 1488 | ld.valueSize_ = desc.valueSize; |
1445 | 1489 | ld.lastAccessTimeSecs_ = desc.lastAccessTimeSecs; |
1446 | 1490 | 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 | + } |
1461 | 1507 | } |
1462 | 1508 | ld.status_ = Status::Ok; |
1463 | 1509 | } |
|
0 commit comments