Skip to content

Commit 5226a92

Browse files
l0rincfanquake
authored andcommitted
coins: warn on oversized -dbcache
Oversized allocations can cause out-of-memory errors or [heavy swapping](getumbrel/umbrel-os#64 (comment)), [grinding the system to a halt](https://x.com/murchandamus/status/1964432335849607224). `LogOversizedDbCache()` now emits a startup warning if the configured `-dbcache` exceeds a cap derived from system RAM, using the same parsing/clamping as cache sizing via CalculateDbCacheBytes(). This isn't meant as a recommended setting, rather a likely upper limit. Note that we're not modifying the set value, just issuing a warning. Also note that the 75% calculation is rounded for the last two numbers since we have to divide first before multiplying, otherwise we wouldn't stay inside size_t on 32-bit systems - and this was simpler than casting back and forth. We could have chosen the remaining free memory for the warning (e.g. warn if free memory is less than 1 GiB), but this is just a heuristic, we assumed that on systems with a lot of memory, other processes are also running, while memory constrained ones run only Core. If total RAM < 2 GiB, cap is `DEFAULT_DB_CACHE` (`450 MiB`), otherwise it's 75% of total RAM. The threshold is chosen to be close to values commonly used in [raspiblitz](https://github.com/raspiblitz/raspiblitz/blob/dev/home.admin/_provision.setup.sh#L98-L115) for common setups: | Total RAM | `dbcache` (MiB) | raspiblitz % | proposed cap (MiB) | |----------:|----------------:|-------------:|-------------------:| | 1 GiB | 512 | 50.0% | 450* | | 2 GiB | 1536 | 75.0% | 1536 | | 4 GiB | 2560 | 62.5% | 3072 | | 8 GiB | 4096 | 50.0% | 6144 | | 16 GiB | 4096 | 25.0% | 12288 | | 32 GiB | 4096 | 12.5% | 24576 | [Umbrel issues](getumbrel/umbrel-os#64 (comment)) also mention 75% being the upper limit. Starting `bitcoind` on an 8 GiB rpi4b with a dbcache of 7 GiB: > ./build/bin/bitcoind -dbcache=7000 warns now as follows: ``` 2025-09-07T17:24:29Z [warning] A 7000 MiB dbcache may be too large for a system memory of only 7800 MiB. 2025-09-07T17:24:29Z Cache configuration: 2025-09-07T17:24:29Z * Using 2.0 MiB for block index database 2025-09-07T17:24:29Z * Using 8.0 MiB for chain state database 2025-09-07T17:24:29Z * Using 6990.0 MiB for in-memory UTXO set (plus up to 286.1 MiB of unused mempool space) ``` Besides the [godbolt](https://godbolt.org/z/EPsaE3xTj) reproducers for the new total memory method, we also tested the warnings manually on: - [x] Apple M4 Max, macOS 15.6.1 - [x] Intel Core i9-9900K, Ubuntu 24.04.2 LTS - [x] Raspberry Pi 4 Model B, Armbian Linux 6.12.22-current-bcm2711 - [x] Intel Xeon x64, Windows 11 Home Version 24H2, OS Build 26100.4351 Co-authored-by: stickies-v <[email protected]> Co-authored-by: Hodlinator <[email protected]> Co-authored-by: w0xlt <[email protected]> Github-Pull: #33333 Rebased-From: 168360f
1 parent 49d4ebc commit 5226a92

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17331733
// ********************************************************* Step 7: load block chain
17341734

17351735
// cache size calculations
1736+
node::LogOversizedDbCache(args);
17361737
const auto [index_cache_sizes, kernel_cache_sizes] = CalculateCacheSizes(args, g_enabled_filter_types.size());
17371738

17381739
LogInfo("Cache configuration:");

src/node/caches.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
#include <node/caches.h>
66

77
#include <common/args.h>
8+
#include <common/system.h>
89
#include <index/txindex.h>
910
#include <kernel/caches.h>
1011
#include <logging.h>
12+
#include <node/interface_ui.h>
13+
#include <tinyformat.h>
1114
#include <util/byte_units.h>
1215

1316
#include <algorithm>
@@ -23,16 +26,20 @@ static constexpr size_t MAX_FILTER_INDEX_CACHE{1024_MiB};
2326
static constexpr size_t MAX_32BIT_DBCACHE{1024_MiB};
2427

2528
namespace node {
26-
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
29+
size_t CalculateDbCacheBytes(const ArgsManager& args)
2730
{
28-
// Convert -dbcache from MiB units to bytes. The total cache is floored by MIN_DB_CACHE and capped by max size_t value.
29-
size_t total_cache{DEFAULT_DB_CACHE};
30-
if (std::optional<int64_t> db_cache = args.GetIntArg("-dbcache")) {
31+
if (auto db_cache{args.GetIntArg("-dbcache")}) {
3132
if (*db_cache < 0) db_cache = 0;
32-
uint64_t db_cache_bytes = SaturatingLeftShift<uint64_t>(*db_cache, 20);
33+
const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)};
3334
constexpr auto max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits<size_t>::max()};
34-
total_cache = std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, max_db_cache));
35+
return std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, max_db_cache));
3536
}
37+
return DEFAULT_DB_CACHE;
38+
}
39+
40+
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
41+
{
42+
size_t total_cache{CalculateDbCacheBytes(args)};
3643

3744
IndexCacheSizes index_sizes;
3845
index_sizes.tx_index = std::min(total_cache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);
@@ -44,4 +51,15 @@ CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
4451
}
4552
return {index_sizes, kernel::CacheSizes{total_cache}};
4653
}
54+
55+
void LogOversizedDbCache(const ArgsManager& args) noexcept
56+
{
57+
if (const auto total_ram{GetTotalRAM()}) {
58+
const size_t db_cache{CalculateDbCacheBytes(args)};
59+
if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) {
60+
InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."),
61+
db_cache >> 20, *total_ram >> 20)});
62+
}
63+
}
64+
}
4765
} // namespace node

