Skip to content

Commit be4ff30

Browse files
committed
Move global scriptcheckqueue into ChainstateManager class
1 parent 4e78834 commit be4ff30

File tree

7 files changed

+38
-42
lines changed

7 files changed

+38
-42
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ int main(int argc, char* argv[])
290290
// dereferencing and UB.
291291
scheduler.stop();
292292
if (chainman.m_thread_load.joinable()) chainman.m_thread_load.join();
293-
StopScriptCheckWorkerThreads();
293+
chainman.StopScriptCheckWorkerThreads();
294294

295295
GetMainSignals().FlushBackgroundCallbacks();
296296
{

src/init.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ void Shutdown(NodeContext& node)
271271
// CScheduler/checkqueue, scheduler and load block thread.
272272
if (node.scheduler) node.scheduler->stop();
273273
if (node.chainman && node.chainman->m_thread_load.joinable()) node.chainman->m_thread_load.join();
274-
StopScriptCheckWorkerThreads();
274+
if (node.chainman) node.chainman->StopScriptCheckWorkerThreads();
275275

276276
// After the threads that potentially access these pointers have been stopped,
277277
// destruct and reset all to nullptr.
@@ -1109,24 +1109,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11091109
return InitError(strprintf(_("Unable to allocate memory for -maxsigcachesize: '%s' MiB"), args.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_BYTES >> 20)));
11101110
}
11111111

1112-
int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
1113-
if (script_threads <= 0) {
1114-
// -par=0 means autodetect (number of cores - 1 script threads)
1115-
// -par=-n means "leave n cores free" (number of cores - n - 1 script threads)
1116-
script_threads += GetNumCores();
1117-
}
1118-
1119-
// Subtract 1 because the main thread counts towards the par threads
1120-
script_threads = std::max(script_threads - 1, 0);
1121-
1122-
// Number of script-checking threads <= MAX_SCRIPTCHECK_THREADS
1123-
script_threads = std::min(script_threads, MAX_SCRIPTCHECK_THREADS);
1124-
1125-
LogPrintf("Script verification uses %d additional threads\n", script_threads);
1126-
if (script_threads >= 1) {
1127-
StartScriptCheckWorkerThreads(script_threads);
1128-
}
1129-
11301112
assert(!node.scheduler);
11311113
node.scheduler = std::make_unique<CScheduler>();
11321114

src/kernel/chainstatemanager_opts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ struct ChainstateManagerOpts {
4545
DBOptions coins_db{};
4646
CoinsViewOptions coins_view{};
4747
Notifications& notifications;
48+
//! Number of script check worker threads. Zero means no parallel verification.
49+
int worker_threads_num{0};
4850
};
4951

5052
} // namespace kernel

src/node/chainstatemanager_args.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
#include <arith_uint256.h>
88
#include <common/args.h>
9+
#include <common/system.h>
910
#include <kernel/chainstatemanager_opts.h>
11+
#include <logging.h>
1012
#include <node/coins_view_args.h>
1113
#include <node/database_args.h>
1214
#include <tinyformat.h>
@@ -16,6 +18,7 @@
1618
#include <util/translation.h>
1719
#include <validation.h>
1820

21+
#include <algorithm>
1922
#include <chrono>
2023
#include <string>
2124

@@ -41,6 +44,16 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
4144
ReadDatabaseArgs(args, opts.coins_db);
4245
ReadCoinsViewArgs(args, opts.coins_view);
4346

47+
int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
48+
if (script_threads <= 0) {
49+
// -par=0 means autodetect (number of cores - 1 script threads)
50+
// -par=-n means "leave n cores free" (number of cores - n - 1 script threads)
51+
script_threads += GetNumCores();
52+
}
53+
// Subtract 1 because the main thread counts towards the par threads.
54+
opts.worker_threads_num = std::clamp(script_threads - 1, 0, MAX_SCRIPTCHECK_THREADS);
55+
LogPrintf("Script verification uses %d additional threads\n", opts.worker_threads_num);
56+
4457
return {};
4558
}
4659
} // namespace node

src/test/util/setup_common.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
176176
.adjusted_time_callback = GetAdjustedTime,
177177
.check_block_index = true,
178178
.notifications = *m_node.notifications,
179+
.worker_threads_num = 2,
179180
};
180181
const BlockManager::Options blockman_opts{
181182
.chainparams = chainman_opts.chainparams,
@@ -187,15 +188,12 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
187188
.path = m_args.GetDataDirNet() / "blocks" / "index",
188189
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),
189190
.memory_only = true});
190-
191-
constexpr int script_check_threads = 2;
192-
StartScriptCheckWorkerThreads(script_check_threads);
193191
}
194192

