mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 09:33:39 +01:00
CpuUsage: support macOS
This commit is contained in:
+6
-24
@@ -1,35 +1,20 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include "detection/cpuUsage/cpuUsage.h"
|
||||
|
||||
#define FF_CPU_USAGE_MODULE_NAME "CPU Usage"
|
||||
#define FF_CPU_USAGE_NUM_FORMAT_ARGS 1
|
||||
|
||||
void ffPrintCPUUsage(FFinstance* instance)
|
||||
{
|
||||
long user, nice, system, idle, iowait, irq, softirq;
|
||||
FILE* procStat = fopen("/proc/stat", "r");
|
||||
if(procStat == NULL)
|
||||
double cpuPercent = 0;
|
||||
const char* error = ffGetCpuUsagePercent(&cpuPercent);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpu, "fopen(\"""/proc/stat\", \"r\") == NULL");
|
||||
ffPrintError(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpu, "%s", error);
|
||||
return;
|
||||
}
|
||||
if (fscanf(procStat, "cpu%ld%ld%ld%ld%ld%ld%ld", &user, &nice, &system, &idle, &iowait, &irq, &softirq) < 0) goto exit;
|
||||
long workJiffies1 = user + nice + system;
|
||||
long totalJiffies1 = workJiffies1 + idle + iowait + irq + softirq;
|
||||
|
||||
sleep(1);
|
||||
|
||||
rewind(procStat);
|
||||
if (fscanf(procStat, "cpu%ld%ld%ld%ld%ld%ld%ld", &user, &nice, &system, &idle, &iowait, &irq, &softirq) < 0) goto exit;
|
||||
long workJiffies2 = user + nice + system;
|
||||
long totalJiffies2 = workJiffies2 + idle + iowait + irq + softirq;
|
||||
|
||||
// https://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-using-c#answer-3017438
|
||||
long workOverPeriod = workJiffies2 - workJiffies1;
|
||||
long totalOverPeriod = totalJiffies2 - totalJiffies1;
|
||||
double cpuPercent = (double)workOverPeriod / (double)totalOverPeriod * 100;
|
||||
|
||||
if(instance->config.cpuUsage.outputFormat.length == 0)
|
||||
{
|
||||
@@ -43,7 +28,4 @@ void ffPrintCPUUsage(FFinstance* instance)
|
||||
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpuPercent}
|
||||
});
|
||||
}
|
||||
|
||||
exit:
|
||||
fclose(procStat);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user