Skip to content

Commit

Permalink
feat: stat disk usage in bg thread (#2554)
Browse files Browse the repository at this point in the history
* stat disk usage in bg thread

---------

Co-authored-by: wangshaoyi <[email protected]>
  • Loading branch information
2 people authored and brother-jin committed Apr 7, 2024
1 parent f70771f commit 9f45be4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
3 changes: 0 additions & 3 deletions include/pika_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,6 @@ class InfoCmd : public Cmd {
bool rescan_ = false; // whether to rescan the keyspace
bool off_ = false;
std::set<std::string> 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;
Expand Down
13 changes: 13 additions & 0 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ class PikaServer : public pstd::noncopyable {
std::unordered_map<std::string, uint64_t> ServerExecCountDB();
std::unordered_map<std::string, QpsStatistic> 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
*/
Expand Down Expand Up @@ -501,6 +511,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_;
Expand Down Expand Up @@ -607,6 +618,8 @@ class PikaServer : public pstd::noncopyable {
*/
Statistic statistic_;

DiskStatistic disk_statistic_;

net::BGThread common_bg_thread_;

/*
Expand Down
5 changes: 5 additions & 0 deletions include/pika_statistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ struct Statistic {
std::unordered_map<std::string, QpsStatistic> db_stat;
};

struct DiskStatistic {
std::atomic<uint64_t> db_size_ = 0;
std::atomic<uint64_t> log_size_ = 0;
};

#endif // PIKA_STATISTIC_H_
18 changes: 4 additions & 14 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1320,20 +1320,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";
Expand Down
12 changes: 12 additions & 0 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,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() {
Expand Down

0 comments on commit 9f45be4

Please sign in to comment.