diff --git a/include/pika_admin.h b/include/pika_admin.h index 7693f0329d..54025b5939 100644 --- a/include/pika_admin.h +++ b/include/pika_admin.h @@ -272,9 +272,6 @@ class InfoCmd : public Cmd { bool rescan_ = false; // whether to rescan the keyspace bool off_ = false; std::set keyspace_scan_dbs_; - time_t db_size_last_time_ = 0; - uint64_t db_size_ = 0; - uint64_t log_size_ = 0; const static std::string kInfoSection; const static std::string kAllSection; const static std::string kServerSection; diff --git a/include/pika_server.h b/include/pika_server.h index 906a5be9fb..6e8c7ae1c4 100644 --- a/include/pika_server.h +++ b/include/pika_server.h @@ -261,6 +261,16 @@ class PikaServer : public pstd::noncopyable { std::unordered_map ServerExecCountDB(); std::unordered_map ServerAllDBStat(); + /* + * Memory and Disk usage statistic + */ + uint64_t GetDBSize() const { + return disk_statistic_.db_size_.load(); + } + uint64_t GetLogSize() const { + return disk_statistic_.log_size_.load(); + } + /* * Network Statistic used */ @@ -504,6 +514,7 @@ class PikaServer : public pstd::noncopyable { void AutoDeleteExpiredDump(); void AutoUpdateNetworkMetric(); void PrintThreadPoolQueueStatus(); + void StatDiskUsage(); int64_t GetLastSaveTime(const std::string& dump_dir); std::string host_; @@ -610,6 +621,8 @@ class PikaServer : public pstd::noncopyable { */ Statistic statistic_; + DiskStatistic disk_statistic_; + net::BGThread common_bg_thread_; /* diff --git a/include/pika_statistic.h b/include/pika_statistic.h index dcfe97d652..ec18d38342 100644 --- a/include/pika_statistic.h +++ b/include/pika_statistic.h @@ -57,4 +57,9 @@ struct Statistic { std::unordered_map db_stat; }; +struct DiskStatistic { + std::atomic db_size_ = 0; + std::atomic log_size_ = 0; +}; + #endif // PIKA_STATISTIC_H_ diff --git a/src/pika_admin.cc b/src/pika_admin.cc index 015d1c2a0e..f8736c96fc 100644 --- a/src/pika_admin.cc +++ b/src/pika_admin.cc @@ -1311,20 +1311,10 @@ void InfoCmd::InfoKeyspace(std::string& info) { void InfoCmd::InfoData(std::string& info) { std::stringstream tmp_stream; std::stringstream db_fatal_msg_stream; - uint64_t db_size = 0; - time_t current_time_s = time(nullptr); - uint64_t log_size = 0; - - if (current_time_s - 60 >= db_size_last_time_) { - db_size_last_time_ = current_time_s; - db_size = pstd::Du(g_pika_conf->db_path()); - db_size_ = db_size; - log_size = pstd::Du(g_pika_conf->log_path()); - log_size_ = log_size; - } else { - db_size = db_size_; - log_size = log_size_; - } + + uint64_t db_size = g_pika_server->GetDBSize(); + uint64_t log_size = g_pika_server->GetLogSize(); + tmp_stream << "# Data" << "\r\n"; tmp_stream << "db_size:" << db_size << "\r\n"; diff --git a/src/pika_server.cc b/src/pika_server.cc index 5f2b2c51ff..dae277cf8a 100644 --- a/src/pika_server.cc +++ b/src/pika_server.cc @@ -1093,7 +1093,19 @@ void PikaServer::DoTimingTask() { UpdateCacheInfo(); // Print the queue status periodically PrintThreadPoolQueueStatus(); + StatDiskUsage(); +} + +void PikaServer::StatDiskUsage() { + thread_local uint64_t last_update_time = 0; + auto current_time = pstd::NowMicros(); + if (current_time - last_update_time < 60 * 1000 * 1000) { + return; + } + last_update_time = current_time; + disk_statistic_.db_size_.store(pstd::Du(g_pika_conf->db_path())); + disk_statistic_.log_size_.store(pstd::Du(g_pika_conf->log_path())); } void PikaServer::AutoCompactRange() {