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
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintCPUUsage(FFCPUUsageOptions* options)
|
2023-03-17 17:56:31 +08:00
|
|
|
{
|
|
|
|
|
double percentage = 0.0/0.0;
|
|
|
|
|
const char* error = ffGetCpuUsageResult(&percentage);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, "%s", error);
|
2023-03-17 17:56:31 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-06-24 10:59:53 +08:00
|
|
|
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
2023-08-16 16:46:21 +08:00
|
|
|
ffAppendPercentBar(&str, percentage, 0, 50, 80);
|
2023-06-24 10:59:53 +08:00
|
|
|
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
2023-03-17 17:56:31 +08:00
|
|
|
{
|
|
|
|
|
if(str.length > 0)
|
|
|
|
|
ffStrbufAppendC(&str, ' ');
|
2023-08-16 16:46:21 +08:00
|
|
|
ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0);
|
2023-03-17 17:56:31 +08:00
|
|
|
}
|
|
|
|
|
ffStrbufPutTo(&str, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-24 09:12:24 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY percentageStr = ffStrbufCreate();
|
|
|
|
|
ffAppendPercentNum(&percentageStr, percentage, 50, 80, false);
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintFormat(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_CPUUSAGE_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2023-08-24 09:12:24 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &percentageStr}
|
2023-03-17 17:56:31 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitCPUUsageOptions(FFCPUUsageOptions* options)
|
|
|
|
|
{
|
2023-08-29 11:13:55 +08:00
|
|
|
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CPUUSAGE_MODULE_NAME, ffParseCPUUsageCommandOptions, ffParseCPUUsageJsonObject, ffPrintCPUUsage, NULL);
|
2023-03-17 17:56:31 +08:00
|
|
|
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-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-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
|
|
|
}
|
|
|
|
|
}
|