From 1cab23c1f1e8bb6cc696bb9a16ddeda291a00910 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 3 Sep 2023 00:55:10 +0300 Subject: [PATCH] Improve ServerCpuStat. --- telegram-bot-api/ClientManager.cpp | 2 +- telegram-bot-api/Stats.cpp | 24 ++++++++++++++++-------- telegram-bot-api/Stats.h | 17 ++++------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index 0cdfe05..b1ea210 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -214,7 +214,7 @@ void ClientManager::get_stats(td::Promise promise, auto now = td::Time::now(); auto top_clients = get_top_clients(50, id_filter); - sb << stat_.get_description() << '\n'; + sb << BotStatActor::get_description() << '\n'; if (id_filter.empty()) { sb << "uptime\t" << now - parameters_->start_time_ << '\n'; sb << "bot_count\t" << clients_.size() << '\n'; diff --git a/telegram-bot-api/Stats.cpp b/telegram-bot-api/Stats.cpp index 350caf1..d682610 100644 --- a/telegram-bot-api/Stats.cpp +++ b/telegram-bot-api/Stats.cpp @@ -7,6 +7,7 @@ #include "telegram-bot-api/Stats.h" #include "td/utils/common.h" +#include "td/utils/logging.h" #include "td/utils/port/thread.h" #include "td/utils/SliceBuilder.h" #include "td/utils/StringBuilder.h" @@ -19,17 +20,24 @@ ServerCpuStat::ServerCpuStat() { } } -void ServerCpuStat::add_event(const td::CpuStat &cpu_stat, double now) { - std::lock_guard guard(mutex_); - for (auto &stat : stat_) { - stat.add_event(cpu_stat, now); +void ServerCpuStat::update(double now) { + auto r_cpu_stat = td::cpu_stat(); + if (r_cpu_stat.is_error()) { + return; } + + auto &cpu_stat = instance(); + std::lock_guard guard(cpu_stat.mutex_); + for (auto &stat : cpu_stat.stat_) { + stat.add_event(r_cpu_stat.ok(), now); + } + LOG(WARNING) << "CPU usage: " << cpu_stat.stat_[1].get_stat(now).as_vector()[0].value_; } -td::string ServerCpuStat::get_description() const { +td::string ServerCpuStat::get_description() { td::string res = "DURATION"; for (auto &descr : DESCR) { - res += "\t"; + res += '\t'; res += descr; } return res; @@ -37,7 +45,7 @@ td::string ServerCpuStat::get_description() const { static td::string to_percentage(td::uint64 ticks, td::uint64 total_ticks) { static double multiplier = 100.0 * (td::thread::hardware_concurrency() ? td::thread::hardware_concurrency() : 1); - return PSTRING() << (static_cast(ticks) / static_cast(total_ticks) * multiplier) << "%"; + return PSTRING() << (static_cast(ticks) / static_cast(total_ticks) * multiplier) << '%'; } td::vector CpuStat::as_vector() const { @@ -140,7 +148,7 @@ td::vector BotStatActor::as_vector(double now) { return res; } -td::string BotStatActor::get_description() const { +td::string BotStatActor::get_description() { td::string res = "DURATION"; for (auto &descr : DESCR) { res += "\t"; diff --git a/telegram-bot-api/Stats.h b/telegram-bot-api/Stats.h index ae38426..119f72f 100644 --- a/telegram-bot-api/Stats.h +++ b/telegram-bot-api/Stats.h @@ -9,7 +9,6 @@ #include "td/actor/actor.h" #include "td/utils/common.h" -#include "td/utils/logging.h" #include "td/utils/port/Stat.h" #include "td/utils/Time.h" #include "td/utils/TimedStat.h" @@ -49,16 +48,10 @@ class ServerCpuStat { static ServerCpuStat stat; return stat; } - static void update(double now) { - auto r_event = td::cpu_stat(); - if (r_event.is_error()) { - return; - } - instance().add_event(r_event.ok(), now); - LOG(WARNING) << "CPU usage: " << instance().stat_[1].get_stat(now).as_vector()[0].value_; - } - td::string get_description() const; + static void update(double now); + + static td::string get_description(); td::vector as_vector(double now); @@ -71,8 +64,6 @@ class ServerCpuStat { td::TimedStat stat_[SIZE]; ServerCpuStat(); - - void add_event(const td::CpuStat &stat, double now); }; class ServerBotInfo { @@ -183,7 +174,7 @@ class BotStatActor final : public td::Actor { td::vector as_vector(double now); - td::string get_description() const; + static td::string get_description(); double get_score(double now);