CPU: add JSON printing support

This commit is contained in:
李通洲
2023-08-30 14:46:04 +08:00
parent 01aa9b9516
commit 28e412b79d
3 changed files with 39 additions and 1 deletions
+3
View File
@@ -35,7 +35,10 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
{
if (ffSysctlGetString("machdep.cpu.brand_string", &cpu->name) != NULL)
return "sysctlbyname(machdep.cpu.brand_string) failed";
ffSysctlGetString("machdep.cpu.vendor", &cpu->vendor);
if (cpu->vendor.length == 0 && ffStrbufStartsWithS(&cpu->name, "Apple "))
ffStrbufAppendS(&cpu->vendor, "Apple");
cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.physicalcpu_max", 1);
if(cpu->coresPhysical == 1)
+35 -1
View File
@@ -79,7 +79,7 @@ void ffPrintCPU(FFCPUOptions* options)
void ffInitCPUOptions(FFCPUOptions* options)
{
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CPU_MODULE_NAME, ffParseCPUCommandOptions, ffParseCPUJsonObject, ffPrintCPU, NULL);
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CPU_MODULE_NAME, ffParseCPUCommandOptions, ffParseCPUJsonObject, ffPrintCPU, ffGenerateCPUJson);
ffOptionInitModuleArg(&options->moduleArgs);
options->temp = false;
}
@@ -127,3 +127,37 @@ void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module)
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
}
}
void ffGenerateCPUJson(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
FFCPUResult cpu;
cpu.temperature = FF_CPU_TEMP_UNSET;
cpu.coresPhysical = cpu.coresLogical = cpu.coresOnline = 0;
cpu.frequencyMax = cpu.frequencyMin = 0;
ffStrbufInit(&cpu.name);
ffStrbufInit(&cpu.vendor);
const char* error = ffDetectCPU(options, &cpu);
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
}
else if(cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1)
{
yyjson_mut_obj_add_str(doc, module, "error", "No CPU detected");
}
else
{
yyjson_mut_val* obj = yyjson_mut_obj(doc);
yyjson_mut_obj_add_val(doc, module, "result", obj);
yyjson_mut_obj_add_strbuf(doc, obj, "cpu", &cpu.name);
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &cpu.vendor);
yyjson_mut_obj_add_uint(doc, obj, "coresPhysical", cpu.coresPhysical);
yyjson_mut_obj_add_uint(doc, obj, "coresLogical", cpu.coresLogical);
yyjson_mut_obj_add_uint(doc, obj, "coresOnline", cpu.coresOnline);
yyjson_mut_obj_add_real(doc, obj, "frequencyMin", cpu.frequencyMin);
yyjson_mut_obj_add_real(doc, obj, "frequencyMax", cpu.frequencyMax);
yyjson_mut_obj_add_real(doc, obj, "temperature", cpu.temperature);
}
}
+1
View File
@@ -9,3 +9,4 @@ void ffInitCPUOptions(FFCPUOptions* options);
bool ffParseCPUCommandOptions(FFCPUOptions* options, const char* key, const char* value);
void ffDestroyCPUOptions(FFCPUOptions* options);
void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module);
void ffGenerateCPUJson(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);