From ac4bf138b849a8544798f3891d6623803040c265 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Tue, 21 Sep 2021 11:37:03 -0400 Subject: 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. --- src/init.cpp | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) (limited to 'src/init.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 #include #include +#include // for CalculateCacheSizes #include // for LoadChainstate #include #include @@ -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(nTxIndexCache, false, fReindex); + g_txindex = std::make_unique(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; } -- cgit v1.2.3