Support cpu_stat on Windows.

GitOrigin-RevId: df9a2fa6d53fb1c19cd30f2da3354bc23b2f3aee
This commit is contained in:
levlam 2020-06-14 04:01:02 +03:00
parent 50a6b5da92
commit 18b8e87e74
3 changed files with 48 additions and 8 deletions

View File

@ -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<uint32>(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<int>(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<uint64> cpu_counter_;
};
CliClient *CliClient::instance_ = nullptr;
std::atomic<uint64> CliClient::cpu_counter_;
void quit() {
CliClient::quit_instance();

View File

@ -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 <psapi.h>
#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<uint64>(Slice(s, t));
stat.process_user_ticks_ = to_integer<uint64>(Slice(s, t));
}
if (pass_cnt == 14) {
stat.process_system_ticks = to_integer<uint64>(Slice(s, t));
stat.process_system_ticks_ = to_integer<uint64>(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<CpuStat> 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<uint64>(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<uint64>(kernel_time.dwHighDateTime) << 32);
stat.process_user_ticks_ = user_time.dwLowDateTime + (static_cast<uint64>(user_time.dwHighDateTime) << 32);
return stat;
#else
return Status::Error("Not supported");

View File

@ -26,9 +26,9 @@ struct Stat {
Result<Stat> 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<CpuStat> cpu_stat() TD_WARN_UNUSED_RESULT;