Skip to content

Commit 1e23475

Browse files
committed
PhysicalMemory (Linux): adds option showEmptySlots to show empty memory slots
Fixes #2222
1 parent 4a4afda commit 1e23475

6 files changed

Lines changed: 75 additions & 28 deletions

File tree

doc/json_schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@
450450
"type": "string"
451451
},
452452
"physicalmemoryFormat": {
453-
"description": "Output format of the module `PhysicalMemory`. See Wiki for formatting syntax\n 1. {bytes}: Size (in bytes)\n 2. {size}: Size formatted\n 3. {max-speed}: Max speed (in MT/s)\n 4. {running-speed}: Running speed (in MT/s)\n 5. {type}: Type (DDR4, DDR5, etc.)\n 6. {form-factor}: Form factor (SODIMM, DIMM, etc.)\n 7. {locator}: Bank/Device Locator (BANK0/SIMM0, BANK0/SIMM1, etc.)\n 8. {vendor}: Vendor\n 9. {serial}: Serial number\n 10. {part-number}: Part number\n 11. {is-ecc-enabled}: True if ECC enabled",
453+
"description": "Output format of the module `PhysicalMemory`. See Wiki for formatting syntax\n 1. {bytes}: Size (in bytes)\n 2. {size}: Size formatted\n 3. {max-speed}: Max speed (in MT/s)\n 4. {running-speed}: Running speed (in MT/s)\n 5. {type}: Type (DDR4, DDR5, etc.)\n 6. {form-factor}: Form factor (SODIMM, DIMM, etc.)\n 7. {locator}: Bank/Device Locator (BANK0/SIMM0, BANK0/SIMM1, etc.)\n 8. {vendor}: Vendor\n 9. {serial}: Serial number\n 10. {part-number}: Part number\n 11. {is-ecc-enabled}: True if ECC enabled\n 12. {is-installed}: True if a memory module is installed in the slot",
454454
"type": "string"
455455
},
456456
"playerFormat": {
@@ -3650,6 +3650,11 @@
36503650
"const": "physicalmemory",
36513651
"description": "Print system physical memory devices"
36523652
},
3653+
"showEmptySlots": {
3654+
"description": "Set if uninstalled memory slots should be printed",
3655+
"type": "boolean",
3656+
"default": false
3657+
},
36533658
"key": {
36543659
"$ref": "#/$defs/key"
36553660
},

src/detection/physicalmemory/physicalmemory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ typedef struct FFPhysicalMemoryResult
88
uint64_t size; // B
99
uint32_t maxSpeed; // MT/s
1010
uint32_t runningSpeed; // MT/s
11+
bool installed;
1112
FFstrbuf type;
1213
FFstrbuf formFactor;
1314
FFstrbuf locator;

