Skip to content

Commit 2d4e7f1

Browse files
committed
CPU: add option --show-pe-core-count
1 parent aa1a5b8 commit 2d4e7f1

File tree

8 files changed

+77
-33
lines changed

8 files changed

+77
-33
lines changed

doc/json_schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,11 @@
10741074
"maximum": 9,
10751075
"default": 2
10761076
},
1077+
"showPeCoreCount": {
1078+
"description": "Detect and display CPU frequency of different core types (eg. Pcore and Ecore) if supported",
1079+
"type": "boolean",
1080+
"default": false
1081+
},
10771082
"key": {
10781083
"$ref": "#/$defs/key"
10791084
},

src/data/help.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,15 @@
11521152
"default": 2
11531153
}
11541154
},
1155+
{
1156+
"long": "cpu-show-pe-core-count",
1157+
"desc": "Detect and display CPU frequency of different core types (eg. Pcore and Ecore) if supported",
1158+
"arg": {
1159+
"type": "bool",
1160+
"optional": true,
1161+
"default": false
1162+
}
1163+
},
11551164
{
11561165
"long": "cpuusage-separate",
11571166
"desc": "Display CPU usage per CPU logical core, instead of an average result",

src/detection/cpu/cpu_apple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
121121
cpu->coresOnline = (uint16_t) ffSysctlGetInt("hw.activecpu", 1);
122122

123123
detectFrequency(cpu);
124-
detectCoreCount(cpu);
124+
if (options->showPeCoreCount) detectCoreCount(cpu);
125125

126126
cpu->temperature = options->temp ? detectCpuTemp(&cpu->name) : FF_CPU_TEMP_UNSET;
127127

src/detection/cpu/cpu_bsd.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
5656
if (!(cpu->frequencyMin <= fmin)) cpu->frequencyMin = fmin; // Counting for NaN
5757
if (!(cpu->frequencyMax >= fmax)) cpu->frequencyMax = fmax;
5858

59-
uint32_t ifreq = 0;
60-
while (cpu->coreTypes[ifreq].freq != fmax && cpu->coreTypes[ifreq].freq > 0)
61-
++ifreq;
62-
if (cpu->coreTypes[ifreq].freq == 0)
63-
cpu->coreTypes[ifreq].freq = fmax;
64-
cpu->coreTypes[ifreq].count++;
59+
if (options->showPeCoreCount)
60+
{
61+
uint32_t ifreq = 0;
62+
while (cpu->coreTypes[ifreq].freq != fmax && cpu->coreTypes[ifreq].freq > 0)
63+
++ifreq;
64+
if (cpu->coreTypes[ifreq].freq == 0)
65+
cpu->coreTypes[ifreq].freq = fmax;
66+
cpu->coreTypes[ifreq].count++;
67+
}
6568
}
6669
}
6770
cpu->frequencyMin /= 1000;

src/detection/cpu/cpu_linux.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static uint8_t getNumCores(FFstrbuf* basePath, FFstrbuf* buffer)
127127
return 0;
128128
}
129129

130-
static bool detectFrequency(FFCPUResult* cpu)
130+
static bool detectFrequency(FFCPUResult* cpu, const FFCPUOptions* options)
131131
{
132132
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/sys/devices/system/cpu/cpufreq/");
133133
FF_AUTO_CLOSE_DIR DIR* dir = opendir(path.chars);
@@ -175,13 +175,16 @@ static bool detectFrequency(FFCPUResult* cpu)
175175
cpu->frequencyMin = fmin;
176176
}
177177

