Files
fastfetch/src/modules/cpuusage/cpuusage.c
T

185 lines
5.8 KiB
C
Raw Normal View History

2023-03-17 17:56:31 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-03-17 17:56:31 +08:00
#include "common/bar.h"
#include "detection/cpuusage/cpuusage.h"
#include "modules/cpuusage/cpuusage.h"
#include "util/stringUtils.h"
2023-03-17 17:56:31 +08:00
#define FF_CPUUSAGE_DISPLAY_NAME "CPU Usage"
2023-10-10 16:00:23 +08:00
#define FF_CPUUSAGE_NUM_FORMAT_ARGS 5
2023-03-17 17:56:31 +08:00
void ffPrintCPUUsage(FFCPUUsageOptions* options)
2023-03-17 17:56:31 +08:00
{
2023-10-10 16:00:23 +08:00
FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double));
const char* error = ffGetCpuUsageResult(&percentages);
2023-03-17 17:56:31 +08:00
if(error)
{
ffPrintError(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, "%s", error);
2023-03-17 17:56:31 +08:00
return;
}
2023-10-10 16:00:23 +08:00
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;
2023-03-17 17:56:31 +08:00
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-03-17 17:56:31 +08:00
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if (!options->separate)
2023-03-17 17:56:31 +08:00
{
2023-10-10 16:00:23 +08:00
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);
}
2023-03-17 17:56:31 +08:00
}
ffStrbufPutTo(&str, stdout);
}
else
{
2023-10-10 16:00:23 +08:00
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[]){
2023-10-10 16:00:23 +08:00
{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},
2023-03-17 17:56:31 +08:00
});
}
}
bool ffParseCPUUsageCommandOptions(FFCPUUsageOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_CPUUSAGE_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
if (ffStrEqualsIgnCase(subKey, "separate"))
2023-10-10 16:00:23 +08:00
{
options->separate = ffOptionParseBoolean(value);
2023-10-10 16:00:23 +08:00
return true;
}
2023-03-17 17:56:31 +08:00
return false;
}
2023-08-17 10:56:54 +08:00
void ffParseCPUUsageJsonObject(FFCPUUsageOptions* options, yyjson_val* module)
2023-03-17 17:56:31 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-03-17 17:56:31 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
2023-06-10 15:01:54 +08:00
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-03-17 17:56:31 +08:00
if (ffStrEqualsIgnCase(key, "separate"))
2023-10-10 16:00:23 +08:00
{
options->separate = yyjson_get_bool(val);
2023-10-10 16:00:23 +08:00
continue;
}
2023-08-17 16:09:41 +08:00
ffPrintError(FF_CPUUSAGE_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-03-17 17:56:31 +08:00
}
}
2023-08-30 15:38:29 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateCPUUsageJsonConfig(FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyCPUUsageOptions))) FFCPUUsageOptions defaultOptions;
ffInitCPUUsageOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
if (options->separate != defaultOptions.separate)
yyjson_mut_obj_add_bool(doc, module, "separate", options->separate);
}
2023-10-23 10:14:22 +08:00
void ffGenerateCPUUsageJsonResult(FF_MAYBE_UNUSED FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-08-30 15:38:29 +08:00
{
2023-10-10 16:00:23 +08:00
FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double));
const char* error = ffGetCpuUsageResult(&percentages);
2023-08-30 15:38:29 +08:00
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return;
}
2023-10-10 16:00:23 +08:00
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);
}
2023-08-30 15:38:29 +08:00
}
2023-10-07 13:40:40 +08:00
void ffPrintCPUUsageHelpFormat(void)
{
ffPrintModuleFormatHelp(FF_CPUUSAGE_MODULE_NAME, "{1}", FF_CPUUSAGE_NUM_FORMAT_ARGS, (const char* []) {
2023-10-10 16:00:23 +08:00
"CPU usage (percentage, average)",
"CPU usage (percentage, maximum)",
"CPU core index of maximum usage",
"CPU usage (percentage, minimum)",
"CPU core index of minimum usage",
2023-10-07 13:40:40 +08:00
});
}
2023-10-23 13:32:18 +08:00
void ffInitCPUUsageOptions(FFCPUUsageOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_CPUUSAGE_MODULE_NAME,
ffParseCPUUsageCommandOptions,
ffParseCPUUsageJsonObject,
ffPrintCPUUsage,
ffGenerateCPUUsageJsonResult,
ffPrintCPUUsageHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateCPUUsageJsonConfig
);
2023-10-23 13:32:18 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}