2023-03-17 17:56:31 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2024-01-20 11:28:38 +08:00
|
|
|
#include "common/percent.h"
|
2023-03-17 17:56:31 +08:00
|
|
|
#include "detection/cpuusage/cpuusage.h"
|
|
|
|
|
#include "modules/cpuusage/cpuusage.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-03-17 17:56:31 +08:00
|
|
|
|
|
|
|
|
#define FF_CPUUSAGE_DISPLAY_NAME "CPU Usage"
|
2024-07-08 15:21:12 +08:00
|
|
|
#define FF_CPUUSAGE_NUM_FORMAT_ARGS 8
|
2023-03-17 17:56:31 +08:00
|
|
|
|
2023-06-24 10:59:53 +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)
|
|
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%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;
|
|
|
|
|
|
2023-11-01 15:55:13 +08:00
|
|
|
uint32_t index = 0, valueCount = 0;
|
2023-10-10 16:00:23 +08:00
|
|
|
FF_LIST_FOR_EACH(double, percent, percentages)
|
|
|
|
|
{
|
2023-11-01 15:49:51 +08:00
|
|
|
if (*percent == *percent)
|
2023-10-10 16:00:23 +08:00
|
|
|
{
|
2023-11-01 15:49:51 +08:00
|
|
|
sumValue += *percent;
|
|
|
|
|
if (*percent > maxValue)
|
|
|
|
|
{
|
|
|
|
|
maxValue = *percent;
|
|
|
|
|
maxIndex = index;
|
|
|
|
|
}
|
|
|
|
|
if (*percent < minValue)
|
|
|
|
|
{
|
|
|
|
|
minValue = *percent;
|
|
|
|
|
minIndex = index;
|
|
|
|
|
}
|
2023-11-01 15:55:13 +08:00
|
|
|
++valueCount;
|
2023-10-10 16:00:23 +08:00
|
|
|
}
|
|
|
|
|
++index;
|
|
|
|
|
}
|
2023-11-01 15:55:13 +08:00
|
|
|
double avgValue = sumValue / (double) valueCount;
|
2023-10-10 16:00:23 +08:00
|
|
|
|
2023-03-17 17:56:31 +08:00
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2023-03-17 17:56:31 +08:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
2023-10-10 19:01:48 +08:00
|
|
|
if (!options->separate)
|
2023-03-17 17:56:31 +08:00
|
|
|
{
|
2023-10-26 10:26:15 +08:00
|
|
|
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
2024-05-08 15:25:59 +08:00
|
|
|
ffPercentAppendBar(&str, avgValue, options->percent, &options->moduleArgs);
|
2023-10-26 10:26:15 +08:00
|
|
|
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
2023-10-10 16:00:23 +08:00
|
|
|
{
|
|
|
|
|
if(str.length > 0)
|
|
|
|
|
ffStrbufAppendC(&str, ' ');
|
2024-05-08 15:25:59 +08:00
|
|
|
ffPercentAppendNum(&str, avgValue, options->percent, str.length > 0, &options->moduleArgs);
|
2023-10-10 16:00:23 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FF_LIST_FOR_EACH(double, percent, percentages)
|
|
|
|
|
{
|
|
|
|
|
if(str.length > 0)
|
|
|
|
|
ffStrbufAppendC(&str, ' ');
|
2024-05-08 15:25:59 +08:00
|
|
|
ffPercentAppendNum(&str, *percent, options->percent, false, &options->moduleArgs);
|
2023-10-10 16:00:23 +08:00
|
|
|
}
|
2023-03-17 17:56:31 +08:00
|
|
|
}
|
|
|
|
|
ffStrbufPutTo(&str, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-08 15:21:12 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY avgNum = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendNum(&avgNum, avgValue, options->percent, false, &options->moduleArgs);
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY avgBar = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendBar(&avgBar, avgValue, options->percent, &options->moduleArgs);
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY minNum = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendNum(&minNum, minValue, options->percent, false, &options->moduleArgs);
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY minBar = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendBar(&minBar, minValue, options->percent, &options->moduleArgs);
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY maxNum = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendNum(&maxNum, maxValue, options->percent, false, &options->moduleArgs);
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY maxBar = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendBar(&maxBar, maxValue, options->percent, &options->moduleArgs);
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_CPUUSAGE_NUM_FORMAT_ARGS, ((FFformatarg[]){
|
2024-07-08 15:21:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &avgNum, "avg"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &maxNum, "max"},
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &maxIndex, "max-index"},
|
2024-07-08 15:21:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &minNum, "min"},
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &minIndex, "min-index"},
|
2024-07-08 15:21:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &avgBar, "avg-bar"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &maxBar, "max-bar"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &minBar, "min-bar"},
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
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;
|
|
|
|
|
|
2023-10-10 19:01:48 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "separate"))
|
2023-10-10 16:00:23 +08:00
|
|
|
{
|
2023-10-10 19:01:48 +08:00
|
|
|
options->separate = ffOptionParseBoolean(value);
|
2023-10-10 16:00:23 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-20 18:40:12 +08:00
|
|
|
if (ffPercentParseCommandOptions(key, subKey, value, &options->percent))
|
|
|
|
|
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
|
|
|
|
2023-10-10 19:01:48 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "separate"))
|
2023-10-10 16:00:23 +08:00
|
|
|
{
|
2023-10-10 19:01:48 +08:00
|
|
|
options->separate = yyjson_get_bool(val);
|
2023-10-10 16:00:23 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-20 18:40:12 +08:00
|
|
|
if (ffPercentParseJsonObject(key, val, &options->percent))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_CPUUSAGE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "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);
|
2024-01-20 18:40:12 +08:00
|
|
|
|
|
|
|
|
ffPercentGenerateJsonConfig(doc, module, defaultOptions.percent, options->percent);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_CPUUSAGE_MODULE_NAME, "{1}", FF_CPUUSAGE_NUM_FORMAT_ARGS, ((const char* []) {
|
2024-07-08 15:21:12 +08:00
|
|
|
"CPU usage (percentage num, average) - avg",
|
|
|
|
|
"CPU usage (percentage num, maximum) - max",
|
2024-05-27 16:41:37 +08:00
|
|
|
"CPU core index of maximum usage - max-index",
|
2024-07-08 15:21:12 +08:00
|
|
|
"CPU usage (percentage num, minimum) - min",
|
2024-05-27 16:41:37 +08:00
|
|
|
"CPU core index of minimum usage - min-index",
|
2024-07-08 15:21:12 +08:00
|
|
|
"CPU usage (percentage bar, average) - avg-bar",
|
|
|
|
|
"CPU usage (percentage bar, maximum) - max-bar",
|
|
|
|
|
"CPU usage (percentage bar, minimum) - min-bar",
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-10-07 13:40:40 +08:00
|
|
|
}
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitCPUUsageOptions(FFCPUUsageOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_CPUUSAGE_MODULE_NAME,
|
2023-11-17 14:12:17 +08:00
|
|
|
"Print CPU usage. Costs some time to collect data",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParseCPUUsageCommandOptions,
|
|
|
|
|
ffParseCPUUsageJsonObject,
|
|
|
|
|
ffPrintCPUUsage,
|
|
|
|
|
ffGenerateCPUUsageJsonResult,
|
|
|
|
|
ffPrintCPUUsageHelpFormat,
|
2023-10-24 14:41:29 +08:00
|
|
|
ffGenerateCPUUsageJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2024-07-23 23:02:35 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2023-11-03 17:03:14 +02:00
|
|
|
options->separate = false;
|
2024-02-26 15:12:04 +08:00
|
|
|
options->percent = (FFColorRangeConfig) { 50, 80 };
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|