From 95f76e959916b849eb3f5c28839140238490aa98 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 20 Dec 2024 15:12:45 +0200 Subject: [PATCH] SysInfo caching for Windows --- openVulkanoCpp/Host/Windows/SystemInfo.cpp | 254 ++++++++++++--------- 1 file changed, 149 insertions(+), 105 deletions(-) diff --git a/openVulkanoCpp/Host/Windows/SystemInfo.cpp b/openVulkanoCpp/Host/Windows/SystemInfo.cpp index 4d61fef..7832435 100644 --- a/openVulkanoCpp/Host/Windows/SystemInfo.cpp +++ b/openVulkanoCpp/Host/Windows/SystemInfo.cpp @@ -253,6 +253,80 @@ namespace OpenVulkano return res; } + std::string GetHumanReadableOSName() + { + NTSTATUS(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW); + OSVERSIONINFOEXW info; + *(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); + info.dwOSVersionInfoSize = sizeof(info); + RtlGetVersion(&info); + if (info.wProductType == VER_NT_WORKSTATION) + { + if (info.dwMajorVersion == 10) + { + if (info.dwBuildNumber >= 22000) + return "Windows 11"; + return "Windows 10"; + } + else if (info.dwMajorVersion == 6) + { + switch (info.dwMinorVersion) + { + case 0: return "Windows Vista"; + case 1: return "Windows 7"; + case 2: return "Windows 8"; + case 3: return "Windows 8.1"; + } + } + + return "Windows " + std::to_string(info.dwMajorVersion) + "." + std::to_string(info.dwMinorVersion); + } + else + { + if (info.dwMajorVersion == 10) + { + switch (info.dwBuildNumber) + { + case 14393: + return "Windows Server 2016"; + case 16299: + return "Windows Server 1709"; + case 17134: + return "Windows Server 1803"; + case 17763: + return "Windows Server 2019"; + case 18362: + return "Windows Server 1903"; + case 18363: + return "Windows Server 1909"; + case 19041: + return "Windows Server 2004"; + case 19042: + return "Windows Server 20H2"; + case 20348: + return "Windows Server 2022"; + case 25398: + return "Windows Server 23H2"; + case 26100: + return "Windows Server 2025"; + default: + return "Windows Server"; + } + } + else if (info.dwMajorVersion == 6) + { + switch (info.dwMinorVersion) + { + case 0: return "Windows Server 2008"; + case 1: return "Windows Server 2008 R2"; + case 2: return "Windows Server 2012"; + case 3: return "Windows Server 2012 R2"; + } + } + + return "Windows Server " + std::to_string(info.dwMajorVersion) + "." + std::to_string(info.dwMinorVersion); + } + } } size_t SystemInfo::GetSystemRam() @@ -287,37 +361,62 @@ namespace OpenVulkano std::string SystemInfo::GetUserName() { - char username[UNLEN+1]; - DWORD username_len = UNLEN+1; - ::GetUserNameA(username, &username_len); - return username; + static std::string userName; + if (userName.empty()) + { + char username[UNLEN + 1]; + DWORD username_len = UNLEN + 1; + ::GetUserNameA(username, &username_len); + userName = username; + } + return userName; } std::string SystemInfo::GetHostName() { - char hostname[UNLEN+1]; - gethostname(hostname, UNLEN+1); - return hostname; + static std::string hostName; + if (hostName.empty()) + { + char hostname[UNLEN+1]; + gethostname(hostname, UNLEN+1); + hostName = hostname; + } + return hostName; } std::string SystemInfo::GetDeviceName() { - char computerName[UNLEN+1]; - DWORD computerName_len = UNLEN+1; - GetComputerNameA(computerName, &computerName_len); - return computerName; + static std::string devName; + if (devName.empty()) + { + char computerName[UNLEN+1]; + DWORD computerName_len = UNLEN+1; + GetComputerNameA(computerName, &computerName_len); + devName = computerName; + } + return devName; } std::string SystemInfo::GetDeviceVendorName() { - std::optional res = GetWMIProperty("Manufacturer"); - return res ? *res : "Unknown"; + static std::string vendorName; + if (vendorName.empty()) + { + std::optional res = GetWMIProperty("Manufacturer"); + vendorName = res ? *res : "Unknown"; + } + return vendorName; } std::string SystemInfo::GetDeviceModelName() { - std::optional res = GetWMIProperty("Model"); - return res ? *res : "Unknown"; + static std::string deviceModelName; + if (deviceModelName.empty()) + { + std::optional res = GetWMIProperty("Model"); + deviceModelName = res ? *res : "Unknown"; + } + return deviceModelName; } std::string SystemInfo::GetOsName() @@ -327,87 +426,27 @@ namespace OpenVulkano OsVersion SystemInfo::GetOsVersion() { - NTSTATUS(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW); - OSVERSIONINFOEXW info; - *(FARPROC*) &RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); - info.dwOSVersionInfoSize = sizeof(info); - RtlGetVersion(&info); - return { (int)info.dwMajorVersion, (int)info.dwMinorVersion, 0, (int)info.dwBuildNumber }; + static OsVersion osVersion; + if (osVersion.major == 0 && osVersion.minor == 0) + { + NTSTATUS(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW); + OSVERSIONINFOEXW info; + *(FARPROC*) &RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); + info.dwOSVersionInfoSize = sizeof(info); + RtlGetVersion(&info); + osVersion = { (int)info.dwMajorVersion, (int)info.dwMinorVersion, 0, (int)info.dwBuildNumber }; + } + return osVersion; } std::string SystemInfo::GetOsNameHumanReadable() { - NTSTATUS(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW); - OSVERSIONINFOEXW info; - *(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); - info.dwOSVersionInfoSize = sizeof(info); - RtlGetVersion(&info); - if (info.wProductType == VER_NT_WORKSTATION) + static std::string osName; + if (osName.empty()) { - if (info.dwMajorVersion == 10) - { - if (info.dwBuildNumber >= 22000) - return "Windows 11"; - return "Windows 10"; - } - else if (info.dwMajorVersion == 6) - { - switch (info.dwMinorVersion) - { - case 0: return "Windows Vista"; - case 1: return "Windows 7"; - case 2: return "Windows 8"; - case 3: return "Windows 8.1"; - } - } - - return "Windows " + std::to_string(info.dwMajorVersion) + "." + std::to_string(info.dwMinorVersion); - } - else - { - if (info.dwMajorVersion == 10) - { - switch (info.dwBuildNumber) - { - case 14393: - return "Windows Server 2016"; - case 16299: - return "Windows Server 1709"; - case 17134: - return "Windows Server 1803"; - case 17763: - return "Windows Server 2019"; - case 18362: - return "Windows Server 1903"; - case 18363: - return "Windows Server 1909"; - case 19041: - return "Windows Server 2004"; - case 19042: - return "Windows Server 20H2"; - case 20348: - return "Windows Server 2022"; - case 25398: - return "Windows Server 23H2"; - case 26100: - return "Windows Server 2025"; - default: - return "Windows Server"; - } - } - else if (info.dwMajorVersion == 6) - { - switch (info.dwMinorVersion) - { - case 0: return "Windows Server 2008"; - case 1: return "Windows Server 2008 R2"; - case 2: return "Windows Server 2012"; - case 3: return "Windows Server 2012 R2"; - } - } - - return "Windows Server " + std::to_string(info.dwMajorVersion) + "." + std::to_string(info.dwMinorVersion); + osName = GetHumanReadableOSName(); } + return osName; } DeviceType SystemInfo::GetDeviceType() @@ -417,23 +456,28 @@ namespace OpenVulkano size_t SystemInfo::GetCpuCoreCount() { - DWORD bufferSize = 0; - GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, nullptr, &bufferSize); - - std::vector buf(bufferSize); - SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* info = reinterpret_cast(buf.data()); - GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, info, &bufferSize); - - size_t physProcessorCount = 0; - BYTE* start = buf.data(); - BYTE* end = buf.data() + bufferSize; - while (start < end) + static size_t coreCount; + if (coreCount == 0) { - SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* current = reinterpret_cast(start); - physProcessorCount++; - start += current->Size; + DWORD bufferSize = 0; + GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, nullptr, &bufferSize); + + std::vector buf(bufferSize); + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* info = reinterpret_cast(buf.data()); + GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, info, &bufferSize); + + size_t physProcessorCount = 0; + BYTE* start = buf.data(); + BYTE* end = buf.data() + bufferSize; + while (start < end) + { + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* current = reinterpret_cast(start); + physProcessorCount++; + start += current->Size; + } + coreCount = physProcessorCount; } - return physProcessorCount; + return coreCount; } size_t SystemInfo::GetCpuThreadCount()