From db6ad5a268dee2b88ce6a109d8a9df48b44cb9a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 27 Sep 2022 22:06:53 +0800 Subject: [PATCH] CpuUsage: improve performance by moving the first `/proc/stat` query earlier --- src/common/init.c | 2 ++ src/detection/cpuUsage/cpuUsage.h | 2 +- src/detection/cpuUsage/cpuUsage_apple.c | 22 ++------------ src/detection/cpuUsage/cpuUsage_linux.c | 37 ++++++------------------ src/fastfetch.h | 1 + src/flashfetch.c | 2 +- src/modules/cpuUsage.c | 38 +++++++++++++++++++++++-- src/util/FFstrbuf.h | 10 +++++++ 8 files changed, 61 insertions(+), 53 deletions(-) diff --git a/src/common/init.c b/src/common/init.c index 6ed30b6e4..53fe6467a 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -358,6 +358,8 @@ static void exitSignalHandler(int signal) void ffStart(FFinstance* instance) { + ffPrepareCPUUsage(); + if(instance->config.multithreading) startDetectionThreads(instance); diff --git a/src/detection/cpuUsage/cpuUsage.h b/src/detection/cpuUsage/cpuUsage.h index 577d2a374..cddd1866b 100644 --- a/src/detection/cpuUsage/cpuUsage.h +++ b/src/detection/cpuUsage/cpuUsage.h @@ -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 diff --git a/src/detection/cpuUsage/cpuUsage_apple.c b/src/detection/cpuUsage/cpuUsage_apple.c index 49f1d34c8..e170a7ceb 100644 --- a/src/detection/cpuUsage/cpuUsage_apple.c +++ b/src/detection/cpuUsage/cpuUsage_apple.c @@ -3,13 +3,13 @@ #include #include -#include -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; -} diff --git a/src/detection/cpuUsage/cpuUsage_linux.c b/src/detection/cpuUsage/cpuUsage_linux.c index 499759a82..3696a8d77 100644 --- a/src/detection/cpuUsage/cpuUsage_linux.c +++ b/src/detection/cpuUsage/cpuUsage_linux.c @@ -1,13 +1,16 @@ #include "fastfetch.h" #include "cpuUsage.h" -#include #include -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; -} diff --git a/src/fastfetch.h b/src/fastfetch.h index 1a2872e30..b4ea8b055 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -235,6 +235,7 @@ void ffLogoBuiltinListAutocompletion(); //Common void ffPrintDateTimeFormat(FFinstance* instance, const char* moduleName, const FFModuleArgs* moduleArgs); +void ffPrepareCPUUsage(); //Printing diff --git a/src/flashfetch.c b/src/flashfetch.c index 296d589f0..15609669c 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -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); diff --git a/src/modules/cpuUsage.c b/src/modules/cpuUsage.c index fe655af99..8ad2cf1dd 100644 --- a/src/modules/cpuUsage.c +++ b/src/modules/cpuUsage.c @@ -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); diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index 23f2e48f1..ec985b84f 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -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);