mirror of
https://github.com/tdlight-team/tdlight-telegram-bot-api.git
synced 2024-08-27 17:52:21 +02:00
Improve ServerCpuStat.
This commit is contained in:
parent
89383695ed
commit
1cab23c1f1
@ -214,7 +214,7 @@ void ClientManager::get_stats(td::Promise<td::BufferSlice> promise,
|
|||||||
|
|
||||||
auto now = td::Time::now();
|
auto now = td::Time::now();
|
||||||
auto top_clients = get_top_clients(50, id_filter);
|
auto top_clients = get_top_clients(50, id_filter);
|
||||||
sb << stat_.get_description() << '\n';
|
sb << BotStatActor::get_description() << '\n';
|
||||||
if (id_filter.empty()) {
|
if (id_filter.empty()) {
|
||||||
sb << "uptime\t" << now - parameters_->start_time_ << '\n';
|
sb << "uptime\t" << now - parameters_->start_time_ << '\n';
|
||||||
sb << "bot_count\t" << clients_.size() << '\n';
|
sb << "bot_count\t" << clients_.size() << '\n';
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include "telegram-bot-api/Stats.h"
|
#include "telegram-bot-api/Stats.h"
|
||||||
|
|
||||||
#include "td/utils/common.h"
|
#include "td/utils/common.h"
|
||||||
|
#include "td/utils/logging.h"
|
||||||
#include "td/utils/port/thread.h"
|
#include "td/utils/port/thread.h"
|
||||||
#include "td/utils/SliceBuilder.h"
|
#include "td/utils/SliceBuilder.h"
|
||||||
#include "td/utils/StringBuilder.h"
|
#include "td/utils/StringBuilder.h"
|
||||||
@ -19,17 +20,24 @@ ServerCpuStat::ServerCpuStat() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerCpuStat::add_event(const td::CpuStat &cpu_stat, double now) {
|
void ServerCpuStat::update(double now) {
|
||||||
std::lock_guard<std::mutex> guard(mutex_);
|
auto r_cpu_stat = td::cpu_stat();
|
||||||
for (auto &stat : stat_) {
|
if (r_cpu_stat.is_error()) {
|
||||||
stat.add_event(cpu_stat, now);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto &cpu_stat = instance();
|
||||||
|
std::lock_guard<std::mutex> 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";
|
td::string res = "DURATION";
|
||||||
for (auto &descr : DESCR) {
|
for (auto &descr : DESCR) {
|
||||||
res += "\t";
|
res += '\t';
|
||||||
res += descr;
|
res += descr;
|
||||||
}
|
}
|
||||||
return res;
|
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 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);
|
static double multiplier = 100.0 * (td::thread::hardware_concurrency() ? td::thread::hardware_concurrency() : 1);
|
||||||
return PSTRING() << (static_cast<double>(ticks) / static_cast<double>(total_ticks) * multiplier) << "%";
|
return PSTRING() << (static_cast<double>(ticks) / static_cast<double>(total_ticks) * multiplier) << '%';
|
||||||
}
|
}
|
||||||
|
|
||||||
td::vector<StatItem> CpuStat::as_vector() const {
|
td::vector<StatItem> CpuStat::as_vector() const {
|
||||||
@ -140,7 +148,7 @@ td::vector<StatItem> BotStatActor::as_vector(double now) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
td::string BotStatActor::get_description() const {
|
td::string BotStatActor::get_description() {
|
||||||
td::string res = "DURATION";
|
td::string res = "DURATION";
|
||||||
for (auto &descr : DESCR) {
|
for (auto &descr : DESCR) {
|
||||||
res += "\t";
|
res += "\t";
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
#include "td/actor/actor.h"
|
#include "td/actor/actor.h"
|
||||||
|
|
||||||
#include "td/utils/common.h"
|
#include "td/utils/common.h"
|
||||||
#include "td/utils/logging.h"
|
|
||||||
#include "td/utils/port/Stat.h"
|
#include "td/utils/port/Stat.h"
|
||||||
#include "td/utils/Time.h"
|
#include "td/utils/Time.h"
|
||||||
#include "td/utils/TimedStat.h"
|
#include "td/utils/TimedStat.h"
|
||||||
@ -49,16 +48,10 @@ class ServerCpuStat {
|
|||||||
static ServerCpuStat stat;
|
static ServerCpuStat stat;
|
||||||
return 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<StatItem> as_vector(double now);
|
td::vector<StatItem> as_vector(double now);
|
||||||
|
|
||||||
@ -71,8 +64,6 @@ class ServerCpuStat {
|
|||||||
td::TimedStat<CpuStat> stat_[SIZE];
|
td::TimedStat<CpuStat> stat_[SIZE];
|
||||||
|
|
||||||
ServerCpuStat();
|
ServerCpuStat();
|
||||||
|
|
||||||
void add_event(const td::CpuStat &stat, double now);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ServerBotInfo {
|
class ServerBotInfo {
|
||||||
@ -183,7 +174,7 @@ class BotStatActor final : public td::Actor {
|
|||||||
|
|
||||||
td::vector<StatItem> as_vector(double now);
|
td::vector<StatItem> as_vector(double now);
|
||||||
|
|
||||||
td::string get_description() const;
|
static td::string get_description();
|
||||||
|
|
||||||
double get_score(double now);
|
double get_score(double now);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user