From 49f590c79742991b22b86599fd210a3a433ba8a2 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 13 Mar 2018 20:04:41 +0100 Subject: [PATCH] - Use the Windows system function to determine the number of physical CPU cores. The generic version does not appear to work as intended. --- src/d_stats.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/d_stats.cpp b/src/d_stats.cpp index ebec250bb4..9f00d2a5cf 100644 --- a/src/d_stats.cpp +++ b/src/d_stats.cpp @@ -185,12 +185,49 @@ static int GetOSVersion() #endif } + +#ifdef _WIN32 + +static int GetCoreInfo() +{ + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; + DWORD returnLength = 0; + int cores = 0; + uint32_t byteOffset = 0; + + auto rc = GetLogicalProcessorInformation(buffer, &returnLength); + + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + { + buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength); + if (!GetLogicalProcessorInformation(buffer, &returnLength)) return 0; + } + else + { + return 0; + } + + ptr = buffer; + + while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) + { + if (ptr->Relationship == RelationProcessorCore) cores++; + byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); + ptr++; + } + free(buffer); + return cores < 2 ? 0 : cores < 4 ? 1 : cores < 6 ? 2 : cores < 8 ? 3 : 4; +} + +#else static int GetCoreInfo() { int cores = std::thread::hardware_concurrency(); if (CPU.HyperThreading) cores /= 2; return cores < 2? 0 : cores < 4? 1 : cores < 6? 2 : cores < 8? 3 : 4; } +#endif static int GetRenderInfo() {