2019-06-06 18:21:11 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
|
|
|
#include "trace_replay/block_cache_tracer.h"
|
|
|
|
|
2019-08-09 20:09:04 +00:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2023-01-18 21:24:37 +00:00
|
|
|
#include <string>
|
2019-08-09 20:09:04 +00:00
|
|
|
|
2019-06-06 18:21:11 +00:00
|
|
|
#include "db/db_impl/db_impl.h"
|
2019-07-23 00:47:54 +00:00
|
|
|
#include "db/dbformat.h"
|
2019-06-06 18:21:11 +00:00
|
|
|
#include "rocksdb/slice.h"
|
2021-08-12 02:31:44 +00:00
|
|
|
#include "rocksdb/trace_record.h"
|
2019-06-06 18:21:11 +00:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/hash.h"
|
|
|
|
#include "util/string_util.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2019-06-06 18:21:11 +00:00
|
|
|
|
|
|
|
namespace {
|
2022-10-21 19:15:35 +00:00
|
|
|
bool ShouldTrace(const Slice& block_key,
|
|
|
|
const BlockCacheTraceOptions& trace_options) {
|
2019-06-13 22:39:52 +00:00
|
|
|
if (trace_options.sampling_frequency == 0 ||
|
|
|
|
trace_options.sampling_frequency == 1) {
|
2019-06-06 18:21:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// We use spatial downsampling so that we have a complete access history for a
|
|
|
|
// block.
|
2020-09-28 18:33:31 +00:00
|
|
|
return 0 == GetSliceRangedNPHash(block_key, trace_options.sampling_frequency);
|
2019-06-06 18:21:11 +00:00
|
|
|
}
|
2019-06-15 00:37:24 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-07-01 22:11:43 +00:00
|
|
|
const uint64_t kMicrosInSecond = 1000 * 1000;
|
2019-07-12 23:52:15 +00:00
|
|
|
const uint64_t kSecondInMinute = 60;
|
|
|
|
const uint64_t kSecondInHour = 3600;
|
2019-06-15 00:37:24 +00:00
|
|
|
const std::string BlockCacheTraceHelper::kUnknownColumnFamilyName =
|
|
|
|
"UnknownColumnFamily";
|
2022-10-21 19:15:35 +00:00
|
|
|
const uint64_t BlockCacheTraceRecord::kReservedGetId = 0;
|
2019-07-04 01:45:36 +00:00
|
|
|
const uint64_t BlockCacheTraceHelper::kReservedGetId = 0;
|
2019-06-15 00:37:24 +00:00
|
|
|
|
2019-07-17 20:02:00 +00:00
|
|
|
bool BlockCacheTraceHelper::IsGetOrMultiGetOnDataBlock(
|
|
|
|
TraceType block_type, TableReaderCaller caller) {
|
2019-06-15 00:37:24 +00:00
|
|
|
return (block_type == TraceType::kBlockTraceDataBlock) &&
|
2019-07-17 20:02:00 +00:00
|
|
|
IsGetOrMultiGet(caller);
|
2019-06-15 00:37:24 +00:00
|
|
|
}
|
2019-06-06 18:21:11 +00:00
|
|
|
|
2019-07-17 20:02:00 +00:00
|
|
|
bool BlockCacheTraceHelper::IsGetOrMultiGet(TableReaderCaller caller) {
|
2019-07-04 01:45:36 +00:00
|
|
|
return caller == TableReaderCaller::kUserGet ||
|
|
|
|
caller == TableReaderCaller::kUserMultiGet;
|
|
|
|
}
|
|
|
|
|
2019-07-11 19:40:08 +00:00
|
|
|
bool BlockCacheTraceHelper::IsUserAccess(TableReaderCaller caller) {
|
|
|
|
return caller == TableReaderCaller::kUserGet ||
|
|
|
|
caller == TableReaderCaller::kUserMultiGet ||
|
|
|
|
caller == TableReaderCaller::kUserIterator ||
|
|
|
|
caller == TableReaderCaller::kUserApproximateSize ||
|
|
|
|
caller == TableReaderCaller::kUserVerifyChecksum;
|
|
|
|
}
|
|
|
|
|
2019-07-23 00:47:54 +00:00
|
|
|
std::string BlockCacheTraceHelper::ComputeRowKey(
|
|
|
|
const BlockCacheTraceRecord& access) {
|
|
|
|
if (!IsGetOrMultiGet(access.caller)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
Slice key = ExtractUserKey(access.referenced_key);
|
Pysim more algorithms (#5644)
Summary:
This PR adds four more eviction policies.
- OPT [1]
- Hyperbolic caching [2]
- ARC [3]
- GreedyDualSize [4]
[1] L. A. Belady. 1966. A Study of Replacement Algorithms for a Virtual-storage Computer. IBM Syst. J. 5, 2 (June 1966), 78-101. DOI=http://dx.doi.org/10.1147/sj.52.0078
[2] Aaron Blankstein, Siddhartha Sen, and Michael J. Freedman. 2017. Hyperbolic caching: flexible caching for web applications. In Proceedings of the 2017 USENIX Conference on Usenix Annual Technical Conference (USENIX ATC '17). USENIX Association, Berkeley, CA, USA, 499-511.
[3] Nimrod Megiddo and Dharmendra S. Modha. 2003. ARC: A Self-Tuning, Low Overhead Replacement Cache. In Proceedings of the 2nd USENIX Conference on File and Storage Technologies (FAST '03). USENIX Association, Berkeley, CA, USA, 115-130.
[4] N. Young. The k-server dual and loose competitiveness for paging. Algorithmica, June 1994, vol. 11,(no.6):525-41. Rewritten version of ''On-line caching as cache size varies'', in The 2nd Annual ACM-SIAM Symposium on Discrete Algorithms, 241-250, 1991.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5644
Differential Revision: D16548817
Pulled By: HaoyuHuang
fbshipit-source-id: 838f76db9179f07911abaab46c97e1c929cfcd63
2019-08-07 01:47:39 +00:00
|
|
|
return std::to_string(access.sst_fd_number) + "_" + key.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t BlockCacheTraceHelper::GetTableId(
|
|
|
|
const BlockCacheTraceRecord& access) {
|
|
|
|
if (!IsGetOrMultiGet(access.caller) || access.referenced_key.size() < 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return static_cast<uint64_t>(DecodeFixed32(access.referenced_key.data())) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t BlockCacheTraceHelper::GetSequenceNumber(
|
|
|
|
const BlockCacheTraceRecord& access) {
|
|
|
|
if (!IsGetOrMultiGet(access.caller)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-01-18 21:24:37 +00:00
|
|
|
if (access.caller == TableReaderCaller::kUserMultiGet &&
|
|
|
|
access.referenced_key.size() < 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-10-21 19:15:35 +00:00
|
|
|
return access.get_from_user_specified_snapshot
|
|
|
|
? 1 + GetInternalKeySeqno(access.referenced_key)
|
|
|
|
: 0;
|
Pysim more algorithms (#5644)
Summary:
This PR adds four more eviction policies.
- OPT [1]
- Hyperbolic caching [2]
- ARC [3]
- GreedyDualSize [4]
[1] L. A. Belady. 1966. A Study of Replacement Algorithms for a Virtual-storage Computer. IBM Syst. J. 5, 2 (June 1966), 78-101. DOI=http://dx.doi.org/10.1147/sj.52.0078
[2] Aaron Blankstein, Siddhartha Sen, and Michael J. Freedman. 2017. Hyperbolic caching: flexible caching for web applications. In Proceedings of the 2017 USENIX Conference on Usenix Annual Technical Conference (USENIX ATC '17). USENIX Association, Berkeley, CA, USA, 499-511.
[3] Nimrod Megiddo and Dharmendra S. Modha. 2003. ARC: A Self-Tuning, Low Overhead Replacement Cache. In Proceedings of the 2nd USENIX Conference on File and Storage Technologies (FAST '03). USENIX Association, Berkeley, CA, USA, 115-130.
[4] N. Young. The k-server dual and loose competitiveness for paging. Algorithmica, June 1994, vol. 11,(no.6):525-41. Rewritten version of ''On-line caching as cache size varies'', in The 2nd Annual ACM-SIAM Symposium on Discrete Algorithms, 241-250, 1991.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5644
Differential Revision: D16548817
Pulled By: HaoyuHuang
fbshipit-source-id: 838f76db9179f07911abaab46c97e1c929cfcd63
2019-08-07 01:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t BlockCacheTraceHelper::GetBlockOffsetInFile(
|
|
|
|
const BlockCacheTraceRecord& access) {
|
|
|
|
Slice input(access.block_key);
|
|
|
|
uint64_t offset = 0;
|
|
|
|
while (true) {
|
|
|
|
uint64_t tmp = 0;
|
|
|
|
if (GetVarint64(&input, &tmp)) {
|
|
|
|
offset = tmp;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offset;
|
2019-07-23 00:47:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 19:15:35 +00:00
|
|
|
BlockCacheTraceWriterImpl::BlockCacheTraceWriterImpl(
|
|
|
|
SystemClock* clock, const BlockCacheTraceWriterOptions& trace_options,
|
2019-06-13 22:39:52 +00:00
|
|
|
std::unique_ptr<TraceWriter>&& trace_writer)
|
2021-03-15 11:32:24 +00:00
|
|
|
: clock_(clock),
|
2019-06-13 22:39:52 +00:00
|
|
|
trace_options_(trace_options),
|
|
|
|
trace_writer_(std::move(trace_writer)) {}
|
|
|
|
|
2022-10-21 19:15:35 +00:00
|
|
|
Status BlockCacheTraceWriterImpl::WriteBlockAccess(
|
2019-06-15 00:37:24 +00:00
|
|
|
const BlockCacheTraceRecord& record, const Slice& block_key,
|
|
|
|
const Slice& cf_name, const Slice& referenced_key) {
|
2019-06-06 18:21:11 +00:00
|
|
|
uint64_t trace_file_size = trace_writer_->GetFileSize();
|
2019-06-13 22:39:52 +00:00
|
|
|
if (trace_file_size > trace_options_.max_trace_file_size) {
|
2019-06-06 18:21:11 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
Trace trace;
|
|
|
|
trace.ts = record.access_timestamp;
|
|
|
|
trace.type = record.block_type;
|
2019-06-15 00:37:24 +00:00
|
|
|
PutLengthPrefixedSlice(&trace.payload, block_key);
|
2019-06-06 18:21:11 +00:00
|
|
|
PutFixed64(&trace.payload, record.block_size);
|
2019-06-15 00:37:24 +00:00
|
|
|
PutFixed64(&trace.payload, record.cf_id);
|
|
|
|
PutLengthPrefixedSlice(&trace.payload, cf_name);
|
2019-06-06 18:21:11 +00:00
|
|
|
PutFixed32(&trace.payload, record.level);
|
2019-06-15 00:37:24 +00:00
|
|
|
PutFixed64(&trace.payload, record.sst_fd_number);
|
2019-06-06 18:21:11 +00:00
|
|
|
trace.payload.push_back(record.caller);
|
|
|
|
trace.payload.push_back(record.is_cache_hit);
|
|
|
|
trace.payload.push_back(record.no_insert);
|
2019-07-17 20:02:00 +00:00
|
|
|
if (BlockCacheTraceHelper::IsGetOrMultiGet(record.caller)) {
|
2019-07-04 01:45:36 +00:00
|
|
|
PutFixed64(&trace.payload, record.get_id);
|
2019-07-17 20:02:00 +00:00
|
|
|
trace.payload.push_back(record.get_from_user_specified_snapshot);
|
2019-06-15 00:37:24 +00:00
|
|
|
PutLengthPrefixedSlice(&trace.payload, referenced_key);
|
2019-07-17 20:02:00 +00:00
|
|
|
}
|
|
|
|
if (BlockCacheTraceHelper::IsGetOrMultiGetOnDataBlock(record.block_type,
|
|
|
|
record.caller)) {
|
2019-06-15 00:37:24 +00:00
|
|
|
PutFixed64(&trace.payload, record.referenced_data_size);
|
2019-06-06 18:21:11 +00:00
|
|
|
PutFixed64(&trace.payload, record.num_keys_in_block);
|
2019-06-15 00:37:24 +00:00
|
|
|
trace.payload.push_back(record.referenced_key_exist_in_block);
|
2019-06-06 18:21:11 +00:00
|
|
|
}
|
|
|
|
std::string encoded_trace;
|
|
|
|
TracerHelper::EncodeTrace(trace, &encoded_trace);
|
|
|
|
return trace_writer_->Write(encoded_trace);
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:15:35 +00:00
|
|
|
Status BlockCacheTraceWriterImpl::WriteHeader() {
|
2019-06-06 18:21:11 +00:00
|
|
|
Trace trace;
|
2021-03-15 11:32:24 +00:00
|
|
|
trace.ts = clock_->NowMicros();
|
2019-06-06 18:21:11 +00:00
|
|
|
trace.type = TraceType::kTraceBegin;
|
|
|
|
PutLengthPrefixedSlice(&trace.payload, kTraceMagic);
|
|
|
|
PutFixed32(&trace.payload, kMajorVersion);
|
|
|
|
PutFixed32(&trace.payload, kMinorVersion);
|
|
|
|
std::string encoded_trace;
|
|
|
|
TracerHelper::EncodeTrace(trace, &encoded_trace);
|
|
|
|
return trace_writer_->Write(encoded_trace);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockCacheTraceReader::BlockCacheTraceReader(
|
|
|
|
std::unique_ptr<TraceReader>&& reader)
|
|
|
|
: trace_reader_(std::move(reader)) {}
|
|
|
|
|
|
|
|
Status BlockCacheTraceReader::ReadHeader(BlockCacheTraceHeader* header) {
|
|
|
|
assert(header != nullptr);
|
|
|
|
std::string encoded_trace;
|
|
|
|
Status s = trace_reader_->Read(&encoded_trace);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
Trace trace;
|
|
|
|
s = TracerHelper::DecodeTrace(encoded_trace, &trace);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
header->start_time = trace.ts;
|
|
|
|
Slice enc_slice = Slice(trace.payload);
|
|
|
|
Slice magnic_number;
|
|
|
|
if (!GetLengthPrefixedSlice(&enc_slice, &magnic_number)) {
|
|
|
|
return Status::Corruption(
|
|
|
|
"Corrupted header in the trace file: Failed to read the magic number.");
|
|
|
|
}
|
|
|
|
if (magnic_number.ToString() != kTraceMagic) {
|
|
|
|
return Status::Corruption(
|
|
|
|
"Corrupted header in the trace file: Magic number does not match.");
|
|
|
|
}
|
|
|
|
if (!GetFixed32(&enc_slice, &header->rocksdb_major_version)) {
|
|
|
|
return Status::Corruption(
|
|
|
|
"Corrupted header in the trace file: Failed to read rocksdb major "
|
|
|
|
"version number.");
|
|
|
|
}
|
|
|
|
if (!GetFixed32(&enc_slice, &header->rocksdb_minor_version)) {
|
|
|
|
return Status::Corruption(
|
|
|
|
"Corrupted header in the trace file: Failed to read rocksdb minor "
|
|
|
|
"version number.");
|
|
|
|
}
|
|
|
|
// We should have retrieved all information in the header.
|
|
|
|
if (!enc_slice.empty()) {
|
|
|
|
return Status::Corruption(
|
|
|
|
"Corrupted header in the trace file: The length of header is too "
|
|
|
|
"long.");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlockCacheTraceReader::ReadAccess(BlockCacheTraceRecord* record) {
|
|
|
|
assert(record);
|
|
|
|
std::string encoded_trace;
|
|
|
|
Status s = trace_reader_->Read(&encoded_trace);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
Trace trace;
|
|
|
|
s = TracerHelper::DecodeTrace(encoded_trace, &trace);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
record->access_timestamp = trace.ts;
|
|
|
|
record->block_type = trace.type;
|
|
|
|
Slice enc_slice = Slice(trace.payload);
|
2019-06-15 00:37:24 +00:00
|
|
|
|
2020-06-24 20:39:26 +00:00
|
|
|
const unsigned int kCharSize = 1;
|
|
|
|
|
2019-06-06 18:21:11 +00:00
|
|
|
Slice block_key;
|
|
|
|
if (!GetLengthPrefixedSlice(&enc_slice, &block_key)) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read block key.");
|
|
|
|
}
|
|
|
|
record->block_key = block_key.ToString();
|
|
|
|
if (!GetFixed64(&enc_slice, &record->block_size)) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read block size.");
|
|
|
|
}
|
2019-06-15 00:37:24 +00:00
|
|
|
if (!GetFixed64(&enc_slice, &record->cf_id)) {
|
2019-06-06 18:21:11 +00:00
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read column family ID.");
|
|
|
|
}
|
|
|
|
Slice cf_name;
|
|
|
|
if (!GetLengthPrefixedSlice(&enc_slice, &cf_name)) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read column family name.");
|
|
|
|
}
|
|
|
|
record->cf_name = cf_name.ToString();
|
|
|
|
if (!GetFixed32(&enc_slice, &record->level)) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read level.");
|
|
|
|
}
|
2019-06-15 00:37:24 +00:00
|
|
|
if (!GetFixed64(&enc_slice, &record->sst_fd_number)) {
|
2019-06-06 18:21:11 +00:00
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read SST file number.");
|
|
|
|
}
|
|
|
|
if (enc_slice.empty()) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read caller.");
|
|
|
|
}
|
2019-06-20 21:28:22 +00:00
|
|
|
record->caller = static_cast<TableReaderCaller>(enc_slice[0]);
|
2019-06-06 18:21:11 +00:00
|
|
|
enc_slice.remove_prefix(kCharSize);
|
|
|
|
if (enc_slice.empty()) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read is_cache_hit.");
|
|
|
|
}
|
2022-10-21 19:15:35 +00:00
|
|
|
record->is_cache_hit = static_cast<char>(enc_slice[0]);
|
2019-06-06 18:21:11 +00:00
|
|
|
enc_slice.remove_prefix(kCharSize);
|
|
|
|
if (enc_slice.empty()) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read no_insert.");
|
|
|
|
}
|
2022-10-21 19:15:35 +00:00
|
|
|
record->no_insert = static_cast<char>(enc_slice[0]);
|
2019-06-06 18:21:11 +00:00
|
|
|
enc_slice.remove_prefix(kCharSize);
|
2019-07-17 20:02:00 +00:00
|
|
|
if (BlockCacheTraceHelper::IsGetOrMultiGet(record->caller)) {
|
2019-07-04 01:45:36 +00:00
|
|
|
if (!GetFixed64(&enc_slice, &record->get_id)) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read the get id.");
|
|
|
|
}
|
2019-07-17 20:02:00 +00:00
|
|
|
if (enc_slice.empty()) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read "
|
|
|
|
"get_from_user_specified_snapshot.");
|
|
|
|
}
|
2022-10-21 19:15:35 +00:00
|
|
|
record->get_from_user_specified_snapshot = static_cast<char>(enc_slice[0]);
|
2019-07-17 20:02:00 +00:00
|
|
|
enc_slice.remove_prefix(kCharSize);
|
2019-06-06 18:21:11 +00:00
|
|
|
Slice referenced_key;
|
|
|
|
if (!GetLengthPrefixedSlice(&enc_slice, &referenced_key)) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read the referenced key.");
|
|
|
|
}
|
|
|
|
record->referenced_key = referenced_key.ToString();
|
2019-07-17 20:02:00 +00:00
|
|
|
}
|
|
|
|
if (BlockCacheTraceHelper::IsGetOrMultiGetOnDataBlock(record->block_type,
|
|
|
|
record->caller)) {
|
2019-06-15 00:37:24 +00:00
|
|
|
if (!GetFixed64(&enc_slice, &record->referenced_data_size)) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read the referenced data size.");
|
|
|
|
}
|
2019-06-06 18:21:11 +00:00
|
|
|
if (!GetFixed64(&enc_slice, &record->num_keys_in_block)) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read the number of keys in the "
|
|
|
|
"block.");
|
|
|
|
}
|
|
|
|
if (enc_slice.empty()) {
|
|
|
|
return Status::Incomplete(
|
|
|
|
"Incomplete access record: Failed to read "
|
2019-06-15 00:37:24 +00:00
|
|
|
"referenced_key_exist_in_block.");
|
2019-06-06 18:21:11 +00:00
|
|
|
}
|
2022-10-21 19:15:35 +00:00
|
|
|
record->referenced_key_exist_in_block = static_cast<char>(enc_slice[0]);
|
2019-06-06 18:21:11 +00:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-08-09 20:09:04 +00:00
|
|
|
BlockCacheHumanReadableTraceWriter::~BlockCacheHumanReadableTraceWriter() {
|
|
|
|
if (human_readable_trace_file_writer_) {
|
2020-08-24 23:41:42 +00:00
|
|
|
human_readable_trace_file_writer_->Flush().PermitUncheckedError();
|
|
|
|
human_readable_trace_file_writer_->Close().PermitUncheckedError();
|
2019-08-09 20:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlockCacheHumanReadableTraceWriter::NewWritableFile(
|
2020-02-20 20:07:53 +00:00
|
|
|
const std::string& human_readable_trace_file_path,
|
|
|
|
ROCKSDB_NAMESPACE::Env* env) {
|
2019-08-09 20:09:04 +00:00
|
|
|
if (human_readable_trace_file_path.empty()) {
|
|
|
|
return Status::InvalidArgument(
|
|
|
|
"The provided human_readable_trace_file_path is null.");
|
|
|
|
}
|
|
|
|
return env->NewWritableFile(human_readable_trace_file_path,
|
|
|
|
&human_readable_trace_file_writer_, EnvOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlockCacheHumanReadableTraceWriter::WriteHumanReadableTraceRecord(
|
|
|
|
const BlockCacheTraceRecord& access, uint64_t block_id,
|
|
|
|
uint64_t get_key_id) {
|
|
|
|
if (!human_readable_trace_file_writer_) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
int ret = snprintf(
|
|
|
|
trace_record_buffer_, sizeof(trace_record_buffer_),
|
|
|
|
"%" PRIu64 ",%" PRIu64 ",%u,%" PRIu64 ",%" PRIu64 ",%s,%" PRIu32
|
|
|
|
",%" PRIu64 ",%u,%u,%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%u,%u,%" PRIu64
|
|
|
|
",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n",
|
|
|
|
access.access_timestamp, block_id, access.block_type, access.block_size,
|
|
|
|
access.cf_id, access.cf_name.c_str(), access.level, access.sst_fd_number,
|
|
|
|
access.caller, access.no_insert, access.get_id, get_key_id,
|
|
|
|
access.referenced_data_size, access.is_cache_hit,
|
|
|
|
access.referenced_key_exist_in_block, access.num_keys_in_block,
|
|
|
|
BlockCacheTraceHelper::GetTableId(access),
|
|
|
|
BlockCacheTraceHelper::GetSequenceNumber(access),
|
|
|
|
static_cast<uint64_t>(access.block_key.size()),
|
|
|
|
static_cast<uint64_t>(access.referenced_key.size()),
|
|
|
|
BlockCacheTraceHelper::GetBlockOffsetInFile(access));
|
|
|
|
if (ret < 0) {
|
|
|
|
return Status::IOError("failed to format the output");
|
|
|
|
}
|
|
|
|
std::string printout(trace_record_buffer_);
|
|
|
|
return human_readable_trace_file_writer_->Append(printout);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockCacheHumanReadableTraceReader::BlockCacheHumanReadableTraceReader(
|
|
|
|
const std::string& trace_file_path)
|
|
|
|
: BlockCacheTraceReader(/*trace_reader=*/nullptr) {
|
|
|
|
human_readable_trace_reader_.open(trace_file_path, std::ifstream::in);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockCacheHumanReadableTraceReader::~BlockCacheHumanReadableTraceReader() {
|
|
|
|
human_readable_trace_reader_.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlockCacheHumanReadableTraceReader::ReadHeader(
|
|
|
|
BlockCacheTraceHeader* /*header*/) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlockCacheHumanReadableTraceReader::ReadAccess(
|
|
|
|
BlockCacheTraceRecord* record) {
|
|
|
|
std::string line;
|
|
|
|
if (!std::getline(human_readable_trace_reader_, line)) {
|
|
|
|
return Status::Incomplete("No more records to read.");
|
|
|
|
}
|
|
|
|
std::stringstream ss(line);
|
|
|
|
std::vector<std::string> record_strs;
|
|
|
|
while (ss.good()) {
|
|
|
|
std::string substr;
|
|
|
|
getline(ss, substr, ',');
|
|
|
|
record_strs.push_back(substr);
|
|
|
|
}
|
|
|
|
if (record_strs.size() != 21) {
|
|
|
|
return Status::Incomplete("Records format is wrong.");
|
|
|
|
}
|
|
|
|
|
|
|
|
record->access_timestamp = ParseUint64(record_strs[0]);
|
|
|
|
uint64_t block_key = ParseUint64(record_strs[1]);
|
|
|
|
record->block_type = static_cast<TraceType>(ParseUint64(record_strs[2]));
|
|
|
|
record->block_size = ParseUint64(record_strs[3]);
|
|
|
|
record->cf_id = ParseUint64(record_strs[4]);
|
|
|
|
record->cf_name = record_strs[5];
|
|
|
|
record->level = static_cast<uint32_t>(ParseUint64(record_strs[6]));
|
|
|
|
record->sst_fd_number = ParseUint64(record_strs[7]);
|
|
|
|
record->caller = static_cast<TableReaderCaller>(ParseUint64(record_strs[8]));
|
2022-10-21 19:15:35 +00:00
|
|
|
record->no_insert = static_cast<char>(ParseUint64(record_strs[9]));
|
2019-08-09 20:09:04 +00:00
|
|
|
record->get_id = ParseUint64(record_strs[10]);
|
|
|
|
uint64_t get_key_id = ParseUint64(record_strs[11]);
|
|
|
|
|
|
|
|
record->referenced_data_size = ParseUint64(record_strs[12]);
|
2022-10-21 19:15:35 +00:00
|
|
|
record->is_cache_hit = static_cast<char>(ParseUint64(record_strs[13]));
|
2019-08-09 20:09:04 +00:00
|
|
|
record->referenced_key_exist_in_block =
|
2022-10-21 19:15:35 +00:00
|
|
|
static_cast<char>(ParseUint64(record_strs[14]));
|
2019-08-09 20:09:04 +00:00
|
|
|
record->num_keys_in_block = ParseUint64(record_strs[15]);
|
|
|
|
uint64_t table_id = ParseUint64(record_strs[16]);
|
|
|
|
if (table_id > 0) {
|
|
|
|
// Decrement since valid table id in the trace file equals traced table id
|
|
|
|
// + 1.
|
|
|
|
table_id -= 1;
|
|
|
|
}
|
|
|
|
uint64_t get_sequence_number = ParseUint64(record_strs[17]);
|
|
|
|
if (get_sequence_number > 0) {
|
2022-10-21 19:15:35 +00:00
|
|
|
record->get_from_user_specified_snapshot = true;
|
2019-08-09 20:09:04 +00:00
|
|
|
// Decrement since valid seq number in the trace file equals traced seq
|
|
|
|
// number + 1.
|
|
|
|
get_sequence_number -= 1;
|
|
|
|
}
|
|
|
|
uint64_t block_key_size = ParseUint64(record_strs[18]);
|
|
|
|
uint64_t get_key_size = ParseUint64(record_strs[19]);
|
|
|
|
uint64_t block_offset = ParseUint64(record_strs[20]);
|
|
|
|
|
|
|
|
std::string tmp_block_key;
|
|
|
|
PutVarint64(&tmp_block_key, block_key);
|
|
|
|
PutVarint64(&tmp_block_key, block_offset);
|
|
|
|
// Append 1 until the size is the same as traced block key size.
|
|
|
|
while (record->block_key.size() < block_key_size - tmp_block_key.size()) {
|
|
|
|
record->block_key += "1";
|
|
|
|
}
|
|
|
|
record->block_key += tmp_block_key;
|
|
|
|
|
|
|
|
if (get_key_id != 0) {
|
|
|
|
std::string tmp_get_key;
|
|
|
|
PutFixed64(&tmp_get_key, get_key_id);
|
|
|
|
PutFixed64(&tmp_get_key, get_sequence_number << 8);
|
|
|
|
PutFixed32(&record->referenced_key, static_cast<uint32_t>(table_id));
|
|
|
|
// Append 1 until the size is the same as traced key size.
|
|
|
|
while (record->referenced_key.size() < get_key_size - tmp_get_key.size()) {
|
|
|
|
record->referenced_key += "1";
|
|
|
|
}
|
|
|
|
record->referenced_key += tmp_get_key;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:39:52 +00:00
|
|
|
BlockCacheTracer::BlockCacheTracer() { writer_.store(nullptr); }
|
|
|
|
|
|
|
|
BlockCacheTracer::~BlockCacheTracer() { EndTrace(); }
|
|
|
|
|
|
|
|
Status BlockCacheTracer::StartTrace(
|
2022-10-21 19:15:35 +00:00
|
|
|
const BlockCacheTraceOptions& trace_options,
|
|
|
|
std::unique_ptr<BlockCacheTraceWriter>&& trace_writer) {
|
2019-06-13 22:39:52 +00:00
|
|
|
InstrumentedMutexLock lock_guard(&trace_writer_mutex_);
|
|
|
|
if (writer_.load()) {
|
2019-07-01 02:54:28 +00:00
|
|
|
return Status::Busy();
|
2019-06-13 22:39:52 +00:00
|
|
|
}
|
2019-07-04 01:45:36 +00:00
|
|
|
get_id_counter_.store(1);
|
2019-06-13 22:39:52 +00:00
|
|
|
trace_options_ = trace_options;
|
2022-10-21 19:15:35 +00:00
|
|
|
writer_.store(trace_writer.release());
|
2019-06-13 22:39:52 +00:00
|
|
|
return writer_.load()->WriteHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockCacheTracer::EndTrace() {
|
|
|
|
InstrumentedMutexLock lock_guard(&trace_writer_mutex_);
|
|
|
|
if (!writer_.load()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete writer_.load();
|
|
|
|
writer_.store(nullptr);
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:37:24 +00:00
|
|
|
Status BlockCacheTracer::WriteBlockAccess(const BlockCacheTraceRecord& record,
|
|
|
|
const Slice& block_key,
|
|
|
|
const Slice& cf_name,
|
|
|
|
const Slice& referenced_key) {
|
Block cache tracing: Fix minor bugs with downsampling and some benchmark results. (#5473)
Summary:
As the code changes for block cache tracing are almost complete, I did a benchmark to compare the performance when block cache tracing is enabled/disabled.
With 1% downsampling ratio, the performance overhead of block cache tracing is negligible. When we trace all block accesses, the throughput drops by 6 folds with 16 threads issuing random reads and all reads are served in block cache.
Setup:
RocksDB: version 6.2
Date: Mon Jun 17 17:11:13 2019
CPU: 24 * Intel Core Processor (Skylake)
CPUCache: 16384 KB
Keys: 20 bytes each
Values: 100 bytes each (100 bytes after compression)
Entries: 10000000
Prefix: 20 bytes
Keys per prefix: 0
RawSize: 1144.4 MB (estimated)
FileSize: 1144.4 MB (estimated)
Write rate: 0 bytes/second
Read rate: 0 ops/second
Compression: NoCompression
Compression sampling rate: 0
Memtablerep: skip_list
Perf Level: 1
I ran the readrandom workload for 1 minute. Detailed throughput results: (ops/second)
Sample rate 0: no block cache tracing.
Sample rate 1: trace all block accesses.
Sample rate 100: trace accesses 1% blocks.
1 thread | | | -- | -- | -- | --
Sample rate | 0 | 1 | 100
1 MB block cache size | 13,094 | 13,166 | 13,341
10 GB block cache size | 202,243 | 188,677 | 229,182
16 threads | | | -- | -- | -- | --
Sample rate | 0 | 1 | 100
1 MB block cache size | 208,761 | 178,700 | 201,872
10 GB block cache size | 2,645,996 | 426,295 | 2,587,605
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5473
Differential Revision: D15869479
Pulled By: HaoyuHuang
fbshipit-source-id: 7ae802abe84811281a6af8649f489887cd7c4618
2019-06-18 00:56:09 +00:00
|
|
|
if (!writer_.load() || !ShouldTrace(block_key, trace_options_)) {
|
2019-06-13 22:39:52 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
InstrumentedMutexLock lock_guard(&trace_writer_mutex_);
|
|
|
|
if (!writer_.load()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-06-15 00:37:24 +00:00
|
|
|
return writer_.load()->WriteBlockAccess(record, block_key, cf_name,
|
|
|
|
referenced_key);
|
2019-06-13 22:39:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 01:45:36 +00:00
|
|
|
uint64_t BlockCacheTracer::NextGetId() {
|
|
|
|
if (!writer_.load(std::memory_order_relaxed)) {
|
|
|
|
return BlockCacheTraceHelper::kReservedGetId;
|
|
|
|
}
|
|
|
|
uint64_t prev_value = get_id_counter_.fetch_add(1);
|
|
|
|
if (prev_value == BlockCacheTraceHelper::kReservedGetId) {
|
|
|
|
// fetch and add again.
|
|
|
|
return get_id_counter_.fetch_add(1);
|
|
|
|
}
|
|
|
|
return prev_value;
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:15:35 +00:00
|
|
|
std::unique_ptr<BlockCacheTraceWriter> NewBlockCacheTraceWriter(
|
|
|
|
SystemClock* clock, const BlockCacheTraceWriterOptions& trace_options,
|
|
|
|
std::unique_ptr<TraceWriter>&& trace_writer) {
|
|
|
|
return std::unique_ptr<BlockCacheTraceWriter>(new BlockCacheTraceWriterImpl(
|
|
|
|
clock, trace_options, std::move(trace_writer)));
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|