178-
uint32_t freq = fbase == 0 ? fmax : fbase; // seems base frequencies are more stable
179-
uint32_t ifreq = 0;
180-
while (cpu->coreTypes[ifreq].freq != freq && cpu->coreTypes[ifreq].freq > 0)
181-
++ifreq;
182-
if (cpu->coreTypes[ifreq].freq == 0)
183-
cpu->coreTypes[ifreq].freq = freq;
184-
cpu->coreTypes[ifreq].count += getNumCores(&path, &buffer);
178+
if (options->showPeCoreCount)
179+
{
180+
uint32_t freq = fbase == 0 ? fmax : fbase; // seems base frequencies are more stable
181+
uint32_t ifreq = 0;
182+
while (cpu->coreTypes[ifreq].freq != freq && cpu->coreTypes[ifreq].freq > 0)
183+
++ifreq;
184+
if (cpu->coreTypes[ifreq].freq == 0)
185+
cpu->coreTypes[ifreq].freq = freq;
186+
cpu->coreTypes[ifreq].count += getNumCores(&path, &buffer);
187+
}
185188
ffStrbufSubstrBefore(&path, baseLen);
186189
}
187190
}
@@ -270,7 +273,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
270273
cpu->coresOnline = (uint16_t) get_nprocs();
271274
cpu->coresPhysical = (uint16_t) ffStrbufToUInt(&physicalCoresBuffer, cpu->coresLogical);
272275

273-
if (!detectFrequency(cpu) || cpu->frequencyBase != cpu->frequencyBase)
276+
if (!detectFrequency(cpu, options) || cpu->frequencyBase != cpu->frequencyBase)
274277
cpu->frequencyBase = ffStrbufToDouble(&cpuMHz) / 1000;
275278

276279
if(cpuUarch.length > 0)

src/detection/cpu/cpu_windows.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ static const char* detectByRegistry(FFCPUResult* cpu)
157157
cpu->coresOnline = cpu->coresPhysical = cpu->coresLogical = (uint16_t) cores;
158158
}
159159

160+
uint32_t mhz;
161+
if(ffRegReadUint(hKey, L"~MHz", &mhz, NULL))
162+
cpu->frequencyBase = mhz / 1000.0;
163+
160164
return NULL;
161165
}
162166

@@ -190,7 +194,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
190194
return error;
191195

192196
detectSpeedByCpuid(cpu);
193-
detectCoreTypes(cpu);
197+
if (options->showPeCoreCount) detectCoreTypes(cpu);
194198

195199
if (cpu->frequencyMax != cpu->frequencyMax)
196200
detectMaxSpeedBySmbios(cpu);

