Bitcoin Core 30.99.0
P2P Digital Currency
caches.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-2022 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 <kernel/caches.h>
11#include <logging.h>
12#include <node/interface_ui.h>
13#include <tinyformat.h>
14#include <util/byte_units.h>
15
16#include <algorithm>
17#include <string>
18
19// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
20// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
22static constexpr size_t MAX_TX_INDEX_CACHE{1024_MiB};
24static constexpr size_t MAX_FILTER_INDEX_CACHE{1024_MiB};
26static constexpr size_t MAX_32BIT_DBCACHE{1024_MiB};
27
28namespace node {
30{
31 if (auto db_cache{args.GetIntArg("-dbcache")}) {
32 if (*db_cache < 0) db_cache = 0;
33 const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)};
34 constexpr auto max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits<size_t>::max()};
35 return std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, max_db_cache));
36 }
37 return DEFAULT_DB_CACHE;
38}
39
41{
42 size_t total_cache{CalculateDbCacheBytes(args)};
43
44 IndexCacheSizes index_sizes;
45 index_sizes.tx_index = std::min(total_cache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);
46 total_cache -= index_sizes.tx_index;
47 if (n_indexes > 0) {
48 size_t max_cache = std::min(total_cache / 8, MAX_FILTER_INDEX_CACHE);
49 index_sizes.filter_index = max_cache / n_indexes;
50 total_cache -= index_sizes.filter_index * n_indexes;
51 }
52 return {index_sizes, kernel::CacheSizes{total_cache}};
53}
54
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}
65} // namespace node
ArgsManager & args
Definition: bitcoind.cpp:282
static constexpr size_t MAX_32BIT_DBCACHE
Maximum dbcache size on 32-bit systems.
Definition: caches.cpp:26
static constexpr size_t MAX_FILTER_INDEX_CACHE
Max memory allocated to all block filter index caches combined in bytes.
Definition: caches.cpp:24
static constexpr size_t MAX_TX_INDEX_CACHE
Max memory allocated to tx index DB specific cache in bytes.
Definition: caches.cpp:22
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:488
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:513
std::optional< size_t > GetTotalRAM()
Return the total RAM available on the current system, if detectable.
Definition: system.cpp:114
void InitWarning(const bilingual_str &str)
Show warning message.
Definition: messages.h:20
constexpr bool ShouldWarnOversizedDbCache(size_t dbcache, size_t total_ram) noexcept
Definition: caches.h:30
size_t CalculateDbCacheBytes(const ArgsManager &args)
Definition: caches.cpp:29
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes)
Definition: caches.cpp:40
void LogOversizedDbCache(const ArgsManager &args) noexcept
Definition: caches.cpp:55
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
static constexpr size_t MIN_DB_CACHE
min. -dbcache (bytes)
Definition: caches.h:16
static constexpr size_t DEFAULT_DB_CACHE
-dbcache default (bytes)
Definition: caches.h:18
Bilingual messages:
Definition: translation.h:24
size_t filter_index
Definition: caches.h:23
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
static constexpr bool DEFAULT_TXINDEX
Definition: txindex.h:10