From f454987be6f140aca6c13ab3abf94c3afa183fdf Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Fri, 22 Nov 2024 22:52:02 +0100 Subject: [PATCH] kernel: Move kernel-related cache constants to kernel cache They are not all related to the txdb, so a better place for them is the new kernel cache file. --- src/bitcoin-chainstate.cpp | 2 +- src/init.cpp | 2 +- src/kernel/caches.h | 15 +++++++++++---- src/node/caches.cpp | 5 ++--- src/qt/optionsdialog.cpp | 6 +++--- src/qt/optionsmodel.cpp | 6 +++--- src/txdb.h | 8 -------- 7 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index 3b551e5c17848..a2120ba73b958 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -123,7 +123,7 @@ int main(int argc, char* argv[]) util::SignalInterrupt interrupt; ChainstateManager chainman{interrupt, chainman_opts, blockman_opts}; - kernel::CacheSizes cache_sizes{nDefaultDbCache << 20}; + kernel::CacheSizes cache_sizes{DEFAULT_DB_CACHE << 20}; node::ChainstateLoadOptions options; auto [status, error] = node::LoadChainstate(chainman, cache_sizes, options); if (status != node::ChainstateLoadStatus::SUCCESS) { diff --git a/src/init.cpp b/src/init.cpp index dd273cfe618ad..951d8304a8877 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -489,7 +489,7 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc) argsman.AddArg("-conf=", strprintf("Specify path to read-only configuration file. Relative paths will be prefixed by datadir location (only useable from command line, not configuration file) (default: %s)", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-datadir=", "Specify data directory", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS); argsman.AddArg("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS); - argsman.AddArg("-dbcache=", strprintf("Maximum database cache size MiB (minimum %d, default: %d). Make sure you have enough RAM. In addition, unused memory allocated to the mempool is shared with this cache (see -maxmempool).", nMinDbCache, nDefaultDbCache), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); + argsman.AddArg("-dbcache=", strprintf("Maximum database cache size MiB (minimum %d, default: %d). Make sure you have enough RAM. In addition, unused memory allocated to the mempool is shared with this cache (see -maxmempool).", MIN_DB_CACHE, DEFAULT_DB_CACHE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-includeconf=", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-allowignoredconf", strprintf("For backwards compatibility, treat an unused %s file in the datadir as a warning, not an error.", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-loadblock=", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); diff --git a/src/kernel/caches.h b/src/kernel/caches.h index b70091cb58584..7f82ab2744f75 100644 --- a/src/kernel/caches.h +++ b/src/kernel/caches.h @@ -5,11 +5,18 @@ #ifndef BITCOIN_KERNEL_CACHES_H #define BITCOIN_KERNEL_CACHES_H -#include - #include #include +//! min. -dbcache (MiB) +static constexpr int64_t MIN_DB_CACHE{4}; +//! -dbcache default (MiB) +static constexpr int64_t DEFAULT_DB_CACHE{450}; +//! Max memory allocated to block tree DB specific cache (MiB) +static constexpr int64_t MAX_BLOCK_DB_CACHE{2}; +//! Max memory allocated to coin DB specific cache (MiB) +static constexpr int64_t MAX_COINS_DB_CACHE{8}; + namespace kernel { struct CacheSizes { int64_t block_tree_db; @@ -18,10 +25,10 @@ struct CacheSizes { CacheSizes(int64_t total_cache) { - block_tree_db = std::min(total_cache / 8, nMaxBlockDBCache << 20); + block_tree_db = std::min(total_cache / 8, MAX_BLOCK_DB_CACHE << 20); total_cache -= block_tree_db; // use 25%-50% of the remainder for disk cache, capped by the maximum. - coins_db = std::min({total_cache / 2, (total_cache / 4) + (1 << 23), nMaxCoinsDBCache << 20}); + coins_db = std::min({total_cache / 2, (total_cache / 4) + (1 << 23), MAX_COINS_DB_CACHE << 20}); total_cache -= coins_db; coins = total_cache; // the rest goes to the coins cache } diff --git a/src/node/caches.cpp b/src/node/caches.cpp index b43d3ec30dc99..330c19520c764 100644 --- a/src/node/caches.cpp +++ b/src/node/caches.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include @@ -21,8 +20,8 @@ static constexpr int64_t MAX_FILTER_INDEX_CACHE{1024}; namespace node { std::tuple 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 + int64_t nTotalCache = (args.GetIntArg("-dbcache", DEFAULT_DB_CACHE) << 20); + nTotalCache = std::max(nTotalCache, MIN_DB_CACHE << 20); // total cache cannot be less than nMinDbCache IndexCacheSizes sizes; sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE << 20 : 0); nTotalCache -= sizes.tx_index; diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index f86b5229db022..76b7852109e78 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -15,9 +15,9 @@ #include #include -#include #include -#include +#include +#include #include #include @@ -95,7 +95,7 @@ OptionsDialog::OptionsDialog(QWidget* parent, bool enableWallet) ui->verticalLayout->setStretchFactor(ui->tabWidget, 1); /* Main elements init */ - ui->databaseCache->setRange(nMinDbCache, std::numeric_limits::max()); + ui->databaseCache->setRange(MIN_DB_CACHE, std::numeric_limits::max()); ui->threadsScriptVerif->setMinimum(-GetNumCores()); ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS); ui->pruneWarning->setVisible(false); diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp index a1dbc6248c076..7cbfff335e4c3 100644 --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -12,11 +12,11 @@ #include #include +#include #include #include #include #include -#include // for -dbcache defaults #include #include // For DEFAULT_SCRIPTCHECK_THREADS #include // For DEFAULT_SPEND_ZEROCONF_CHANGE @@ -470,7 +470,7 @@ QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) con suffix.empty() ? getOption(option, "-prev") : DEFAULT_PRUNE_TARGET_GB; case DatabaseCache: - return qlonglong(SettingToInt(setting(), nDefaultDbCache)); + return qlonglong(SettingToInt(setting(), DEFAULT_DB_CACHE)); case ThreadsScriptVerif: return qlonglong(SettingToInt(setting(), DEFAULT_SCRIPTCHECK_THREADS)); case Listen: @@ -733,7 +733,7 @@ void OptionsModel::checkAndMigrate() // see https://github.com/bitcoin/bitcoin/pull/8273 // force people to upgrade to the new value if they are using 100MB if (settingsVersion < 130000 && settings.contains("nDatabaseCache") && settings.value("nDatabaseCache").toLongLong() == 100) - settings.setValue("nDatabaseCache", (qint64)nDefaultDbCache); + settings.setValue("nDatabaseCache", (qint64)DEFAULT_DB_CACHE); settings.setValue(strSettingsVersionKey, CLIENT_VERSION); } diff --git a/src/txdb.h b/src/txdb.h index a92f19631ca96..968b7c278102d 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -21,16 +21,8 @@ class COutPoint; class uint256; -//! -dbcache default (MiB) -static const int64_t nDefaultDbCache = 450; //! -dbbatchsize default (bytes) static const int64_t nDefaultDbBatchSize = 16 << 20; -//! min. -dbcache (MiB) -static const int64_t nMinDbCache = 4; -//! Max memory allocated to block tree DB specific cache (MiB) -static const int64_t nMaxBlockDBCache = 2; -//! Max memory allocated to coin DB specific cache (MiB) -static const int64_t nMaxCoinsDBCache = 8; //! User-controlled performance and debug options. struct CoinsViewOptions {