Skip to content

Commit ee564de

Browse files
committed
CPU: change cpuCount to packages; support custom format
Fix #1413
1 parent 9955e9d commit ee564de

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

src/detection/cpu/cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef struct FFCPUResult
1515
FFstrbuf name;
1616
FFstrbuf vendor;
1717

18-
uint16_t cpuCount;
18+
uint16_t packages;
1919
uint16_t coresPhysical;
2020
uint16_t coresLogical;
2121
uint16_t coresOnline;

src/detection/cpu/cpu_apple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
109109
return "sysctlbyname(machdep.cpu.brand_string) failed";
110110

111111
ffSysctlGetString("machdep.cpu.vendor", &cpu->vendor);
112-
cpu->cpuCount = (uint16_t) ffSysctlGetInt("hw.packages", 1);
112+
cpu->packages = (uint16_t) ffSysctlGetInt("hw.packages", 1);
113113
if (cpu->vendor.length == 0 && ffStrbufStartsWithS(&cpu->name, "Apple "))
114114
ffStrbufAppendS(&cpu->vendor, "Apple");
115115

src/detection/cpu/cpu_linux.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ FF_MAYBE_UNUSED static void detectArmSoc(FFCPUResult* cpu)
464464
}
465465
}
466466

467-
FF_MAYBE_UNUSED static uint16_t getCPUCount(FFstrbuf* cpuinfo)
467+
FF_MAYBE_UNUSED static uint16_t getPackageCount(FFstrbuf* cpuinfo)
468468
{
469469
const char* p = cpuinfo->chars;
470470
uint64_t bits = 0;
@@ -502,7 +502,9 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
502502
cpu->coresLogical = (uint16_t) get_nprocs_conf();
503503
cpu->coresOnline = (uint16_t) get_nprocs();
504504
cpu->coresPhysical = (uint16_t) ffStrbufToUInt(&physicalCoresBuffer, cpu->coresLogical);
505-
cpu->cpuCount = getCPUCount(&cpuinfo);
505+
#if __x86_64__ || __i386__
506+
cpu->packages = getPackageCount(&cpuinfo);
507+
#endif
506508

507509
// Ref https://github.com/fastfetch-cli/fastfetch/issues/1194#issuecomment-2295058252
508510
ffCPUDetectSpeedByCpuid(cpu);

src/detection/cpu/cpu_windows.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static const char* detectNCores(FFCPUResult* cpu)
112112
}
113113

114114
if (ptr->Relationship == RelationProcessorPackage) {
115-
cpu->cpuCount++;
115+
cpu->packages++;
116116
}
117117
}
118118

src/modules/cpu/cpu.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "modules/cpu/cpu.h"
77
#include "util/stringUtils.h"
88

9-
#define FF_CPU_NUM_FORMAT_ARGS 9
9+
#define FF_CPU_NUM_FORMAT_ARGS 10
1010

1111
static int sortCores(const FFCPUCore* a, const FFCPUCore* b)
1212
{
@@ -55,8 +55,8 @@ void ffPrintCPU(FFCPUOptions* options)
5555

5656
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
5757

58-
if(cpu.cpuCount > 1)
59-
ffStrbufAppendF(&str, "%u x ", cpu.cpuCount);
58+
if(cpu.packages > 1)
59+
ffStrbufAppendF(&str, "%u x ", cpu.packages);
6060

6161
if(cpu.name.length > 0)
6262
ffStrbufAppend(&str, &cpu.name);
@@ -72,7 +72,7 @@ void ffPrintCPU(FFCPUOptions* options)
7272
ffStrbufAppendF(&str, " (%s)", coreTypes.chars);
7373
else if(cpu.coresOnline > 1)
7474
{
75-
if(cpu.cpuCount > 1)
75+
if(cpu.packages > 1)
7676
ffStrbufAppendF(&str, " (%u)", cpu.coresOnline / 2);
7777
else
7878
ffStrbufAppendF(&str, " (%u)", cpu.coresOnline);
@@ -114,6 +114,7 @@ void ffPrintCPU(FFCPUOptions* options)
114114
FF_FORMAT_ARG(freqMax, "freq-max"),
115115
FF_FORMAT_ARG(tempStr, "temperature"),
116116
FF_FORMAT_ARG(coreTypes, "core-types"),
117+
FF_FORMAT_ARG(cpu.packages, "packages"),
117118
}));
118119
}
119120
}
@@ -211,6 +212,10 @@ void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_
211212
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
212213
yyjson_mut_obj_add_strbuf(doc, obj, "cpu", &cpu.name);
213214
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &cpu.vendor);
215+
if (cpu.packages == 0)
216+
yyjson_mut_obj_add_null(doc, obj, "packages");
217+
else
218+
yyjson_mut_obj_add_uint(doc, obj, "packages", cpu.packages);
214219

215220
yyjson_mut_val* cores = yyjson_mut_obj_add_obj(doc, obj, "cores");
216221
yyjson_mut_obj_add_uint(doc, cores, "physical", cpu.coresPhysical);
@@ -248,6 +253,7 @@ void ffPrintCPUHelpFormat(void)
248253
"Max frequency (formatted) - freq-max",
249254
"Temperature (formatted) - temperature",
250255
"Logical core count grouped by frequency - core-types",
256+
"Processor package count - packages",
251257
}));
252258
}
253259

0 commit comments

Comments
 (0)