Flush Log every 5 seconds

Summary: This might help with p99 performance, but does not solve the real problem. More discussion on #2947135

Test Plan: make check

Reviewers: dhruba, haobo

Reviewed By: dhruba

CC: leveldb

Differential Revision: https://reviews.facebook.net/D13809
This commit is contained in:
Igor Canadi 2013-10-31 15:36:40 -07:00
parent 82b7e37f6e
commit b572e81f94
2 changed files with 12 additions and 4 deletions

View File

@ -1121,7 +1121,7 @@ class PosixEnv : public Env {
} else {
int fd = fileno(f);
SetFD_CLOEXEC(fd, nullptr);
result->reset(new PosixLogger(f, &PosixEnv::gettid));
result->reset(new PosixLogger(f, &PosixEnv::gettid, this));
return Status::OK();
}
}

View File

@ -33,9 +33,13 @@ class PosixLogger : public Logger {
uint64_t (*gettid_)(); // Return the thread id for the current thread
std::atomic_size_t log_size_;
int fd_;
const static uint64_t flush_every_seconds_ = 5;
uint64_t last_flush_micros_;
Env* env_;
public:
PosixLogger(FILE* f, uint64_t (*gettid)()) :
file_(f), gettid_(gettid), log_size_(0), fd_(fileno(f)) { }
PosixLogger(FILE* f, uint64_t (*gettid)(), Env* env) :
file_(f), gettid_(gettid), log_size_(0), fd_(fileno(f)),
last_flush_micros_(0), env_(env) { }
virtual ~PosixLogger() {
fclose(file_);
}
@ -118,7 +122,11 @@ class PosixLogger : public Logger {
size_t sz = fwrite(base, 1, write_size, file_);
assert(sz == write_size);
if (sz > 0) {
if (env_->NowMicros() - last_flush_micros_ >=
flush_every_seconds_ * 1000000) {
fflush(file_);
last_flush_micros_ = env_->NowMicros();
}
log_size_ += write_size;
}
if (base != buffer) {