Skip to content

Commit 34fefb6

Browse files
committed
Merge bitcoin/bitcoin#33435: system: improve handling around GetTotalRAM()
56791b5 test: split out `system_ram_tests` to signal when total ram cannot be determined (Lőrinc) 337a6e7 system: improve handling around GetTotalRAM() (Vasil Dimov) Pull request description: 1. Fix unused variable warning (bitcoin/bitcoin#33333 (comment)) 2. Enable `GetTotalRAM()` on other platforms where it was tested to work. 3. Skip the `GetTotalRAM()` unit test on unsupported platforms. Prior discussion: bitcoin/bitcoin#33333 (comment) ACKs for top commit: l0rinc: ACK 56791b5 hebasto: ACK 56791b5. Tree-SHA512: bc419aa55edad77473dbcf810f02d02fa0c45a6355a93d17f7881051117b753c584296ab3840893270ecdc9bb2bee0fe4e070607c6560b794e97a25da733c47d
2 parents 953544d + 56791b5 commit 34fefb6

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

src/common/system.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,15 @@ int GetNumCores()
113113

114114
std::optional<size_t> GetTotalRAM()
115115
{
116-
auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }};
116+
[[maybe_unused]] auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }};
117117
#ifdef WIN32
118118
if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return clamp(m.ullTotalPhys);
119-
#elif defined(__linux__) || defined(__APPLE__)
119+
#elif defined(__APPLE__) || \
120+
defined(__FreeBSD__) || \
121+
defined(__NetBSD__) || \
122+
defined(__OpenBSD__) || \
123+
defined(__illumos__) || \
124+
defined(__linux__)
120125
if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return clamp(1ULL * p * s);
121126
#endif
122127
return std::nullopt;

src/test/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ add_executable(test_bitcoin
102102
span_tests.cpp
103103
streams_tests.cpp
104104
sync_tests.cpp
105+
system_ram_tests.cpp
105106
system_tests.cpp
106107
testnet4_miner_tests.cpp
107108
timeoffsets_tests.cpp
@@ -197,7 +198,10 @@ function(add_boost_test source_file)
197198
COMMAND test_bitcoin --run_test=${test_suite_name} --catch_system_error=no --log_level=test_suite -- DEBUG_LOG_OUT
198199
)
199200
set_property(TEST ${test_suite_name} PROPERTY
200-
SKIP_REGULAR_EXPRESSION "no test cases matching filter" "skipping script_assets_test"
201+
SKIP_REGULAR_EXPRESSION
202+
"no test cases matching filter"
203+
"skipping script_assets_test"
204+
"skipping total_ram"
201205
)
202206
endforeach()
203207
endfunction()

src/test/system_ram_tests.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2025-present The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or https://opensource.org/license/mit/.
4+
5+
#include <common/system.h>
6+
#include <test/util/setup_common.h>
7+
8+
#include <boost/test/unit_test.hpp>
9+
10+
#include <cstdint>
11+
#include <optional>
12+
13+
BOOST_AUTO_TEST_SUITE(system_ram_tests)
14+
15+
BOOST_AUTO_TEST_CASE(total_ram)
16+
{
17+
const auto total{GetTotalRAM()};
18+
if (!total) {
19+
BOOST_WARN_MESSAGE(false, "skipping total_ram: total RAM unknown");
20+
return;
21+
}
22+
23+
BOOST_CHECK_GE(*total, 1000_MiB);
24+
25+
if constexpr (SIZE_MAX == UINT64_MAX) {
26+
// Upper bound check only on 64-bit: 32-bit systems can reasonably have max memory,
27+
// but extremely large values on 64-bit likely indicate detection errors
28+
BOOST_CHECK_LT(*total, 10'000'000_MiB); // >10 TiB memory is unlikely
29+
}
30+
}
31+
32+
BOOST_AUTO_TEST_SUITE_END()

src/test/system_tests.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <common/run_command.h>
99
#include <univalue.h>
1010

11-
#include <common/system.h>
12-
1311
#ifdef ENABLE_EXTERNAL_SIGNER
1412
#include <util/subprocess.h>
1513
#endif // ENABLE_EXTERNAL_SIGNER
@@ -18,17 +16,6 @@
1816

1917
BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup)
2018

21-
BOOST_AUTO_TEST_CASE(total_ram)
22-
{
23-
BOOST_CHECK_GE(GetTotalRAM(), 1000_MiB);
24-
25-
if constexpr (SIZE_MAX == UINT64_MAX) {
26-
// Upper bound check only on 64-bit: 32-bit systems can reasonably have max memory,
27-
// but extremely large values on 64-bit likely indicate detection errors
28-
BOOST_CHECK_LT(GetTotalRAM(), 10'000'000_MiB); // >10 TiB memory is unlikely
29-
}
30-
}
31-
3219
#ifdef ENABLE_EXTERNAL_SIGNER
3320

3421
BOOST_AUTO_TEST_CASE(run_command)

0 commit comments

Comments
 (0)