Bitcoin Core  27.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 <index/txindex.h>
9 #include <txdb.h>
10 
11 namespace node {
13 {
14  int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
15  nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
16  nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
17  CacheSizes sizes;
18  sizes.block_tree_db = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
19  nTotalCache -= sizes.block_tree_db;
20  sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
21  nTotalCache -= sizes.tx_index;
22  sizes.filter_index = 0;
23  if (n_indexes > 0) {
24  int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
25  sizes.filter_index = max_cache / n_indexes;
26  nTotalCache -= sizes.filter_index * n_indexes;
27  }
28  sizes.coins_db = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
29  sizes.coins_db = std::min(sizes.coins_db, nMaxCoinsDBCache << 20); // cap total coins db cache
30  nTotalCache -= sizes.coins_db;
31  sizes.coins = nTotalCache; // the rest goes to in-memory cache
32  return sizes;
33 }
34 } // namespace node
ArgsManager & args
Definition: bitcoind.cpp:268
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:480
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:505
Definition: init.h:25
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes)
Definition: caches.cpp:12
int64_t tx_index
Definition: caches.h:18
int64_t coins
Definition: caches.h:17
int64_t block_tree_db
Definition: caches.h:15
int64_t filter_index
Definition: caches.h:19
int64_t coins_db
Definition: caches.h:16
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:31
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:41
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:33
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:37
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:29
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:25
static const int64_t max_filter_index_cache
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:39
static constexpr bool DEFAULT_TXINDEX
Definition: txindex.h:10