Support cpu_stat on Windows.
GitOrigin-RevId: df9a2fa6d53fb1c19cd30f2da3354bc23b2f3aee
This commit is contained in:
parent
50a6b5da92
commit
18b8e87e74
@ -4061,13 +4061,27 @@ class CliClient final : public Actor {
|
|||||||
fd.seek(size).ignore();
|
fd.seek(size).ignore();
|
||||||
fd.truncate_to_current_position(size).ignore();
|
fd.truncate_to_current_position(size).ignore();
|
||||||
} else if (op == "mem") {
|
} else if (op == "mem") {
|
||||||
new int[1000000];
|
|
||||||
auto r_mem_stats = mem_stat();
|
auto r_mem_stats = mem_stat();
|
||||||
if (r_mem_stats.is_error()) {
|
if (r_mem_stats.is_error()) {
|
||||||
LOG(ERROR) << r_mem_stats.error();
|
LOG(ERROR) << r_mem_stats.error();
|
||||||
} else {
|
} else {
|
||||||
auto stats = r_mem_stats.move_as_ok();
|
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") {
|
} else if (op == "SetVerbosity" || op == "SV") {
|
||||||
Log::set_verbosity_level(to_integer<int>(args));
|
Log::set_verbosity_level(to_integer<int>(args));
|
||||||
@ -4254,8 +4268,11 @@ class CliClient final : public Actor {
|
|||||||
bool disable_network_ = false;
|
bool disable_network_ = false;
|
||||||
int api_id_ = 0;
|
int api_id_ = 0;
|
||||||
std::string api_hash_;
|
std::string api_hash_;
|
||||||
|
|
||||||
|
static std::atomic<uint64> cpu_counter_;
|
||||||
};
|
};
|
||||||
CliClient *CliClient::instance_ = nullptr;
|
CliClient *CliClient::instance_ = nullptr;
|
||||||
|
std::atomic<uint64> CliClient::cpu_counter_;
|
||||||
|
|
||||||
void quit() {
|
void quit() {
|
||||||
CliClient::quit_instance();
|
CliClient::quit_instance();
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "td/utils/port/FileFd.h"
|
#include "td/utils/port/FileFd.h"
|
||||||
|
|
||||||
#if TD_PORT_POSIX
|
#if TD_PORT_POSIX
|
||||||
|
|
||||||
#include "td/utils/format.h"
|
#include "td/utils/format.h"
|
||||||
#include "td/utils/logging.h"
|
#include "td/utils/logging.h"
|
||||||
#include "td/utils/misc.h"
|
#include "td/utils/misc.h"
|
||||||
@ -38,10 +39,14 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#elif TD_PORT_WINDOWS
|
#elif TD_PORT_WINDOWS
|
||||||
|
|
||||||
|
#include "td/utils/port/thread.h"
|
||||||
|
|
||||||
#ifndef PSAPI_VERSION
|
#ifndef PSAPI_VERSION
|
||||||
#define PSAPI_VERSION 1
|
#define PSAPI_VERSION 1
|
||||||
#endif
|
#endif
|
||||||
#include <psapi.h>
|
#include <psapi.h>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
@ -313,10 +318,10 @@ Status cpu_stat_self(CpuStat &stat) {
|
|||||||
|
|
||||||
while (pass_cnt < 15) {
|
while (pass_cnt < 15) {
|
||||||
if (pass_cnt == 13) {
|
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) {
|
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 != ' ') {
|
while (*s && *s != ' ') {
|
||||||
s++;
|
s++;
|
||||||
@ -360,7 +365,7 @@ Status cpu_stat_total(CpuStat &stat) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stat.total_ticks = sum;
|
stat.total_ticks_ = sum;
|
||||||
return Status::OK();
|
return Status::OK();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -370,6 +375,24 @@ Result<CpuStat> cpu_stat() {
|
|||||||
CpuStat stat;
|
CpuStat stat;
|
||||||
TRY_STATUS(cpu_stat_self(stat));
|
TRY_STATUS(cpu_stat_self(stat));
|
||||||
TRY_STATUS(cpu_stat_total(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;
|
return stat;
|
||||||
#else
|
#else
|
||||||
return Status::Error("Not supported");
|
return Status::Error("Not supported");
|
||||||
|
@ -26,9 +26,9 @@ struct Stat {
|
|||||||
Result<Stat> stat(CSlice path) TD_WARN_UNUSED_RESULT;
|
Result<Stat> stat(CSlice path) TD_WARN_UNUSED_RESULT;
|
||||||
|
|
||||||
struct CpuStat {
|
struct CpuStat {
|
||||||
uint64 total_ticks{0};
|
uint64 total_ticks_{0};
|
||||||
uint64 process_user_ticks{0};
|
uint64 process_user_ticks_{0};
|
||||||
uint64 process_system_ticks{0};
|
uint64 process_system_ticks_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
Result<CpuStat> cpu_stat() TD_WARN_UNUSED_RESULT;
|
Result<CpuStat> cpu_stat() TD_WARN_UNUSED_RESULT;
|
||||||
|
Loading…
Reference in New Issue
Block a user