diff --git a/CMakeLists.txt b/CMakeLists.txt index 1811f0a72..add970e57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -424,7 +424,7 @@ elseif(WIN32) src/detection/battery/battery_windows.cpp src/detection/bios/bios_windows.c src/detection/board/board_windows.c - src/detection/cpu/cpu_windows.cpp + src/detection/cpu/cpu_windows.c src/detection/cpuUsage/cpuUsage_nowait_windows.cpp src/detection/cpuUsage/cpuUsage_windows.c src/detection/disk/disk_windows.c diff --git a/src/detection/cpu/cpu_windows.c b/src/detection/cpu/cpu_windows.c new file mode 100644 index 000000000..c7db11aad --- /dev/null +++ b/src/detection/cpu/cpu_windows.c @@ -0,0 +1,57 @@ +#include "cpu.h" +#include "util/windows/register.h" + +static inline void wrapFree(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX** ptr) +{ + free(*ptr); +} + +void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu, bool cached) +{ + FF_UNUSED(instance); + + cpu->temperature = FF_CPU_TEMP_UNSET; + + if(cached) + return; + + cpu->coresPhysical = cpu->coresLogical = cpu->coresOnline = 0; + cpu->frequencyMax = cpu->frequencyMin = 0; + ffStrbufInit(&cpu->name); + ffStrbufInit(&cpu->vendor); + + { + DWORD length = 0; + GetLogicalProcessorInformationEx(RelationProcessorCore, NULL, &length); + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* __attribute__((__cleanup__(wrapFree))) + pLogicalInfo = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(length); + + if(pLogicalInfo && GetLogicalProcessorInformationEx(RelationProcessorCore, pLogicalInfo, &length)) + { + for( + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* ptr = pLogicalInfo; + (uint8_t*)ptr < ((uint8_t*)pLogicalInfo) + length; + ptr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)(((uint8_t*)ptr) + ptr->Size) + ) + { + if(ptr->Relationship == RelationProcessorCore) + ++cpu->coresPhysical; + } + } + } + cpu->coresOnline = (uint16_t)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + cpu->coresLogical = (uint16_t)GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS); + + FF_HKEY_AUTO_DESTROY hKey; + if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", &hKey, NULL)) + return; + + { + uint32_t mhz; + if(ffRegReadUint(hKey, "~MHz", &mhz, NULL)) + cpu->frequencyMax = mhz / 1000.0; + } + + ffRegReadStrbuf(hKey, "ProcessorNameString", &cpu->name, NULL); + ffRegReadStrbuf(hKey, "VendorIdentifier", &cpu->vendor, NULL); +} diff --git a/src/detection/cpu/cpu_windows.cpp b/src/detection/cpu/cpu_windows.cpp deleted file mode 100644 index e5939cd60..000000000 --- a/src/detection/cpu/cpu_windows.cpp +++ /dev/null @@ -1,51 +0,0 @@ -extern "C" { -#include "cpu.h" -} -#include "util/windows/wmi.hpp" - -extern "C" -void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu, bool cached) -{ - FF_UNUSED(instance); - - cpu->temperature = FF_CPU_TEMP_UNSET; - - if(cached) - return; - - cpu->coresPhysical = cpu->coresLogical = cpu->coresOnline = 0; - ffStrbufInit(&cpu->name); - ffStrbufInit(&cpu->vendor); - - FFWmiQuery query(L"SELECT Name, Manufacturer, NumberOfCores, NumberOfLogicalProcessors, NumberOfEnabledCore, CurrentClockSpeed, MaxClockSpeed FROM Win32_Processor WHERE ProcessorType = 3"); - if(!query) - return; - - FFWmiRecord record = query.next(); - if(!record) - { - //NumberOfEnabledCore is not supported on Windows 10- - query = FFWmiQuery(L"SELECT Name, Manufacturer, NumberOfCores, NumberOfLogicalProcessors, CurrentClockSpeed, MaxClockSpeed FROM Win32_Processor WHERE ProcessorType = 3"); - if(!query) - return; - record = query.next(); - } - - if(record) - { - record.getString(L"Name", &cpu->name); - record.getString(L"Manufacturer", &cpu->vendor); - - uint64_t value; - - record.getUnsigned(L"NumberOfCores", &value); - cpu->coresPhysical = (uint16_t)value; - record.getUnsigned(L"NumberOfLogicalProcessors", &value); - cpu->coresLogical = (uint16_t)value; - cpu->coresOnline = record.getUnsigned(L"NumberOfEnabledCore", &value) ? (uint16_t)value : cpu->coresPhysical; - record.getUnsigned(L"CurrentClockSpeed", &value); //There's no MinClockSpeed in Win32_Processor - cpu->frequencyMin = (double)value / 1000.0; - record.getUnsigned(L"MaxClockSpeed", &value); - cpu->frequencyMax = (double)value / 1000.0; - } -}