From e7ccdf9319a100e08dd6e592568f03feb7942b1f Mon Sep 17 00:00:00 2001 From: "chien-hsing.wu" Date: Fri, 31 Dec 2021 02:29:16 +0800 Subject: [PATCH] LogCleaner: avoid scanning logs too frequently --- src/cleanup_with_absolute_prefix_unittest.cc | 4 +++ src/logging.cc | 26 +++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/cleanup_with_absolute_prefix_unittest.cc b/src/cleanup_with_absolute_prefix_unittest.cc index 08732b8ea..d4bb47eea 100644 --- a/src/cleanup_with_absolute_prefix_unittest.cc +++ b/src/cleanup_with_absolute_prefix_unittest.cc @@ -64,6 +64,10 @@ TEST(CleanImmediatelyWithAbsolutePrefix, logging) { LOG(INFO) << "cleanup test"; } + for (unsigned i = 0; i < 10; ++i) { + LOG(ERROR) << "cleanup test"; + } + google::DisableLogCleaner(); } diff --git a/src/logging.cc b/src/logging.cc index 7a64a78c3..04ba58934 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -154,6 +154,10 @@ GLOG_DEFINE_int32(logbuflevel, 0, " ...)"); GLOG_DEFINE_int32(logbufsecs, 30, "Buffer log messages for at most this many seconds"); + +GLOG_DEFINE_int32(logcleansecs, 300, + "Clean overdue logs every this many seconds"); + GLOG_DEFINE_int32(logemaillevel, 999, "Email log messages logged at this level or higher" " (0 means email all; 3 means email FATAL only;" @@ -484,9 +488,12 @@ class LogCleaner { void Enable(unsigned int overdue_days); void Disable(); + // update next_cleanup_time_ + void UpdateCleanUpTime(); + void Run(bool base_filename_selected, const string& base_filename, - const string& filename_extension) const; + const string& filename_extension); bool enabled() const { return enabled_; } @@ -503,6 +510,7 @@ class LogCleaner { bool enabled_; unsigned int overdue_days_; + int64 next_cleanup_time_; // cycle count at which to clean overdue log }; LogCleaner log_cleaner; @@ -1303,7 +1311,7 @@ void LogFileObject::Write(bool force_flush, } } -LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7) {} +LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7), next_cleanup_time_(0) {} void LogCleaner::Enable(unsigned int overdue_days) { enabled_ = true; @@ -1314,12 +1322,24 @@ void LogCleaner::Disable() { enabled_ = false; } +void LogCleaner::UpdateCleanUpTime() { + const int64 next = (FLAGS_logcleansecs + * static_cast(1000000)); // in usec + next_cleanup_time_ = CycleClock_Now() + UsecToCycles(next); +} + void LogCleaner::Run(bool base_filename_selected, const string& base_filename, - const string& filename_extension) const { + const string& filename_extension) { assert(enabled_); assert(!base_filename_selected || !base_filename.empty()); + // avoid scanning logs too frequently + if (CycleClock_Now() < next_cleanup_time_) { + return; + } + UpdateCleanUpTime(); + vector dirs; if (!base_filename_selected) {