Skip to content

Commit 2c49e0b

Browse files
committed
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.
1 parent ab921fe commit 2c49e0b

File tree

8 files changed

+25
-24
lines changed

8 files changed

+25
-24
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
#include <consensus/validation.h>
2121
#include <core_io.h>
22+
#include <kernel/caches.h>
2223
#include <logging.h>
2324
#include <node/blockstorage.h>
24-
#include <node/caches.h>
2525
#include <node/chainstate.h>
2626
#include <random.h>
2727
#include <script/sigcache.h>
@@ -123,7 +123,7 @@ int main(int argc, char* argv[])
123123
util::SignalInterrupt interrupt;
124124
ChainstateManager chainman{interrupt, chainman_opts, blockman_opts};
125125

126-
kernel::CacheSizes cache_sizes{nDefaultDbCache << 20};
126+
kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE << 20};
127127
node::ChainstateLoadOptions options;
128128
auto [status, error] = node::LoadChainstate(chainman, cache_sizes, options);
129129
if (status != node::ChainstateLoadStatus::SUCCESS) {

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
489489
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);
490490
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
491491
argsman.AddArg("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
492-
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);
492+
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);
493493
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);
494494
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);
495495
argsman.AddArg("-loadblock=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);

src/kernel/caches.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
#ifndef BITCOIN_KERNEL_CACHES_H
66
#define BITCOIN_KERNEL_CACHES_H
77

8-
#include <txdb.h>
9-
108
#include <algorithm>
119
#include <cstdint>
1210

11+
//! Suggested default amount of cache reserved for the kernel (MiB)
12+
static constexpr int64_t DEFAULT_KERNEL_CACHE{450};
13+
//! Max memory allocated to block tree DB specific cache (MiB)
14+
static constexpr int64_t MAX_BLOCK_DB_CACHE{2};
15+
//! Max memory allocated to coin DB specific cache (MiB)
16+
static constexpr int64_t MAX_COINS_DB_CACHE{8};
17+
1318
namespace kernel {
1419
struct CacheSizes {
1520
int64_t block_tree_db;
@@ -18,10 +23,10 @@ struct CacheSizes {
1823

1924
CacheSizes(int64_t total_cache)
2025
{
21-
block_tree_db = std::min(total_cache / 8, nMaxBlockDBCache << 20);
26+
block_tree_db = std::min(total_cache / 8, MAX_BLOCK_DB_CACHE << 20);
2227
total_cache -= block_tree_db;
2328
// use 25%-50% of the remainder for disk cache, capped by the maximum.
24-
coins_db = std::min({total_cache / 2, (total_cache / 4) + (1 << 23), nMaxCoinsDBCache << 20});
29+
coins_db = std::min({total_cache / 2, (total_cache / 4) + (1 << 23), MAX_COINS_DB_CACHE << 20});
2530
total_cache -= coins_db;
2631
coins = total_cache; // the rest goes to the coins cache
2732
}

src/node/caches.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <common/args.h>
88
#include <index/txindex.h>
99
#include <kernel/caches.h>
10-
#include <txdb.h>
1110

1211
#include <algorithm>
1312
#include <string>
@@ -21,8 +20,8 @@ static constexpr int64_t MAX_FILTER_INDEX_CACHE{1024};
2120
namespace node {
2221
std::tuple<IndexCacheSizes, kernel::CacheSizes> CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
2322
{
24-
int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
25-
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
23+
int64_t nTotalCache = (args.GetIntArg("-dbcache", DEFAULT_DB_CACHE) << 20);
24+
nTotalCache = std::max(nTotalCache, MIN_DB_CACHE << 20); // total cache cannot be less than nMinDbCache
2625
IndexCacheSizes sizes;
2726
sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE << 20 : 0);
2827
nTotalCache -= sizes.tx_index;

src/node/caches.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
class ArgsManager;
1515

16+
//! min. -dbcache (MiB)
17+
static constexpr int64_t MIN_DB_CACHE{4};
18+
//! -dbcache default (MiB)
19+
static constexpr int64_t DEFAULT_DB_CACHE{DEFAULT_KERNEL_CACHE};
20+
1621
namespace node {
1722
struct IndexCacheSizes {
1823
int64_t tx_index;

src/qt/optionsdialog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
#include <common/system.h>
1717
#include <interfaces/node.h>
18-
#include <node/chainstatemanager_args.h>
1918
#include <netbase.h>
20-
#include <txdb.h>
19+
#include <node/caches.h>
20+
#include <node/chainstatemanager_args.h>
2121
#include <util/strencodings.h>
2222

2323
#include <chrono>
@@ -95,7 +95,7 @@ OptionsDialog::OptionsDialog(QWidget* parent, bool enableWallet)
9595
ui->verticalLayout->setStretchFactor(ui->tabWidget, 1);
9696

9797
/* Main elements init */
98-
ui->databaseCache->setRange(nMinDbCache, std::numeric_limits<int>::max());
98+
ui->databaseCache->setRange(MIN_DB_CACHE, std::numeric_limits<int>::max());
9999
ui->threadsScriptVerif->setMinimum(-GetNumCores());
100100
ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
101101
ui->pruneWarning->setVisible(false);

src/qt/optionsmodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include <mapport.h>
1616
#include <net.h>
1717
#include <netbase.h>
18+
#include <node/caches.h>
1819
#include <node/chainstatemanager_args.h>
19-
#include <txdb.h> // for -dbcache defaults
2020
#include <util/string.h>
2121
#include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS
2222
#include <wallet/wallet.h> // For DEFAULT_SPEND_ZEROCONF_CHANGE
@@ -470,7 +470,7 @@ QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) con
470470
suffix.empty() ? getOption(option, "-prev") :
471471
DEFAULT_PRUNE_TARGET_GB;
472472
case DatabaseCache:
473-
return qlonglong(SettingToInt(setting(), nDefaultDbCache));
473+
return qlonglong(SettingToInt(setting(), DEFAULT_DB_CACHE));
474474
case ThreadsScriptVerif:
475475
return qlonglong(SettingToInt(setting(), DEFAULT_SCRIPTCHECK_THREADS));
476476
case Listen:
@@ -733,7 +733,7 @@ void OptionsModel::checkAndMigrate()
733733
// see https://github.com/bitcoin/bitcoin/pull/8273
734734
// force people to upgrade to the new value if they are using 100MB
735735
if (settingsVersion < 130000 && settings.contains("nDatabaseCache") && settings.value("nDatabaseCache").toLongLong() == 100)
736-
settings.setValue("nDatabaseCache", (qint64)nDefaultDbCache);
736+
settings.setValue("nDatabaseCache", (qint64)DEFAULT_DB_CACHE);
737737

738738
settings.setValue(strSettingsVersionKey, CLIENT_VERSION);
739739
}

src/txdb.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,8 @@
2121
class COutPoint;
2222
class uint256;
2323

24-
//! -dbcache default (MiB)
25-
static const int64_t nDefaultDbCache = 450;
2624
//! -dbbatchsize default (bytes)
2725
static const int64_t nDefaultDbBatchSize = 16 << 20;
28-
//! min. -dbcache (MiB)
29-
static const int64_t nMinDbCache = 4;
30-
//! Max memory allocated to block tree DB specific cache (MiB)
31-
static const int64_t nMaxBlockDBCache = 2;
32-
//! Max memory allocated to coin DB specific cache (MiB)
33-
static const int64_t nMaxCoinsDBCache = 8;
3426

3527
//! User-controlled performance and debug options.
3628
struct CoinsViewOptions {

0 commit comments

Comments
 (0)