Bitcoin Core 28.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
11namespace 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 CacheSizes sizes;
17 sizes.block_tree_db = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
18 nTotalCache -= sizes.block_tree_db;
19 sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
20 nTotalCache -= sizes.tx_index;
21 sizes.filter_index = 0;
22 if (n_indexes > 0) {
23 int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
24 sizes.filter_index = max_cache / n_indexes;
25 nTotalCache -= sizes.filter_index * n_indexes;
26 }
27 sizes.coins_db = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
28 sizes.coins_db = std::min(sizes.coins_db, nMaxCoinsDBCache << 20); // cap total coins db cache
29 nTotalCache -= sizes.coins_db;
30 sizes.coins = nTotalCache; // the rest goes to in-memory cache
31 return sizes;
32}
33} // namespace node
ArgsManager & args
Definition: bitcoind.cpp:277
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:482
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:507
Definition: messages.h:20
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:29
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:39
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:31
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:35
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:37
static constexpr bool DEFAULT_TXINDEX
Definition: txindex.h:10