mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-04 04:04:17 +01:00
CpuUsage: improve performance
by moving the first `/proc/stat` query earlier
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
#ifndef FF_INCLUDED_detection_cpu_cpuUsage
|
||||
#define FF_INCLUDED_detection_cpu_cpuUsage
|
||||
|
||||
const char* ffGetCpuUsagePercent(double* result);
|
||||
const char* ffGetCpuUsageInfo(long* inUseAll, long* totalAll);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
|
||||
#include <mach/processor_info.h>
|
||||
#include <mach/mach_host.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const char* getCpuUsageInfo(long* inUseAll, long* totalAll)
|
||||
const char* ffGetCpuUsageInfo(long* inUseAll, long* totalAll)
|
||||
{
|
||||
natural_t numCPUs = 0U;
|
||||
processor_info_array_t cpuInfo;
|
||||
mach_msg_type_number_t numCpuInfo;
|
||||
*inUseAll = *totalAll = 0;
|
||||
|
||||
if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUs, &cpuInfo, &numCpuInfo) != KERN_SUCCESS)
|
||||
return "host_processor_info() failed";
|
||||
@@ -26,21 +26,3 @@ static const char* getCpuUsageInfo(long* inUseAll, long* totalAll)
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* ffGetCpuUsagePercent(double* result)
|
||||
{
|
||||
long inUseAll1 = 0, totalAll1 = 0;
|
||||
const char* error = getCpuUsageInfo(&inUseAll1, &totalAll1);
|
||||
if(error)
|
||||
return error;
|
||||
|
||||
sleep(1);
|
||||
|
||||
long inUseAll2 = 0, totalAll2 = 0;
|
||||
error = getCpuUsageInfo(&inUseAll2, &totalAll2);
|
||||
if(error)
|
||||
return error;
|
||||
|
||||
*result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
#include "fastfetch.h"
|
||||
#include "cpuUsage.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static const char* getCpuUsageInfo(FILE* procStat, long* inUseAll, long* totalAll)
|
||||
const char* ffGetCpuUsageInfo(long* inUseAll, long* totalAll)
|
||||
{
|
||||
long user, nice, system, idle, iowait, irq, softirq;
|
||||
|
||||
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)
|
||||
{
|
||||
fclose(procStat);
|
||||
@@ -15,32 +18,8 @@ static const char* getCpuUsageInfo(FILE* procStat, long* inUseAll, long* totalAl
|
||||
}
|
||||
*inUseAll = user + nice + system;
|
||||
*totalAll = *inUseAll + idle + iowait + irq + softirq;
|
||||
|
||||
fclose(procStat);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* ffGetCpuUsagePercent(double* result)
|
||||
{
|
||||
FILE* procStat = fopen("/proc/stat", "r");
|
||||
if(procStat == NULL)
|
||||
return "fopen(\"""/proc/stat\", \"r\") == NULL";
|
||||
|
||||
const char* error = NULL;
|
||||
long inUseAll1 = 0, totalAll1 = 0;
|
||||
error = getCpuUsageInfo(procStat, &inUseAll1, &totalAll1);
|
||||
if(error)
|
||||
goto exit;
|
||||
|
||||
sleep(1);
|
||||
rewind(procStat);
|
||||
|
||||
long inUseAll2 = 0, totalAll2 = 0;
|
||||
error = getCpuUsageInfo(procStat, &inUseAll2, &totalAll2);
|
||||
if(error)
|
||||
goto exit;
|
||||
|
||||
*result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
|
||||
|
||||
exit:
|
||||
fclose(procStat);
|
||||
return error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user