From 6791b683713e62eac436c1f0d48038ea6a7ced95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Wed, 25 Feb 2026 10:25:29 +0100 Subject: [PATCH 1/6] system: migrate `TryGetTotalRam()` to `common/system_ram.cpp` Move the OS-dependent total RAM query from `common/system.cpp` into a dedicated `common/system_ram.cpp` translation unit and declare it in `common/system_ram.h`. Update call sites to include `common/system_ram.h` and rename `GetTotalRAM()` to `TryGetTotalRam()`. Add `common/system_ram` to `FILES_WITH_ENFORCED_IWYU` so this new source is checked at error level. Since we use it for more than just warnings now, make `system_ram_tests` require RAM detection instead of skipping when unavailable. Co-authored-by: sipa Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> --- ci/test/03_test_script.sh | 2 +- src/CMakeLists.txt | 1 + src/common/system.cpp | 16 ---------------- src/common/system.h | 9 --------- src/common/system_ram.cpp | 30 ++++++++++++++++++++++++++++++ src/common/system_ram.h | 17 +++++++++++++++++ src/node/caches.cpp | 6 +++--- src/test/CMakeLists.txt | 1 - src/test/system_ram_tests.cpp | 13 ++++--------- 9 files changed, 56 insertions(+), 39 deletions(-) create mode 100644 src/common/system_ram.cpp create mode 100644 src/common/system_ram.h diff --git a/ci/test/03_test_script.sh b/ci/test/03_test_script.sh index ff9f2fd60a5e..9ee7073ebce1 100755 --- a/ci/test/03_test_script.sh +++ b/ci/test/03_test_script.sh @@ -201,7 +201,7 @@ fi if [[ "${RUN_IWYU}" == true ]]; then # TODO: Consider enforcing IWYU across the entire codebase. - FILES_WITH_ENFORCED_IWYU="/src/((crypto|index|kernel|primitives|univalue/(lib|test)|zmq)/.*\\.cpp|node/blockstorage\\.cpp|node/utxo_snapshot\\.cpp|core_io\\.cpp|signet\\.cpp)" + FILES_WITH_ENFORCED_IWYU="/src/((crypto|index|kernel|primitives|univalue/(lib|test)|zmq)/.*\\.cpp|node/blockstorage\\.cpp|node/utxo_snapshot\\.cpp|core_io\\.cpp|signet\\.cpp|common/system_ram\\.cpp)" jq --arg patterns "$FILES_WITH_ENFORCED_IWYU" 'map(select(.file | test($patterns)))' "${BASE_BUILD_DIR}/compile_commands.json" > "${BASE_BUILD_DIR}/compile_commands_iwyu_errors.json" jq --arg patterns "$FILES_WITH_ENFORCED_IWYU" 'map(select(.file | test($patterns) | not))' "${BASE_BUILD_DIR}/compile_commands.json" > "${BASE_BUILD_DIR}/compile_commands_iwyu_warnings.json" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ad18115bbc5f..55929ef172d8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -104,6 +104,7 @@ add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL common/settings.cpp common/signmessage.cpp common/system.cpp + common/system_ram.cpp common/url.cpp compressor.cpp core_io.cpp diff --git a/src/common/system.cpp b/src/common/system.cpp index 08c0c692711b..3abb60cc458e 100644 --- a/src/common/system.cpp +++ b/src/common/system.cpp @@ -111,22 +111,6 @@ int GetNumCores() return std::thread::hardware_concurrency(); } -std::optional GetTotalRAM() -{ - [[maybe_unused]] auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits::max()})); }}; -#ifdef WIN32 - if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return clamp(m.ullTotalPhys); -#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; -} - namespace { const auto g_startup_time{SteadyClock::now()}; } // namespace diff --git a/src/common/system.h b/src/common/system.h index a3100fecbc17..c6c0305cf3fa 100644 --- a/src/common/system.h +++ b/src/common/system.h @@ -8,10 +8,6 @@ #include // IWYU pragma: keep #include - -#include -#include -#include #include /// Monotonic uptime (not affected by system time changes). @@ -32,9 +28,4 @@ void runCommand(const std::string& strCommand); */ int GetNumCores(); -/** - * Return the total RAM available on the current system, if detectable. - */ -std::optional GetTotalRAM(); - #endif // BITCOIN_COMMON_SYSTEM_H diff --git a/src/common/system_ram.cpp b/src/common/system_ram.cpp new file mode 100644 index 000000000000..5904e924adec --- /dev/null +++ b/src/common/system_ram.cpp @@ -0,0 +1,30 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#ifdef WIN32 +#include +#else +#include +#endif + +#include +#include +#include + +std::optional TryGetTotalRam() +{ + static const auto total_ram{[]() -> std::optional { + [[maybe_unused]] auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits::max()})); }}; +#ifdef WIN32 + if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return clamp(m.ullTotalPhys); +#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; + }()}; + return total_ram; +} diff --git a/src/common/system_ram.h b/src/common/system_ram.h new file mode 100644 index 000000000000..031f9c2768c6 --- /dev/null +++ b/src/common/system_ram.h @@ -0,0 +1,17 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_COMMON_SYSTEM_RAM_H +#define BITCOIN_COMMON_SYSTEM_RAM_H + +#include +#include + +/** + * Return the total RAM available on the current system, if detectable. + */ +std::optional TryGetTotalRam(); + +#endif // BITCOIN_COMMON_SYSTEM_RAM_H diff --git a/src/node/caches.cpp b/src/node/caches.cpp index cb8afbc967d3..07812f5b9ff7 100644 --- a/src/node/caches.cpp +++ b/src/node/caches.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include #include @@ -36,7 +36,7 @@ namespace node { size_t GetDefaultDBCache() { if constexpr (sizeof(void*) >= 8) { - if (GetTotalRAM().value_or(0) >= HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM) { + if (TryGetTotalRam().value_or(0) >= HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM) { return HIGH_DEFAULT_DBCACHE; } } @@ -73,7 +73,7 @@ CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes) void LogOversizedDbCache(const ArgsManager& args) noexcept { - if (const auto total_ram{GetTotalRAM()}) { + if (const auto total_ram{TryGetTotalRam()}) { const size_t db_cache{CalculateDbCacheBytes(args)}; if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) { InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."), diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 7d9dccba8ef7..d5d3514d6435 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -201,7 +201,6 @@ function(add_boost_test source_file) SKIP_REGULAR_EXPRESSION "no test cases matching filter" "skipping script_assets_test" - "skipping total_ram" ) endforeach() endfunction() diff --git a/src/test/system_ram_tests.cpp b/src/test/system_ram_tests.cpp index 8ad622642f5f..72155157dbd5 100644 --- a/src/test/system_ram_tests.cpp +++ b/src/test/system_ram_tests.cpp @@ -2,7 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or https://opensource.org/license/mit/. -#include +#include #include #include @@ -14,18 +14,13 @@ 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); + const auto total{*Assert(TryGetTotalRam())}; + BOOST_CHECK_GE(total, 1024_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_CHECK_LT(total, 10'000'000_MiB); // >10 TiB memory is unlikely } } From cb89964064a8692cc52273cf6b741969a202b8be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Fri, 20 Feb 2026 16:56:19 +0100 Subject: [PATCH 2/6] node: share existing `dbcache` default selection The current `dbcache` policy is split across `node`, `qt`, and `kernel`. The node startup path already chooses between `450 MiB` and `1024 MiB` based on detected RAM, while `bitcoinkernel` and the Qt settings migration still use fixed `450 MiB` values. Move the default selection and oversized warning helpers into dedicated `node/dbcache` code and use them from all current callers. This keeps the existing default selection logic in one place and makes the other users follow that same policy. This also changes the low-memory oversized warning to use the shared default helper instead of a fixed `450 MiB` cap. It changes `bitcoinkernel` and the Qt migration on 64-bit systems with at least 4 GiB RAM, where they now use the same `1024 MiB` default as the node path instead of a fixed `450 MiB`. Co-authored-by: Luke Dashjr --- ci/test/03_test_script.sh | 2 +- src/CMakeLists.txt | 1 + src/init.cpp | 1 + src/kernel/CMakeLists.txt | 2 ++ src/kernel/bitcoinkernel.cpp | 5 +++-- src/kernel/caches.h | 2 -- src/node/caches.cpp | 26 ++++++-------------------- src/node/caches.h | 13 ------------- src/node/dbcache.cpp | 26 ++++++++++++++++++++++++++ src/node/dbcache.h | 29 +++++++++++++++++++++++++++++ src/qt/optionsdialog.cpp | 2 +- src/qt/optionsmodel.cpp | 4 ++-- src/test/caches_tests.cpp | 9 +++++---- 13 files changed, 77 insertions(+), 45 deletions(-) create mode 100644 src/node/dbcache.cpp create mode 100644 src/node/dbcache.h diff --git a/ci/test/03_test_script.sh b/ci/test/03_test_script.sh index 9ee7073ebce1..1b6831ae562b 100755 --- a/ci/test/03_test_script.sh +++ b/ci/test/03_test_script.sh @@ -201,7 +201,7 @@ fi if [[ "${RUN_IWYU}" == true ]]; then # TODO: Consider enforcing IWYU across the entire codebase. - FILES_WITH_ENFORCED_IWYU="/src/((crypto|index|kernel|primitives|univalue/(lib|test)|zmq)/.*\\.cpp|node/blockstorage\\.cpp|node/utxo_snapshot\\.cpp|core_io\\.cpp|signet\\.cpp|common/system_ram\\.cpp)" + FILES_WITH_ENFORCED_IWYU="/src/((crypto|index|kernel|primitives|univalue/(lib|test)|zmq)/.*\\.cpp|node/blockstorage\\.cpp|node/utxo_snapshot\\.cpp|core_io\\.cpp|signet\\.cpp|common/system_ram\\.cpp|node/dbcache\\.cpp)" jq --arg patterns "$FILES_WITH_ENFORCED_IWYU" 'map(select(.file | test($patterns)))' "${BASE_BUILD_DIR}/compile_commands.json" > "${BASE_BUILD_DIR}/compile_commands_iwyu_errors.json" jq --arg patterns "$FILES_WITH_ENFORCED_IWYU" 'map(select(.file | test($patterns) | not))' "${BASE_BUILD_DIR}/compile_commands.json" > "${BASE_BUILD_DIR}/compile_commands_iwyu_warnings.json" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 55929ef172d8..cf597cd6c702 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -211,6 +211,7 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL node/blockmanager_args.cpp node/blockstorage.cpp node/caches.cpp + node/dbcache.cpp node/chainstate.cpp node/chainstatemanager_args.cpp node/coin.cpp diff --git a/src/init.cpp b/src/init.cpp index fb4b8fa9e27a..ebd4a2404afa 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 244f0098fdf9..9e240f66085b 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -18,6 +18,7 @@ add_library(bitcoinkernel cs_main.cpp disconnected_transactions.cpp mempool_removal_reason.cpp + ../common/system_ram.cpp ../arith_uint256.cpp ../chain.cpp ../coins.cpp @@ -32,6 +33,7 @@ add_library(bitcoinkernel ../hash.cpp ../logging.cpp ../node/blockstorage.cpp + ../node/dbcache.cpp ../node/chainstate.cpp ../node/utxo_snapshot.cpp ../policy/ephemeral_policy.cpp diff --git a/src/kernel/bitcoinkernel.cpp b/src/kernel/bitcoinkernel.cpp index 09b4853fa91e..5da4ea24795d 100644 --- a/src/kernel/bitcoinkernel.cpp +++ b/src/kernel/bitcoinkernel.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include