Merge pull request 'SystemInfo caching' (#178) from sysinfo_caching into master

Reviewed-on: https://git.madvoxel.net/OpenVulkano/OpenVulkano/pulls/178
Reviewed-by: Georg Hagen <georg.hagen@madvoxel.com>
This commit is contained in:
Vladyslav_Baranovskyi_EXT
2024-12-20 15:34:07 +01:00
4 changed files with 286 additions and 167 deletions

View File

@@ -73,7 +73,8 @@ namespace OpenVulkano
size_t SystemInfo::GetSystemRam() size_t SystemInfo::GetSystemRam()
{ {
return ReadMemInfo("MemTotal"); static const size_t ram = ReadMemInfo("MemTotal");
return ram;
} }
size_t SystemInfo::GetSystemRamAvailable() size_t SystemInfo::GetSystemRamAvailable()
@@ -98,27 +99,47 @@ namespace OpenVulkano
size_t SystemInfo::GetAppVirtualMemoryMax() size_t SystemInfo::GetAppVirtualMemoryMax()
{ {
rlimit limit; static size_t vramMax;
if (getrlimit(RLIMIT_AS, &limit) == 0) if (vramMax == 0)
{ {
return limit.rlim_cur; rlimit limit;
if (getrlimit(RLIMIT_AS, &limit) == 0)
{
vramMax = limit.rlim_cur;
}
Logger::PERF->error("Failed to query max application memory");
} }
Logger::PERF->error("Failed to query max application memory"); return vramMax;
return 0;
} }
std::string SystemInfo::GetUserName() std::string SystemInfo::GetUserName()
{ {
char* name = getlogin(); static std::string userName;
if (!name) return "unknown"; if (userName.empty())
return name; {
char* name = getlogin();
if (!name)
{
userName = "unknown";
}
else
{
userName = name;
}
}
return userName;
} }
std::string SystemInfo::GetHostName() std::string SystemInfo::GetHostName()
{ {
char hostname[HOST_NAME_MAX + 1]; static std::string hostName;
gethostname(hostname, HOST_NAME_MAX + 1); if (hostName.empty())
return hostname; {
char hostname[HOST_NAME_MAX + 1];
gethostname(hostname, HOST_NAME_MAX + 1);
hostName = hostname;
}
return hostName;
} }
std::string SystemInfo::GetDeviceName() std::string SystemInfo::GetDeviceName()
@@ -128,30 +149,44 @@ namespace OpenVulkano
std::string SystemInfo::GetDeviceVendorName() std::string SystemInfo::GetDeviceVendorName()
{ {
std::ifstream dmiStream("/sys/class/dmi/id/board_vendor"); static std::string vendor;
if (!dmiStream) if (vendor.empty())
{ {
Logger::PERF->error("Failed to read /sys/class/dmi/id/board_vendor"); std::ifstream dmiStream("/sys/class/dmi/id/board_vendor");
return "Unknown"; if (!dmiStream)
{
Logger::PERF->error("Failed to read /sys/class/dmi/id/board_vendor");
vendor = "Unknown";
}
else
{
std::string motherboardName;
std::getline(dmiStream, motherboardName);
vendor = motherboardName;
}
} }
return vendor;
std::string motherboardName;
std::getline(dmiStream, motherboardName);
return motherboardName;
} }
std::string SystemInfo::GetDeviceModelName() std::string SystemInfo::GetDeviceModelName()
{ {
std::ifstream dmiStream("/sys/class/dmi/id/board_name"); static std::string modelName;
if (!dmiStream) if (modelName.empty())
{ {
Logger::PERF->error("Failed to read /sys/class/dmi/id/board_name"); std::ifstream dmiStream("/sys/class/dmi/id/board_name");
return "Unknown"; if (!dmiStream)
{
Logger::PERF->error("Failed to read /sys/class/dmi/id/board_name");
modelName = "Unknown";
}
else
{
std::string motherboardName;
std::getline(dmiStream, motherboardName);
modelName = motherboardName;
}
} }
return modelName;
std::string motherboardName;
std::getline(dmiStream, motherboardName);
return motherboardName;
} }
namespace namespace
@@ -275,7 +310,8 @@ namespace OpenVulkano
size_t SystemInfo::GetCpuThreadCount() size_t SystemInfo::GetCpuThreadCount()
{ {
return std::thread::hardware_concurrency(); static const size_t threadCount = std::thread::hardware_concurrency();
return threadCount;
} }
int32_t SystemInfo::GetCpuTemperature() int32_t SystemInfo::GetCpuTemperature()

View File

@@ -7,6 +7,7 @@
#include "Host/SystemInfo.hpp" #include "Host/SystemInfo.hpp"
#include "Base/Logger.hpp" #include "Base/Logger.hpp"
#include <sstream> #include <sstream>
#include <fmt/core.h>
#include <mach/mach.h> #include <mach/mach.h>
#include <os/proc.h> #include <os/proc.h>
@@ -24,7 +25,8 @@ namespace OpenVulkano
size_t SystemInfo::GetSystemRam() size_t SystemInfo::GetSystemRam()
{ {
return [NSProcessInfo processInfo].physicalMemory; static const size_t ram = [NSProcessInfo processInfo].physicalMemory;
return ram;
} }
size_t SystemInfo::GetSystemRamAvailable() size_t SystemInfo::GetSystemRamAvailable()
@@ -67,12 +69,14 @@ namespace OpenVulkano
std::string SystemInfo::GetHostName() std::string SystemInfo::GetHostName()
{ {
return [[NSProcessInfo processInfo].hostName UTF8String]; static const std::string hostName = [[NSProcessInfo processInfo].hostName UTF8String];
return hostName;
} }
std::string SystemInfo::GetDeviceName() std::string SystemInfo::GetDeviceName()
{ {
return "Mac"; //TODO static const std::string devName = "Mac"; //TODO
return devName;
} }
std::string SystemInfo::GetDeviceVendorName() std::string SystemInfo::GetDeviceVendorName()
@@ -82,9 +86,14 @@ namespace OpenVulkano
std::string SystemInfo::GetDeviceModelName() std::string SystemInfo::GetDeviceModelName()
{ {
struct utsname systemInfo; static std::string machine;
uname(&systemInfo); if (machine.empty())
return systemInfo.machine; {
struct utsname systemInfo;
uname(&systemInfo);
machine = systemInfo.machine;
}
return machine;
} }
std::string SystemInfo::GetOsName() std::string SystemInfo::GetOsName()
@@ -94,15 +103,20 @@ namespace OpenVulkano
OsVersion SystemInfo::GetOsVersion() OsVersion SystemInfo::GetOsVersion()
{ {
NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion; static OsVersion osVersion = {};
return { static_cast<int>(osVersion.majorVersion), static_cast<int>(osVersion.minorVersion), static_cast<int>(osVersion.patchVersion), 0 }; if (osVersion.major == 0 && osVersion.minor == 0)
{
NSOperatingSystemVersion sysVersion = [NSProcessInfo processInfo].operatingSystemVersion;
osVersion = { static_cast<int>(sysVersion.majorVersion), static_cast<int>(sysVersion.minorVersion),
static_cast<int>(sysVersion.patchVersion), 0 };
}
return osVersion;
} }
std::string SystemInfo::GetOsNameHumanReadable() std::string SystemInfo::GetOsNameHumanReadable()
{ {
std::stringstream name; static const std::string hrName = fmt::format("{} {}", GetOsName(), GetOsVersion().major);
name << GetOsName() << ' ' << GetOsVersion().major; return hrName;
return name.str();
} }
DeviceType SystemInfo::GetDeviceType() DeviceType SystemInfo::GetDeviceType()
@@ -112,12 +126,14 @@ namespace OpenVulkano
size_t SystemInfo::GetCpuCoreCount() size_t SystemInfo::GetCpuCoreCount()
{ {
return [NSProcessInfo processInfo].processorCount; static const size_t coreCount = [NSProcessInfo processInfo].processorCount;
return coreCount;
} }
size_t SystemInfo::GetCpuThreadCount() size_t SystemInfo::GetCpuThreadCount()
{ {
return [NSProcessInfo processInfo].activeProcessorCount; static size_t procCount = [NSProcessInfo processInfo].activeProcessorCount;
return procCount;
} }
int32_t SystemInfo::GetCpuTemperature() int32_t SystemInfo::GetCpuTemperature()

View File

@@ -253,6 +253,80 @@ namespace OpenVulkano
return res; 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() size_t SystemInfo::GetSystemRam()
@@ -287,37 +361,62 @@ namespace OpenVulkano
std::string SystemInfo::GetUserName() std::string SystemInfo::GetUserName()
{ {
char username[UNLEN+1]; static std::string userName;
DWORD username_len = UNLEN+1; if (userName.empty())
::GetUserNameA(username, &username_len); {
return username; char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
::GetUserNameA(username, &username_len);
userName = username;
}
return userName;
} }
std::string SystemInfo::GetHostName() std::string SystemInfo::GetHostName()
{ {
char hostname[UNLEN+1]; static std::string hostName;
gethostname(hostname, UNLEN+1); if (hostName.empty())
return hostname; {
char hostname[UNLEN+1];
gethostname(hostname, UNLEN+1);
hostName = hostname;
}
return hostName;
} }
std::string SystemInfo::GetDeviceName() std::string SystemInfo::GetDeviceName()
{ {
char computerName[UNLEN+1]; static std::string devName;
DWORD computerName_len = UNLEN+1; if (devName.empty())
GetComputerNameA(computerName, &computerName_len); {
return computerName; char computerName[UNLEN+1];
DWORD computerName_len = UNLEN+1;
GetComputerNameA(computerName, &computerName_len);
devName = computerName;
}
return devName;
} }
std::string SystemInfo::GetDeviceVendorName() std::string SystemInfo::GetDeviceVendorName()
{ {
std::optional<std::string> res = GetWMIProperty("Manufacturer"); static std::string vendorName;
return res ? *res : "Unknown"; if (vendorName.empty())
{
std::optional<std::string> res = GetWMIProperty("Manufacturer");
vendorName = res ? *res : "Unknown";
}
return vendorName;
} }
std::string SystemInfo::GetDeviceModelName() std::string SystemInfo::GetDeviceModelName()
{ {
std::optional<std::string> res = GetWMIProperty("Model"); static std::string deviceModelName;
return res ? *res : "Unknown"; if (deviceModelName.empty())
{
std::optional<std::string> res = GetWMIProperty("Model");
deviceModelName = res ? *res : "Unknown";
}
return deviceModelName;
} }
std::string SystemInfo::GetOsName() std::string SystemInfo::GetOsName()
@@ -327,87 +426,23 @@ namespace OpenVulkano
OsVersion SystemInfo::GetOsVersion() OsVersion SystemInfo::GetOsVersion()
{ {
NTSTATUS(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW); static OsVersion osVersion = {};
OSVERSIONINFOEXW info; if (osVersion.major == 0 && osVersion.minor == 0)
*(FARPROC*) &RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); {
info.dwOSVersionInfoSize = sizeof(info); NTSTATUS(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW);
RtlGetVersion(&info); OSVERSIONINFOEXW info;
return { (int)info.dwMajorVersion, (int)info.dwMinorVersion, 0, (int)info.dwBuildNumber }; *(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() std::string SystemInfo::GetOsNameHumanReadable()
{ {
NTSTATUS(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW); static const std::string osName = GetHumanReadableOSName();
OSVERSIONINFOEXW info; return osName;
*(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);
}
} }
DeviceType SystemInfo::GetDeviceType() DeviceType SystemInfo::GetDeviceType()
@@ -417,23 +452,28 @@ namespace OpenVulkano
size_t SystemInfo::GetCpuCoreCount() size_t SystemInfo::GetCpuCoreCount()
{ {
DWORD bufferSize = 0; static size_t coreCount = 0;
GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, nullptr, &bufferSize); if (coreCount == 0)
std::vector<BYTE> buf(bufferSize);
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* info = reinterpret_cast<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*>(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<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*>(start); DWORD bufferSize = 0;
physProcessorCount++; GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, nullptr, &bufferSize);
start += current->Size;
std::vector<BYTE> buf(bufferSize);
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* info = reinterpret_cast<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*>(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<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*>(start);
physProcessorCount++;
start += current->Size;
}
coreCount = physProcessorCount;
} }
return physProcessorCount; return coreCount;
} }
size_t SystemInfo::GetCpuThreadCount() size_t SystemInfo::GetCpuThreadCount()

