Skip to content

Commit 81fb146

Browse files
committed
CPU (Windows): detect max CPU freq with CPUID instruction
Note this only works when Hyper-V is not enabled Ultimately fixes #800 #812 #815
1 parent 35b54fd commit 81fb146

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/detection/cpu/cpu_windows.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ typedef struct FFSmbiosProcessorInfo
5050
uint16_t ThreadEnabled; // varies
5151
} FFSmbiosProcessorInfo;
5252

53+
#if defined(__x86_64__) || defined(__i386__)
54+
55+
#include <cpuid.h>
56+
57+
inline static const char* detectSpeedByCpuid(FFCPUResult* cpu)
58+
{
59+
uint32_t base = 0, max = 0, bus = 0, unused = 0;
60+
if (!__get_cpuid(0x16, &base, &max, &bus, &unused))
61+
return "Unsupported instruction";
62+
63+
// cpuid returns 0 MHz when hyper-v is enabled
64+
if (base) cpu->frequencyBase = base / 1000.0;
65+
if (max) cpu->frequencyMax = max / 1000.0;
66+
return NULL;
67+
}
68+
69+
#else
70+
71+
inline static const char* detectSpeedByCpuid(FFCPUResult* cpu)
72+
{
73+
return "Unsupported platform";
74+
}
75+
76+
#endif
77+
5378
static const char* detectMaxSpeedBySmbios(FFCPUResult* cpu)
5479
{
5580
const FFSmbiosProcessorInfo* data = (const FFSmbiosProcessorInfo*) (*ffGetSmbiosHeaderTable())[FF_SMBIOS_TYPE_PROCESSOR_INFO];
@@ -114,6 +139,7 @@ static const char* detectByRegistry(FFCPUResult* cpu)
114139
if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", &hKey, NULL))
115140
return "ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L\"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\", &hKey, NULL) failed";
116141

142+
if (detectSpeedByCpuid(cpu) == NULL && cpu->frequencyBase == 0)
117143
{
118144
uint32_t mhz;
119145
if(ffRegReadUint(hKey, L"~MHz", &mhz, NULL))
@@ -141,7 +167,8 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
141167
if (error)
142168
return error;
143169

144-
detectMaxSpeedBySmbios(cpu);
170+
if (cpu->frequencyMax == 0)
171+
detectMaxSpeedBySmbios(cpu);
145172

146173
if(options->temp)
147174
ffDetectSmbiosTemp(&cpu->temperature, NULL);

0 commit comments

Comments
 (0)