Bitcoin Core 31.99.0
P2P Digital Currency
caches.cpp
Go to the documentation of this file.
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>
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 <string>
22
23// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
24// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
26static constexpr uint64_t MAX_TX_INDEX_CACHE{1_GiB};
28static constexpr uint64_t MAX_FILTER_INDEX_CACHE{1_GiB};
30static constexpr uint64_t MAX_TXOSPENDER_INDEX_CACHE{1_GiB};
32static constexpr uint64_t HIGH_DEFAULT_DBCACHE{1_GiB};
34static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM{4_GiB};
35
36namespace node {
38{
39 if constexpr (sizeof(void*) >= 8) {
42 }
43 }
45}
46
48{
49 if (auto db_cache{args.GetIntArg("-dbcache")}) {
50 if (*db_cache < 0) db_cache = 0;
51 const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)};
52 return std::max(MIN_DBCACHE_BYTES, std::min(db_cache_bytes, MAX_DBCACHE_BYTES));
53 }
54 return GetDefaultDBCache();
55}
56
58{
59 uint64_t total_cache{CalculateDbCacheBytes(args)};
60
61 // Allocate proportional to usage pattern benefit:
62 // - txindex (10%): serves getrawtransaction RPCs with mostly unique,
63 // non-repetitive lookups across the entire blockchain.
64 // - blockfilterindex (5%): serves BIP 157 light clients that repeatedly
65 // query recent blocks, benefiting from LevelDB cache, but the
66 // working set for a typical 2-week offline gap is ~200kiB, well within 5%
67 // of the total cache.
68 // - txospenderindex (5%): serves gettxspendingprevout RPCs with very
69 // specific, rarely repeated outpoint queries.
70 // - coinstatsindex: intentionally not included here, since usage pattern
71 // does not seem to suggest it would be necessary to cache.
72 IndexCacheSizes index_sizes;
73 index_sizes.tx_index = std::min(total_cache * 10 / 100, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);
74 index_sizes.txospender_index = std::min(total_cache * 5 / 100, args.GetBoolArg("-txospenderindex", DEFAULT_TXOSPENDERINDEX) ? MAX_TXOSPENDER_INDEX_CACHE : 0);
75 if (n_indexes > 0) {
76 uint64_t max_cache = std::min(total_cache * 5 / 100, MAX_FILTER_INDEX_CACHE);
77 index_sizes.filter_index = max_cache / n_indexes;
78 total_cache -= index_sizes.filter_index * n_indexes;
79 }
80 total_cache -= index_sizes.tx_index;
81 total_cache -= index_sizes.txospender_index;
82 return {index_sizes, kernel::CacheSizes{total_cache}};
83}
84
86{
87 if (const auto total_ram{TryGetTotalRam()}) {
88 const uint64_t db_cache{CalculateDbCacheBytes(args)};
89 if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) {
90 InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."),
91 db_cache / 1_MiB, *total_ram / 1_MiB)});
92 }
93 }
94}
95} // namespace node
ArgsManager & args
Definition: bitcoind.cpp:280
static constexpr uint64_t MAX_TXOSPENDER_INDEX_CACHE
Max memory allocated to tx spenderindex DB specific cache in bytes.
Definition: caches.cpp:30
static constexpr uint64_t HIGH_DEFAULT_DBCACHE
Larger default dbcache on 64-bit systems with enough RAM.
Definition: caches.cpp:32
static constexpr uint64_t MAX_FILTER_INDEX_CACHE
Max memory allocated to all block filter index caches combined in bytes.
Definition: caches.cpp:28
static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM
Minimum detected RAM required for HIGH_DEFAULT_DBCACHE.
Definition: caches.cpp:34
static constexpr uint64_t MAX_TX_INDEX_CACHE
Max memory allocated to tx index DB specific cache in bytes.
Definition: caches.cpp:26
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition: args.h:323
bool GetBoolArg(const std::string &strArg, bool fDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return boolean argument or default value.
Definition: args.cpp:571
std::optional< uint64_t > TryGetTotalRam()
Return the total RAM available on the current system, if detectable.
Definition: system.cpp:108
void InitWarning(const bilingual_str &str)
Show warning message.
constexpr uint64_t MAX_DBCACHE_BYTES
Maximum total database cache on current architecture (bytes)
Definition: caches.h:17
constexpr uint64_t DEFAULT_KERNEL_CACHE
Suggested default amount of cache reserved for the kernel (bytes)
Definition: caches.h:19
constexpr uint64_t MIN_DBCACHE_BYTES
Minimum total database cache (bytes)
Definition: caches.h:15
Definition: messages.h:21
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes)
Definition: caches.cpp:57
void LogOversizedDbCache(const ArgsManager &args) noexcept
Definition: caches.cpp:85
uint64_t CalculateDbCacheBytes(const ArgsManager &args)
Definition: caches.cpp:47
constexpr bool ShouldWarnOversizedDbCache(uint64_t dbcache, uint64_t total_ram) noexcept
Definition: caches.h:32
uint64_t GetDefaultDBCache()
Definition: caches.cpp:37
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
Bilingual messages:
Definition: translation.h:24
uint64_t txospender_index
Definition: caches.h:25
uint64_t filter_index
Definition: caches.h:24
uint64_t tx_index
Definition: caches.h:23
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
constexpr bool DEFAULT_TXINDEX
Definition: txindex.h:23
constexpr bool DEFAULT_TXOSPENDERINDEX