/tmp/bitcoin/src/node/caches.cpp
Line | Count | Source |
1 | | // Copyright (c) 2021-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <node/caches.h> |
6 | | |
7 | | #include <common/args.h> |
8 | | #include <common/system.h> |
9 | | #include <index/txindex.h> |
10 | | #include <index/txospenderindex.h> |
11 | | #include <kernel/caches.h> |
12 | | #include <node/interface_ui.h> |
13 | | #include <tinyformat.h> |
14 | | #include <util/byte_units.h> |
15 | | #include <util/log.h> |
16 | | #include <util/overflow.h> |
17 | | #include <util/translation.h> |
18 | | |
19 | | #include <algorithm> |
20 | | #include <cstdint> |
21 | | #include <limits> |
22 | | #include <string> |
23 | | |
24 | | // Unlike for the UTXO database, for the txindex scenario the leveldb cache make |
25 | | // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991 |
26 | | //! Max memory allocated to tx index DB specific cache in bytes. |
27 | | static constexpr uint64_t MAX_TX_INDEX_CACHE{1_GiB}; |
28 | | //! Max memory allocated to all block filter index caches combined in bytes. |
29 | | static constexpr uint64_t MAX_FILTER_INDEX_CACHE{1_GiB}; |
30 | | //! Max memory allocated to tx spenderindex DB specific cache in bytes. |
31 | | static constexpr uint64_t MAX_TXOSPENDER_INDEX_CACHE{1_GiB}; |
32 | | //! Maximum dbcache size on 32-bit systems. |
33 | | static constexpr uint64_t MAX_32BIT_DBCACHE{1_GiB}; |
34 | | //! Larger default dbcache on 64-bit systems with enough RAM. |
35 | | static constexpr uint64_t HIGH_DEFAULT_DBCACHE{1_GiB}; |
36 | | //! Minimum detected RAM required for HIGH_DEFAULT_DBCACHE. |
37 | | static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM{4_GiB}; |
38 | | |
39 | | namespace node { |
40 | | uint64_t GetDefaultDBCache() |
41 | 4.18k | { |
42 | 4.18k | if constexpr (sizeof(void*) >= 8) { |
43 | 4.18k | if (GetTotalRAM().value_or(0) >= HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM) { |
44 | 4.18k | return HIGH_DEFAULT_DBCACHE; |
45 | 4.18k | } |
46 | 4.18k | } |
47 | 0 | return DEFAULT_DB_CACHE; |
48 | 4.18k | } |
49 | | |
50 | | uint64_t CalculateDbCacheBytes(const ArgsManager& args) |
51 | 2.28k | { |
52 | 2.28k | if (auto db_cache{args.GetIntArg("-dbcache")}) { |
53 | 0 | if (*db_cache < 0) db_cache = 0; |
54 | 0 | const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)}; |
55 | 0 | constexpr uint64_t max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits<uint64_t>::max()}; |
56 | 0 | return std::max<uint64_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, max_db_cache)); |
57 | 0 | } |
58 | 2.28k | return GetDefaultDBCache(); |
59 | 2.28k | } |
60 | | |
61 | | CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes) |
62 | 1.22k | { |
63 | 1.22k | uint64_t total_cache{CalculateDbCacheBytes(args)}; |
64 | | |
65 | | // Allocate proportional to usage pattern benefit: |
66 | | // - txindex (10%): serves getrawtransaction RPCs with mostly unique, |
67 | | // non-repetitive lookups across the entire blockchain. |
68 | | // - blockfilterindex (5%): serves BIP 157 light clients that repeatedly |
69 | | // query recent blocks, benefiting from LevelDB cache, but the |
70 | | // working set for a typical 2-week offline gap is ~200kiB, well within 5% |
71 | | // of the total cache. |
72 | | // - txospenderindex (5%): serves gettxspendingprevout RPCs with very |
73 | | // specific, rarely repeated outpoint queries. |
74 | | // - coinstatsindex: intentionally not included here, since usage pattern |
75 | | // does not seem to suggest it would be necessary to cache. |
76 | 1.22k | IndexCacheSizes index_sizes; |
77 | 1.22k | index_sizes.tx_index = std::min(total_cache * 10 / 100, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0); |
78 | 1.22k | index_sizes.txospender_index = std::min(total_cache * 5 / 100, args.GetBoolArg("-txospenderindex", DEFAULT_TXOSPENDERINDEX) ? MAX_TXOSPENDER_INDEX_CACHE : 0); |
79 | 1.22k | if (n_indexes > 0) { |
80 | 50 | uint64_t max_cache = std::min(total_cache * 5 / 100, MAX_FILTER_INDEX_CACHE); |
81 | 50 | index_sizes.filter_index = max_cache / n_indexes; |
82 | 50 | total_cache -= index_sizes.filter_index * n_indexes; |
83 | 50 | } |
84 | 1.22k | total_cache -= index_sizes.tx_index; |
85 | 1.22k | total_cache -= index_sizes.txospender_index; |
86 | 1.22k | return {index_sizes, kernel::CacheSizes{total_cache}}; |
87 | 1.22k | } |
88 | | |
89 | | void LogOversizedDbCache(const ArgsManager& args) noexcept |
90 | 1.05k | { |
91 | 1.05k | if (const auto total_ram{GetTotalRAM()}) { |
92 | 1.05k | const uint64_t db_cache{CalculateDbCacheBytes(args)}; |
93 | 1.05k | if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) { |
94 | 0 | InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."), |
95 | 0 | db_cache >> 20, *total_ram >> 20)}); |
96 | 0 | } |
97 | 1.05k | } |
98 | 1.05k | } |
99 | | } // namespace node |