Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Platform: add totalRAM. #1840

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rts/Lua/LuaConstPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -34,6 +35,7 @@
* @number sdlVersionLinkedMajor
* @number sdlVersionLinkedMinor
* @number sdlVersionLinkedPatch
* @number totalRAM Total physical system RAM in MBs.
* @bool glSupportNonPowerOfTwoTex
* @bool glSupportTextureQueryLOD
* @bool glSupport24bitDepthBuffer
Expand Down Expand Up @@ -109,6 +111,7 @@ bool LuaConstPlatform::PushEntries(lua_State* L)
LuaPushNamedString(L, "hwConfig", Platform::GetHardwareStr());
LuaPushNamedNumber(L, "cpuLogicalCores", Threading::GetLogicalCpuCores());
LuaPushNamedNumber(L, "cpuPhysicalCores", Threading::GetPhysicalCpuCores());
LuaPushNamedNumber(L, "totalRAM", Platform::TotalRAM()/1e6);

LuaPushNamedString(L, "sysInfoHash", Platform::GetSysInfoHash());
LuaPushNamedString(L, "macAddrHash", Platform::GetMacAddrHash());
Expand Down
3 changes: 3 additions & 0 deletions rts/System/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions rts/System/Platform/Hardware.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

namespace Platform
{
uint64_t TotalRAM();
uint64_t TotalPageFile();
}

#endif // PLATFORM_HARDWARE_H
19 changes: 19 additions & 0 deletions rts/System/Platform/Linux/Hardware.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <unistd.h>

#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;
}

}
31 changes: 4 additions & 27 deletions rts/System/Platform/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand All @@ -432,37 +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


std::string GetSysInfoHash() {
std::vector<uint8_t> sysInfo;

Expand Down
25 changes: 25 additions & 0 deletions rts/System/Platform/Win/Hardware.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <windows.h>

#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;
}
}
10 changes: 3 additions & 7 deletions rts/System/Platform/Win/WinVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -325,13 +326,8 @@ 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 << (Platform::TotalRAM() / div) << "MB RAM, ";
oss << (Platform::TotalPageFile() / div) << "MB pagefile";
return oss.str();
}
5 changes: 4 additions & 1 deletion rts/builds/dedicated/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions tools/DemoTool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion tools/unitsync/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down