system: add helper for fetching total system memory
What changed, and why it matters
This commit adds a simple helper function that asks the operating system how much total physical memory (RAM) the computer has. It supports Linux, macOS, and Windows, and returns no value on other platforms. It also adds a test to make sure the reported number looks reasonable. There is no security issue visible in this change.
No security action needed. This is a benign utility addition. Normal code-review approval is sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces GetTotalRAM() in src/common/system.cpp/.h. On Windows it uses GlobalMemoryStatusEx(MEMORYSTATUSEX.ullTotalPhys); on Linux/macOS it uses sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE). The result is clamped to size_t max and returned as std::optional
Changed components
src/common/system.cppsrc/common/system.hsrc/test/system_tests.cppInspect captured patch +40 / −4
diff --git a/src/common/system.cpp b/src/common/system.cpp
index 7af792db..cf032adf 100644
--- a/src/common/system.cpp
+++ b/src/common/system.cpp
@@ -11,19 +11,25 @@
#include <util/string.h>
#include <util/time.h>
-#ifndef WIN32
-#include <sys/stat.h>
-#else
-#include <compat/compat.h>
+#ifdef WIN32
#include <codecvt>
+#include <compat/compat.h>
+#include <windows.h>
+#else
+#include <sys/stat.h>
+#include <unistd.h>
#endif
#ifdef HAVE_MALLOPT_ARENA_MAX
#include <malloc.h>
#endif
+#include <algorithm>
+#include <cstddef>
+#include <cstdint>
#include <cstdlib>
#include <locale>
+#include <optional>
#include <stdexcept>
#include <string>
#include <thread>
@@ -105,6 +111,17 @@ int GetNumCores()
return std::thread::hardware_concurrency();
}
+std::optional<size_t> GetTotalRAM()
+{
+ auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }};
+#ifdef WIN32
+ if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return clamp(m.ullTotalPhys);
+#elif defined(__linux__) || defined(__APPLE__)
+ if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return clamp(1ULL * p * s);
+#endif
+ return std::nullopt;
+}
+
// Obtain the application startup time (used for uptime calculation)
int64_t GetStartupTime()
{
diff --git a/src/common/system.h b/src/common/system.h
index a4b56be9..2184f1d4 100644
--- a/src/common/system.h
+++ b/src/common/system.h
@@ -9,6 +9,7 @@
#include <bitcoin-build-config.h> // IWYU pragma: keep
#include <cstdint>
+#include <optional>
#include <string>
// Application startup time (used for uptime calculation)
@@ -29,4 +30,9 @@ void runCommand(const std::string& strCommand);
*/
int GetNumCores();
+/**
+ * Return the total RAM available on the current system, if detectable.
+ */
+std::optional<size_t> GetTotalRAM();
+
#endif // BITCOIN_COMMON_SYSTEM_H
diff --git a/src/test/system_tests.cpp b/src/test/system_tests.cpp
index dec4d418..53db3200 100644
--- a/src/test/system_tests.cpp
+++ b/src/test/system_tests.cpp
@@ -8,6 +8,8 @@
#include <common/run_command.h>
#include <univalue.h>
+#include <common/system.h>
+
#ifdef ENABLE_EXTERNAL_SIGNER
#include <util/subprocess.h>
#endif // ENABLE_EXTERNAL_SIGNER
@@ -16,6 +18,17 @@
BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup)
+BOOST_AUTO_TEST_CASE(total_ram)
+{
+ BOOST_CHECK_GE(GetTotalRAM(), 1000_MiB);
+
+ if constexpr (SIZE_MAX == UINT64_MAX) {
+ // Upper bound check only on 64-bit: 32-bit systems can reasonably have max memory,
+ // but extremely large values on 64-bit likely indicate detection errors
+ BOOST_CHECK_LT(GetTotalRAM(), 10'000'000_MiB); // >10 TiB memory is unlikely
+ }
+}
+
#ifdef ENABLE_EXTERNAL_SIGNER
BOOST_AUTO_TEST_CASE(run_command)
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.