195193
ChainTestingSetup::~ChainTestingSetup()
196194
{
197195
if (m_node.scheduler) m_node.scheduler->stop();
198-
StopScriptCheckWorkerThreads();
196+
m_node.chainman->StopScriptCheckWorkerThreads();
199197
GetMainSignals().FlushBackgroundCallbacks();
200198
GetMainSignals().UnregisterBackgroundSignalScheduler();
201199
m_node.connman.reset();

src/validation.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,16 +2047,9 @@ DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIn
20472047
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
20482048
}
20492049

2050-
static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
2051-
2052-
void StartScriptCheckWorkerThreads(int threads_num)
2053-
{
2054-
scriptcheckqueue.StartWorkerThreads(threads_num);
2055-
}
2056-
2057-
void StopScriptCheckWorkerThreads()
2050+
void ChainstateManager::StopScriptCheckWorkerThreads()
20582051
{
2059-
scriptcheckqueue.StopWorkerThreads();
2052+
m_script_check_queue.StopWorkerThreads();
20602053
}
20612054

20622055
/**
@@ -2147,7 +2140,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
21472140

21482141
uint256 block_hash{block.GetHash()};
21492142
assert(*pindex->phashBlock == block_hash);
2150-
const bool parallel_script_checks{scriptcheckqueue.HasThreads()};
2143+
const bool parallel_script_checks{m_chainman.GetCheckQueue().HasThreads()};
21512144

21522145
const auto time_start{SteadyClock::now()};
21532146
const CChainParams& params{m_chainman.GetParams()};
@@ -2336,7 +2329,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
23362329
// in multiple threads). Preallocate the vector size so a new allocation
23372330
// doesn't invalidate pointers into the vector, and keep txsdata in scope
23382331
// for as long as `control`.
2339-
CCheckQueueControl<CScriptCheck> control(fScriptChecks && parallel_script_checks ? &scriptcheckqueue : nullptr);
2332+
CCheckQueueControl<CScriptCheck> control(fScriptChecks && parallel_script_checks ? &m_chainman.GetCheckQueue() : nullptr);
23402333
std::vector<PrecomputedTransactionData> txsdata(block.vtx.size());
23412334

23422335
std::vector<int> prevheights;
@@ -5751,12 +5744,18 @@ static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
57515744
}
57525745

57535746
ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
5754-
: m_interrupt{interrupt},
5747+
: m_script_check_queue{/*nBatchSizeIn=*/128},
5748+
m_interrupt{interrupt},
57555749
m_options{Flatten(std::move(options))},
5756-
m_blockman{interrupt, std::move(blockman_options)} {}
5750+
m_blockman{interrupt, std::move(blockman_options)}
5751+
{
5752+
m_script_check_queue.StartWorkerThreads(m_options.worker_threads_num);
5753+
}
57575754

57585755
ChainstateManager::~ChainstateManager()
57595756
{
5757+
StopScriptCheckWorkerThreads();
5758+
57605759
LOCK(::cs_main);
57615760

57625761
m_versionbitscache.Clear();

src/validation.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <arith_uint256.h>
1414
#include <attributes.h>
1515
#include <chain.h>
16+
#include <checkqueue.h>
1617
#include <kernel/chain.h>
1718
#include <consensus/amount.h>
1819
#include <deploymentstatus.h>
@@ -98,11 +99,6 @@ extern uint256 g_best_block;
9899
/** Documentation for argument 'checklevel'. */
99100
extern const std::vector<std::string> CHECKLEVEL_DOC;
100101

101-
/** Run instances of script checking worker threads */
102-
void StartScriptCheckWorkerThreads(int threads_num);
103-
/** Stop all of the script checking worker threads */
104-
void StopScriptCheckWorkerThreads();
105-
106102
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
107103

108104
bool FatalError(kernel::Notifications& notifications, BlockValidationState& state, const std::string& strMessage, const bilingual_str& userMessage = {});
@@ -896,6 +892,9 @@ class ChainstateManager
896892
return cs && !cs->m_disabled;
897893
}
898894

895+
//! A queue for script verifications that have to be performed by worker threads.
896+
CCheckQueue<CScriptCheck> m_script_check_queue;
897+
899898
public:
900899
using Options = kernel::ChainstateManagerOpts;
901900

@@ -1246,6 +1245,9 @@ class ChainstateManager
12461245
//! nullopt.
12471246
std::optional<int> GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
12481247

1248+
CCheckQueue<CScriptCheck>& GetCheckQueue() { return m_script_check_queue; }
1249+
void StopScriptCheckWorkerThreads();
1250+
12491251
~ChainstateManager();
12501252
};
12511253

0 commit comments

Comments
 (0)