Skip to content

Commit 2e201ea

Browse files
committed
CPU: bring min frequency back & code optimizations
1 parent 7c54dcb commit 2e201ea

File tree

6 files changed

+42
-24
lines changed

6 files changed

+42
-24
lines changed

src/detection/cpu/cpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef struct FFCPUResult
1515

1616
double frequencyBase; // GHz
1717
double frequencyMax; // GHz
18+
double frequencyMin; // GHz
1819

1920
double temperature;
2021
} FFCPUResult;

src/detection/cpu/cpu_apple.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,21 @@ static const char* detectFrequency(FFCPUResult* cpu)
5353
if (ffCfDictGetData(properties, CFSTR("voltage-states1-sram"), 0, 4, (uint8_t*) &eMin, NULL) != NULL) // eCore
5454
return "\"voltage-states1-sram\" in \"pmgr\" is not found";
5555

56-
cpu->frequencyBase = (pMin < eMin ? pMin : eMin) / (1000.0 * 1000 * 1000);
56+
cpu->frequencyMin = (pMin < eMin ? pMin : eMin) / (1000.0 * 1000 * 1000);
5757

5858
if (pCoreLength >= 8)
5959
{
6060
ffCfDictGetData(properties, CFSTR("voltage-states5-sram"), pCoreLength - 8, 4, (uint8_t*) &aMax, NULL);
6161
cpu->frequencyMax = aMax / (1000.0 * 1000 * 1000);
6262
}
63-
else
64-
cpu->frequencyMax = 0.0;
6563

6664
return NULL;
6765
}
6866
#else
6967
static const char* detectFrequency(FFCPUResult* cpu)
7068
{
7169
cpu->frequencyBase = ffSysctlGetInt64("hw.cpufrequency", 0) / 1000.0 / 1000.0 / 1000.0;
70+
cpu->frequencyMin = ffSysctlGetInt64("hw.cpufrequency_min", 0) / 1000.0 / 1000.0 / 1000.0;
7271
cpu->frequencyMax = ffSysctlGetInt64("hw.cpufrequency_max", 0);
7372
if(cpu->frequencyMax > 0.0)
7473
cpu->frequencyMax /= 1000.0 * 1000.0 * 1000.0;

src/detection/cpu/cpu_bsd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
1212
cpu->coresOnline = cpu->coresPhysical;
1313

1414
cpu->frequencyBase = ffSysctlGetInt("hw.clockrate", 0) / 1000.0;
15-
cpu->frequencyMax = cpu->frequencyBase;
1615
cpu->temperature = FF_CPU_TEMP_UNSET;
1716

1817
if (options->temp)

src/detection/cpu/cpu_linux.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void detectAndroid(FFCPUResult* cpu)
2626
}
2727
#endif
2828

29-
static const char* parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer, FFstrbuf* cpuMHz, FFstrbuf* cpuIsa, FFstrbuf* cpuUarch)
29+
static const char* parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer, FFstrbuf* cpuIsa, FFstrbuf* cpuUarch)
3030
{
3131
FF_AUTO_CLOSE_FILE FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
3232
if(cpuinfo == NULL)
@@ -49,7 +49,6 @@ static const char* parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer,
4949
ffParsePropLine(line, "model name :", &cpu->name) ||
5050
ffParsePropLine(line, "vendor_id :", &cpu->vendor) ||
5151
ffParsePropLine(line, "cpu cores :", physicalCoresBuffer) ||
52-
ffParsePropLine(line, "cpu MHz :", cpuMHz) ||
5352
ffParsePropLine(line, "isa :", cpuIsa) ||
5453
ffParsePropLine(line, "uarch :", cpuUarch) ||
5554

@@ -102,7 +101,7 @@ static double getFrequency(FFstrbuf* basePath, const char* cpuinfoFileName, cons
102101
if (ok)
103102
return ffStrbufToDouble(buffer) / 1e6;
104103
}
105-
104+
106105
return 0.0/0.0;
107106
}
108107

@@ -123,10 +122,28 @@ static bool detectFrequency(FFCPUResult* cpu)
123122
ffStrbufAppendS(&path, entry->d_name);
124123
double fbase = getFrequency(&path, "/base_frequency", NULL, &buffer);
125124
if (fbase == fbase)
126-
cpu->frequencyBase = cpu->frequencyBase > fbase ? cpu->frequencyBase : fbase;
125+
{
126+
if (cpu->frequencyBase == cpu->frequencyBase)
127+
cpu->frequencyBase = cpu->frequencyBase > fbase ? cpu->frequencyBase : fbase;
128+
else
129+
cpu->frequencyBase = fbase;
130+
}
127131
double fmax = getFrequency(&path, "/cpuinfo_max_freq", "/scaling_max_freq", &buffer);
128132
if (fmax == fmax)
129-
cpu->frequencyMax = cpu->frequencyMax > fmax ? cpu->frequencyMax : fmax;
133+
{
134+
if (cpu->frequencyMax == cpu->frequencyMax)
135+
cpu->frequencyMax = cpu->frequencyMax > fmax ? cpu->frequencyMax : fmax;
136+
else
137+
cpu->frequencyMax = fmax;
138+
}
139+
double fmin = getFrequency(&path, "/cpuinfo_min_freq", "/scaling_min_freq", &buffer);
140+
if (fmin == fmin)
141+
{
142+
if (cpu->frequencyMin == cpu->frequencyMin)
143+
cpu->frequencyMin = cpu->frequencyMin < fmin ? cpu->frequencyMin : fmin;
144+
else
145+
cpu->frequencyMin = fmin;
146+
}
130147
ffStrbufSubstrBefore(&path, baseLen);
131148
}
132149
}
@@ -215,19 +232,18 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
215232
cpu->temperature = options->temp ? detectCPUTemp() : FF_CPU_TEMP_UNSET;
216233

