Skip to content

Commit 4ab1827

Browse files
committed
node/caches: Extract cache calculation logic
1 parent d7f1e23 commit 4ab1827

File tree

4 files changed

+75
-28
lines changed

4 files changed

+75
-28
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ BITCOIN_CORE_H = \
269269
netgroup.h \
270270
netmessagemaker.h \
271271
node/blockstorage.h \
272+
node/caches.h \
272273
node/chainstate.h \
273274
node/coin.h \
274275
node/coinstats.h \
@@ -502,6 +503,7 @@ libbitcoin_server_a_SOURCES = \
502503
netgroup.cpp \
503504
net_processing.cpp \
504505
node/blockstorage.cpp \
506+
node/caches.cpp \
505507
node/chainstate.cpp \
506508
node/coin.cpp \
507509
node/coinstats.cpp \

src/init.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <netbase.h>
3939
#include <netgroup.h>
4040
#include <node/blockstorage.h>
41+
#include <node/caches.h> // for CalculateCacheSizes
4142
#include <node/chainstate.h> // for LoadChainstate
4243
#include <node/context.h>
4344
#include <node/ui_interface.h>
@@ -1805,36 +1806,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
18051806
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
18061807

18071808
// cache size calculations
1808-
int64_t nTotalCache = (args.GetArg("-dbcache", nDefaultDbCache) << 20);
1809-
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
1810-
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
1811-
int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
1812-
nTotalCache -= nBlockTreeDBCache;
1813-
int64_t nTxIndexCache = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
1814-
nTotalCache -= nTxIndexCache;
1815-
int64_t filter_index_cache = 0;
1816-
if (!g_enabled_filter_types.empty()) {
1817-
size_t n_indexes = g_enabled_filter_types.size();
1818-
int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
1819-
filter_index_cache = max_cache / n_indexes;
1820-
nTotalCache -= filter_index_cache * n_indexes;
1821-
}
1822-
int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
1823-
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
1824-
nTotalCache -= nCoinDBCache;
1825-
int64_t nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
1809+
CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
1810+
18261811
int64_t nMempoolSizeMax = args.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
18271812
LogPrintf("Cache configuration:\n");
1828-
LogPrintf("* Using %.1f MiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
1813+
LogPrintf("* Using %.1f MiB for block index database\n", cache_sizes.block_tree_db * (1.0 / 1024 / 1024));
18291814
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
1830-
LogPrintf("* Using %.1f MiB for transaction index database\n", nTxIndexCache * (1.0 / 1024 / 1024));
1815+
LogPrintf("* Using %.1f MiB for transaction index database\n", cache_sizes.tx_index * (1.0 / 1024 / 1024));
18311816
}
18321817
for (BlockFilterType filter_type : g_enabled_filter_types) {
18331818
LogPrintf("* Using %.1f MiB for %s block filter index database\n",
1834-
filter_index_cache * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
1819+
cache_sizes.filter_index * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
18351820
}
1836-
LogPrintf("* Using %.1f MiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
1837-
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
1821+
LogPrintf("* Using %.1f MiB for chain state database\n", cache_sizes.coins_db * (1.0 / 1024 / 1024));
1822+
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
18381823

18391824
assert(!node.mempool);
18401825
assert(!node.chainman);
@@ -1893,9 +1878,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
18931878
chainparams.GetConsensus(),
18941879
chainparams.NetworkIDString(),
18951880
fReindexChainState,
1896-
nBlockTreeDBCache,
1897-
nCoinDBCache,
1898-
nCoinCacheUsage,
1881+
cache_sizes.block_tree_db,
1882+
cache_sizes.coins_db,
1883+
cache_sizes.coins,
18991884
ShutdownRequested,
19001885
[]() {
19011886
uiInterface.ThreadSafeMessageBox(
@@ -2091,14 +2076,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
20912076
return InitError(*error);
20922077
}
20932078

2094-
g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex);
2079+
g_txindex = std::make_unique<TxIndex>(cache_sizes.tx_index, false, fReindex);
20952080
if (!g_txindex->Start(chainman.ActiveChainstate())) {
20962081
return false;
20972082
}
20982083
}
20992084

21002085
for (const auto& filter_type : g_enabled_filter_types) {
2101-
InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
2086+
InitBlockFilterIndex(filter_type, cache_sizes.filter_index, false, fReindex);
21022087
if (!GetBlockFilterIndex(filter_type)->Start(chainman.ActiveChainstate())) {
21032088
return false;
21042089
}

src/node/caches.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2021 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 <txdb.h>
8+
#include <util/system.h>
9+
#include <validation.h>
10+
11+
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
12+
{
13+
int64_t nTotalCache = (args.GetArg("-dbcache", nDefaultDbCache) << 20);
14+
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
15+
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
16+
int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
17+
nTotalCache -= nBlockTreeDBCache;
18+
int64_t nTxIndexCache = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
19+
nTotalCache -= nTxIndexCache;
20+
int64_t filter_index_cache = 0;
21+
if (n_indexes > 0) {
22+
int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
23+
filter_index_cache = max_cache / n_indexes;
24+
nTotalCache -= filter_index_cache * n_indexes;
25+
}
26+
int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
27+
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
28+
nTotalCache -= nCoinDBCache;
29+
int64_t nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
30+
31+
return {
32+
nBlockTreeDBCache,
33+
nCoinDBCache,
34+
nCoinCacheUsage,
35+
nTxIndexCache,
36+
filter_index_cache,
37+
};
38+
}

src/node/caches.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2021 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+
#ifndef BITCOIN_NODE_CACHES_H
6+
#define BITCOIN_NODE_CACHES_H
7+
8+
#include <cstddef> // for size_t
9+
#include <cstdint> // for int64_t
10+
11+
class ArgsManager;
12+
13+
struct CacheSizes {
14+
int64_t block_tree_db;
15+
int64_t coins_db;
16+
int64_t coins;
17+
int64_t tx_index;
18+
int64_t filter_index;
19+
};
20+
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
21+
22+
#endif // BITCOIN_NODE_CACHES_H

0 commit comments

Comments
 (0)