Merge bitcoin/bitcoin#33435: system: improve handling around GetTotalRAM()

56791b5829 test: split out `system_ram_tests` to signal when total ram cannot be determined (Lőrinc)
337a6e7386 system: improve handling around GetTotalRAM() (Vasil Dimov)

Pull request description:

  1. Fix unused variable warning (https://github.com/bitcoin/bitcoin/pull/33333#discussion_r2362493046)
  2. Enable `GetTotalRAM()` on other platforms where it was tested to work.
  3. Skip the `GetTotalRAM()` unit test on unsupported platforms.

  Prior discussion: https://github.com/bitcoin/bitcoin/pull/33333#discussion_r2362493046

ACKs for top commit:
  l0rinc:
    ACK 56791b5829
  hebasto:
    ACK 56791b5829.

Tree-SHA512: bc419aa55edad77473dbcf810f02d02fa0c45a6355a93d17f7881051117b753c584296ab3840893270ecdc9bb2bee0fe4e070607c6560b794e97a25da733c47d
This commit is contained in:
Hennadii Stepanov 2025-09-22 17:00:34 +01:00
commit 34fefb6335
No known key found for this signature in database
GPG Key ID: 410108112E7EA81F
4 changed files with 44 additions and 16 deletions

View File

@ -113,10 +113,15 @@ int GetNumCores()
std::optional<size_t> GetTotalRAM()
{
auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }};
[[maybe_unused]] 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__)
#elif defined(__APPLE__) || \
defined(__FreeBSD__) || \
defined(__NetBSD__) || \
defined(__OpenBSD__) || \
defined(__illumos__) || \
defined(__linux__)
if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return clamp(1ULL * p * s);
#endif
return std::nullopt;

View File

@ -102,6 +102,7 @@ add_executable(test_bitcoin
span_tests.cpp
streams_tests.cpp
sync_tests.cpp
system_ram_tests.cpp
system_tests.cpp
testnet4_miner_tests.cpp
timeoffsets_tests.cpp
@ -197,7 +198,10 @@ function(add_boost_test source_file)
COMMAND test_bitcoin --run_test=${test_suite_name} --catch_system_error=no --log_level=test_suite -- DEBUG_LOG_OUT
)
set_property(TEST ${test_suite_name} PROPERTY
SKIP_REGULAR_EXPRESSION "no test cases matching filter" "skipping script_assets_test"
SKIP_REGULAR_EXPRESSION
"no test cases matching filter"
"skipping script_assets_test"
"skipping total_ram"
)
endforeach()
endfunction()

View File

@ -0,0 +1,32 @@
// Copyright (c) 2025-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://opensource.org/license/mit/.
#include <common/system.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <cstdint>
#include <optional>
BOOST_AUTO_TEST_SUITE(system_ram_tests)
BOOST_AUTO_TEST_CASE(total_ram)
{
const auto total{GetTotalRAM()};
if (!total) {
BOOST_WARN_MESSAGE(false, "skipping total_ram: total RAM unknown");
return;
}
BOOST_CHECK_GE(*total, 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(*total, 10'000'000_MiB); // >10 TiB memory is unlikely
}
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -8,8 +8,6 @@
#include <common/run_command.h>
#include <univalue.h>
#include <common/system.h>
#ifdef ENABLE_EXTERNAL_SIGNER
#include <util/subprocess.h>
#endif // ENABLE_EXTERNAL_SIGNER
@ -18,17 +16,6 @@
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)