src/detection/physicalmemory/physicalmemory_apple.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static void appendDevice(
3232
device->size = 0;
3333
device->maxSpeed = 0;
3434
device->runningSpeed = 0;
35+
device->installed = true;
3536
device->ecc = ecc;
3637

3738
if (size)
@@ -143,6 +144,7 @@ static void appendDevice(
143144
device->size = 0;
144145
device->maxSpeed = 0;
145146
device->runningSpeed = 0;
147+
device->installed = true;
146148
device->ecc = false;
147149

148150
ffCfStrGetString(dramType, &device->type);

src/detection/physicalmemory/physicalmemory_linux.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ const char* ffDetectPhysicalMemory(FFlist* result)
7878
for (; data->Header.Type < FF_SMBIOS_TYPE_END_OF_TABLE;
7979
data = (const FFSmbiosMemoryDevice*) ffSmbiosNextEntry(&data->Header))
8080
{
81-
if (data->Header.Type != FF_SMBIOS_TYPE_MEMORY_DEVICE || data->Size == 0) continue;
81+
if (data->Header.Type != FF_SMBIOS_TYPE_MEMORY_DEVICE) continue;
8282

8383
const char* strings = (const char*) data + data->Header.Length;
84+
bool installed = data->Size != 0;
8485

8586
FFPhysicalMemoryResult* device = ffListAdd(result);
8687
ffStrbufInit(&device->type);
@@ -92,12 +93,13 @@ const char* ffDetectPhysicalMemory(FFlist* result)
9293
device->size = 0;
9394
device->maxSpeed = 0;
9495
device->runningSpeed = 0;
96+
device->installed = installed;
9597
device->ecc = false;
9698

97-
if (data->TotalWidth != 0xFFFF && data->DataWidth != 0xFFFF)
99+
if (installed && data->TotalWidth != 0xFFFF && data->DataWidth != 0xFFFF)
98100
device->ecc = data->TotalWidth > data->DataWidth;
99101

100-
if (data->Size != 0xFFFF)
102+
if (installed && data->Size != 0xFFFF)
101103
{
102104
if (data->Size == 0x7FFF)
103105
device->size = (data->ExtendedSize & ~(1ULL << 31)) * 1024ULL * 1024ULL;
@@ -186,12 +188,14 @@ const char* ffDetectPhysicalMemory(FFlist* result)
186188
"LPDDR5", // 0x23
187189
"HBM3", // 0x24
188190
};
189-
if (data->MemoryType > 0 && data->MemoryType < ARRAY_SIZE(memoryTypeNames))
191+
if (!installed)
192+
ffStrbufSetStatic(&device->type, "Empty");
193+
else if (data->MemoryType > 0 && data->MemoryType < ARRAY_SIZE(memoryTypeNames))
190194
ffStrbufSetStatic(&device->type, memoryTypeNames[data->MemoryType]);
191195
else
192196
ffStrbufSetF(&device->type, "Unknown (%d)", (int) data->MemoryType);
193197

194-
if (data->Header.Length > offsetof(FFSmbiosMemoryDevice, Speed)) // 2.3+
198+
if (installed && data->Header.Length > offsetof(FFSmbiosMemoryDevice, Speed)) // 2.3+
195199
{
196200
if (data->Speed)
197201
device->maxSpeed = data->Speed == 0xFFFF ? data->ExtendedSpeed : data->Speed;
@@ -207,7 +211,7 @@ const char* ffDetectPhysicalMemory(FFlist* result)
207211
ffCleanUpSmbiosValue(&device->partNumber);
208212
}
209213

210-
if (data->Header.Length > offsetof(FFSmbiosMemoryDevice, ConfiguredMemorySpeed)) // 2.7+
214+
if (installed && data->Header.Length > offsetof(FFSmbiosMemoryDevice, ConfiguredMemorySpeed)) // 2.7+
211215
{
212216
if (data->ConfiguredMemorySpeed)
213217
device->runningSpeed = data->ConfiguredMemorySpeed == 0xFFFF
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#pragma once
22

33
#include "common/option.h"
4-
#include "common/percent.h"
54

65
typedef struct FFPhysicalMemoryOptions
76
{
87
FFModuleArgs moduleArgs;
8+
bool showEmptySlots;
99
} FFPhysicalMemoryOptions;
1010

1111
static_assert(sizeof(FFPhysicalMemoryOptions) <= FF_OPTION_MAX_SIZE, "FFPhysicalMemoryOptions size exceeds maximum allowed size");

src/modules/physicalmemory/physicalmemory.c

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include "common/printing.h"
22
#include "common/jsonconfig.h"
3-
#include "common/percent.h"
43
#include "common/size.h"
5-
#include "common/stringUtils.h"
64
#include "detection/physicalmemory/physicalmemory.h"
75
#include "modules/physicalmemory/physicalmemory.h"
86

@@ -25,35 +23,61 @@ bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options)
2523
return false;
2624
}
2725

26+
FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFPhysicalMemoryResult*));
27+
FF_LIST_FOR_EACH(FFPhysicalMemoryResult, device, result)
28+
{
29+
if (!options->showEmptySlots && !device->installed)
30+
continue;
31+
32+
*(FFPhysicalMemoryResult**) ffListAdd(&filtered) = device;
33+
}
34+
35+
if (filtered.length == 0)
36+
{
37+
ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No installed physical memory detected");
38+
return false;
39+
}
40+
2841
FF_STRBUF_AUTO_DESTROY prettySize = ffStrbufCreate();
2942

