2023-03-15 18:17:24 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-07-27 15:58:49 +08:00
|
|
|
#include "common/parsing.h"
|
2024-02-26 16:39:41 +08:00
|
|
|
#include "common/temps.h"
|
2023-03-15 18:17:24 +08:00
|
|
|
#include "detection/cpu/cpu.h"
|
|
|
|
|
#include "modules/cpu/cpu.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2024-05-18 09:58:04 +08:00
|
|
|
static int sortCores(const FFCPUCore* a, const FFCPUCore* b)
|
|
|
|
|
{
|
|
|
|
|
return (int)b->freq - (int)a->freq;
|
|
|
|
|
}
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintCPU(FFCPUOptions* options)
|
2023-03-15 18:17:24 +08:00
|
|
|
{
|
2024-05-17 16:14:35 +08:00
|
|
|
FFCPUResult cpu = {
|
|
|
|
|
.temperature = FF_CPU_TEMP_UNSET,
|
2024-07-17 10:56:30 +08:00
|
|
|
.frequencyMax = 0,
|
|
|
|
|
.frequencyBase = 0,
|
2024-05-17 16:14:35 +08:00
|
|
|
.name = ffStrbufCreate(),
|
2024-06-04 23:56:00 +08:00
|
|
|
.vendor = ffStrbufCreate(),
|
2024-05-17 16:14:35 +08:00
|
|
|
};
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
const char* error = ffDetectCPU(options, &cpu);
|
2023-06-12 09:56:03 +08:00
|
|
|
|
2023-06-17 15:23:05 +08:00
|
|
|
if(error)
|
|
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2023-06-17 15:23:05 +08:00
|
|
|
}
|
|
|
|
|
else if(cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1)
|
2023-03-15 18:17:24 +08:00
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No CPU detected");
|
2023-03-15 18:17:24 +08:00
|
|
|
}
|
2023-06-12 23:00:37 +08:00
|
|
|
else
|
2023-03-15 18:17:24 +08:00
|
|
|
{
|
2024-05-21 09:09:08 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY coreTypes = ffStrbufCreate();
|
|
|
|
|
if (options->showPeCoreCount)
|
|
|
|
|
{
|
|
|
|
|
uint32_t typeCount = 0;
|
2024-10-18 16:15:48 +08:00
|
|
|
while (cpu.coreTypes[typeCount].count != 0 && typeCount < ARRAY_SIZE(cpu.coreTypes)) typeCount++;
|
2024-05-21 09:09:08 +08:00
|
|
|
if (typeCount > 0)
|
|
|
|
|
{
|
|
|
|
|
qsort(cpu.coreTypes, typeCount, sizeof(cpu.coreTypes[0]), (void*) sortCores);
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < typeCount; i++)
|
|
|
|
|
ffStrbufAppendF(&coreTypes, "%s%u", i == 0 ? "" : "+", cpu.coreTypes[i].count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 23:00:37 +08:00
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
2023-03-15 18:17:24 +08:00
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2023-07-27 15:58:49 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
|
|
|
|
|
2024-11-26 21:37:00 +08:00
|
|
|
if(cpu.packages > 1)
|
|
|
|
|
ffStrbufAppendF(&str, "%u x ", cpu.packages);
|
2024-11-26 02:53:01 +02:00
|
|
|
|
2023-06-12 23:00:37 +08:00
|
|
|
if(cpu.name.length > 0)
|
2023-07-27 15:58:49 +08:00
|
|
|
ffStrbufAppend(&str, &cpu.name);
|
2023-06-12 23:00:37 +08:00
|
|
|
else if(cpu.vendor.length > 0)
|
|
|
|
|
{
|
2023-07-27 15:58:49 +08:00
|
|
|
ffStrbufAppend(&str, &cpu.vendor);
|
|
|
|
|
ffStrbufAppendS(&str, " CPU");
|
2023-06-12 23:00:37 +08:00
|
|
|
}
|
|
|
|
|
else
|
2023-07-27 15:58:49 +08:00
|
|
|
ffStrbufAppendS(&str, "Unknown");
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2024-05-21 09:09:08 +08:00
|
|
|
if(coreTypes.length > 0)
|
|
|
|
|
ffStrbufAppendF(&str, " (%s)", coreTypes.chars);
|
|
|
|
|
else if(cpu.coresOnline > 1)
|
2025-01-05 19:37:57 +08:00
|
|
|
ffStrbufAppendF(&str, " (%u)", cpu.coresOnline);
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2024-09-28 16:49:03 +08:00
|
|
|
uint32_t freq = cpu.frequencyMax;
|
2024-07-17 10:56:30 +08:00
|
|
|
if(freq == 0)
|
2024-03-14 18:45:08 +08:00
|
|
|
freq = cpu.frequencyBase;
|
2024-07-17 10:56:30 +08:00
|
|
|
if(freq > 0)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&str, " @ ");
|
2024-07-17 14:42:09 +08:00
|
|
|
ffParseFrequency(freq, &str);
|
2024-07-17 10:56:30 +08:00
|
|
|
}
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2023-06-12 23:00:37 +08:00
|
|
|
if(cpu.temperature == cpu.temperature) //FF_CPU_TEMP_UNSET
|
2023-07-27 15:58:49 +08:00
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&str, " - ");
|
2024-05-08 15:25:59 +08:00
|
|
|
ffTempsAppendNum(cpu.temperature, &str, options->tempConfig, &options->moduleArgs);
|
2023-07-27 15:58:49 +08:00
|
|
|
}
|
2023-06-12 23:00:37 +08:00
|
|
|
|
2023-07-27 15:58:49 +08:00
|
|
|
ffStrbufPutTo(&str, stdout);
|
2023-06-12 23:00:37 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-17 10:56:30 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY freqBase = ffStrbufCreate();
|
2024-07-17 14:42:09 +08:00
|
|
|
ffParseFrequency(cpu.frequencyBase, &freqBase);
|
2024-07-17 10:56:30 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY freqMax = ffStrbufCreate();
|
2024-07-29 16:44:54 +08:00
|
|
|
ffParseFrequency(cpu.frequencyMax, &freqMax);
|
2024-05-19 19:41:23 +08:00
|
|
|
|
2024-02-26 10:46:13 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
|
2024-05-08 15:25:59 +08:00
|
|
|
ffTempsAppendNum(cpu.temperature, &tempStr, options->tempConfig, &options->moduleArgs);
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
|
2024-09-03 14:52:40 +08:00
|
|
|
FF_FORMAT_ARG(cpu.name, "name"),
|
|
|
|
|
FF_FORMAT_ARG(cpu.vendor, "vendor"),
|
|
|
|
|
FF_FORMAT_ARG(cpu.coresPhysical, "cores-physical"),
|
|
|
|
|
FF_FORMAT_ARG(cpu.coresLogical, "cores-logical"),
|
|
|
|
|
FF_FORMAT_ARG(cpu.coresOnline, "cores-online"),
|
|
|
|
|
FF_FORMAT_ARG(freqBase, "freq-base"),
|
|
|
|
|
FF_FORMAT_ARG(freqMax, "freq-max"),
|
|
|
|
|
FF_FORMAT_ARG(tempStr, "temperature"),
|
|
|
|
|
FF_FORMAT_ARG(coreTypes, "core-types"),
|
2024-11-26 21:37:00 +08:00
|
|
|
FF_FORMAT_ARG(cpu.packages, "packages"),
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-06-12 23:00:37 +08:00
|
|
|
}
|
2023-03-15 18:17:24 +08:00
|
|
|
}
|
2023-06-12 23:00:37 +08:00
|
|
|
|
|
|
|
|
ffStrbufDestroy(&cpu.name);
|
|
|
|
|
ffStrbufDestroy(&cpu.vendor);
|
2023-03-15 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-21 09:09:08 +08:00
|
|
|
bool ffParseCPUCommandOptions(FFCPUOptions* options, const char* key, const char* value)
|
2023-03-15 18:17:24 +08:00
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_CPU_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
2024-02-26 16:39:41 +08:00
|
|
|
if (ffTempsParseCommandOptions(key, subKey, value, &options->temp, &options->tempConfig))
|
2023-03-15 18:17:24 +08:00
|
|
|
return true;
|
|
|
|
|
|
2024-05-21 09:09:08 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-pe-core-count"))
|
|
|
|
|
{
|
|
|
|
|
options->showPeCoreCount = ffOptionParseBoolean(value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 18:17:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module)
|
2023-03-15 18:17:24 +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-15 18:17:24 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
2023-03-15 18:17:24 +08:00
|
|
|
|
2024-02-26 16:39:41 +08:00
|
|
|
if (ffTempsParseJsonObject(key, val, &options->temp, &options->tempConfig))
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
|
|
|
|
|
2023-10-08 10:09:56 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "freqNdigits"))
|
|
|
|
|
{
|
2024-07-17 14:42:09 +08:00
|
|
|
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "modules.CPU.freqNdigits has been moved to display.freq.ndigits");
|
2023-10-08 10:09:56 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 09:09:08 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showPeCoreCount"))
|
|
|
|
|
{
|
|
|
|
|
options->showPeCoreCount = yyjson_get_bool(val);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
2023-03-15 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-30 14:46:04 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGenerateCPUJsonConfig(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyCPUOptions))) FFCPUOptions defaultOptions;
|
|
|
|
|
ffInitCPUOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
|
2024-02-26 16:39:41 +08:00
|
|
|
ffTempsGenerateJsonConfig(doc, module, defaultOptions.temp, defaultOptions.tempConfig, options->temp, options->tempConfig);
|
2023-10-24 14:41:29 +08:00
|
|
|
|
2024-05-21 09:09:08 +08:00
|
|
|
if (defaultOptions.showPeCoreCount != options->showPeCoreCount)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "showPeCoreCount", options->showPeCoreCount);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-08-30 14:46:04 +08:00
|
|
|
{
|
2024-05-17 16:14:35 +08:00
|
|
|
FFCPUResult cpu = {
|
|
|
|
|
.temperature = FF_CPU_TEMP_UNSET,
|
2024-07-17 10:56:30 +08:00
|
|
|
.frequencyMax = 0,
|
|
|
|
|
.frequencyBase = 0,
|
2024-05-17 16:14:35 +08:00
|
|
|
.name = ffStrbufCreate(),
|
2024-06-04 23:56:00 +08:00
|
|
|
.vendor = ffStrbufCreate(),
|
2024-05-17 16:14:35 +08:00
|
|
|
};
|
2023-08-30 14:46:04 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2023-08-31 13:54:46 +08:00
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
2023-08-30 14:46:04 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "cpu", &cpu.name);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &cpu.vendor);
|
2024-11-26 21:37:00 +08:00
|
|
|
if (cpu.packages == 0)
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "packages");
|
|
|
|
|
else
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "packages", cpu.packages);
|
2023-09-19 10:50:52 +08:00
|
|
|
|
|
|
|
|
yyjson_mut_val* cores = yyjson_mut_obj_add_obj(doc, obj, "cores");
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, cores, "physical", cpu.coresPhysical);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, cores, "logical", cpu.coresLogical);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, cores, "online", cpu.coresOnline);
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* frequency = yyjson_mut_obj_add_obj(doc, obj, "frequency");
|
2024-07-17 10:56:30 +08:00
|
|
|
yyjson_mut_obj_add_uint(doc, frequency, "base", cpu.frequencyBase);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, frequency, "max", cpu.frequencyMax);
|
2023-09-19 10:50:52 +08:00
|
|
|
|
2024-05-18 09:58:04 +08:00
|
|
|
yyjson_mut_val* coreTypes = yyjson_mut_obj_add_arr(doc, obj, "coreTypes");
|
|
|
|
|
for (uint32_t i = 0; i < sizeof (cpu.coreTypes) / sizeof (cpu.coreTypes[0]) && cpu.coreTypes[i].count > 0; i++)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* core = yyjson_mut_arr_add_obj(doc, coreTypes);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, core, "count", cpu.coreTypes[i].count);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, core, "freq", cpu.coreTypes[i].freq);
|
|
|
|
|
}
|
2024-05-17 16:14:35 +08:00
|
|
|
|
2023-09-18 20:12:33 +08:00
|
|
|
yyjson_mut_obj_add_real(doc, obj, "temperature", cpu.temperature);
|
2023-08-30 14:46:04 +08:00
|
|
|
}
|
2023-09-08 20:03:26 +08:00
|
|
|
|
|
|
|
|
ffStrbufDestroy(&cpu.name);
|
|
|
|
|
ffStrbufDestroy(&cpu.vendor);
|
2023-08-30 14:46:04 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
2024-12-11 10:47:02 +08:00
|
|
|
static FFModuleBaseInfo ffModuleInfo = {
|
|
|
|
|
.name = FF_CPU_MODULE_NAME,
|
|
|
|
|
.description = "Print CPU name, frequency, etc",
|
|
|
|
|
.parseCommandOptions = (void*) ffParseCPUCommandOptions,
|
|
|
|
|
.parseJsonObject = (void*) ffParseCPUJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintCPU,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateCPUJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateCPUJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
|
|
|
|
{"Name", "name"},
|
|
|
|
|
{"Vendor", "vendor"},
|
|
|
|
|
{"Physical core count", "cores-physical"},
|
|
|
|
|
{"Logical core count", "cores-logical"},
|
|
|
|
|
{"Online core count", "cores-online"},
|
|
|
|
|
{"Base frequency (formatted)", "freq-base"},
|
|
|
|
|
{"Max frequency (formatted)", "freq-max"},
|
|
|
|
|
{"Temperature (formatted)", "temperature"},
|
|
|
|
|
{"Logical core count grouped by frequency", "core-types"},
|
|
|
|
|
{"Processor package count", "packages"},
|
|
|
|
|
}))
|
|
|
|
|
};
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitCPUOptions(FFCPUOptions* options)
|
|
|
|
|
{
|
2024-12-11 10:47:02 +08:00
|
|
|
options->moduleInfo = ffModuleInfo;
|
2024-07-23 23:02:35 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2023-10-23 13:32:18 +08:00
|
|
|
options->temp = false;
|
2024-02-26 16:39:41 +08:00
|
|
|
options->tempConfig = (FFColorRangeConfig) { 60, 80 };
|
2024-05-21 09:09:08 +08:00
|
|
|
options->showPeCoreCount = false;
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyCPUOptions(FFCPUOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|