Bitcoin Core  29.1.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 <index/txindex.h>
9 #include <kernel/caches.h>
10 #include <logging.h>
11 #include <util/byte_units.h>
12 
13 #include <algorithm>
14 #include <string>
15 
16 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
17 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
19 static constexpr size_t MAX_TX_INDEX_CACHE{1024_MiB};
21 static constexpr size_t MAX_FILTER_INDEX_CACHE{1024_MiB};
23 static constexpr size_t MAX_32BIT_DBCACHE{1024_MiB};
24 
25 namespace node {
27 {
28  // Convert -dbcache from MiB units to bytes. The total cache is floored by MIN_DB_CACHE and capped by max size_t value.
29  size_t total_cache{DEFAULT_DB_CACHE};
30  if (std::optional<int64_t> db_cache = args.GetIntArg("-dbcache")) {
31  if (*db_cache < 0) db_cache = 0;
32  uint64_t db_cache_bytes = SaturatingLeftShift<uint64_t>(*db_cache, 20);
33  constexpr auto max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits<size_t>::max()};
34  total_cache = std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, max_db_cache));
35  }
36 
37  IndexCacheSizes index_sizes;
38  index_sizes.tx_index = std::min(total_cache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);
39  total_cache -= index_sizes.tx_index;
40  if (n_indexes > 0) {
41  size_t max_cache = std::min(total_cache / 8, MAX_FILTER_INDEX_CACHE);
42  index_sizes.filter_index = max_cache / n_indexes;
43  total_cache -= index_sizes.filter_index * n_indexes;
44  }
45  return {index_sizes, kernel::CacheSizes{total_cache}};
46 }
47 } // namespace node
static constexpr size_t MIN_DB_CACHE
min. -dbcache (bytes)
Definition: caches.h:16
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:507
ArgsManager & args
Definition: bitcoind.cpp:277
static constexpr size_t MAX_FILTER_INDEX_CACHE
Max memory allocated to all block filter index caches combined in bytes.
Definition: caches.cpp:21
static constexpr size_t MAX_TX_INDEX_CACHE
Max memory allocated to tx index DB specific cache in bytes.
Definition: caches.cpp:19
Definition: messages.h:20
static constexpr size_t MAX_32BIT_DBCACHE
Maximum dbcache size on 32-bit systems.
Definition: caches.cpp:23
static constexpr size_t DEFAULT_DB_CACHE
-dbcache default (bytes)
Definition: caches.h:18
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:482
size_t filter_index
Definition: caches.h:23
static constexpr bool DEFAULT_TXINDEX
Definition: txindex.h:10
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes)
Definition: caches.cpp:26