From 42cf88be9dab87154603e50f729f09bc80dcee8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 10 Oct 2023 16:00:23 +0800 Subject: [PATCH] CPUUsage: support display value per core --- src/detection/cpuusage/cpuusage.c | 58 +++++++----- src/detection/cpuusage/cpuusage.h | 10 +- src/detection/cpuusage/cpuusage_apple.c | 14 ++- src/detection/cpuusage/cpuusage_bsd.c | 12 ++- src/detection/cpuusage/cpuusage_linux.c | 13 +-- src/detection/cpuusage/cpuusage_windows.c | 15 +-- src/modules/cpuusage/cpuusage.c | 106 ++++++++++++++++++---- src/modules/cpuusage/option.h | 7 ++ src/util/FFlist.h | 5 + 9 files changed, 179 insertions(+), 61 deletions(-) diff --git a/src/detection/cpuusage/cpuusage.c b/src/detection/cpuusage/cpuusage.c index 563c6fae5..040d68780 100644 --- a/src/detection/cpuusage/cpuusage.c +++ b/src/detection/cpuusage/cpuusage.c @@ -4,42 +4,54 @@ #include -// We need to use uint64_t because sizeof(long) == 4 on Windows -const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll); - -static uint64_t inUseAll1, totalAll1; +static FFlist cpuTimes1; void ffPrepareCPUUsage(void) { - ffGetCpuUsageInfo(&inUseAll1, &totalAll1); + assert(cpuTimes1.elementSize == 0); + ffListInit(&cpuTimes1, sizeof(FFCpuUsageInfo)); + ffGetCpuUsageInfo(&cpuTimes1); } -const char* ffGetCpuUsageResult(double* result) +const char* ffGetCpuUsageResult(FFlist* result) { const char* error = NULL; - if(inUseAll1 == 0 && totalAll1 == 0) + if(cpuTimes1.elementSize == 0) { - error = ffGetCpuUsageInfo(&inUseAll1, &totalAll1); - if(error) - return error; + ffListInit(&cpuTimes1, sizeof(FFCpuUsageInfo)); + error = ffGetCpuUsageInfo(&cpuTimes1); + if(error) return error; ffTimeSleep(200); } - while(true) - { - uint64_t inUseAll2, totalAll2; - error = ffGetCpuUsageInfo(&inUseAll2, &totalAll2); - if(error) - return error; + if(cpuTimes1.length == 0) return "No CPU cores found"; - if(inUseAll2 != inUseAll1) + FF_LIST_AUTO_DESTROY cpuTimes2 = ffListCreate(sizeof(FFCpuUsageInfo)); + +retry: + error = ffGetCpuUsageInfo(&cpuTimes2); + if(error) return error; + if(cpuTimes1.length != cpuTimes2.length) return "Unexpected CPU usage result"; + + for (uint32_t i = 0; i < cpuTimes1.length; ++i) + { + FFCpuUsageInfo* cpuTime1 = ffListGet(&cpuTimes1, i); + FFCpuUsageInfo* cpuTime2 = ffListGet(&cpuTimes2, i); + if (cpuTime2->totalAll <= cpuTime1->totalAll) { - *result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100; - inUseAll1 = inUseAll2; - totalAll1 = totalAll2; - return NULL; - } - else + ffListClear(&cpuTimes2); ffTimeSleep(200); + goto retry; + } } + + for (uint32_t i = 0; i < cpuTimes1.length; ++i) + { + FFCpuUsageInfo* cpuTime1 = ffListGet(&cpuTimes1, i); + FFCpuUsageInfo* cpuTime2 = ffListGet(&cpuTimes2, i); + *(double*) ffListAdd(result) = (double)(cpuTime2->inUseAll - cpuTime1->inUseAll) / (double)(cpuTime2->totalAll - cpuTime1->totalAll) * 100; + cpuTime1->inUseAll = cpuTime2->inUseAll; + cpuTime1->totalAll = cpuTime2->totalAll; + } + return NULL; } diff --git a/src/detection/cpuusage/cpuusage.h b/src/detection/cpuusage/cpuusage.h index fb3c81e8e..655239350 100644 --- a/src/detection/cpuusage/cpuusage.h +++ b/src/detection/cpuusage/cpuusage.h @@ -3,6 +3,14 @@ #ifndef FF_INCLUDED_detection_cpuusage_cpuusage #define FF_INCLUDED_detection_cpuusage_cpuusage -const char* ffGetCpuUsageResult(double* result); +#include "fastfetch.h" + +typedef struct FFCpuUsageInfo { + uint64_t inUseAll; + uint64_t totalAll; +} FFCpuUsageInfo; +const char* ffGetCpuUsageInfo(FFlist* cpuTimes); + +const char* ffGetCpuUsageResult(FFlist* result); // list of double #endif diff --git a/src/detection/cpuusage/cpuusage_apple.c b/src/detection/cpuusage/cpuusage_apple.c index d0dd42920..513b5e930 100644 --- a/src/detection/cpuusage/cpuusage_apple.c +++ b/src/detection/cpuusage/cpuusage_apple.c @@ -4,25 +4,29 @@ #include #include -const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) +const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { 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"; if (numCPUs * CPU_STATE_MAX != numCpuInfo) return "Unexpected host_processor_info() result"; - for (natural_t i = 0U; i < numCPUs; ++i) { + for (natural_t i = 0U; i < numCPUs; ++i) + { integer_t inUse = cpuInfo[CPU_STATE_MAX * i + CPU_STATE_USER] + 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 += (uint64_t)inUse; - *totalAll += (uint64_t)total; + + FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + *info = (FFCpuUsageInfo) { + .inUseAll = (uint64_t)inUse, + .totalAll = (uint64_t)total, + }; } return NULL; } diff --git a/src/detection/cpuusage/cpuusage_bsd.c b/src/detection/cpuusage/cpuusage_bsd.c index 8f888491b..6c58832e0 100644 --- a/src/detection/cpuusage/cpuusage_bsd.c +++ b/src/detection/cpuusage/cpuusage_bsd.c @@ -6,13 +6,13 @@ #include #include -const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) +const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { size_t neededLength = 0; if(sysctlbyname("kern.cp_times", NULL, &neededLength, NULL, 0) != 0) return "sysctlbyname(kern.cp_times, NULL) failed"; - uint32_t coreCount = neededLength / (CPUSTATES * sizeof(uint64_t)); + uint32_t coreCount = neededLength / (CPUSTATES * (uint32_t) sizeof(uint64_t)); assert(coreCount > 0); FF_AUTO_FREE uint64_t (*cpTimes)[CPUSTATES] = malloc(neededLength); @@ -24,8 +24,12 @@ const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) uint64_t* cpTime = cpTimes[i]; uint64_t inUse = cpTime[CP_USER] + cpTime[CP_NICE] + cpTime[CP_SYS]; uint64_t total = cpTime[CP_INTR] + cpTime[CP_IDLE]; - *inUseAll += inUse; - *totalAll += total; + + FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + *info = (FFCpuUsageInfo) { + .inUseAll = (uint64_t)inUse, + .totalAll = (uint64_t)total, + }; } return NULL; diff --git a/src/detection/cpuusage/cpuusage_linux.c b/src/detection/cpuusage/cpuusage_linux.c index ac73d39c8..85b63c450 100644 --- a/src/detection/cpuusage/cpuusage_linux.c +++ b/src/detection/cpuusage/cpuusage_linux.c @@ -5,7 +5,7 @@ #include #include -const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) +const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { FF_AUTO_CLOSE_FILE FILE* procStat = fopen("/proc/stat", "r"); if(procStat == NULL) @@ -20,16 +20,17 @@ const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) if (fscanf(procStat, "cpu%*[^\n]\n") < 0) return "fscanf() first line failed"; - *inUseAll = 0; - *totalAll = 0; - uint64_t user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0; while (fscanf(procStat, "cpu%*d%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64, &user, &nice, &system, &idle, &iowait, &irq, &softirq) == 7) { uint64_t inUse = user + nice + system; uint64_t total = inUse + idle + iowait + irq + softirq; - *inUseAll += inUse; - *totalAll += total; + + FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + *info = (FFCpuUsageInfo) { + .inUseAll = (uint64_t)inUse, + .totalAll = (uint64_t)total, + }; } return NULL; diff --git a/src/detection/cpuusage/cpuusage_windows.c b/src/detection/cpuusage/cpuusage_windows.c index a22250d83..cf513340e 100644 --- a/src/detection/cpuusage/cpuusage_windows.c +++ b/src/detection/cpuusage/cpuusage_windows.c @@ -6,7 +6,7 @@ #include #include -const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) +const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { ULONG size = 0; if(NtQuerySystemInformation(SystemProcessorPerformanceInformation, NULL, 0, &size) != STATUS_INFO_LENGTH_MISMATCH) @@ -16,8 +16,6 @@ const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) if(!NT_SUCCESS(NtQuerySystemInformation(SystemProcessorPerformanceInformation, pinfo, size, &size))) return "NtQuerySystemInformation(SystemProcessorPerformanceInformation, size) failed"; - *inUseAll = *totalAll = 0; - for (uint32_t i = 0; i < size / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION); ++i) { SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* coreInfo = pinfo + i; @@ -28,9 +26,14 @@ const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) coreInfo->KernelTime.QuadPart -= coreInfo->IdleTime.QuadPart; coreInfo->KernelTime.QuadPart += dpcTime + interruptTime; - LONGLONG inUse = coreInfo->UserTime.QuadPart + coreInfo->KernelTime.QuadPart; - *inUseAll += (uint64_t)inUse; - *totalAll += (uint64_t)(inUse + coreInfo->IdleTime.QuadPart); + uint64_t inUse = (uint64_t) (coreInfo->UserTime.QuadPart + coreInfo->KernelTime.QuadPart); + uint64_t total = inUse + (uint64_t)coreInfo->IdleTime.QuadPart; + + FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + *info = (FFCpuUsageInfo) { + .inUseAll = (uint64_t)inUse, + .totalAll = (uint64_t)total, + }; } return NULL; diff --git a/src/modules/cpuusage/cpuusage.c b/src/modules/cpuusage/cpuusage.c index f658d832f..26b24e8ec 100644 --- a/src/modules/cpuusage/cpuusage.c +++ b/src/modules/cpuusage/cpuusage.c @@ -6,12 +6,12 @@ #include "util/stringUtils.h" #define FF_CPUUSAGE_DISPLAY_NAME "CPU Usage" -#define FF_CPUUSAGE_NUM_FORMAT_ARGS 1 +#define FF_CPUUSAGE_NUM_FORMAT_ARGS 5 void ffPrintCPUUsage(FFCPUUsageOptions* options) { - double percentage = 0.0/0.0; - const char* error = ffGetCpuUsageResult(&percentage); + FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double)); + const char* error = ffGetCpuUsageResult(&percentages); if(error) { @@ -19,27 +19,68 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options) return; } + double maxValue = -999, minValue = 999, sumValue = 0; + uint32_t maxIndex = 999, minIndex = 999; + + uint32_t index = 0; + FF_LIST_FOR_EACH(double, percent, percentages) + { + sumValue += *percent; + if (*percent > maxValue) + { + maxValue = *percent; + maxIndex = index; + } + if (*percent < minValue) + { + minValue = *percent; + minIndex = index; + } + ++index; + } + double avgValue = sumValue / (double) percentages.length; + if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate(); - if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) - ffAppendPercentBar(&str, percentage, 0, 50, 80); - if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) + if (options->displayType == FF_CPUUSAGE_DISPLAY_TYPE_DEFAULT) { - if(str.length > 0) - ffStrbufAppendC(&str, ' '); - ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0); + if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) + ffAppendPercentBar(&str, avgValue, 0, 50, 80); + if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) + { + if(str.length > 0) + ffStrbufAppendC(&str, ' '); + ffAppendPercentNum(&str, avgValue, 50, 80, str.length > 0); + } + } + else + { + FF_LIST_FOR_EACH(double, percent, percentages) + { + if(str.length > 0) + ffStrbufAppendC(&str, ' '); + ffAppendPercentNum(&str, *percent, 50, 80, false); + } } ffStrbufPutTo(&str, stdout); } else { - FF_STRBUF_AUTO_DESTROY percentageStr = ffStrbufCreate(); - ffAppendPercentNum(&percentageStr, percentage, 50, 80, false); + FF_STRBUF_AUTO_DESTROY avgStr = ffStrbufCreate(); + ffAppendPercentNum(&avgStr, avgValue, 50, 80, false); + FF_STRBUF_AUTO_DESTROY minStr = ffStrbufCreate(); + ffAppendPercentNum(&minStr, minValue, 50, 80, false); + FF_STRBUF_AUTO_DESTROY maxStr = ffStrbufCreate(); + ffAppendPercentNum(&maxStr, maxValue, 50, 80, false); ffPrintFormat(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_CPUUSAGE_NUM_FORMAT_ARGS, (FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRBUF, &percentageStr} + {FF_FORMAT_ARG_TYPE_STRBUF, &avgStr}, + {FF_FORMAT_ARG_TYPE_STRBUF, &maxStr}, + {FF_FORMAT_ARG_TYPE_UINT, &maxIndex}, + {FF_FORMAT_ARG_TYPE_STRBUF, &minStr}, + {FF_FORMAT_ARG_TYPE_UINT, &minIndex}, }); } } @@ -57,6 +98,16 @@ bool ffParseCPUUsageCommandOptions(FFCPUUsageOptions* options, const char* key, if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) return true; + if (ffStrEqualsIgnCase(subKey, "display-type")) + { + options->displayType = (FFCPUUsageDisplayType) ffOptionParseEnum(key, value, (FFKeyValuePair[]) { + { "default", FF_CPUUSAGE_DISPLAY_TYPE_DEFAULT }, + { "separate", FF_CPUUSAGE_DISPLAY_TYPE_SEPARATE }, + {}, + }); + return true; + } + return false; } @@ -78,26 +129,49 @@ void ffParseCPUUsageJsonObject(FFCPUUsageOptions* options, yyjson_val* module) if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) continue; + if (ffStrEqualsIgnCase(key, "display-type")) + { + int value; + const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) { + { "default", FF_CPUUSAGE_DISPLAY_TYPE_DEFAULT }, + { "separate", FF_CPUUSAGE_DISPLAY_TYPE_SEPARATE }, + {}, + }); + if (error) + ffPrintErrorString(FF_CPUUSAGE_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Invalid %s value: %s", key, error); + else + options->displayType = (FFCPUUsageDisplayType) value; + continue; + } + ffPrintError(FF_CPUUSAGE_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } void ffGenerateCPUUsageJson(FF_MAYBE_UNUSED FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - double percentage = 0.0/0.0; - const char* error = ffGetCpuUsageResult(&percentage); + FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double)); + const char* error = ffGetCpuUsageResult(&percentages); if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); return; } - yyjson_mut_obj_add_real(doc, module, "result", percentage); + yyjson_mut_val* result = yyjson_mut_obj_add_arr(doc, module, "result"); + FF_LIST_FOR_EACH(double, percent, percentages) + { + yyjson_mut_arr_add_real(doc, result, *percent); + } } void ffPrintCPUUsageHelpFormat(void) { ffPrintModuleFormatHelp(FF_CPUUSAGE_MODULE_NAME, "{1}", FF_CPUUSAGE_NUM_FORMAT_ARGS, (const char* []) { - "CPU usage (percentage)" + "CPU usage (percentage, average)", + "CPU usage (percentage, maximum)", + "CPU core index of maximum usage", + "CPU usage (percentage, minimum)", + "CPU core index of minimum usage", }); } diff --git a/src/modules/cpuusage/option.h b/src/modules/cpuusage/option.h index 946855301..3a7069bce 100644 --- a/src/modules/cpuusage/option.h +++ b/src/modules/cpuusage/option.h @@ -4,8 +4,15 @@ #include "common/option.h" +typedef enum FFCPUUsageDisplayType { + FF_CPUUSAGE_DISPLAY_TYPE_DEFAULT, + FF_CPUUSAGE_DISPLAY_TYPE_SEPARATE, +} FFCPUUsageDisplayType; + typedef struct FFCPUUsageOptions { FFModuleBaseInfo moduleInfo; FFModuleArgs moduleArgs; + + FFCPUUsageDisplayType displayType; } FFCPUUsageOptions; diff --git a/src/util/FFlist.h b/src/util/FFlist.h index 4961adda3..276a65e13 100644 --- a/src/util/FFlist.h +++ b/src/util/FFlist.h @@ -90,6 +90,11 @@ static inline void ffListDestroy(FFlist* list) list->data = NULL; } +static inline void ffListClear(FFlist* list) +{ + list->length = 0; +} + #define FF_LIST_FOR_EACH(itemType, itemVarName, listVar) \ assert(sizeof(itemType) == (listVar).elementSize); \ for(itemType* itemVarName = (itemType*)(listVar).data; \