217234
FF_STRBUF_AUTO_DESTROY physicalCoresBuffer = ffStrbufCreate();
218-
FF_STRBUF_AUTO_DESTROY cpuMHz = ffStrbufCreate();
219235
FF_STRBUF_AUTO_DESTROY cpuIsa = ffStrbufCreate();
220236
FF_STRBUF_AUTO_DESTROY cpuUarch = ffStrbufCreate();
221237

222-
const char* error = parseCpuInfo(cpu, &physicalCoresBuffer, &cpuMHz, &cpuIsa, &cpuUarch);
238+
const char* error = parseCpuInfo(cpu, &physicalCoresBuffer, &cpuIsa, &cpuUarch);
223239
if (error) return error;
224240

225241
cpu->coresLogical = (uint16_t) get_nprocs_conf();
226242
cpu->coresOnline = (uint16_t) get_nprocs();
227243
cpu->coresPhysical = (uint16_t) ffStrbufToUInt(&physicalCoresBuffer, cpu->coresLogical);
228244

229-
if (!detectFrequency(cpu))
230-
cpu->frequencyBase = cpu->frequencyMax = ffStrbufToDouble(&cpuMHz) / 1000;
245+
detectFrequency(cpu);
246+
// cpu MHz is current frequency, not max or base
231247

232248
if(cpuUarch.length > 0)
233249
{

src/detection/cpu/cpu_windows.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ static const char* detectMaxSpeedBySmbios(FFCPUResult* cpu)
6464
return "No active CPU is found in SMBIOS data";
6565
}
6666

67-
double speed;
6867
if (data->MaxSpeed > 0 && data->MaxSpeed < 30000) // VMware reports weird values
69-
speed = data->MaxSpeed / 1000.0;
70-
else
71-
speed = data->CurrentSpeed / 1000.0;
68+
{
69+
double speed = data->MaxSpeed / 1000.0;
70+
if (cpu->frequencyBase < speed)
71+
cpu->frequencyMax = speed;
72+
}
7273

73-
if (cpu->frequencyMax < speed)
74-
cpu->frequencyMax = speed;
7574

7675
return NULL;
7776
}
@@ -119,7 +118,7 @@ static const char* detectByRegistry(FFCPUResult* cpu)
119118
{
120119
uint32_t mhz;
121120
if(ffRegReadUint(hKey, L"~MHz", &mhz, NULL))
122-
cpu->frequencyBase = cpu->frequencyMax = mhz / 1000.0;
121+
cpu->frequencyBase = mhz / 1000.0;
123122
}
124123

125124
ffRegReadStrbuf(hKey, L"ProcessorNameString", &cpu->name, NULL);

src/modules/cpu/cpu.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void ffPrintCPU(FFCPUOptions* options)
1313
FFCPUResult cpu;
1414
cpu.temperature = FF_CPU_TEMP_UNSET;
1515
cpu.coresPhysical = cpu.coresLogical = cpu.coresOnline = 0;
16-
cpu.frequencyMax = cpu.frequencyBase = 0;
16+
cpu.frequencyMin = cpu.frequencyMax = cpu.frequencyBase = 0.0/0.0;
1717
ffStrbufInit(&cpu.name);
1818
ffStrbufInit(&cpu.vendor);
1919

@@ -48,8 +48,11 @@ void ffPrintCPU(FFCPUOptions* options)
4848
if(cpu.coresOnline > 1)
4949
ffStrbufAppendF(&str, " (%u)", cpu.coresOnline);
5050

51-
if(cpu.frequencyMax > 0.0)
52-
ffStrbufAppendF(&str, " @ %.*f GHz", options->freqNdigits, cpu.frequencyMax);
51+
double freq = cpu.frequencyMax;
52+
if(freq != freq)
53+
freq = cpu.frequencyBase;
54+
if(freq == freq)
55+
ffStrbufAppendF(&str, " @ %.*f GHz", options->freqNdigits, freq);
5356

5457
if(cpu.temperature == cpu.temperature) //FF_CPU_TEMP_UNSET
5558
{
@@ -143,7 +146,7 @@ void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_
143146
FFCPUResult cpu;
144147
cpu.temperature = FF_CPU_TEMP_UNSET;
145148
cpu.coresPhysical = cpu.coresLogical = cpu.coresOnline = 0;
146-
cpu.frequencyMax = cpu.frequencyBase = 0;
149+
cpu.frequencyMin = cpu.frequencyMax = cpu.frequencyBase = 0.0/0.0;
147150
ffStrbufInit(&cpu.name);
148151
ffStrbufInit(&cpu.vendor);
149152

@@ -171,6 +174,7 @@ void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_
171174
yyjson_mut_val* frequency = yyjson_mut_obj_add_obj(doc, obj, "frequency");
172175
yyjson_mut_obj_add_real(doc, frequency, "base", cpu.frequencyBase);
173176
yyjson_mut_obj_add_real(doc, frequency, "max", cpu.frequencyMax);
177+
yyjson_mut_obj_add_real(doc, frequency, "min", cpu.frequencyMin);
174178

175179
yyjson_mut_obj_add_real(doc, obj, "temperature", cpu.temperature);
176180
}

0 commit comments

Comments
 (0)