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.
This commit is contained in:
TheCharlatan 2024-11-22 22:52:02 +01:00
parent 136e4080c2
commit 1f81be60f5
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
8 changed files with 25 additions and 23 deletions

View file

@ -19,9 +19,9 @@
#include <consensus/validation.h> #include <consensus/validation.h>
#include <core_io.h> #include <core_io.h>
#include <kernel/caches.h>
#include <logging.h> #include <logging.h>
#include <node/blockstorage.h> #include <node/blockstorage.h>
#include <node/caches.h>
#include <node/chainstate.h> #include <node/chainstate.h>
#include <random.h> #include <random.h>
#include <script/sigcache.h> #include <script/sigcache.h>
@ -123,7 +123,7 @@ int main(int argc, char* argv[])
util::SignalInterrupt interrupt; util::SignalInterrupt interrupt;
ChainstateManager chainman{interrupt, chainman_opts, blockman_opts}; ChainstateManager chainman{interrupt, chainman_opts, blockman_opts};
kernel::CacheSizes cache_sizes{MiBToBytes(nDefaultDbCache)}; kernel::CacheSizes cache_sizes{MiBToBytes(DEFAULT_KERNEL_CACHE)};
node::ChainstateLoadOptions options; node::ChainstateLoadOptions options;
auto [status, error] = node::LoadChainstate(chainman, cache_sizes, options); auto [status, error] = node::LoadChainstate(chainman, cache_sizes, options);
if (status != node::ChainstateLoadStatus::SUCCESS) { if (status != node::ChainstateLoadStatus::SUCCESS) {

View file

@ -489,7 +489,7 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
argsman.AddArg("-conf=<file>", 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("-conf=<file>", 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=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS); argsman.AddArg("-datadir=<dir>", "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("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
argsman.AddArg("-dbcache=<n>", strprintf("Maximum database cache size <n> 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=<n>", strprintf("Maximum database cache size <n> 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=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-includeconf=<file>", "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("-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=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-loadblock=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);

View file

@ -5,7 +5,6 @@
#ifndef BITCOIN_KERNEL_CACHES_H #ifndef BITCOIN_KERNEL_CACHES_H
#define BITCOIN_KERNEL_CACHES_H #define BITCOIN_KERNEL_CACHES_H
#include <txdb.h>
#include <util/check.h> #include <util/check.h>
#include <algorithm> #include <algorithm>
@ -13,6 +12,13 @@
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
//! Suggested default amount of cache reserved for the kernel (MiB)
static constexpr int64_t DEFAULT_KERNEL_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};
//! Guard against truncation of values before converting. //! Guard against truncation of values before converting.
constexpr size_t MiBToBytes(int64_t mib) constexpr size_t MiBToBytes(int64_t mib)
{ {
@ -30,9 +36,9 @@ struct CacheSizes {
CacheSizes(size_t total_cache) CacheSizes(size_t total_cache)
{ {
block_tree_db = std::min(total_cache / 8, MiBToBytes(nMaxBlockDBCache)); block_tree_db = std::min(total_cache / 8, MiBToBytes(MAX_BLOCK_DB_CACHE));
total_cache -= block_tree_db; total_cache -= block_tree_db;
coins_db = std::min(total_cache / 2, MiBToBytes(nMaxCoinsDBCache)); coins_db = std::min(total_cache / 2, MiBToBytes(MAX_COINS_DB_CACHE));
total_cache -= coins_db; total_cache -= coins_db;
coins = total_cache; // the rest goes to the coins cache coins = total_cache; // the rest goes to the coins cache
} }

View file

@ -7,7 +7,6 @@
#include <common/args.h> #include <common/args.h>
#include <index/txindex.h> #include <index/txindex.h>
#include <kernel/caches.h> #include <kernel/caches.h>
#include <txdb.h>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
@ -22,8 +21,8 @@ static constexpr int64_t MAX_FILTER_INDEX_CACHE{1024};
namespace node { namespace node {
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes) CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
{ {
int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20); int64_t nTotalCache = (args.GetIntArg("-dbcache", DEFAULT_DB_CACHE) << 20);
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache nTotalCache = std::max(nTotalCache, MIN_DB_CACHE << 20);
IndexCacheSizes sizes; IndexCacheSizes sizes;
sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE << 20 : 0); sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE << 20 : 0);
nTotalCache -= sizes.tx_index; nTotalCache -= sizes.tx_index;

View file

@ -12,6 +12,11 @@
class ArgsManager; class ArgsManager;
//! min. -dbcache (MiB)
static constexpr int64_t MIN_DB_CACHE{4};
//! -dbcache default (MiB)
static constexpr int64_t DEFAULT_DB_CACHE{DEFAULT_KERNEL_CACHE};
namespace node { namespace node {
struct IndexCacheSizes { struct IndexCacheSizes {
int64_t tx_index; int64_t tx_index;

View file

@ -15,9 +15,9 @@
#include <common/system.h> #include <common/system.h>
#include <interfaces/node.h> #include <interfaces/node.h>
#include <node/chainstatemanager_args.h>
#include <netbase.h> #include <netbase.h>
#include <txdb.h> #include <node/caches.h>
#include <node/chainstatemanager_args.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <chrono> #include <chrono>
@ -95,7 +95,7 @@ OptionsDialog::OptionsDialog(QWidget* parent, bool enableWallet)
ui->verticalLayout->setStretchFactor(ui->tabWidget, 1); ui->verticalLayout->setStretchFactor(ui->tabWidget, 1);
/* Main elements init */ /* Main elements init */
ui->databaseCache->setRange(nMinDbCache, std::numeric_limits<int>::max()); ui->databaseCache->setRange(MIN_DB_CACHE, std::numeric_limits<int>::max());
ui->threadsScriptVerif->setMinimum(-GetNumCores()); ui->threadsScriptVerif->setMinimum(-GetNumCores());
ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS); ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
ui->pruneWarning->setVisible(false); ui->pruneWarning->setVisible(false);

View file

@ -15,8 +15,8 @@
#include <mapport.h> #include <mapport.h>
#include <net.h> #include <net.h>
#include <netbase.h> #include <netbase.h>
#include <node/caches.h>
#include <node/chainstatemanager_args.h> #include <node/chainstatemanager_args.h>
#include <txdb.h> // for -dbcache defaults
#include <util/string.h> #include <util/string.h>
#include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS #include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS
#include <wallet/wallet.h> // For DEFAULT_SPEND_ZEROCONF_CHANGE #include <wallet/wallet.h> // For DEFAULT_SPEND_ZEROCONF_CHANGE
@ -470,7 +470,7 @@ QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) con
suffix.empty() ? getOption(option, "-prev") : suffix.empty() ? getOption(option, "-prev") :
DEFAULT_PRUNE_TARGET_GB; DEFAULT_PRUNE_TARGET_GB;
case DatabaseCache: case DatabaseCache:
return qlonglong(SettingToInt(setting(), nDefaultDbCache)); return qlonglong(SettingToInt(setting(), DEFAULT_DB_CACHE));
case ThreadsScriptVerif: case ThreadsScriptVerif:
return qlonglong(SettingToInt(setting(), DEFAULT_SCRIPTCHECK_THREADS)); return qlonglong(SettingToInt(setting(), DEFAULT_SCRIPTCHECK_THREADS));
case Listen: case Listen:
@ -733,7 +733,7 @@ void OptionsModel::checkAndMigrate()
// see https://github.com/bitcoin/bitcoin/pull/8273 // see https://github.com/bitcoin/bitcoin/pull/8273
// force people to upgrade to the new value if they are using 100MB // force people to upgrade to the new value if they are using 100MB
if (settingsVersion < 130000 && settings.contains("nDatabaseCache") && settings.value("nDatabaseCache").toLongLong() == 100) 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); settings.setValue(strSettingsVersionKey, CLIENT_VERSION);
} }

View file

@ -21,16 +21,8 @@
class COutPoint; class COutPoint;
class uint256; class uint256;
//! -dbcache default (MiB)
static const int64_t nDefaultDbCache = 450;
//! -dbbatchsize default (bytes) //! -dbbatchsize default (bytes)
static const int64_t nDefaultDbBatchSize = 16 << 20; 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. //! User-controlled performance and debug options.
struct CoinsViewOptions { struct CoinsViewOptions {