Skip to content

Commit

Permalink
Allow per-platform log buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
dango-msft authored and yabmek-msft committed Dec 9, 2024
1 parent 23e35d7 commit 91bbf16
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
2 changes: 0 additions & 2 deletions db/log_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ enum RecordType {
};
static const int kMaxRecordType = kLastType;

static const int kBlockSize = 32768;

// Header is checksum (4 bytes), length (2 bytes), type (1 byte).
static const int kHeaderSize = 4 + 2 + 1;

Expand Down
14 changes: 7 additions & 7 deletions db/log_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Reader::Reader(SequentialFile* file, Reporter* reporter, bool checksum,
: file_(file),
reporter_(reporter),
checksum_(checksum),
backing_store_(new char[kBlockSize]),
backing_store_(new char[port::kLogBlockSize]),
buffer_(),
eof_(false),
last_record_offset_(0),
Expand All @@ -31,12 +31,12 @@ Reader::Reader(SequentialFile* file, Reporter* reporter, bool checksum,
Reader::~Reader() { delete[] backing_store_; }

bool Reader::SkipToInitialBlock() {
const size_t offset_in_block = initial_offset_ % kBlockSize;
const size_t offset_in_block = initial_offset_ % port::kLogBlockSize;
uint64_t block_start_location = initial_offset_ - offset_in_block;

// Don't search a block if we'd be in the trailer
if (offset_in_block > kBlockSize - 6) {
block_start_location += kBlockSize;
if (offset_in_block > port::kLogBlockSize - 6) {
block_start_location += port::kLogBlockSize;
}

end_of_buffer_offset_ = block_start_location;
Expand Down Expand Up @@ -192,14 +192,14 @@ unsigned int Reader::ReadPhysicalRecord(Slice* result) {
if (!eof_) {
// Last read was a full read, so this is a trailer to skip
buffer_.clear();
Status status = file_->Read(kBlockSize, &buffer_, backing_store_);
Status status = file_->Read(port::kLogBlockSize, &buffer_, backing_store_);
end_of_buffer_offset_ += buffer_.size();
if (!status.ok()) {
buffer_.clear();
ReportDrop(kBlockSize, status);
ReportDrop(port::kLogBlockSize, status);
eof_ = true;
return kEof;
} else if (buffer_.size() < kBlockSize) {
} else if (buffer_.size() < port::kLogBlockSize) {
eof_ = true;
}
continue;
Expand Down
2 changes: 1 addition & 1 deletion db/log_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Reader {
bool const checksum_;
char* const backing_store_;
Slice buffer_;
bool eof_; // Last Read() indicated EOF by returning < kBlockSize
bool eof_; // Last Read() indicated EOF by returning < port::kLogBlockSize

// Offset of the last record returned by ReadRecord.
uint64_t last_record_offset_;
Expand Down
10 changes: 5 additions & 5 deletions db/log_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Writer::Writer(WritableFile* dest) : dest_(dest), block_offset_(0) {
}

Writer::Writer(WritableFile* dest, uint64_t dest_length)
: dest_(dest), block_offset_(dest_length % kBlockSize) {
: dest_(dest), block_offset_(dest_length % port::kLogBlockSize) {
InitTypeCrc(type_crc_);
}

Expand All @@ -41,7 +41,7 @@ Status Writer::AddRecord(const Slice& slice) {
Status s;
bool begin = true;
do {
const int leftover = kBlockSize - block_offset_;
const int leftover = port::kLogBlockSize - block_offset_;
assert(leftover >= 0);
if (leftover < kHeaderSize) {
// Switch to a new block
Expand All @@ -54,9 +54,9 @@ Status Writer::AddRecord(const Slice& slice) {
}

// Invariant: we never leave < kHeaderSize bytes in a block.
assert(kBlockSize - block_offset_ - kHeaderSize >= 0);
assert(port::kLogBlockSize - block_offset_ - kHeaderSize >= 0);

const size_t avail = kBlockSize - block_offset_ - kHeaderSize;
const size_t avail = port::kLogBlockSize - block_offset_ - kHeaderSize;
const size_t fragment_length = (left < avail) ? left : avail;

RecordType type;
Expand All @@ -82,7 +82,7 @@ Status Writer::AddRecord(const Slice& slice) {
Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr,
size_t length) {
assert(length <= 0xffff); // Must fit in two bytes
assert(block_offset_ + kHeaderSize + length <= kBlockSize);
assert(block_offset_ + kHeaderSize + length <= port::kLogBlockSize);

// Format the header
char buf[kHeaderSize];
Expand Down
3 changes: 3 additions & 0 deletions port/port_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace port {
// TODO(jorlow): Many of these belong more in the environment class rather than
// here. We should try moving them and see if it affects perf.

// Buffer size for log
static const int kLogBlockSize = 32768;

// ------------------ Threading -------------------

// A Mutex represents an exclusive lock.
Expand Down
2 changes: 2 additions & 0 deletions port/port_stdcxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ namespace port {

class CondVar;

static const int kLogBlockSize = 32768;

// Thinly wraps std::mutex.
class LOCKABLE Mutex {
public:
Expand Down

0 comments on commit 91bbf16

Please sign in to comment.