diff --git a/db/db_impl.cc b/db/db_impl.cc index 76fec5ef6..1cec13464 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -162,6 +162,7 @@ DBImpl::DBImpl(const Options& options, const std::string& dbname) disable_delete_obsolete_files_(false), delete_obsolete_files_last_run_(0), purge_wal_files_last_run_(0), + last_stats_dump_time_microsec_(0), stall_level0_slowdown_(0), stall_memtable_compaction_(0), stall_level0_num_files_(0), @@ -303,6 +304,24 @@ const Status DBImpl::CreateArchivalDirectory() { return Status::OK(); } +void DBImpl::MaybeDumpStats() { + mutex_.AssertHeld(); + + if (options_.stats_dump_period_sec != 0) { + const uint64_t now_micros = env_->NowMicros(); + if (last_stats_dump_time_microsec_ + + options_.stats_dump_period_sec * 1000000 + <= now_micros) { + last_stats_dump_time_microsec_ = now_micros; + mutex_.Unlock(); + std::string stats; + GetProperty("leveldb.stats", &stats); + Log(options_.info_log, "%s", stats.c_str()); + mutex_.Lock(); + } + } +} + // Returns the list of live files in 'live' and the list // of all files in the filesystem in 'allfiles'. void DBImpl::FindObsoleteFiles(DeletionState& deletion_state) { @@ -1253,6 +1272,8 @@ void DBImpl::BackgroundCall() { MaybeScheduleCompaction(); } bg_cv_.SignalAll(); + + MaybeDumpStats(); } Status DBImpl::BackgroundCompaction(bool* madeProgress, diff --git a/db/db_impl.h b/db/db_impl.h index a8c0f5b2f..ba166b9ac 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -208,6 +208,10 @@ class DBImpl : public DB { Status ReadFirstLine(const std::string& fname, WriteBatch* const batch); + + // dump leveldb.stats to LOG + void MaybeDumpStats(); + // Constant after construction const InternalFilterPolicy internal_filter_policy_; bool owns_info_log_; @@ -272,6 +276,9 @@ class DBImpl : public DB { // last time when PurgeObsoleteWALFiles ran. uint64_t purge_wal_files_last_run_; + // last time stats were dumped to LOG + uint64_t last_stats_dump_time_microsec_; + // These count the number of microseconds for which MakeRoomForWrite stalls. uint64_t stall_level0_slowdown_; uint64_t stall_memtable_compaction_; diff --git a/include/leveldb/options.h b/include/leveldb/options.h index 043991872..f5fcdbf99 100644 --- a/include/leveldb/options.h +++ b/include/leveldb/options.h @@ -430,6 +430,9 @@ struct Options { // Default: false bool skip_log_error_on_recovery; + // if not zero, dump leveldb.stats to LOG every stats_dump_period_sec + // Default: 3600 (1 hour) + unsigned int stats_dump_period_sec; }; // Options that control read operations diff --git a/util/options.cc b/util/options.cc index 7e67e8cbd..2c4bd6370 100644 --- a/util/options.cc +++ b/util/options.cc @@ -69,7 +69,8 @@ Options::Options() allow_mmap_reads(false), allow_mmap_writes(true), is_fd_close_on_exec(true), - skip_log_error_on_recovery(false) { + skip_log_error_on_recovery(false), + stats_dump_period_sec(3600) { } void @@ -161,7 +162,7 @@ Options::Dump(Logger* log) const Log(log," Options.no_block_cache: %d", no_block_cache); Log(log," Options.table_cache_numshardbits: %d", - table_cache_numshardbits); + table_cache_numshardbits); Log(log," Options.delete_obsolete_files_period_micros: %ld", delete_obsolete_files_period_micros); Log(log," Options.max_background_compactions: %d", @@ -192,6 +193,8 @@ Options::Dump(Logger* log) const is_fd_close_on_exec); Log(log," Options.skip_log_error_on_recovery: %d", skip_log_error_on_recovery); + Log(log," Options.stats_dump_period_sec: %d", + stats_dump_period_sec); } // Options::Dump //