From c5b5e31db173499eea51f08e15fb70b984e25647 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Fri, 20 Dec 2024 11:23:16 +0100 Subject: [PATCH 1/9] Add Platform.totalMemory. --- rts/Lua/LuaConstPlatform.cpp | 1 + rts/System/Platform/Misc.cpp | 12 ++++++++++++ rts/System/Platform/Misc.h | 1 + 3 files changed, 14 insertions(+) diff --git a/rts/Lua/LuaConstPlatform.cpp b/rts/Lua/LuaConstPlatform.cpp index 7f897c0c68..2b77cc57ad 100644 --- a/rts/Lua/LuaConstPlatform.cpp +++ b/rts/Lua/LuaConstPlatform.cpp @@ -109,6 +109,7 @@ bool LuaConstPlatform::PushEntries(lua_State* L) LuaPushNamedString(L, "hwConfig", Platform::GetHardwareStr()); LuaPushNamedNumber(L, "cpuLogicalCores", Threading::GetLogicalCpuCores()); LuaPushNamedNumber(L, "cpuPhysicalCores", Threading::GetPhysicalCpuCores()); + LuaPushNamedNumber(L, "totalMemory", Platform::TotalRAM()); LuaPushNamedString(L, "sysInfoHash", Platform::GetSysInfoHash()); LuaPushNamedString(L, "macAddrHash", Platform::GetMacAddrHash()); diff --git a/rts/System/Platform/Misc.cpp b/rts/System/Platform/Misc.cpp index e353dc9726..8b9d2ffcb5 100644 --- a/rts/System/Platform/Misc.cpp +++ b/rts/System/Platform/Misc.cpp @@ -462,6 +462,18 @@ namespace Platform } #endif + uint64_t TotalRAM() { +#ifdef _WIN32 + MEMORYSTATUSEX status; + status.dwLength = sizeof(status); + GlobalMemoryStatusEx(&status); + return status.ullTotalPhys; +#else + long pages = sysconf(_SC_PHYS_PAGES); + long page_size = sysconf(_SC_PAGE_SIZE); + return pages * page_size; +#endif + } std::string GetSysInfoHash() { std::vector sysInfo; diff --git a/rts/System/Platform/Misc.h b/rts/System/Platform/Misc.h index ff3d470c0d..5523024ba1 100644 --- a/rts/System/Platform/Misc.h +++ b/rts/System/Platform/Misc.h @@ -96,6 +96,7 @@ namespace Platform bool Is32BitEmulation(); bool IsRunningInDebugger(); + uint64_t TotalRAM(); uint64_t FreeDiskSpace(const std::string& path); uint32_t NativeWordSize(); // compiled process code uint32_t SystemWordSize(); // host operating system From b4078eeb33e4ce3e23f6cffd9bed0f799b0a8370 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Fri, 20 Dec 2024 11:34:56 +0100 Subject: [PATCH 2/9] Make Plaform.availableMemory report MBs instead of bytes. --- rts/Lua/LuaConstPlatform.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rts/Lua/LuaConstPlatform.cpp b/rts/Lua/LuaConstPlatform.cpp index 2b77cc57ad..327c67d0ae 100644 --- a/rts/Lua/LuaConstPlatform.cpp +++ b/rts/Lua/LuaConstPlatform.cpp @@ -109,7 +109,7 @@ bool LuaConstPlatform::PushEntries(lua_State* L) LuaPushNamedString(L, "hwConfig", Platform::GetHardwareStr()); LuaPushNamedNumber(L, "cpuLogicalCores", Threading::GetLogicalCpuCores()); LuaPushNamedNumber(L, "cpuPhysicalCores", Threading::GetPhysicalCpuCores()); - LuaPushNamedNumber(L, "totalMemory", Platform::TotalRAM()); + LuaPushNamedNumber(L, "totalMemory", Platform::TotalRAM()/1e6); LuaPushNamedString(L, "sysInfoHash", Platform::GetSysInfoHash()); LuaPushNamedString(L, "macAddrHash", Platform::GetMacAddrHash()); From 99dd4545214a7471c011136ccdc3457e7a62f1cc Mon Sep 17 00:00:00 2001 From: Saurtron Date: Fri, 20 Dec 2024 11:36:37 +0100 Subject: [PATCH 3/9] Document the new field. --- rts/Lua/LuaConstPlatform.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/rts/Lua/LuaConstPlatform.cpp b/rts/Lua/LuaConstPlatform.cpp index 327c67d0ae..57eac55247 100644 --- a/rts/Lua/LuaConstPlatform.cpp +++ b/rts/Lua/LuaConstPlatform.cpp @@ -34,6 +34,7 @@ * @number sdlVersionLinkedMajor * @number sdlVersionLinkedMinor * @number sdlVersionLinkedPatch + * @number totalMemory Total physical system RAM in MBs. * @bool glSupportNonPowerOfTwoTex * @bool glSupportTextureQueryLOD * @bool glSupport24bitDepthBuffer From 180127d9adaa8b6b9e571fd08c16e27c60999f47 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Fri, 20 Dec 2024 11:42:35 +0100 Subject: [PATCH 4/9] totalMemory -> totalRAM. --- rts/Lua/LuaConstPlatform.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rts/Lua/LuaConstPlatform.cpp b/rts/Lua/LuaConstPlatform.cpp index 57eac55247..0a8410570b 100644 --- a/rts/Lua/LuaConstPlatform.cpp +++ b/rts/Lua/LuaConstPlatform.cpp @@ -34,7 +34,7 @@ * @number sdlVersionLinkedMajor * @number sdlVersionLinkedMinor * @number sdlVersionLinkedPatch - * @number totalMemory Total physical system RAM in MBs. + * @number totalRAM Total physical system RAM in MBs. * @bool glSupportNonPowerOfTwoTex * @bool glSupportTextureQueryLOD * @bool glSupport24bitDepthBuffer @@ -110,7 +110,7 @@ bool LuaConstPlatform::PushEntries(lua_State* L) LuaPushNamedString(L, "hwConfig", Platform::GetHardwareStr()); LuaPushNamedNumber(L, "cpuLogicalCores", Threading::GetLogicalCpuCores()); LuaPushNamedNumber(L, "cpuPhysicalCores", Threading::GetPhysicalCpuCores()); - LuaPushNamedNumber(L, "totalMemory", Platform::TotalRAM()/1e6); + LuaPushNamedNumber(L, "totalRAM", Platform::TotalRAM()/1e6); LuaPushNamedString(L, "sysInfoHash", Platform::GetSysInfoHash()); LuaPushNamedString(L, "macAddrHash", Platform::GetMacAddrHash()); From 0d598f6e4538d53828cb79adc66bad6968cd8f3d Mon Sep 17 00:00:00 2001 From: Saurtron Date: Thu, 2 Jan 2025 11:06:16 +0100 Subject: [PATCH 5/9] Move TotalRAM() to Platform/Hardware.h with platform specific implementations. --- rts/Lua/LuaConstPlatform.cpp | 1 + rts/System/CMakeLists.txt | 3 ++ rts/System/Platform/Hardware.h | 14 +++++++++ rts/System/Platform/Linux/Hardware.cpp | 19 ++++++++++++ rts/System/Platform/Misc.cpp | 43 +++----------------------- rts/System/Platform/Misc.h | 1 - rts/System/Platform/Win/Hardware.cpp | 25 +++++++++++++++ rts/System/Platform/Win/WinVersion.cpp | 11 ++----- 8 files changed, 69 insertions(+), 48 deletions(-) create mode 100644 rts/System/Platform/Hardware.h create mode 100644 rts/System/Platform/Linux/Hardware.cpp create mode 100644 rts/System/Platform/Win/Hardware.cpp diff --git a/rts/Lua/LuaConstPlatform.cpp b/rts/Lua/LuaConstPlatform.cpp index 0a8410570b..ddc90e0435 100644 --- a/rts/Lua/LuaConstPlatform.cpp +++ b/rts/Lua/LuaConstPlatform.cpp @@ -2,6 +2,7 @@ #include "LuaConstPlatform.h" #include "LuaUtils.h" +#include "System/Platform/Hardware.h" #include "System/Platform/Misc.h" #include "Rendering/GlobalRendering.h" #include "Rendering/GlobalRenderingInfo.h" diff --git a/rts/System/CMakeLists.txt b/rts/System/CMakeLists.txt index 6b24caf79c..7f65fb4287 100644 --- a/rts/System/CMakeLists.txt +++ b/rts/System/CMakeLists.txt @@ -127,11 +127,13 @@ make_global_var(sources_engine_System_Log_sinkOutputDebugString ) set(sources_engine_System_Platform_Linux "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Linux/CrashHandler.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Linux/Hardware.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Linux/SoLib.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Linux/MessageBox.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Linux/WindowManagerHelper.cpp" ) set(sources_engine_System_Platform_Mac + "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Linux/Hardware.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Linux/SoLib.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Mac/MessageBox.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Mac/CrashHandler.cpp" @@ -141,6 +143,7 @@ set(sources_engine_System_Platform_Mac set(sources_engine_System_Platform_Windows "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Win/CrashHandler.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Win/DllLib.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Win/Hardware.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Win/MessageBox.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Win/seh.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Platform/Win/WinVersion.cpp" diff --git a/rts/System/Platform/Hardware.h b/rts/System/Platform/Hardware.h new file mode 100644 index 0000000000..6acfefb0ea --- /dev/null +++ b/rts/System/Platform/Hardware.h @@ -0,0 +1,14 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ + +#ifndef PLATFORM_HARDWARE_H +#define PLATFORM_HARDWARE_H + +#include + +namespace Platform +{ + uint64_t TotalRAM(); + uint64_t TotalPageFile(); +} + +#endif // PLATFORM_HARDWARE_H diff --git a/rts/System/Platform/Linux/Hardware.cpp b/rts/System/Platform/Linux/Hardware.cpp new file mode 100644 index 0000000000..4ecd07dde7 --- /dev/null +++ b/rts/System/Platform/Linux/Hardware.cpp @@ -0,0 +1,19 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ + +#include + +#include "System/Platform/Hardware.h" + +namespace Platform +{ + uint64_t TotalRAM() { + long pages = sysconf(_SC_PHYS_PAGES); + long page_size = sysconf(_SC_PAGE_SIZE); + return pages * page_size; + } + uint64_t TotalPageFile() { + // NOT IMPLEMENTED + return 0; + } + +} diff --git a/rts/System/Platform/Misc.cpp b/rts/System/Platform/Misc.cpp index 8b9d2ffcb5..3e9f3727dc 100644 --- a/rts/System/Platform/Misc.cpp +++ b/rts/System/Platform/Misc.cpp @@ -58,6 +58,7 @@ #include "System/StringUtil.h" #include "System/Log/ILog.h" #include "System/FileSystem/FileSystem.h" +#include "System/Platform/Hardware.h" #if defined(_WIN32) #include "System/Platform/Win/WinVersion.h" #endif @@ -410,7 +411,6 @@ namespace Platform std::string ret; FILE* cpuInfo = fopen("/proc/cpuinfo", "r"); - FILE* memInfo = fopen("/proc/meminfo", "r"); char buf[1024]; char tmp[1024]; @@ -432,49 +432,14 @@ namespace Platform fclose(cpuInfo); } - if (memInfo != nullptr) { - while (fgets(buf, sizeof(buf), memInfo) != nullptr) { - if (strstr(buf, "MemTotal") != nullptr) { - const char* s = strstr(buf, ": ") + 2; - const char* e = s; - - for ( ; !std::isdigit(*s); s++) {} - for (e = s; std::isdigit(*e); e++) {} - - memset(tmp, 0, sizeof(tmp)); - memcpy(tmp, s, e - s); - - // sufficient up to 4TB - uint32_t kb = 0; - - sscanf(tmp, "%u", &kb); - sprintf(tmp, "%u", kb / 1024); - - ret += (std::string(tmp) + "MB RAM"); - break; - } - } - - fclose(memInfo); - } + uint64_t totalRam = TotalRAM(); + sprintf(tmp, "%lu", totalRam / (1024*1024)); + ret += (std::string(tmp) + "MB RAM"); return ret; } #endif - uint64_t TotalRAM() { -#ifdef _WIN32 - MEMORYSTATUSEX status; - status.dwLength = sizeof(status); - GlobalMemoryStatusEx(&status); - return status.ullTotalPhys; -#else - long pages = sysconf(_SC_PHYS_PAGES); - long page_size = sysconf(_SC_PAGE_SIZE); - return pages * page_size; -#endif - } - std::string GetSysInfoHash() { std::vector sysInfo; diff --git a/rts/System/Platform/Misc.h b/rts/System/Platform/Misc.h index 5523024ba1..ff3d470c0d 100644 --- a/rts/System/Platform/Misc.h +++ b/rts/System/Platform/Misc.h @@ -96,7 +96,6 @@ namespace Platform bool Is32BitEmulation(); bool IsRunningInDebugger(); - uint64_t TotalRAM(); uint64_t FreeDiskSpace(const std::string& path); uint32_t NativeWordSize(); // compiled process code uint32_t SystemWordSize(); // host operating system diff --git a/rts/System/Platform/Win/Hardware.cpp b/rts/System/Platform/Win/Hardware.cpp new file mode 100644 index 0000000000..d104f395a4 --- /dev/null +++ b/rts/System/Platform/Win/Hardware.cpp @@ -0,0 +1,25 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ + +#include + +#include "System/Platform/Hardware.h" + +namespace Platform +{ + MEMORYSTATUSEX GetMemoryInfo() { + MEMORYSTATUSEX status; + status.dwLength = sizeof(status); + GlobalMemoryStatusEx(&status); + return status; + } + + uint64_t TotalRAM() { + MEMORYSTATUSEX status = GetMemoryInfo(); + return status.ullTotalPhys; + } + + uint64_t TotalPageFile() { + MEMORYSTATUSEX status = GetMemoryInfo(); + return status.ullTotalPageFile; + } +} diff --git a/rts/System/Platform/Win/WinVersion.cpp b/rts/System/Platform/Win/WinVersion.cpp index 50bc674a26..79e92012eb 100644 --- a/rts/System/Platform/Win/WinVersion.cpp +++ b/rts/System/Platform/Win/WinVersion.cpp @@ -67,6 +67,7 @@ typedef _Return_type_success_(return >= 0) LONG NTSTATUS; #define SM_SERVERR2 89 #endif +#include "System/Platform/Hardware.h" // from http://msdn.microsoft.com/en-us/library/ms724429(VS.85).aspx // Windows version table mapping (as of november 2018) is as follows: @@ -325,13 +326,7 @@ std::string windows::GetHardwareString() oss << "cannot open key with processor data; "; } - MEMORYSTATUSEX statex; - constexpr int div = 1024 * 1024; - statex.dwLength = sizeof(statex); - - GlobalMemoryStatusEx(&statex); - - oss << (statex.ullTotalPhys / div) << "MB RAM, "; - oss << (statex.ullTotalPageFile / div) << "MB pagefile"; + oss << (TotalRAM() / div) << "MB RAM, "; + oss << (TotalPageFile() / div) << "MB pagefile"; return oss.str(); } From c3d543f44ff92b8e0d6f99b4277358d15c73f523 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Thu, 2 Jan 2025 11:32:41 +0100 Subject: [PATCH 6/9] Add Hardware.cpp to dedicated builds. --- rts/builds/dedicated/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rts/builds/dedicated/CMakeLists.txt b/rts/builds/dedicated/CMakeLists.txt index 6562b6ab4b..962dd97ef6 100644 --- a/rts/builds/dedicated/CMakeLists.txt +++ b/rts/builds/dedicated/CMakeLists.txt @@ -127,8 +127,11 @@ set(system_files ${ENGINE_SRC_ROOT_DIR}/System/float4.cpp ) if (WIN32) + list(APPEND system_files ${ENGINE_SRC_ROOT_DIR}/System/Platform/Win/Hardware.cpp) list(APPEND system_files ${ENGINE_SRC_ROOT_DIR}/System/Platform/Win/WinVersion.cpp) -endif (WIN32) +else () + list(APPEND system_files ${ENGINE_SRC_ROOT_DIR}/System/Platform/Linux/Hardware.cpp) +endif () set(engineDedicatedSources ${system_files} From d01eb1a4c76adb1eceb6e474f1b0b3d460cd21c1 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Thu, 2 Jan 2025 11:35:46 +0100 Subject: [PATCH 7/9] Add Platform namespace. --- rts/System/Platform/Win/WinVersion.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rts/System/Platform/Win/WinVersion.cpp b/rts/System/Platform/Win/WinVersion.cpp index 79e92012eb..07ef0bd717 100644 --- a/rts/System/Platform/Win/WinVersion.cpp +++ b/rts/System/Platform/Win/WinVersion.cpp @@ -326,7 +326,7 @@ std::string windows::GetHardwareString() oss << "cannot open key with processor data; "; } - oss << (TotalRAM() / div) << "MB RAM, "; - oss << (TotalPageFile() / div) << "MB pagefile"; + oss << (Platform::TotalRAM() / div) << "MB RAM, "; + oss << (Platform::TotalPageFile() / div) << "MB pagefile"; return oss.str(); } From 2f3fc0e3878b5f28d19e9b4084a312121b076bae Mon Sep 17 00:00:00 2001 From: Saurtron Date: Thu, 2 Jan 2025 11:43:47 +0100 Subject: [PATCH 8/9] Add Hardware.cpp to demotool and unitsync CMakeLists. --- tools/DemoTool/CMakeLists.txt | 2 ++ tools/unitsync/CMakeLists.txt | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/DemoTool/CMakeLists.txt b/tools/DemoTool/CMakeLists.txt index c055f50741..5456301130 100644 --- a/tools/DemoTool/CMakeLists.txt +++ b/tools/DemoTool/CMakeLists.txt @@ -21,10 +21,12 @@ set(PLATFORM_SRCS "") set(PLATFORM_LIBS "") if (WIN32) + list(APPEND PLATFORM_SRCS "${ENGINE_SRC_ROOT_DIR}/System/Platform/Win/Hardware.cpp") list(APPEND PLATFORM_SRCS "${ENGINE_SRC_ROOT_DIR}/System/Platform/Win/WinVersion.cpp") list(APPEND PLATFORM_LIBS "iphlpapi") elseif (UNIX) + list(APPEND PLATFORM_SRCS "${ENGINE_SRC_ROOT_DIR}/System/Platform/Linux/Hardware.cpp") list(APPEND PLATFORM_LIBS "${CMAKE_DL_LIBS}") endif (WIN32) diff --git a/tools/unitsync/CMakeLists.txt b/tools/unitsync/CMakeLists.txt index cb07b5d0df..406470bc42 100644 --- a/tools/unitsync/CMakeLists.txt +++ b/tools/unitsync/CMakeLists.txt @@ -92,9 +92,11 @@ set(main_files "${ENGINE_SRC_ROOT}/System/StringUtil.cpp" ) if (WIN32) + list(APPEND main_files "${ENGINE_SRC_ROOT}/System/Platform/Win/Hardware.cpp") list(APPEND main_files "${ENGINE_SRC_ROOT}/System/Platform/Win/WinVersion.cpp") else (WIN32) - list(APPEND main_files "${ENGINE_SRC_ROOT}/System/Platform/Linux/ThreadSupport.cpp") + list(APPEND main_files "${ENGINE_SRC_ROOT}/System/Platform/Linux/Hardware.cpp") + list(APPEND main_files "${ENGINE_SRC_ROOT}/System/Platform/Linux/ThreadSupport.cpp") endif (WIN32) set(unitsync_files From f12d4c4872fbfac9f7af2ad37ba97b33f66f3a84 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Thu, 2 Jan 2025 12:20:18 +0100 Subject: [PATCH 9/9] Re-add missing divisor. --- rts/System/Platform/Win/WinVersion.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/rts/System/Platform/Win/WinVersion.cpp b/rts/System/Platform/Win/WinVersion.cpp index 07ef0bd717..2593d87597 100644 --- a/rts/System/Platform/Win/WinVersion.cpp +++ b/rts/System/Platform/Win/WinVersion.cpp @@ -326,6 +326,7 @@ std::string windows::GetHardwareString() oss << "cannot open key with processor data; "; } + constexpr int div = 1024 * 1024; oss << (Platform::TotalRAM() / div) << "MB RAM, "; oss << (Platform::TotalPageFile() / div) << "MB pagefile"; return oss.str();