From 227eb707d2cada6b79f4b976f3b0901922c07ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 15 Nov 2022 17:30:16 +0800 Subject: [PATCH] CpuUsage: fix calucation on Windows --- src/detection/cpuUsage/cpuUsage_windows.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/detection/cpuUsage/cpuUsage_windows.c b/src/detection/cpuUsage/cpuUsage_windows.c index 1aa9ba3f6..bf4cc7644 100644 --- a/src/detection/cpuUsage/cpuUsage_windows.c +++ b/src/detection/cpuUsage/cpuUsage_windows.c @@ -1,8 +1,7 @@ #include "fastfetch.h" #include "cpuUsage.h" -#define WIN32_LEAN_AND_MEAN 1 -#include +#include static inline uint64_t fileTimeToUint64(const FILETIME* ft) { return (((uint64_t)ft->dwHighDateTime) << 32) | ((uint64_t)ft->dwLowDateTime); @@ -11,10 +10,12 @@ static inline uint64_t fileTimeToUint64(const FILETIME* ft) { const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) { FILETIME idleTime, kernelTime, userTime; - if(GetSystemTimes(&idleTime, &kernelTime, &userTime) == 0) + if(!GetSystemTimes(&idleTime, &kernelTime, &userTime)) return "GetSystemTimes() failed"; - *inUseAll = fileTimeToUint64(&userTime) + fileTimeToUint64(&kernelTime); - *totalAll = *inUseAll + fileTimeToUint64(&idleTime); + // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getsystemtimes + // `kernelTime` also includes the amount of time the system has been idle. + *totalAll = fileTimeToUint64(&userTime) + fileTimeToUint64(&kernelTime); + *inUseAll = *totalAll - fileTimeToUint64(&idleTime); return NULL; }