aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am2
-rw-r--r--src/init.cpp41
-rw-r--r--src/node/caches.cpp38
-rw-r--r--src/node/caches.h22
4 files changed, 75 insertions, 28 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 880e6b5362..a7911c49b2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -174,6 +174,7 @@ BITCOIN_CORE_H = \
netbase.h \
netmessagemaker.h \
node/blockstorage.h \
+ node/caches.h \
node/chainstate.h \
node/coin.h \
node/coinstats.h \
@@ -340,6 +341,7 @@ libbitcoin_server_a_SOURCES = \
net.cpp \
net_processing.cpp \
node/blockstorage.cpp \
+ node/caches.cpp \
node/chainstate.cpp \
node/coin.cpp \
node/coinstats.cpp \
diff --git a/src/init.cpp b/src/init.cpp
index e6f00717fb..3cac626e9d 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -34,6 +34,7 @@
#include <net_processing.h>
#include <netbase.h>
#include <node/blockstorage.h>
+#include <node/caches.h> // for CalculateCacheSizes
#include <node/chainstate.h> // for LoadChainstate
#include <node/context.h>
#include <node/miner.h>
@@ -1381,36 +1382,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
// cache size calculations
- int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
- nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
- nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
- int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
- nTotalCache -= nBlockTreeDBCache;
- int64_t nTxIndexCache = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
- nTotalCache -= nTxIndexCache;
- int64_t filter_index_cache = 0;
- if (!g_enabled_filter_types.empty()) {
- size_t n_indexes = g_enabled_filter_types.size();
- int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
- filter_index_cache = max_cache / n_indexes;
- nTotalCache -= filter_index_cache * n_indexes;
- }
- int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
- nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
- nTotalCache -= nCoinDBCache;
- int64_t nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
+ CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
+
int64_t nMempoolSizeMax = args.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
LogPrintf("Cache configuration:\n");
- LogPrintf("* Using %.1f MiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
+ LogPrintf("* Using %.1f MiB for block index database\n", cache_sizes.block_tree_db * (1.0 / 1024 / 1024));
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
- LogPrintf("* Using %.1f MiB for transaction index database\n", nTxIndexCache * (1.0 / 1024 / 1024));
+ LogPrintf("* Using %.1f MiB for transaction index database\n", cache_sizes.tx_index * (1.0 / 1024 / 1024));
}
for (BlockFilterType filter_type : g_enabled_filter_types) {
LogPrintf("* Using %.1f MiB for %s block filter index database\n",
- filter_index_cache * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
+ cache_sizes.filter_index * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
}
- LogPrintf("* Using %.1f MiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
- 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));
+ LogPrintf("* Using %.1f MiB for chain state database\n", cache_sizes.coins_db * (1.0 / 1024 / 1024));
+ 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));
bool fLoaded = false;
while (!fLoaded && !ShutdownRequested()) {
@@ -1427,9 +1412,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
fPruneMode,
chainparams.GetConsensus(),
fReindexChainState,
- nBlockTreeDBCache,
- nCoinDBCache,
- nCoinCacheUsage,
+ cache_sizes.block_tree_db,
+ cache_sizes.coins_db,
+ cache_sizes.coins,
ShutdownRequested,
[]() {
uiInterface.ThreadSafeMessageBox(
@@ -1548,14 +1533,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
return InitError(*error);
}
- g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex);
+ g_txindex = std::make_unique<TxIndex>(cache_sizes.tx_index, false, fReindex);
if (!g_txindex->Start(chainman.ActiveChainstate())) {
return false;
}
}
for (const auto& filter_type : g_enabled_filter_types) {
- InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
+ InitBlockFilterIndex(filter_type, cache_sizes.filter_index, false, fReindex);
if (!GetBlockFilterIndex(filter_type)->Start(chainman.ActiveChainstate())) {
return false;
}
diff --git a/src/node/caches.cpp b/src/node/caches.cpp
new file mode 100644
index 0000000000..be09c7e055
--- /dev/null
+++ b/src/node/caches.cpp
@@ -0,0 +1,38 @@
+// Copyright (c) 2021 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <node/caches.h>
+
+#include <txdb.h>
+#include <util/system.h>
+#include <validation.h>
+
+CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
+{
+ int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
+ nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
+ nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
+ int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
+ nTotalCache -= nBlockTreeDBCache;
+ int64_t nTxIndexCache = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
+ nTotalCache -= nTxIndexCache;
+ int64_t filter_index_cache = 0;
+ if (n_indexes > 0) {
+ int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
+ filter_index_cache = max_cache / n_indexes;
+ nTotalCache -= filter_index_cache * n_indexes;
+ }
+ int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
+ nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
+ nTotalCache -= nCoinDBCache;
+ int64_t nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
+
+ return {
+ nBlockTreeDBCache,
+ nCoinDBCache,
+ nCoinCacheUsage,
+ nTxIndexCache,
+ filter_index_cache,
+ };
+}
diff --git a/src/node/caches.h b/src/node/caches.h
new file mode 100644
index 0000000000..437e7d10e5
--- /dev/null
+++ b/src/node/caches.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2021 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_NODE_CACHES_H
+#define BITCOIN_NODE_CACHES_H
+
+#include <cstddef> // for size_t
+#include <cstdint> // for int64_t
+
+class ArgsManager;
+
+struct CacheSizes {
+ int64_t block_tree_db;
+ int64_t coins_db;
+ int64_t coins;
+ int64_t tx_index;
+ int64_t filter_index;
+};
+CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
+
+#endif // BITCOIN_NODE_CACHES_H