diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index da7d97e4c..5e41c8558 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4061,13 +4061,27 @@ class CliClient final : public Actor { fd.seek(size).ignore(); fd.truncate_to_current_position(size).ignore(); } else if (op == "mem") { - new int[1000000]; auto r_mem_stats = mem_stat(); if (r_mem_stats.is_error()) { LOG(ERROR) << r_mem_stats.error(); } else { auto stats = r_mem_stats.move_as_ok(); - LOG(ERROR) << stats.resident_size_ << " " << stats.resident_size_peak_ << " " << stats.virtual_size_ << " " << stats.virtual_size_peak_; + LOG(ERROR) << "RSS = " << stats.resident_size_ << ", peak RSS = " << stats.resident_size_peak_ << ", VSZ " + << stats.virtual_size_ << ", peak VSZ = " << stats.virtual_size_peak_; + } + } else if (op == "cpu") { + uint32 inc_count = to_integer(args); + while (inc_count-- > 0) { + cpu_counter_++; + } + auto r_cpu_stats = cpu_stat(); + if (r_cpu_stats.is_error()) { + LOG(ERROR) << r_cpu_stats.error(); + } else { + auto stats = r_cpu_stats.move_as_ok(); + LOG(ERROR) << cpu_counter_ << ", total ticks = " << stats.total_ticks_ + << ", user ticks = " << stats.process_user_ticks_ + << ", system ticks = " << stats.process_system_ticks_; } } else if (op == "SetVerbosity" || op == "SV") { Log::set_verbosity_level(to_integer(args)); @@ -4254,8 +4268,11 @@ class CliClient final : public Actor { bool disable_network_ = false; int api_id_ = 0; std::string api_hash_; + + static std::atomic cpu_counter_; }; CliClient *CliClient::instance_ = nullptr; +std::atomic CliClient::cpu_counter_; void quit() { CliClient::quit_instance(); diff --git a/tdutils/td/utils/port/Stat.cpp b/tdutils/td/utils/port/Stat.cpp index 33c863a37..b3ae8d4a9 100644 --- a/tdutils/td/utils/port/Stat.cpp +++ b/tdutils/td/utils/port/Stat.cpp @@ -10,6 +10,7 @@ #include "td/utils/port/FileFd.h" #if TD_PORT_POSIX + #include "td/utils/format.h" #include "td/utils/logging.h" #include "td/utils/misc.h" @@ -38,10 +39,14 @@ #endif #elif TD_PORT_WINDOWS + +#include "td/utils/port/thread.h" + #ifndef PSAPI_VERSION #define PSAPI_VERSION 1 #endif #include + #endif namespace td { @@ -313,10 +318,10 @@ Status cpu_stat_self(CpuStat &stat) { while (pass_cnt < 15) { if (pass_cnt == 13) { - stat.process_user_ticks = to_integer(Slice(s, t)); + stat.process_user_ticks_ = to_integer(Slice(s, t)); } if (pass_cnt == 14) { - stat.process_system_ticks = to_integer(Slice(s, t)); + stat.process_system_ticks_ = to_integer(Slice(s, t)); } while (*s && *s != ' ') { s++; @@ -360,7 +365,7 @@ Status cpu_stat_total(CpuStat &stat) { } } - stat.total_ticks = sum; + stat.total_ticks_ = sum; return Status::OK(); } #endif @@ -370,6 +375,24 @@ Result cpu_stat() { CpuStat stat; TRY_STATUS(cpu_stat_self(stat)); TRY_STATUS(cpu_stat_total(stat)); + return stat; +#elif TD_WINDOWS + CpuStat stat; + stat.total_ticks_ = static_cast(GetTickCount64()) * 10000; + auto hardware_concurrency = thread::hardware_concurrency(); + if (hardware_concurrency != 0) { + stat.total_ticks_ *= hardware_concurrency; + } + + FILETIME ignored_time; + FILETIME kernel_time; + FILETIME user_time; + if (!GetProcessTimes(GetCurrentProcess(), &ignored_time, &ignored_time, &kernel_time, &user_time)) { + return Status::Error("Failed to call GetProcessTimes"); + } + stat.process_system_ticks_ = kernel_time.dwLowDateTime + (static_cast(kernel_time.dwHighDateTime) << 32); + stat.process_user_ticks_ = user_time.dwLowDateTime + (static_cast(user_time.dwHighDateTime) << 32); + return stat; #else return Status::Error("Not supported"); diff --git a/tdutils/td/utils/port/Stat.h b/tdutils/td/utils/port/Stat.h index 4af3a7bfa..f0ad5bdfa 100644 --- a/tdutils/td/utils/port/Stat.h +++ b/tdutils/td/utils/port/Stat.h @@ -26,9 +26,9 @@ struct Stat { Result stat(CSlice path) TD_WARN_UNUSED_RESULT; struct CpuStat { - uint64 total_ticks{0}; - uint64 process_user_ticks{0}; - uint64 process_system_ticks{0}; + uint64 total_ticks_{0}; + uint64 process_user_ticks_{0}; + uint64 process_system_ticks_{0}; }; Result cpu_stat() TD_WARN_UNUSED_RESULT;