Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stat disk usage in bg thread #2554

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -261,6 +261,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 @@ -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_;
Expand Down Expand Up @@ -610,6 +621,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 @@ -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";
Expand Down
19 changes: 19 additions & 0 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,26 @@ void PikaServer::DoTimingTask() {
UpdateCacheInfo();
// Print the queue status periodically
PrintThreadPoolQueueStatus();
StatDiskUsage();
}

void PikaServer::StatDiskUsage() {
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved
thread_local uint64_t last_update_time = 0;
if (pstd::NowMicros() - last_update_time < 60 * 1000 * 1000) {
return;
}

last_update_time = pstd::NowMicros();
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;

db_size = pstd::Du(g_pika_conf->db_path());
disk_statistic_.db_size_.store(db_size);
log_size = pstd::Du(g_pika_conf->log_path());
disk_statistic_.log_size_.store(log_size);
}

void PikaServer::AutoCompactRange() {
Expand Down
Loading