2024-05-10 23:47:15 +08:00
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
|
|
|
|
#include "common/percent.h"
|
2025-07-30 14:30:36 +08:00
|
|
|
#include "common/size.h"
|
2024-05-10 23:47:15 +08:00
|
|
|
#include "detection/physicalmemory/physicalmemory.h"
|
|
|
|
|
#include "modules/physicalmemory/physicalmemory.h"
|
|
|
|
|
#include "util/stringUtils.h"
|
|
|
|
|
|
|
|
|
|
#define FF_PHYSICALMEMORY_DISPLAY_NAME "Physical Memory"
|
|
|
|
|
|
|
|
|
|
void ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options)
|
|
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalMemoryResult));
|
|
|
|
|
const char* error = ffDetectPhysicalMemory(&result);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 11:13:04 +08:00
|
|
|
if (result.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No physical memory detected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 23:47:15 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY prettySize = ffStrbufCreate();
|
|
|
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
FF_LIST_FOR_EACH(FFPhysicalMemoryResult, device, result)
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
ffStrbufClear(&prettySize);
|
2025-07-30 14:30:36 +08:00
|
|
|
ffSizeAppendNum(device->size, &prettySize);
|
2024-05-10 23:47:15 +08:00
|
|
|
|
|
|
|
|
if (options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2024-06-27 17:49:30 +08:00
|
|
|
ffPrintLogoAndKey(FF_PHYSICALMEMORY_DISPLAY_NAME, result.length == 1 ? 0 : (uint8_t) i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2024-05-12 19:13:21 +08:00
|
|
|
|
|
|
|
|
fputs(prettySize.chars, stdout);
|
|
|
|
|
fputs(" - ", stdout);
|
|
|
|
|
ffStrbufWriteTo(&device->type, stdout);
|
|
|
|
|
if (device->maxSpeed > 0)
|
|
|
|
|
printf("-%u", device->maxSpeed);
|
2024-05-11 14:46:52 +08:00
|
|
|
if (device->runningSpeed > 0 && device->runningSpeed != device->maxSpeed)
|
2024-05-12 20:17:57 +08:00
|
|
|
printf(" @ %u MT/s", device->runningSpeed);
|
2024-05-12 19:13:21 +08:00
|
|
|
if (device->vendor.length > 0)
|
|
|
|
|
printf(" (%s)", device->vendor.chars);
|
2024-05-12 19:55:04 +08:00
|
|
|
if (device->ecc)
|
|
|
|
|
fputs(" - ECC", stdout);
|
2024-05-12 19:13:21 +08:00
|
|
|
putchar('\n');
|
2024-05-10 23:47:15 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_PHYSICALMEMORY_DISPLAY_NAME, (uint8_t) i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
2024-09-03 14:52:40 +08:00
|
|
|
FF_FORMAT_ARG(device->size, "bytes"),
|
|
|
|
|
FF_FORMAT_ARG(prettySize, "size"),
|
|
|
|
|
FF_FORMAT_ARG(device->maxSpeed, "max-speed"),
|
|
|
|
|
FF_FORMAT_ARG(device->runningSpeed, "running-speed"),
|
|
|
|
|
FF_FORMAT_ARG(device->type, "type"),
|
|
|
|
|
FF_FORMAT_ARG(device->formFactor, "form-factor"),
|
|
|
|
|
FF_FORMAT_ARG(device->locator, "locator"),
|
|
|
|
|
FF_FORMAT_ARG(device->vendor, "vendor"),
|
|
|
|
|
FF_FORMAT_ARG(device->serial, "serial"),
|
|
|
|
|
FF_FORMAT_ARG(device->partNumber, "part-number"),
|
|
|
|
|
FF_FORMAT_ARG(device->ecc, "is-ecc-enabled"),
|
2024-05-10 23:47:15 +08:00
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFPhysicalMemoryResult, device, result)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&device->type);
|
2024-05-12 19:55:04 +08:00
|
|
|
ffStrbufDestroy(&device->locator);
|
2024-05-10 23:47:15 +08:00
|
|
|
ffStrbufDestroy(&device->formFactor);
|
|
|
|
|
ffStrbufDestroy(&device->vendor);
|
|
|
|
|
ffStrbufDestroy(&device->serial);
|
2024-05-12 19:13:21 +08:00
|
|
|
ffStrbufDestroy(&device->partNumber);
|
2024-05-10 23:47:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffParsePhysicalMemoryJsonObject(FFPhysicalMemoryOptions* options, yyjson_val* module)
|
|
|
|
|
{
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2024-05-10 23:47:15 +08:00
|
|
|
size_t idx, max;
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_obj_foreach(module, idx, max, key, val)
|
2024-05-10 23:47:15 +08:00
|
|
|
{
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_PHYSICALMEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2024-05-10 23:47:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffGeneratePhysicalMemoryJsonConfig(FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyPhysicalMemoryOptions))) FFPhysicalMemoryOptions defaultOptions;
|
|
|
|
|
ffInitPhysicalMemoryOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalMemoryResult));
|
|
|
|
|
const char* error = ffDetectPhysicalMemory(&result);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
|
|
|
|
FF_LIST_FOR_EACH(FFPhysicalMemoryResult, device, result)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "size", device->size);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "maxSpeed", device->maxSpeed);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "runningSpeed", device->runningSpeed);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "type", &device->type);
|
2024-05-12 19:55:04 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "locator", &device->locator);
|
2024-05-10 23:47:15 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "formFactor", &device->formFactor);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &device->vendor);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &device->serial);
|
2024-05-12 19:13:21 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "partNumber", &device->partNumber);
|
2024-05-12 19:55:04 +08:00
|
|
|
yyjson_mut_obj_add_bool(doc, obj, "ecc", device->ecc);
|
2024-05-10 23:47:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFPhysicalMemoryResult, device, result)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&device->type);
|
2024-05-12 19:55:04 +08:00
|
|
|
ffStrbufDestroy(&device->locator);
|
2024-05-10 23:47:15 +08:00
|
|
|
ffStrbufDestroy(&device->formFactor);
|
|
|
|
|
ffStrbufDestroy(&device->vendor);
|
|
|
|
|
ffStrbufDestroy(&device->serial);
|
2024-05-12 19:13:21 +08:00
|
|
|
ffStrbufDestroy(&device->partNumber);
|
2024-05-10 23:47:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 10:47:02 +08:00
|
|
|
static FFModuleBaseInfo ffModuleInfo = {
|
|
|
|
|
.name = FF_PHYSICALMEMORY_MODULE_NAME,
|
|
|
|
|
.description = "Print system physical memory devices",
|
2025-08-03 18:39:41 +08:00
|
|
|
.initOptions = (void*) ffInitPhysicalMemoryOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyPhysicalMemoryOptions,
|
2024-12-11 10:47:02 +08:00
|
|
|
.parseJsonObject = (void*) ffParsePhysicalMemoryJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintPhysicalMemory,
|
|
|
|
|
.generateJsonConfig = (void*) ffGeneratePhysicalMemoryJsonConfig,
|
2025-02-14 19:31:35 +08:00
|
|
|
.generateJsonResult = (void*) ffGeneratePhysicalMemoryJsonResult,
|
2024-12-11 10:47:02 +08:00
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
|
|
|
|
{"Size (in bytes)", "bytes"},
|
|
|
|
|
{"Size formatted", "size"},
|
|
|
|
|
{"Max speed (in MT/s)", "max-speed"},
|
|
|
|
|
{"Running speed (in MT/s)", "running-speed"},
|
|
|
|
|
{"Type (DDR4, DDR5, etc.)", "type"},
|
|
|
|
|
{"Form factor (SODIMM, DIMM, etc.)", "form-factor"},
|
|
|
|
|
{"Bank/Device Locator (BANK0/SIMM0, BANK0/SIMM1, etc.)", "locator"},
|
|
|
|
|
{"Vendor", "vendor"},
|
|
|
|
|
{"Serial number", "serial"},
|
|
|
|
|
{"Part number", "part-number"},
|
|
|
|
|
{"True if ECC enabled", "is-ecc-enabled"},
|
|
|
|
|
}))
|
|
|
|
|
};
|
2024-05-10 23:47:15 +08:00
|
|
|
|
|
|
|
|
void ffInitPhysicalMemoryOptions(FFPhysicalMemoryOptions* options)
|
|
|
|
|
{
|
2024-12-11 10:47:02 +08:00
|
|
|
options->moduleInfo = ffModuleInfo;
|
2024-07-23 23:02:35 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2024-05-10 23:47:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyPhysicalMemoryOptions(FFPhysicalMemoryOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|