30-
uint32_t i = 0;
31-
FF_LIST_FOR_EACH(FFPhysicalMemoryResult, device, result)
43+
for (uint32_t i = 0; i < filtered.length; ++i)
3244
{
33-
++i;
45+
FFPhysicalMemoryResult* device = *FF_LIST_GET(FFPhysicalMemoryResult*, filtered, i);
3446
ffStrbufClear(&prettySize);
35-
ffSizeAppendNum(device->size, &prettySize);
47+
if (device->installed)
48+
ffSizeAppendNum(device->size, &prettySize);
3649

3750
if (options->moduleArgs.outputFormat.length == 0)
3851
{
39-
ffPrintLogoAndKey(FF_PHYSICALMEMORY_DISPLAY_NAME, result.length == 1 ? 0 : (uint8_t) i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
40-
41-
fputs(prettySize.chars, stdout);
42-
fputs(" - ", stdout);
43-
ffStrbufWriteTo(&device->type, stdout);
44-
if (device->maxSpeed > 0)
45-
printf("-%u", device->maxSpeed);
46-
if (device->runningSpeed > 0 && device->runningSpeed != device->maxSpeed)
47-
printf(" @ %u MT/s", device->runningSpeed);
48-
if (device->vendor.length > 0)
49-
printf(" (%s)", device->vendor.chars);
50-
if (device->ecc)
51-
fputs(" - ECC", stdout);
52+
ffPrintLogoAndKey(FF_PHYSICALMEMORY_DISPLAY_NAME, filtered.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
53+
54+
if (device->installed)
55+
{
56+
fputs(prettySize.chars, stdout);
57+
fputs(" - ", stdout);
58+
ffStrbufWriteTo(&device->type, stdout);
59+
if (device->maxSpeed > 0)
60+
printf("-%u", device->maxSpeed);
61+
if (device->runningSpeed > 0 && device->runningSpeed != device->maxSpeed)
62+
printf(" @ %u MT/s", device->runningSpeed);
63+
if (device->vendor.length > 0)
64+
printf(" (%s)", device->vendor.chars);
65+
if (device->ecc)
66+
fputs(" - ECC", stdout);
67+
}
68+
else
69+
{
70+
fputs("Empty", stdout);
71+
if (device->formFactor.length > 0)
72+
printf(" - %s", device->formFactor.chars);
73+
if (device->locator.length > 0)
74+
printf(" (%s)", device->locator.chars);
75+
}
5276
putchar('\n');
5377
}
5478
else
5579
{
56-
FF_PRINT_FORMAT_CHECKED(FF_PHYSICALMEMORY_DISPLAY_NAME, (uint8_t) i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
80+
FF_PRINT_FORMAT_CHECKED(FF_PHYSICALMEMORY_DISPLAY_NAME, (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
5781
FF_FORMAT_ARG(device->size, "bytes"),
5882
FF_FORMAT_ARG(prettySize, "size"),
5983
FF_FORMAT_ARG(device->maxSpeed, "max-speed"),
@@ -65,6 +89,7 @@ bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options)
6589
FF_FORMAT_ARG(device->serial, "serial"),
6690
FF_FORMAT_ARG(device->partNumber, "part-number"),
6791
FF_FORMAT_ARG(device->ecc, "is-ecc-enabled"),
92+
FF_FORMAT_ARG(device->installed, "is-installed"),
6893
}));
6994
}
7095
}
@@ -91,13 +116,20 @@ void ffParsePhysicalMemoryJsonObject(FFPhysicalMemoryOptions* options, yyjson_va
91116
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
92117
continue;
93118

119+
if (unsafe_yyjson_equals_str(key, "showEmptySlots"))
120+
{
121+
options->showEmptySlots = yyjson_get_bool(val);
122+
continue;
123+
}
124+
94125
ffPrintError(FF_PHYSICALMEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
95126
}
96127
}
97128

98129
void ffGeneratePhysicalMemoryJsonConfig(FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
99130
{
100131
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
132+
yyjson_mut_obj_add_bool(doc, module, "showEmptySlots", options->showEmptySlots);
101133
}
102134

103135
bool ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
@@ -116,6 +148,7 @@ bool ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions*
116148
{
117149
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
118150
yyjson_mut_obj_add_uint(doc, obj, "size", device->size);
151+
yyjson_mut_obj_add_bool(doc, obj, "installed", device->installed);
119152
yyjson_mut_obj_add_uint(doc, obj, "maxSpeed", device->maxSpeed);
120153
yyjson_mut_obj_add_uint(doc, obj, "runningSpeed", device->runningSpeed);
121154
yyjson_mut_obj_add_strbuf(doc, obj, "type", &device->type);
@@ -143,6 +176,7 @@ bool ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions*
143176
void ffInitPhysicalMemoryOptions(FFPhysicalMemoryOptions* options)
144177
{
145178
ffOptionInitModuleArg(&options->moduleArgs, "󰑭");
179+
options->showEmptySlots = false;
146180
}
147181

148182
void ffDestroyPhysicalMemoryOptions(FFPhysicalMemoryOptions* options)
@@ -171,5 +205,6 @@ FFModuleBaseInfo ffPhysicalMemoryModuleInfo = {
171205
{"Serial number", "serial"},
172206
{"Part number", "part-number"},
173207
{"True if ECC enabled", "is-ecc-enabled"},
208+
{"True if a memory module is installed in the slot", "is-installed"},
174209
}))
175210
};

0 commit comments

Comments
 (0)