src/node/caches.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ struct CacheSizes {
2727
kernel::CacheSizes kernel;
2828
};
2929
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
30+
constexpr bool ShouldWarnOversizedDbCache(size_t dbcache, size_t total_ram) noexcept
31+
{
32+
const size_t cap{(total_ram < 2048_MiB) ? DEFAULT_DB_CACHE : (total_ram / 100) * 75};
33+
return dbcache > cap;
34+
}
35+
36+
void LogOversizedDbCache(const ArgsManager& args) noexcept;
3037
} // namespace node
3138

3239
#endif // BITCOIN_NODE_CACHES_H

src/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ add_executable(test_bitcoin
2525
blockmanager_tests.cpp
2626
bloom_tests.cpp
2727
bswap_tests.cpp
28+
caches_tests.cpp
2829
chainstate_write_tests.cpp
2930
checkqueue_tests.cpp
3031
cluster_linearize_tests.cpp

src/test/caches_tests.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <node/caches.h>
2+
#include <util/byte_units.h>
3+
4+
#include <boost/test/unit_test.hpp>
5+
6+
using namespace node;
7+
8+
BOOST_AUTO_TEST_SUITE(caches_tests)
9+
10+
BOOST_AUTO_TEST_CASE(oversized_dbcache_warning)
11+
{
12+
// memory restricted setup - cap is DEFAULT_DB_CACHE (450 MiB)
13+
BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/4_MiB, /*total_ram=*/1024_MiB)); // Under cap
14+
BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/512_MiB, /*total_ram=*/1024_MiB)); // At cap
15+
BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/1500_MiB, /*total_ram=*/1024_MiB)); // Over cap
16+
17+
// 2 GiB RAM - cap is 75%
18+
BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/1500_MiB, /*total_ram=*/2048_MiB)); // Under cap
19+
BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/1600_MiB, /*total_ram=*/2048_MiB)); // Over cap
20+
21+
if constexpr (SIZE_MAX == UINT64_MAX) {
22+
// 4 GiB RAM - cap is 75%
23+
BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/2500_MiB, /*total_ram=*/4096_MiB)); // Under cap
24+
BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/3500_MiB, /*total_ram=*/4096_MiB)); // Over cap
25+
26+
// 8 GiB RAM - cap is 75%
27+
BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/6000_MiB, /*total_ram=*/8192_MiB)); // Under cap
28+
BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/7000_MiB, /*total_ram=*/8192_MiB)); // Over cap
29+
30+
// 16 GiB RAM - cap is 75%
31+
BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/10'000_MiB, /*total_ram=*/16384_MiB)); // Under cap
32+
BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/15'000_MiB, /*total_ram=*/16384_MiB)); // Over cap
33+
34+
// 32 GiB RAM - cap is 75%
35+
BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/20'000_MiB, /*total_ram=*/32768_MiB)); // Under cap
36+
BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/30'000_MiB, /*total_ram=*/32768_MiB)); // Over cap
37+
}
38+
}
39+
40+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)