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"
|
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"
|
|
|
|
|
#define FF_CPUUSAGE_NUM_FORMAT_ARGS 1
|
|
|
|
|
|
|
|
|
|
void ffPrintCPUUsage(FFinstance* instance, FFCPUUsageOptions* options)
|
|
|
|
|
{
|
|
|
|
|
double percentage = 0.0/0.0;
|
|
|
|
|
const char* error = ffGetCpuUsageResult(&percentage);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(instance, FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, "%s", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-06-18 12:32:47 +08:00
|
|
|
ffPrintLogoAndKey(instance, FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor);
|
2023-03-17 17:56:31 +08:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
2023-03-17 17:56:31 +08:00
|
|
|
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
|
|
|
|
ffAppendPercentBar(instance, &str, (uint8_t)percentage, 0, 5, 8);
|
|
|
|
|
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
|
|
|
|
{
|
|
|
|
|
if(str.length > 0)
|
|
|
|
|
ffStrbufAppendC(&str, ' ');
|
|
|
|
|
ffAppendPercentNum(instance, &str, (uint8_t) percentage, 50, 80, str.length > 0);
|
|
|
|
|
}
|
|
|
|
|
ffStrbufPutTo(&str, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffPrintFormat(instance, FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_CPUUSAGE_NUM_FORMAT_ARGS, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_DOUBLE, &percentage}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitCPUUsageOptions(FFCPUUsageOptions* options)
|
|
|
|
|
{
|
|
|
|
|
options->moduleName = FF_CPUUSAGE_MODULE_NAME;
|
|
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 15:01:54 +08:00
|
|
|
void ffParseCPUUsageJsonObject(FFinstance* instance, yyjson_val* module)
|
2023-03-17 17:56:31 +08:00
|
|
|
{
|
|
|
|
|
FFCPUUsageOptions __attribute__((__cleanup__(ffDestroyCPUUsageOptions))) options;
|
|
|
|
|
ffInitCPUUsageOptions(&options);
|
|
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
|
{
|
2023-06-10 15:01:54 +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-06-10 15:01:54 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
2023-06-10 15:01:54 +08:00
|
|
|
continue;
|
|
|
|
|
|
2023-03-17 17:56:31 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ffPrintError(instance, FF_CPUUSAGE_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffPrintCPUUsage(instance, &options);
|
|
|
|
|
}
|