From b572e81f942e4305569be2d2b0a80678b87f34dd Mon Sep 17 00:00:00 2001 From: Igor Canadi Date: Thu, 31 Oct 2013 15:36:40 -0700 Subject: [PATCH] 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 --- util/env_posix.cc | 2 +- util/posix_logger.h | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/util/env_posix.cc b/util/env_posix.cc index 80141fd58..994c45a2a 100644 --- a/util/env_posix.cc +++ b/util/env_posix.cc @@ -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(); } } diff --git a/util/posix_logger.h b/util/posix_logger.h index ec0ad1d32..b7ad5ac14 100644 --- a/util/posix_logger.h +++ b/util/posix_logger.h @@ -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) { - fflush(file_); + if (env_->NowMicros() - last_flush_micros_ >= + flush_every_seconds_ * 1000000) { + fflush(file_); + last_flush_micros_ = env_->NowMicros(); + } log_size_ += write_size; } if (base != buffer) {