diff options
| author | ruki <[email protected]> | 2022-07-29 00:59:23 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-29 00:59:23 +0800 |
| commit | 31baf90f4306821056d43e4eaff28b80e87b29f5 (patch) | |
| tree | fb77fec42594e89916544f00ac6788f1715d496b /core/src | |
| parent | 91c60d63adf90538ac9520cb0b83b979a5f534c5 (diff) | |
get cpu usage rate for win
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/os/cpuinfo.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/core/src/xmake/os/cpuinfo.c b/core/src/xmake/os/cpuinfo.c index 143c3b3af..fa03420b9 100644 --- a/core/src/xmake/os/cpuinfo.c +++ b/core/src/xmake/os/cpuinfo.c @@ -35,11 +35,27 @@ # include <mach/mach.h> # include <mach/processor_info.h> # include <mach/mach_host.h> +#elif defined(TB_CONFIG_OS_WINDOWS) +# include <windows.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ +#if defined(TB_CONFIG_OS_WINDOWS) +static tb_uint64_t xm_os_cpuinfo_subtract_times(FILETIME const* one, FILETIME const* two) +{ + LARGE_INTEGER a, b; + a.LowPart = one->dwLowDateTime; + a.HighPart = one->dwHighDateTime; + + b.LowPart = two->dwLowDateTime; + b.HighPart = two->dwHighDateTime; + + return (tb_uint64_t)(a.QuadPart - b.QuadPart); +} +#endif + static tb_float_t xm_os_cpuinfo_usagerate() { #if defined(TB_CONFIG_OS_MACOSX) @@ -76,6 +92,35 @@ static tb_float_t xm_os_cpuinfo_usagerate() s_cpuinfo_count_prev = cpuinfo_count; } return cpu_count > 0? usagerate / cpu_count : 0; +#elif defined(TB_CONFIG_OS_WINDOWS) + // kernel include IdleTime + tb_float_t usagerate = 0; + FILETIME idle, kernel, user; + if (GetSystemTimes(&idle, &kernel, &user)) + { + static FILETIME idle_prev = {0}; + static FILETIME kernel_prev = {0}; + static FILETIME user_prev = {0}; + + if (idle_prev.dwLowDateTime != 0 && idle_prev.dwHighDateTime != 0) + { + tb_uint64_t idle_diff = xm_os_cpuinfo_subtract_times(&idle, &idle_prev); + tb_uint64_t kernel_diff = xm_os_cpuinfo_subtract_times(&kernel, &kernel_prev); + tb_uint64_t user_diff = xm_os_cpuinfo_subtract_times(&user, &user_prev); + + // kernelTime - IdleTime = kernelTime, because kernel include IdleTime + tb_uint64_t sys_total = kernel_diff + user_diff; + tb_uint64_t kernel_total = kernel_diff - idle_diff; + + // sometimes kernelTime > idleTime + if (sys_total > 0) + usagerate = (tb_float_t)((tb_double_t)(kernel_total + user_diff) / sys_total); + } + + idle_prev = idle; + kernel_prev = kernel; + user_prev = user; + } #else return 0; #endif |