src/modules/cpu/cpu.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ void ffPrintCPU(FFCPUOptions* options)
3737
}
3838
else
3939
{
40+
FF_STRBUF_AUTO_DESTROY coreTypes = ffStrbufCreate();
41+
if (options->showPeCoreCount)
42+
{
43+
uint32_t typeCount = 0;
44+
while (cpu.coreTypes[typeCount].count != 0 && typeCount < sizeof(cpu.coreTypes) / sizeof(cpu.coreTypes[0])) typeCount++;
45+
if (typeCount > 0)
46+
{
47+
qsort(cpu.coreTypes, typeCount, sizeof(cpu.coreTypes[0]), (void*) sortCores);
48+
49+
for (uint32_t i = 0; i < typeCount; i++)
50+
ffStrbufAppendF(&coreTypes, "%s%u", i == 0 ? "" : "+", cpu.coreTypes[i].count);
51+
}
52+
}
53+
4054
if(options->moduleArgs.outputFormat.length == 0)
4155
{
4256
ffPrintLogoAndKey(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
@@ -53,7 +67,9 @@ void ffPrintCPU(FFCPUOptions* options)
5367
else
5468
ffStrbufAppendS(&str, "Unknown");
5569

56-
if(cpu.coresOnline > 1)
70+
if(coreTypes.length > 0)
71+
ffStrbufAppendF(&str, " (%s)", coreTypes.chars);
72+
else if(cpu.coresOnline > 1)
5773
ffStrbufAppendF(&str, " (%u)", cpu.coresOnline);
5874

5975
double freq = cpu.frequencyBiosLimit;
@@ -74,19 +90,6 @@ void ffPrintCPU(FFCPUOptions* options)
7490
}
7591
else
7692
{
77-
FF_STRBUF_AUTO_DESTROY coreTypes = ffStrbufCreate();
78-
uint32_t typeCount = 0;
79-
while (cpu.coreTypes[typeCount].count != 0 && typeCount < sizeof(cpu.coreTypes) / sizeof(cpu.coreTypes[0])) typeCount++;
80-
if (typeCount > 0)
81-
{
82-
qsort(cpu.coreTypes, typeCount, sizeof(cpu.coreTypes[0]), (void*) sortCores);
83-
84-
for (uint32_t i = 0; i < typeCount; i++)
85-
ffStrbufAppendF(&coreTypes, "%s%u", i == 0 ? "" : "+", cpu.coreTypes[i].count);
86-
}
87-
else
88-
ffStrbufAppendF(&coreTypes, "%u", cpu.coresOnline);
89-
9093
char freqBase[32], freqMax[32], freqBioslimit[32];
9194
if (cpu.frequencyBase > 0)
9295
snprintf(freqBase, sizeof(freqBase), "%.*f", options->freqNdigits, cpu.frequencyBase);
@@ -122,7 +125,7 @@ void ffPrintCPU(FFCPUOptions* options)
122125
ffStrbufDestroy(&cpu.vendor);
123126
}
124127

125-
bool ffParseCPUCPUOptions(FFCPUOptions* options, const char* key, const char* value)
128+
bool ffParseCPUCommandOptions(FFCPUOptions* options, const char* key, const char* value)
126129
{
127130
const char* subKey = ffOptionTestPrefix(key, FF_CPU_MODULE_NAME);
128131
if (!subKey) return false;
@@ -138,6 +141,12 @@ bool ffParseCPUCPUOptions(FFCPUOptions* options, const char* key, const char* va
138141
return true;
139142
}
140143

144+
if (ffStrEqualsIgnCase(subKey, "show-pe-core-count"))
145+
{
146+
options->showPeCoreCount = ffOptionParseBoolean(value);
147+
return true;
148+
}
149+
141150
return false;
142151
}
143152

@@ -163,6 +172,12 @@ void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module)
163172
continue;
164173
}
165174

175+
if (ffStrEqualsIgnCase(key, "showPeCoreCount"))
176+
{
177+
options->showPeCoreCount = yyjson_get_bool(val);
178+
continue;
179+
}
180+
166181
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
167182
}
168183
}
@@ -178,6 +193,9 @@ void ffGenerateCPUJsonConfig(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_
178193

179194
if (defaultOptions.freqNdigits != options->freqNdigits)
180195
yyjson_mut_obj_add_uint(doc, module, "freqNdigits", options->freqNdigits);
196+
197+
if (defaultOptions.showPeCoreCount != options->showPeCoreCount)
198+
yyjson_mut_obj_add_bool(doc, module, "showPeCoreCount", options->showPeCoreCount);
181199
}
182200

183201
void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
@@ -256,7 +274,7 @@ void ffInitCPUOptions(FFCPUOptions* options)
256274
&options->moduleInfo,
257275
FF_CPU_MODULE_NAME,
258276
"Print CPU name, frequency, etc",
259-
ffParseCPUCPUOptions,
277+
ffParseCPUCommandOptions,
260278
ffParseCPUJsonObject,
261279
ffPrintCPU,
262280
ffGenerateCPUJsonResult,
@@ -267,6 +285,7 @@ void ffInitCPUOptions(FFCPUOptions* options)
267285
options->temp = false;
268286
options->tempConfig = (FFColorRangeConfig) { 60, 80 };
269287
options->freqNdigits = 2;
288+
options->showPeCoreCount = false;
270289
}
271290

272291
void ffDestroyCPUOptions(FFCPUOptions* options)

src/modules/cpu/option.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ typedef struct FFCPUOptions
1212
bool temp;
1313
FFColorRangeConfig tempConfig;
1414
uint8_t freqNdigits;
15+
bool showPeCoreCount;
1516
} FFCPUOptions;

0 commit comments

Comments
 (0)