Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
30bfcdf to
0f93881
Compare
|
It took a few rounds to tame (I had a Kernel Panic myself when I saw missing |
| - `-dbcache=<n>` - the UTXO database cache size, this defaults to `450`. The unit is MiB (1024). | ||
| - The minimum value for `-dbcache` is 4. | ||
| - A lower `-dbcache` makes initial sync time much longer. After the initial sync, the effect is less pronounced for most use-cases, unless fast validation of blocks is important, such as for mining. | ||
| - `-dbcache=<n>` - UTXO database cache size in MiB (minimum `4`). |
There was a problem hiding this comment.
Why are we changing this so much?
I think we should just remove this defaults to 450`` from this line, and add the below point:
- Automatic default scales with system RAM and can be as high as 1000` MiB.`
Then keep the rest the same.
There was a problem hiding this comment.
Updated, simplified, thanks.
| ---------------------------------- | ||
|
|
||
| - When `-dbcache` is not set explicitly, Bitcoin Core now chooses a RAM-aware default that can be as high as `3000` MiB. | ||
| Explicit `-dbcache` values continue to override the automatic default. (#34641) |
There was a problem hiding this comment.
I think we should also say something like:
"To maintain the same behavior of previous releases, set dbcache to 450."
| //! Automatic -dbcache floor (bytes) | ||
| static constexpr size_t MIN_DEFAULT_DBCACHE{100_MiB}; | ||
| //! Automatic -dbcache cap (bytes) | ||
| static constexpr size_t MAX_DEFAULT_DBCACHE{3000_MiB}; |
There was a problem hiding this comment.
I think we should be more conservative and set this to 1GB as discussed in the IRC meeting. Such a large bump in memory might be unexpected, especially when running bitcoind alongside other applications and not a dedicated node machine.
| static constexpr size_t MAX_DEFAULT_DBCACHE{3000_MiB}; | |
| static constexpr size_t MAX_DEFAULT_DBCACHE{1000_MiB}; |
There was a problem hiding this comment.
The 1 GB limit was a suggestion for a constant dbcache, which I opposed because it was too high for cheaper nodes (and too low for performant ones).
But since we can scale with total memory now, it makes sense to go higher, especially since 3-4 GB already achieves most of the speedup we saw.
This way, we have a simple formula instead of custom values for each memory.
| System RAM | Automatic default |
|---|---|
| 1 GiB | 100 MiB |
| 2 GiB | 100 MiB |
| 4 GiB | 512 MiB |
| 8 GiB | 1536 MiB |
| 16 GiB | 3000 MiB |
There was a problem hiding this comment.
I'm not too worried about this, because I suspect that most low-memory systems have explicit configurations anyway (either because they need it already, or because they're managed by node-in-a-box / distributions). And we should have loud and clear release notes for this.
There was a problem hiding this comment.
Not really about low memory systems. We don't clear the cache anymore, so if we sync and then go to steady state the node will use lots of cache for pretty much its lifetime. For instance it finishes IBD at 2GB, it will take maybe a year to fill up to 3GB and finally clear. That's just being a bad OS citizen using that much memory when we don't need it.
There was a problem hiding this comment.
Ok that sort of calls for a dbcache value that is different between IBD and later, which I guess makes sense if you're planning to run other things post-IBD. But on the other hand, a 1 GiB difference seems tiny for sufficiently large systems (which I suspect most non-dedicated setups are).
Assuming we don't want to do that in this PR, reducing to 2 GiB or 1 GiB default makes sense perhaps, but that reduces the benefit too.
There was a problem hiding this comment.
What is the difference in benefit? I think more than doubling the default (450->1000) is a pretty big speedup. What's going from 1->2->3?
I would think the initial bump would have the lions share of speedup. Could be wrong though.
There was a problem hiding this comment.
You may be right. I haven't actually used a dbcache setting below 8 GiB or so in years, so 🤷♂️.
There was a problem hiding this comment.
I'm measuring with 1 and 2 cap so that we can compare.
Other that that, does the formula make sense?
There was a problem hiding this comment.
There still looks like a substantial benefit to bumping to 1GB.
Another user posted the same concern as I https://x.com/lukechilds/status/2026875795080359955.
Perhaps it's worth doing a conservative bump for now, and work on a separate steady state dbcache value for v32. We could even have a lower general steady state value than 450MB, so running normally uses less RAM. Then we can bump to 3GB or more for IBD in v32.
There was a problem hiding this comment.
Yes, now that a few more measurements have come in, it's clear that the 2 GiB to 3 GiB speedup is negligible.
The 1 GiB to 2 GiB is still very much relevant - and affect 16 GiB machines only, 8 GiB machines were already capped at 1536. Changed the upper cap to 2 GiB for now.
I will push the steady state dbcache change after this change, separately, it makes more sense to restrict that once your parallelization PR lands and we don't need so much memory anyway.
0f93881 to
4e68c80
Compare
4e68c80 to
b67bb06
Compare
|
Q: Now that the total memory is needed for more than just the warning, should we mandate that this method works for the whole CI infra (cc: @hebasto), see bitcoin/src/test/system_ram_tests.cpp Lines 18 to 21 in 56791b5 I have addresses a few nits, I'm still measuring 1 and 2 GB dbcache values for comparison, added a few ones already to the PR description. The difference between 1 and 3 still seems relevant. I don't think a 16 GB machine should be surprised by that amount and an 8 GB is already capped at 1536 MB. Let's see the results and we can decide. |
A related discussion happened here: #33435 (review). We could make the test mandatory. If it fails in an unsupported environment, users can still explicitly disable it. |
hebasto
left a comment
There was a problem hiding this comment.
Could the new source files common/system_ram.cpp and node/dbcache.cpp be added to FILES_WITH_ENFORCED_IWYU?
willcl-ark
left a comment
There was a problem hiding this comment.
Concept ACK
Seems like a reasonable first step towards something more like #8437, which I've wanted to see for a long time... In the case that never happens, this seems perfectly worthwhile on it's own, too.
Just one comment so far, re. handling the implementation-defined truncation, but mostly LGTM.
b67bb06 to
7385c5b
Compare
|
Thanks @hebasto & @willcl-ark, addressed your concerns, added both as coauthor. |
The IWYU warnings in the CI seem to be legit. |
7385c5b to
0348bda
Compare
0348bda to
c4077b3
Compare
| //! -dbcache default (bytes) | ||
| static constexpr size_t DEFAULT_DB_CACHE{DEFAULT_KERNEL_CACHE}; | ||
| //! Automatic -dbcache floor (bytes) | ||
| static constexpr size_t MIN_DEFAULT_DBCACHE{100_MiB}; |
There was a problem hiding this comment.
I'm still not sure about reducing the default dbcache. The project has defaulted to 450 for nearly a decade. Looking through the issue tracker, I have not found too many users complaining about bitcoind oom-ing on low-RAM devices. Even the most popular raspberry pi installation guide bumps cache to 2GB: https://raspibolt.org/guide/bitcoin/bitcoin-client.html#configuration (I realize though that they are targeting devices with 4+GB of RAM). Do we really need to change this on the minimum side?
There was a problem hiding this comment.
I have not found too many users complaining about bitcoind oom-ing on low-RAM devices
I have tried it and we can't do it with 2 GB total memory currently with default dbcache. The memory usage grows with threads, with mempool, with sigcache, connection count, memory buffers, etc.
With 4 GB ram we're already increasing the dbcache in this PR, so reduction only happens for extremely constrained machines. A 2 GB machine can barely finish an IBD and only if we explicitly reduce the dbcache to 100 (and even this OOM-ed on some flushes). That's what we're doing here automatically now.
A 2 GB machine can't even compile the source code without explicit swapping, so it makes sense to make these more realistic.
There was a problem hiding this comment.
How does the same reasoning apply to virtualizations where e.g. 2GB are allocated to running bitcoind in a jail? Reading through some of the issues in the tracker that seems a likely deployment.
There was a problem hiding this comment.
I am running nodes in 2GB AWS EC2 instances just fine. Although mempool is fairly empty. I don't think IBD in such an instance is feasible though, with or without dbcache. Reducing this for steady state seems fine. I doubt users will notice.
Not sure why anyone would want to build on such an instance either.
| #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); |
There was a problem hiding this comment.
I'm not sure if this will work correctly in a containerized environment. E.g. what happens if you create a docker container with bounded memory? From what I am gathering it will attempt to measure the hosts memory, not whatever the system administrator allocated for the container. I'm not sure if we can account for all these scenarios, meaning this change is likely to break a few deployments. A log message on init indicating what was measured and how much db cache allocation was scaled up would already go a ways to at least make this a bit more transparent.
There was a problem hiding this comment.
Thanks, this was exactly what I was working on after talking to @openoms.
We could extend the total memory calculator to take cgroups and BSD jails into consideration, but for now (to avoid bikeshedding here), I only added a warning if /sys/fs/cgroup exists.
That extra warning also made me realize that we are not displaying the cache sizes in a uniform way, and there is no point in pretending we care about decimals for MiB, but we do for GiB. Since GiB is now the expected unit for -dbcache, I unified the display of the cache units.
Total RAM is also cached now so that we can call it multiple times, and it was renamed to TryGetTotalRam to differentiate it from the alternative that falls back to the default value.
Also made GetDefaultCache more testable by reverting to system ran if the optional parameter is not provided.
And lastly I regrouped the tests (data separated from logic) to simplify the RAM-aware commit diff.
There was a problem hiding this comment.
Reverted many of these based on followup reviews
I would add that more informative/meaningful warnings in cases where the specified cache size value seems sub-optimal are also valuable, even if the default is not changed (though my vote goes with the RAM-aware default). |
|
Thanks for the reviews, I'll investigate later how to move forward, moving to draft in the meantime |
Replace bare `>> 20` shifts in dbcache-related MiB conversions with `/ 1_MiB`, so the unit is visible at the call site before the dbcache helpers are split out. Also switch a touched Qt settings migration cast to the local functional-cast style.
Move the OS-dependent total RAM query out of `common/system.cpp` into a dedicated `common/system_ram.cpp` translation unit and declare it in `common/system_ram.h`. Co-authored-by: sipa <[email protected]>
-BEGIN VERIFY SCRIPT- git grep -l 'GetTotalRAM' -- src | xargs perl -pi -e 's/\bGetTotalRAM\b/TryGetTotalRam/g' -END VERIFY SCRIPT-
The dbcache default and oversized-warning paths can both query total RAM during startup. Cache the OS query result in a function-local static, so callers can use `TryGetTotalRam()` without repeating the underlying `sysconf` or `GlobalMemoryStatusEx` call.
Move the `dbcache`-specific declarations and definitions out of `node/caches.{h,cpp}` into a dedicated `node/dbcache` translation unit.
Co-authored-by: Luke Dashjr <[email protected]>
PR bitcoin#34692 made node startup and Qt settings use `node::GetDefaultDBCache()`, but `bitcoinkernel` still used the fixed `DEFAULT_KERNEL_CACHE` value. Use the shared helper in the two kernel initialization paths, so kernel users get the same `450 MiB` or `1024 MiB` default as the node path. Also align the Qt settings migration default with the same helper now that `DEFAULT_KERNEL_CACHE` is removed. Drop `DEFAULT_KERNEL_CACHE` now that the node helper is the single default source.
The oversized dbcache warning still switches from a fixed `450 MiB` cap below `2 GiB` RAM to `75%` of total RAM at `2 GiB`. That creates a discontinuity where adding one MiB of RAM can raise the warning threshold from `450 MiB` to `1536 MiB`. Use a single monotonic rule based on a `2 GiB` reserved-memory budget instead: warn if dbcache > max(DEFAULT_DB_CACHE, 0.75 * (total_ram - RESERVED_RAM)) Above `RESERVED_RAM`, the threshold grows linearly at `0.75 MiB` per MiB of additional RAM. This keeps a `25%` margin above the reserve for OS and mempool variance while avoiding the cliff. Move the implementation out of the header because it is no longer constexpr. Update the warning string in the same commit so the format specifiers match the byte-unit result types after switching these conversions away from `size_t`-specific shifts. Co-authored-by: Bortlesboat <[email protected]> Co-authored-by: optout <[email protected]>
Express the manual `-dbcache` cap once as `MAX_DBCACHE_BYTES` instead of hard-wiring the 32-bit branch at the clamp site. The effective cap is unchanged: 1 GiB on 32-bit builds and the full `uint64_t` range on 64-bit builds. Keep parsed `-dbcache` bytes in `uint64_t`, matching the byte-unit literals, and cast back only while the surrounding cache split types still use `size_t`.
Use the `_BYTES` suffix for `MIN_DBCACHE_BYTES`, `DEFAULT_DBCACHE_BYTES`, and `MAX_DBCACHE_BYTES` so the units are obvious and the dbcache bounds/default read as one group.
-BEGIN VERIFY SCRIPT-
git grep -l -w \
-e 'MIN_DB_CACHE' \
-e 'DEFAULT_DB_CACHE' \
-- src test | \
xargs perl -pi \
-e 's/\bMIN_DB_CACHE\b/MIN_DBCACHE_BYTES/g;' \
-e 's/\bDEFAULT_DB_CACHE\b/DEFAULT_DBCACHE_BYTES/g;'
-END VERIFY SCRIPT-
Co-authored-by: optout <[email protected]>
c75a1c9 to
3c627bb
Compare
|
I'll push a separate PR instead with only the shared |
…ersions Replace bare `>> 20` shifts that will be touched later with `/ 1_MiB`, so the unit is visible at the call site instead of relying on the reader to recognize the magic shift width. Github-Pull: bitcoin#34641 Rebased-From: 4a5cd1748f944f405225c276aa1590d002d5ada0
Move the OS-dependent total RAM query out of `common/system.cpp` into a dedicated `common/system_ram.cpp` translation unit and declare it in `common/system_ram.h`. Co-authored-by: sipa <[email protected]> Github-Pull: bitcoin#34641 Rebased-From: 89730e159fc391819558841a0a9fdbb77e97980a
-BEGIN VERIFY SCRIPT- git grep -l 'GetTotalRAM' -- src | xargs perl -pi -e 's/\bGetTotalRAM\b/TryGetTotalRam/g' -END VERIFY SCRIPT- Github-Pull: bitcoin#34641 Rebased-From: 3b2f7b472d1dd75c8197b577ca976d90c09ec409
Wrap the OS query in a static immediately-invoked lambda so the underlying `sysconf`/`GlobalMemoryStatusEx` call only happens once per process. Subsequent calls return the cached `std::optional<size_t>`, letting call sites use the helper freely without worrying about repeated syscalls. Github-Pull: bitcoin#34641 Rebased-From: 7832b674ae1e5a2f137037eed707314ed970509a
Move the `dbcache`-specific declarations and definitions out of `node/caches.{h,cpp}` into a dedicated `node/dbcache` translation unit.
Co-authored-by: Luke Dashjr <[email protected]>
Github-Pull: bitcoin#34641
Rebased-From: 7cb69c71166af21c14b90082556d03e7f1f61e57
Drop the fixed `DEFAULT_KERNEL_CACHE` constant and have the two `bitcoinkernel` initialisation sites pick up the same default the node startup path uses (`node::GetDefaultDBCache()`). That helper already chooses between `450 MiB` and `1024 MiB` based on detected RAM, so kernel consumers on 64-bit systems with at least 4 GiB RAM now get the same `1024 MiB` default instead of the previous fixed `450 MiB`. Github-Pull: bitcoin#34641 Rebased-From: dbd8897dbed3303d09752d39c869f0e50d682745
Problem
-dbcachecontrols how much RAM Bitcoin Core uses for the in-memory view of the UTXO set, while the authoritative chainstate remains on disk.The default is still the fixed
450 MiBvalue, chosen almost a decade ago, even though chainstate size and typical hardware profiles have changed significantly since then.In practice, a fixed
450 MiBdefault is often too conservative on modern systems, where larger cache sizes can reduce flush churn and disk roundtrips during validation, especially in IBD. At the same time, memory pressure remains important on smaller systems.Historical changes
IRC
The discussion converged on avoiding a single new fixed
-dbcachevalue and moving to RAM-aware defaults instead, balancing performance gains on modern hardware against OOM risk on lower-memory systems.IRC log links
Node provider defaults
Containerization
Container memory constraints may or may not be reflected by RAM detection.
We have considered adding container-detection but decided against it. Startup logs show detected/assumed system memory so users can set
-dbcacheexplicitly when needed.Fix
Measuring
-reindex-chainstateon different machines indicates that 450-1024-2048 jumps are all very relevant after which the effects taper off.Replace the fixed default with a RAM-aware policy:
If system RAM cannot be determined, assume 4 GiB on 64-bit builds and 2 GiB on 32-bit builds.
Manual
-dbcachevalues are unchanged and still override the automatic default.Warning threshold update
Oversized
-dbcachewarnings are kept and aligned with the new policy.The previous low-memory/high-memory cliff at
FALLBACK_RAM_BYTESis replaced by a single monotonic rule that re-uses the sameRESERVED_RAMbudget the auto default already accounts for:The auto default uses 25% of the post-
RESERVED_RAMheadroom; warning at 75% of that headroom keeps a 25% margin aboveRESERVED_RAMfor OS / mempool variance, while ensuring the auto default never trips its own warning.Performance measurements
reindex-chainstate
N150 (Umbrel) - 1.18x faster
Rpi5 (16) - 1.24x faster
Rpi5 (8) - 1.43x faster
Apple M4 Max - 2.46x faster
Benchmark 1: ./build/bin/bitcoind -datadir="/Users/lorinc/Library/Application Support/Bitcoin" -stopatheight=900000 -dbcache=450 -reindex-chainstate -blocksonly -connect=0 -printtoconsole=0 (COMMIT = 097c18239b58a8ee03794b3ebc6e722a0da30d8d) Time (abs ≡): 20261.315 s [User: 18768.521 s, System: 4271.673 s]Benchmark 1: ./build/bin/bitcoind -datadir="/Users/lorinc/Library/Application Support/Bitcoin" -stopatheight=900000 -dbcache=1000 -reindex-chainstate -blocksonly -connect=0 -printtoconsole=0 (COMMIT = 097c18239b58a8ee03794b3ebc6e722a0da30d8d) Time (abs ≡): 11574.620 s [User: 12147.446 s, System: 2417.914 s]Benchmark 1: ./build/bin/bitcoind -datadir="/Users/lorinc/Library/Application Support/Bitcoin" -stopatheight=900000 -dbcache=2000 -reindex-chainstate -blocksonly -connect=0 -printtoconsole=0 (COMMIT = 097c18239b58a8ee03794b3ebc6e722a0da30d8d) Time (abs ≡): 8226.676 s [User: 9083.949 s, System: 1179.971 s]Benchmark 1: ./build/bin/bitcoind -datadir="/Users/lorinc/Library/Application Support/Bitcoin" -stopatheight=900000 -dbcache=3000 -reindex-chainstate -blocksonly -connect=0 -printtoconsole=0 (COMMIT = 097c18239b58a8ee03794b3ebc6e722a0da30d8d) Time (abs ≡): 7064.184 s [User: 7701.391 s, System: 668.060 s]i7 HDD - 1.51x faster
Windows Xeon - 1.24x faster
Benchmark 1: ./build/bin/bitcoind.exe -datadir="C:\\my_storage\\BitcoinData" -stopatheight=933339 -dbcache=450 -reindex-chainstate -blocksonly -connect=0 -printtoconsole=0 (COMMIT = 5401e673d56198f2c0bad366581e70d5d9cd765c) Time (abs ≡): 37691.648 s [User: 0.000 s, System: 0.001 s]Benchmark 1: ./build/bin/bitcoind.exe -datadir="C:\\my_storage\\BitcoinData" -stopatheight=933339 -dbcache=1000 -reindex-chainstate -blocksonly -connect=0 -printtoconsole=0 (COMMIT = 5401e673d56198f2c0bad366581e70d5d9cd765c) Time (abs ≡): 34250.996 s [User: 0.002 s, System: 0.000 s]Benchmark 1: ./build/bin/bitcoind.exe -datadir="C:\\my_storage\\BitcoinData" -stopatheight=933339 -dbcache=2000 -reindex-chainstate -blocksonly -connect=0 -printtoconsole=0 (COMMIT = 5401e673d56198f2c0bad366581e70d5d9cd765c) Time (abs ≡): 31001.091 s [User: 0.000 s, System: 0.002 s]Benchmark 2: ./build/bin/bitcoind.exe -datadir="C:\\my_storage\\BitcoinData" -stopatheight=933339 -dbcache=3000 -reindex-chainstate -blocksonly -connect=0 -printtoconsole=0 (COMMIT = 5401e673d56198f2c0bad366581e70d5d9cd765c) Time (abs ≡): 30276.311 s [User: 0.001 s, System: 0.000 s]i9 - 1.20x faster
Default/Warning quick reference