CpuUsage: fix calucation on Windows

This commit is contained in:
李通洲
2022-11-15 17:30:16 +08:00
parent a5306d60ee
commit 227eb707d2
+6 -5
View File
@@ -1,8 +1,7 @@
#include "fastfetch.h"
#include "cpuUsage.h"
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h>
#include <processthreadsapi.h>
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;
}