|
38 | 38 | #include <netbase.h> |
39 | 39 | #include <netgroup.h> |
40 | 40 | #include <node/blockstorage.h> |
| 41 | +#include <node/caches.h> // for CalculateCacheSizes |
41 | 42 | #include <node/chainstate.h> // for LoadChainstate |
42 | 43 | #include <node/context.h> |
43 | 44 | #include <node/ui_interface.h> |
@@ -1805,36 +1806,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) |
1805 | 1806 | bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false); |
1806 | 1807 |
|
1807 | 1808 | // 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 | + |
1826 | 1811 | int64_t nMempoolSizeMax = args.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000; |
1827 | 1812 | 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)); |
1829 | 1814 | 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)); |
1831 | 1816 | } |
1832 | 1817 | for (BlockFilterType filter_type : g_enabled_filter_types) { |
1833 | 1818 | 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)); |
1835 | 1820 | } |
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)); |
1838 | 1823 |
|
1839 | 1824 | assert(!node.mempool); |
1840 | 1825 | assert(!node.chainman); |
@@ -1893,9 +1878,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) |
1893 | 1878 | chainparams.GetConsensus(), |
1894 | 1879 | chainparams.NetworkIDString(), |
1895 | 1880 | fReindexChainState, |
1896 | | - nBlockTreeDBCache, |
1897 | | - nCoinDBCache, |
1898 | | - nCoinCacheUsage, |
| 1881 | + cache_sizes.block_tree_db, |
| 1882 | + cache_sizes.coins_db, |
| 1883 | + cache_sizes.coins, |
1899 | 1884 | ShutdownRequested, |
1900 | 1885 | []() { |
1901 | 1886 | uiInterface.ThreadSafeMessageBox( |
@@ -2091,14 +2076,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) |
2091 | 2076 | return InitError(*error); |
2092 | 2077 | } |
2093 | 2078 |
|
2094 | | - g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex); |
| 2079 | + g_txindex = std::make_unique<TxIndex>(cache_sizes.tx_index, false, fReindex); |
2095 | 2080 | if (!g_txindex->Start(chainman.ActiveChainstate())) { |
2096 | 2081 | return false; |
2097 | 2082 | } |
2098 | 2083 | } |
2099 | 2084 |
|
2100 | 2085 | 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); |
2102 | 2087 | if (!GetBlockFilterIndex(filter_type)->Start(chainman.ActiveChainstate())) { |
2103 | 2088 | return false; |
2104 | 2089 | } |
|
0 commit comments