mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
CpuUsage: improve performance
by moving the first `/proc/stat` query earlier
This commit is contained in:
@@ -358,6 +358,8 @@ static void exitSignalHandler(int signal)
|
||||
|
||||
void ffStart(FFinstance* instance)
|
||||
{
|
||||
ffPrepareCPUUsage();
|
||||
|
||||
if(instance->config.multithreading)
|
||||
startDetectionThreads(instance);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -235,6 +235,7 @@ void ffLogoBuiltinListAutocompletion();
|
||||
//Common
|
||||
|
||||
void ffPrintDateTimeFormat(FFinstance* instance, const char* moduleName, const FFModuleArgs* moduleArgs);
|
||||
void ffPrepareCPUUsage();
|
||||
|
||||
//Printing
|
||||
|
||||
|
||||
+1
-1
@@ -34,7 +34,6 @@ int main(int argc, char** argv)
|
||||
ffPrintTerminal(&instance);
|
||||
ffPrintTerminalFont(&instance);
|
||||
ffPrintCPU(&instance);
|
||||
//ffPrintCPUUsage(&instance);
|
||||
ffPrintGPU(&instance);
|
||||
ffPrintMemory(&instance);
|
||||
//ffPrintSwap(&instance);
|
||||
@@ -45,6 +44,7 @@ int main(int argc, char** argv)
|
||||
//ffPrintSong(&instance);
|
||||
//ffPrintLocalIp(&instance);
|
||||
//ffPrintPublicIp(&instance);
|
||||
//ffPrintCPUUsage(&instance);
|
||||
ffPrintLocale(&instance);
|
||||
//ffPrintDateTime(&instance);
|
||||
//ffPrintDate(&instance);
|
||||
|
||||
+36
-2
@@ -1,21 +1,55 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/cpuUsage/cpuUsage.h"
|
||||
#include "sys/time.h"
|
||||
|
||||
#define FF_CPU_USAGE_MODULE_NAME "CPU Usage"
|
||||
#define FF_CPU_USAGE_NUM_FORMAT_ARGS 1
|
||||
|
||||
time_t getTimeInMs()
|
||||
{
|
||||
struct timeval timeNow;
|
||||
gettimeofday(&timeNow, NULL);
|
||||
return (timeNow.tv_sec * 1000) + (timeNow.tv_usec / 1000);
|
||||
}
|
||||
|
||||
static long inUseAll1, totalAll1, startTime;
|
||||
|
||||
void ffPrepareCPUUsage()
|
||||
{
|
||||
startTime = getTimeInMs();
|
||||
ffGetCpuUsageInfo(&inUseAll1, &totalAll1);
|
||||
}
|
||||
|
||||
void ffPrintCPUUsage(FFinstance* instance)
|
||||
{
|
||||
double cpuPercent = 0;
|
||||
const char* error = ffGetCpuUsagePercent(&cpuPercent);
|
||||
const char* error = NULL;
|
||||
if(startTime == 0)
|
||||
{
|
||||
error = ffGetCpuUsageInfo(&inUseAll1, &totalAll1);
|
||||
if(error)
|
||||
goto error;
|
||||
nanosleep(&(struct timespec){ 1, 0 }, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
time_t duration = getTimeInMs() - startTime;
|
||||
if(duration < 1000)
|
||||
nanosleep(&(struct timespec){ 0, (1000 - duration) * 1000000L }, NULL);
|
||||
}
|
||||
|
||||
long inUseAll2, totalAll2;
|
||||
error = ffGetCpuUsageInfo(&inUseAll2, &totalAll2);
|
||||
|
||||
if(error)
|
||||
{
|
||||
error:
|
||||
ffPrintError(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpu, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
double cpuPercent = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
|
||||
|
||||
if(instance->config.cpuUsage.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsage.key);
|
||||
|
||||
@@ -157,6 +157,16 @@ static inline FF_C_NODISCARD int ffStrbufIgnCaseComp(const FFstrbuf* strbuf, con
|
||||
return ffStrbufIgnCaseCompS(strbuf, comp->chars);
|
||||
}
|
||||
|
||||
static inline FF_C_NODISCARD bool ffStrbufContainS(const FFstrbuf* strbuf, const char* str)
|
||||
{
|
||||
return strnstr(strbuf->chars, str, strbuf->length) != NULL;
|
||||
}
|
||||
|
||||
static inline FF_C_NODISCARD bool ffStrbufContainIgnCaseS(const FFstrbuf* strbuf, const char* str)
|
||||
{
|
||||
return strcasestr(strbuf->chars, str) != NULL;
|
||||
}
|
||||
|
||||
static inline FF_C_NODISCARD uint32_t ffStrbufFirstIndexC(const FFstrbuf* strbuf, char c)
|
||||
{
|
||||
return ffStrbufNextIndexC(strbuf, 0, c);
|
||||
|
||||
Reference in New Issue
Block a user