View File

@@ -27,7 +27,8 @@ namespace OpenVulkano
size_t SystemInfo::GetSystemRam() size_t SystemInfo::GetSystemRam()
{ {
return [NSProcessInfo processInfo].physicalMemory; static const size_t ram = [NSProcessInfo processInfo].physicalMemory;
return ram;
} }
size_t SystemInfo::GetSystemRamAvailable() size_t SystemInfo::GetSystemRamAvailable()
@@ -70,12 +71,14 @@ namespace OpenVulkano
std::string SystemInfo::GetHostName() std::string SystemInfo::GetHostName()
{ {
return [[NSProcessInfo processInfo].hostName UTF8String]; static const std::string hostName = [[NSProcessInfo processInfo].hostName UTF8String];
return hostName;
} }
std::string SystemInfo::GetDeviceName() std::string SystemInfo::GetDeviceName()
{ {
return [[[UIDevice currentDevice] name] UTF8String]; static const std::string devName = [[[UIDevice currentDevice] name] UTF8String];
return devName;
} }
std::string SystemInfo::GetDeviceVendorName() std::string SystemInfo::GetDeviceVendorName()
@@ -98,43 +101,67 @@ namespace OpenVulkano
std::string SystemInfo::GetOsName() std::string SystemInfo::GetOsName()
{ {
return [[[UIDevice currentDevice] systemName] UTF8String]; static const std::string osName = [[[UIDevice currentDevice] systemName] UTF8String];
return osName;
} }
OsVersion SystemInfo::GetOsVersion() OsVersion SystemInfo::GetOsVersion()
{ {
NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion; static OsVersion osv = {};
return { static_cast<int>(osVersion.majorVersion), static_cast<int>(osVersion.minorVersion), static_cast<int>(osVersion.patchVersion), 0 }; if (osv.major == 0 && osv.minor == 0)
{
NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion;
osv = { static_cast<int>(osVersion.majorVersion), static_cast<int>(osVersion.minorVersion),
static_cast<int>(osVersion.patchVersion), 0 };
}
return osv;
} }
std::string SystemInfo::GetOsNameHumanReadable() std::string SystemInfo::GetOsNameHumanReadable()
{ {
OsVersion osVersion = GetOsVersion(); static std::string hrName;
return fmt::format("{} {}.{}", GetOsName(), osVersion.major, osVersion.minor); if (hrName.empty())
{
OsVersion osVersion = GetOsVersion();
hrName = fmt::format("{} {}.{}", GetOsName(), osVersion.major, osVersion.minor);
}
return hrName;
} }
DeviceType SystemInfo::GetDeviceType() DeviceType SystemInfo::GetDeviceType()
{ {
switch ([UIDevice currentDevice].userInterfaceIdiom) static DeviceType devType = DeviceType::Unknown;
if (devType == DeviceType::Unknown)
{ {
case UIUserInterfaceIdiomPhone: return DeviceType::Phone; switch ([UIDevice currentDevice].userInterfaceIdiom)
case UIUserInterfaceIdiomPad: return DeviceType::Tablet; {
case UIUserInterfaceIdiomTV: return DeviceType::TV; case UIUserInterfaceIdiomPhone:
case UIUserInterfaceIdiomMac: return DeviceType::PC; devType = DeviceType::Phone;
case UIUserInterfaceIdiomVision: return DeviceType::VR; case UIUserInterfaceIdiomPad:
default: break; devType = DeviceType::Tablet;
case UIUserInterfaceIdiomTV:
devType = DeviceType::TV;
case UIUserInterfaceIdiomMac:
devType = DeviceType::PC;
case UIUserInterfaceIdiomVision:
devType = DeviceType::VR;
default:
break;
}
} }
return DeviceType::Unknown; return devType;
} }
size_t SystemInfo::GetCpuCoreCount() size_t SystemInfo::GetCpuCoreCount()
{ {
return [NSProcessInfo processInfo].processorCount; static const size_t coreCount = [NSProcessInfo processInfo].processorCount;
return coreCount;
} }
size_t SystemInfo::GetCpuThreadCount() size_t SystemInfo::GetCpuThreadCount()
{ {
return [NSProcessInfo processInfo].activeProcessorCount; static const size_t threadCount = [NSProcessInfo processInfo].activeProcessorCount;
return threadCount;
} }
int32_t SystemInfo::GetCpuTemperature() int32_t SystemInfo::GetCpuTemperature()