mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:52:08 +02:00
CpuUsage: add support for Windows
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
#ifndef FF_INCLUDED_detection_cpu_cpuUsage
|
||||
#define FF_INCLUDED_detection_cpu_cpuUsage
|
||||
|
||||
const char* ffGetCpuUsageInfo(long* inUseAll, long* totalAll);
|
||||
#include <stdint.h>
|
||||
|
||||
// We need to use uint64_t because sizeof(long) == 4 on Windows
|
||||
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <mach/processor_info.h>
|
||||
#include <mach/mach_host.h>
|
||||
|
||||
const char* ffGetCpuUsageInfo(long* inUseAll, long* totalAll)
|
||||
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll)
|
||||
{
|
||||
natural_t numCPUs = 0U;
|
||||
processor_info_array_t cpuInfo;
|
||||
@@ -21,8 +21,8 @@ const char* ffGetCpuUsageInfo(long* inUseAll, long* totalAll)
|
||||
+ cpuInfo[CPU_STATE_MAX * i + CPU_STATE_SYSTEM]
|
||||
+ cpuInfo[CPU_STATE_MAX * i + CPU_STATE_NICE];
|
||||
integer_t total = inUse + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_IDLE];
|
||||
*inUseAll += inUse;
|
||||
*totalAll += total;
|
||||
*inUseAll += (uint64_t)inUse;
|
||||
*totalAll += (uint64_t)total;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -2,16 +2,17 @@
|
||||
#include "cpuUsage.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
const char* ffGetCpuUsageInfo(long* inUseAll, long* totalAll)
|
||||
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll)
|
||||
{
|
||||
long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0;
|
||||
uint64_t user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0;
|
||||
|
||||
FILE* procStat = fopen("/proc/stat", "r");
|
||||
if(procStat == NULL)
|
||||
return "fopen(\"""/proc/stat\", \"r\") == NULL";
|
||||
|
||||
if (fscanf(procStat, "cpu%ld%ld%ld%ld%ld%ld%ld", &user, &nice, &system, &idle, &iowait, &irq, &softirq) < 0)
|
||||
if (fscanf(procStat, "cpu%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64, &user, &nice, &system, &idle, &iowait, &irq, &softirq) < 0)
|
||||
{
|
||||
fclose(procStat);
|
||||
return "fscanf() failed";
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "fastfetch.h"
|
||||
#include "cpuUsage.h"
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <Windows.h>
|
||||
|
||||
static inline uint64_t fileTimeToUint64(const FILETIME* ft) {
|
||||
return (((uint64_t)ft->dwHighDateTime) << 32) | ((uint64_t)ft->dwLowDateTime);
|
||||
}
|
||||
|
||||
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll)
|
||||
{
|
||||
FILETIME idleTime, kernelTime, userTime;
|
||||
if(GetSystemTimes(&idleTime, &kernelTime, &userTime) == 0)
|
||||
return "GetSystemTimes() failed";
|
||||
|
||||
*inUseAll = fileTimeToUint64(&userTime) + fileTimeToUint64(&kernelTime);
|
||||
*totalAll = *inUseAll + fileTimeToUint64(&idleTime);
|
||||
return NULL;
|
||||
}
|
||||
+30
-11
@@ -1,20 +1,39 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/cpuUsage/cpuUsage.h"
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#if defined(_WIN32) || defined(__CYGWIN__)
|
||||
#include <synchapi.h>
|
||||
#include <sysinfoapi.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#define FF_CPU_USAGE_MODULE_NAME "CPU Usage"
|
||||
#define FF_CPU_USAGE_NUM_FORMAT_ARGS 1
|
||||
|
||||
time_t getTimeInMs()
|
||||
static inline uint64_t getTimeInMs()
|
||||
{
|
||||
struct timeval timeNow;
|
||||
gettimeofday(&timeNow, NULL);
|
||||
return (timeNow.tv_sec * 1000) + (timeNow.tv_usec / 1000);
|
||||
#if defined(_WIN32) || defined(__CYGWIN__)
|
||||
return GetTickCount64();
|
||||
#else
|
||||
struct timeval timeNow;
|
||||
gettimeofday(&timeNow, NULL);
|
||||
return (uint64_t)((timeNow.tv_sec * 1000) + (timeNow.tv_usec / 1000));
|
||||
#endif
|
||||
}
|
||||
|
||||
static long inUseAll1, totalAll1, startTime;
|
||||
static inline void sleepInMs(uint32_t msec)
|
||||
{
|
||||
#if defined(_WIN32) || defined(__CYGWIN__)
|
||||
SleepEx(msec, TRUE);
|
||||
#else
|
||||
nanosleep(&(struct timespec){ msec / 1000, (msec % 1000) * 1000000 }, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint64_t inUseAll1, totalAll1, startTime;
|
||||
|
||||
void ffPrepareCPUUsage()
|
||||
{
|
||||
@@ -30,16 +49,16 @@ void ffPrintCPUUsage(FFinstance* instance)
|
||||
error = ffGetCpuUsageInfo(&inUseAll1, &totalAll1);
|
||||
if(error)
|
||||
goto error;
|
||||
nanosleep(&(struct timespec){ 1, 0 }, NULL);
|
||||
sleepInMs(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
time_t duration = getTimeInMs() - startTime;
|
||||
uint64_t duration = getTimeInMs() - startTime;
|
||||
if(duration < 1000)
|
||||
nanosleep(&(struct timespec){ 0, (1000 - duration) * 1000000L }, NULL);
|
||||
sleepInMs(1000 - (uint32_t) duration);
|
||||
}
|
||||
|
||||
long inUseAll2, totalAll2;
|
||||
uint64_t inUseAll2, totalAll2;
|
||||
error = ffGetCpuUsageInfo(&inUseAll2, &totalAll2);
|
||||
|
||||
if(error)
|
||||
|
||||
Reference in New Issue
Block a user