Skip to content

Commit ac4bf13

Browse files
committed
node/caches: Extract cache calculation logic
I strongly recommend reviewing with the following git-diff flags: --color-moved=dimmed_zebra --color-moved-ws=allow-indentation-change [META] In a future commit, this function will be re-used in TestingSetup so that the behaviour matches across test and non-test init codepaths.
1 parent 15f2e33 commit ac4bf13

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
@@ -174,6 +174,7 @@ BITCOIN_CORE_H = \
174174
netbase.h \
175175
netmessagemaker.h \
176176
node/blockstorage.h \
177+
node/caches.h \
177178
node/chainstate.h \
178179
node/coin.h \
179180
node/coinstats.h \
@@ -340,6 +341,7 @@ libbitcoin_server_a_SOURCES = \
340341
net.cpp \
341342
net_processing.cpp \
342343
node/blockstorage.cpp \
344+
node/caches.cpp \
343345
node/chainstate.cpp \
344346
node/coin.cpp \
345347
node/coinstats.cpp \

src/init.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <net_processing.h>
3535
#include <netbase.h>
3636
#include <node/blockstorage.h>
37+
#include <node/caches.h> // for CalculateCacheSizes
3738
#include <node/chainstate.h> // for LoadChainstate
3839
#include <node/context.h>
3940
#include <node/miner.h>
@@ -1381,36 +1382,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13811382
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
13821383

13831384
// cache size calculations
1384-
int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
1385-
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
1386-
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
1387-
int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
1388-
nTotalCache -= nBlockTreeDBCache;
1389-
int64_t nTxIndexCache = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
1390-
nTotalCache -= nTxIndexCache;
1391-
int64_t filter_index_cache = 0;
1392-
if (!g_enabled_filter_types.empty()) {
1393-
size_t n_indexes = g_enabled_filter_types.size();
1394-
int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
1395-
filter_index_cache = max_cache / n_indexes;
1396-
nTotalCache -= filter_index_cache * n_indexes;
1397-
}
1398-
int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
1399-
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
1400-
nTotalCache -= nCoinDBCache;
1401-
int64_t nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
1385+
CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
1386+
14021387
int64_t nMempoolSizeMax = args.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
14031388
LogPrintf("Cache configuration:\n");
1404-
LogPrintf("* Using %.1f MiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
1389+
LogPrintf("* Using %.1f MiB for block index database\n", cache_sizes.block_tree_db * (1.0 / 1024 / 1024));
14051390
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
1406-
LogPrintf("* Using %.1f MiB for transaction index database\n", nTxIndexCache * (1.0 / 1024 / 1024));
1391+
LogPrintf("* Using %.1f MiB for transaction index database\n", cache_sizes.tx_index * (1.0 / 1024 / 1024));
14071392
}
14081393
for (BlockFilterType filter_type : g_enabled_filter_types) {
14091394
LogPrintf("* Using %.1f MiB for %s block filter index database\n",
1410-
filter_index_cache * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
1395+
cache_sizes.filter_index * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
14111396
}
1412-
LogPrintf("* Using %.1f MiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
1413-
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));
1397+
LogPrintf("* Using %.1f MiB for chain state database\n", cache_sizes.coins_db * (1.0 / 1024 / 1024));
1398+
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));
14141399

14151400
bool fLoaded = false;
14161401
while (!fLoaded && !ShutdownRequested()) {
@@ -1427,9 +1412,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14271412
fPruneMode,
14281413
chainparams.GetConsensus(),
14291414
fReindexChainState,
1430-
nBlockTreeDBCache,
1431-
nCoinDBCache,
1432-
nCoinCacheUsage,
1415+
cache_sizes.block_tree_db,
1416+
cache_sizes.coins_db,
1417+
cache_sizes.coins,
14331418
ShutdownRequested,
14341419
[]() {
14351420
uiInterface.ThreadSafeMessageBox(
@@ -1548,14 +1533,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15481533
return InitError(*error);
15491534
}
15501535

1551-
g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex);
1536+
g_txindex = std::make_unique<TxIndex>(cache_sizes.tx_index, false, fReindex);
15521537
if (!g_txindex->Start(chainman.ActiveChainstate())) {
15531538
return false;
15541539
}
15551540
}
15561541

15571542
for (const auto& filter_type : g_enabled_filter_types) {
1558-
InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
1543+
InitBlockFilterIndex(filter_type, cache_sizes.filter_index, false, fReindex);
15591544
if (!GetBlockFilterIndex(filter_type)->Start(chainman.ActiveChainstate())) {
15601545
return false;
15611546
}

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.GetIntArg("-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)