Branch data Line data Source code
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 <logging.h>
13 : : #include <node/interface_ui.h>
14 : : #include <tinyformat.h>
15 : : #include <util/byte_units.h>
16 : :
17 : : #include <algorithm>
18 : : #include <string>
19 : :
20 : : // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
21 : : // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
22 : : //! Max memory allocated to tx index DB specific cache in bytes.
23 : : static constexpr size_t MAX_TX_INDEX_CACHE{1024_MiB};
24 : : //! Max memory allocated to all block filter index caches combined in bytes.
25 : : static constexpr size_t MAX_FILTER_INDEX_CACHE{1024_MiB};
26 : : //! Max memory allocated to tx spenderindex DB specific cache in bytes.
27 : : static constexpr size_t MAX_TXOSPENDER_INDEX_CACHE{1024_MiB};
28 : : //! Maximum dbcache size on 32-bit systems.
29 : : static constexpr size_t MAX_32BIT_DBCACHE{1024_MiB};
30 : : //! Larger default dbcache on 64-bit systems with enough RAM.
31 : : static constexpr size_t HIGH_DEFAULT_DBCACHE{1024_MiB};
32 : : //! Minimum detected RAM required for HIGH_DEFAULT_DBCACHE.
33 : : static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM{4096ULL << 20};
34 : :
35 : : namespace node {
36 : 3073 : size_t GetDefaultDBCache()
37 : : {
38 : 3073 : if constexpr (sizeof(void*) >= 8) {
39 [ + - + - ]: 3073 : if (GetTotalRAM().value_or(0) >= HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM) {
40 : 3073 : return HIGH_DEFAULT_DBCACHE;
41 : : }
42 : : }
43 : : return DEFAULT_DB_CACHE;
44 : : }
45 : :
46 : 1506 : size_t CalculateDbCacheBytes(const ArgsManager& args)
47 : : {
48 [ - + ]: 3012 : if (auto db_cache{args.GetIntArg("-dbcache")}) {
49 [ # # ]: 0 : if (*db_cache < 0) db_cache = 0;
50 [ # # ]: 0 : const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)};
51 : 0 : constexpr auto max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits<size_t>::max()};
52 [ # # ]: 0 : return std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, max_db_cache));
53 : : }
54 : 1506 : return GetDefaultDBCache();
55 : : }
56 : :
57 : 1506 : CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
58 : : {
59 : 1506 : size_t total_cache{CalculateDbCacheBytes(args)};
60 : :
61 : 1506 : IndexCacheSizes index_sizes;
62 [ + - + - : 3012 : index_sizes.tx_index = std::min(total_cache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);
- + ]
63 : 1506 : total_cache -= index_sizes.tx_index;
64 [ + - + - : 3012 : index_sizes.txospender_index = std::min(total_cache / 8, args.GetBoolArg("-txospenderindex", DEFAULT_TXOSPENDERINDEX) ? MAX_TXOSPENDER_INDEX_CACHE : 0);
- + ]
65 : 1506 : total_cache -= index_sizes.txospender_index;
66 [ - + ]: 1506 : if (n_indexes > 0) {
67 [ # # ]: 0 : size_t max_cache = std::min(total_cache / 8, MAX_FILTER_INDEX_CACHE);
68 : 0 : index_sizes.filter_index = max_cache / n_indexes;
69 : 0 : total_cache -= index_sizes.filter_index * n_indexes;
70 : : }
71 : 1506 : return {index_sizes, kernel::CacheSizes{total_cache}};
72 : : }
73 : :
74 : 0 : void LogOversizedDbCache(const ArgsManager& args) noexcept
75 : : {
76 [ # # ]: 0 : if (const auto total_ram{GetTotalRAM()}) {
77 : 0 : const size_t db_cache{CalculateDbCacheBytes(args)};
78 [ # # # # ]: 0 : if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) {
79 : 0 : InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."),
80 : 0 : db_cache >> 20, *total_ram >> 20)});
81 : : }
82 : : }
83 : 0 : }
84 : : } // namespace node
|