This commit is contained in:
l0rinc 2025-04-29 11:57:26 +02:00 committed by GitHub
commit a13274d79e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 50 additions and 9 deletions

View file

@ -48,6 +48,7 @@
#include <node/caches.h> #include <node/caches.h>
#include <node/chainstate.h> #include <node/chainstate.h>
#include <node/chainstatemanager_args.h> #include <node/chainstatemanager_args.h>
#include <node/coins_view_args.h>
#include <node/context.h> #include <node/context.h>
#include <node/interface_ui.h> #include <node/interface_ui.h>
#include <node/kernel_notifications.h> #include <node/kernel_notifications.h>
@ -487,7 +488,7 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
argsman.AddArg("-coinstatsindex", strprintf("Maintain coinstats index used by the gettxoutsetinfo RPC (default: %u)", DEFAULT_COINSTATSINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-coinstatsindex", strprintf("Maintain coinstats index used by the gettxoutsetinfo RPC (default: %u)", DEFAULT_COINSTATSINDEX), 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("-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: calculated from the `-dbcache` value or %u if not set)", node::GetDbBatchSize(DEFAULT_DB_CACHE)), 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).", MIN_DB_CACHE >> 20, DEFAULT_DB_CACHE >> 20), 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 >> 20, DEFAULT_DB_CACHE >> 20), 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);

View file

@ -16,6 +16,9 @@ static constexpr size_t MAX_BLOCK_DB_CACHE{2_MiB};
//! Max memory allocated to coin DB specific cache (bytes) //! Max memory allocated to coin DB specific cache (bytes)
static constexpr size_t MAX_COINS_DB_CACHE{8_MiB}; static constexpr size_t MAX_COINS_DB_CACHE{8_MiB};
//! The batch size of DEFAULT_KERNEL_CACHE
static constexpr size_t DEFAULT_DB_CACHE_BATCH{16_MiB};
namespace kernel { namespace kernel {
struct CacheSizes { struct CacheSizes {
size_t block_tree_db; size_t block_tree_db;

View file

@ -2,6 +2,8 @@
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <kernel/caches.h>
#include <node/coins_view_args.h> #include <node/coins_view_args.h>
#include <common/args.h> #include <common/args.h>
@ -10,7 +12,9 @@
namespace node { namespace node {
void ReadCoinsViewArgs(const ArgsManager& args, CoinsViewOptions& options) void ReadCoinsViewArgs(const ArgsManager& args, CoinsViewOptions& options)
{ {
if (auto value = args.GetIntArg("-dbbatchsize")) options.batch_write_bytes = *value; if (const auto value = args.GetIntArg("-dbbatchsize")) options.batch_write_bytes = *value;
if (auto value = args.GetIntArg("-dbcrashratio")) options.simulate_crash_ratio = *value; else options.batch_write_bytes = GetDbBatchSize(args.GetIntArg("-dbcache", DEFAULT_KERNEL_CACHE));
if (const auto value = args.GetIntArg("-dbcrashratio")) options.simulate_crash_ratio = *value;
} }
} // namespace node } // namespace node

View file

@ -5,10 +5,22 @@
#ifndef BITCOIN_NODE_COINS_VIEW_ARGS_H #ifndef BITCOIN_NODE_COINS_VIEW_ARGS_H
#define BITCOIN_NODE_COINS_VIEW_ARGS_H #define BITCOIN_NODE_COINS_VIEW_ARGS_H
#include <kernel/caches.h>
class ArgsManager; class ArgsManager;
struct CoinsViewOptions; struct CoinsViewOptions;
namespace node { namespace node
{
static constexpr size_t GetDbBatchSize(const size_t dbcache_bytes)
{
return std::clamp(
(dbcache_bytes / DEFAULT_KERNEL_CACHE) * DEFAULT_DB_CACHE_BATCH,
/*lo=*/DEFAULT_DB_CACHE_BATCH,
/*hi=*/256_MiB
);
}
void ReadCoinsViewArgs(const ArgsManager& args, CoinsViewOptions& options); void ReadCoinsViewArgs(const ArgsManager& args, CoinsViewOptions& options);
} // namespace node } // namespace node

View file

@ -20,6 +20,7 @@
#include <vector> #include <vector>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <node/coins_view_args.h>
using namespace util::hex_literals; using namespace util::hex_literals;
@ -1074,4 +1075,26 @@ BOOST_AUTO_TEST_CASE(coins_resource_is_used)
PoolResourceTester::CheckAllDataAccountedFor(resource); PoolResourceTester::CheckAllDataAccountedFor(resource);
} }
BOOST_AUTO_TEST_CASE(db_batch_sizes)
{
BOOST_REQUIRE_EQUAL(node::GetDbBatchSize(DEFAULT_KERNEL_CACHE), DEFAULT_DB_CACHE_BATCH);
BOOST_REQUIRE_EQUAL(node::GetDbBatchSize(0_MiB), DEFAULT_DB_CACHE_BATCH);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(4_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(10_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(45_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(100_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(450_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(1000_MiB), 33'554'432);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(2000_MiB), 67'108'864);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(3000_MiB), 100'663'296);
#if SIZE_MAX > UINT32_MAX
BOOST_CHECK_EQUAL(node::GetDbBatchSize(4500_MiB), 167'772'160);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(7000_MiB), 251'658'240);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(10000_MiB), 268'435'456);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(45000_MiB), 268'435'456);
#endif
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View file

@ -8,6 +8,7 @@
#include <coins.h> #include <coins.h>
#include <dbwrapper.h> #include <dbwrapper.h>
#include <kernel/caches.h>
#include <kernel/cs_main.h> #include <kernel/cs_main.h>
#include <sync.h> #include <sync.h>
#include <util/fs.h> #include <util/fs.h>
@ -21,16 +22,13 @@
class COutPoint; class COutPoint;
class uint256; class uint256;
//! -dbbatchsize default (bytes)
static const int64_t nDefaultDbBatchSize = 16 << 20;
//! User-controlled performance and debug options. //! User-controlled performance and debug options.
struct CoinsViewOptions { struct CoinsViewOptions {
//! Maximum database write batch size in bytes. //! Maximum database write batch size in bytes.
size_t batch_write_bytes = nDefaultDbBatchSize; size_t batch_write_bytes{DEFAULT_DB_CACHE_BATCH};
//! If non-zero, randomly exit when the database is flushed with (1/ratio) //! If non-zero, randomly exit when the database is flushed with (1/ratio)
//! probability. //! probability.
int simulate_crash_ratio = 0; int simulate_crash_ratio{0};
}; };
/** CCoinsView backed by the coin database (chainstate/) */ /** CCoinsView backed by the coin database (chainstate/) */