Skip to content

Commit 5506ecf

Browse files
committed
[refactor] Replace global int nScriptCheckThreads with bool
The global nScriptCheckThreads int is confusing and is only needed for its int-ness in AppInitMain. Move all `-par` parsing logic there and replace the int nScriptCheckThreads with a bool g_parallel_script_checks. Also tidy up logic and improve comments.
1 parent d995762 commit 5506ecf

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

src/init.cpp

+18-12
Original file line numberDiff line numberDiff line change
@@ -1061,15 +1061,6 @@ bool AppInitParameterInteraction()
10611061
incrementalRelayFee = CFeeRate(n);
10621062
}
10631063

1064-
// -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency
1065-
nScriptCheckThreads = gArgs.GetArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
1066-
if (nScriptCheckThreads <= 0)
1067-
nScriptCheckThreads += GetNumCores();
1068-
if (nScriptCheckThreads <= 1)
1069-
nScriptCheckThreads = 0;
1070-
else if (nScriptCheckThreads > MAX_SCRIPTCHECK_THREADS)
1071-
nScriptCheckThreads = MAX_SCRIPTCHECK_THREADS;
1072-
10731064
// block pruning; get the amount of disk space (in MiB) to allot for block & undo files
10741065
int64_t nPruneArg = gArgs.GetArg("-prune", 0);
10751066
if (nPruneArg < 0) {
@@ -1256,10 +1247,25 @@ bool AppInitMain(NodeContext& node)
12561247
InitSignatureCache();
12571248
InitScriptExecutionCache();
12581249

1259-
LogPrintf("Script verification uses %d additional threads\n", std::max(nScriptCheckThreads - 1, 0));
1260-
if (nScriptCheckThreads) {
1261-
for (int i=0; i<nScriptCheckThreads-1; i++)
1250+
int script_threads = gArgs.GetArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
1251+
if (script_threads <= 0) {
1252+
// -par=0 means autodetect (number of cores - 1 script threads)
1253+
// -par=-n means "leave n cores free" (number of cores - n - 1 script threads)
1254+
script_threads += GetNumCores();
1255+
}
1256+
1257+
// Subtract 1 because the main thread counts towards the par threads
1258+
script_threads = std::max(script_threads - 1, 0);
1259+
1260+
// Number of script-checking threads <= MAX_SCRIPTCHECK_THREADS
1261+
script_threads = std::min(script_threads, MAX_SCRIPTCHECK_THREADS);
1262+
1263+
LogPrintf("Script verification uses %d additional threads\n", script_threads);
1264+
if (script_threads >= 1) {
1265+
g_parallel_script_checks = true;
1266+
for (int i = 0; i < script_threads; ++i) {
12621267
threadGroup.create_thread([i]() { return ThreadScriptCheck(i); });
1268+
}
12631269
}
12641270

12651271
// Start the lightweight task scheduler thread

src/test/setup_common.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,12 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
102102
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", FormatStateMessage(state)));
103103
}
104104

105-
nScriptCheckThreads = 3;
106-
for (int i = 0; i < nScriptCheckThreads - 1; i++)
105+
// Start script-checking threads. Set g_parallel_script_checks to true so they are used.
106+
constexpr int script_check_threads = 2;
107+
for (int i = 0; i < script_check_threads; ++i) {
107108
threadGroup.create_thread([i]() { return ThreadScriptCheck(i); });
109+
}
110+
g_parallel_script_checks = true;
108111

109112
m_node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
110113
m_node.connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.

src/validation.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ CBlockIndex *pindexBestHeader = nullptr;
107107
Mutex g_best_block_mutex;
108108
std::condition_variable g_best_block_cv;
109109
uint256 g_best_block;
110-
int nScriptCheckThreads = 0;
110+
bool g_parallel_script_checks{false};
111111
std::atomic_bool fImporting(false);
112112
std::atomic_bool fReindex(false);
113113
bool fHavePruned = false;
@@ -2069,7 +2069,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
20692069

20702070
CBlockUndo blockundo;
20712071

2072-
CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : nullptr);
2072+
CCheckQueueControl<CScriptCheck> control(fScriptChecks && g_parallel_script_checks ? &scriptcheckqueue : nullptr);
20732073

20742074
std::vector<int> prevheights;
20752075
CAmount nFees = 0;
@@ -2130,7 +2130,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
21302130
std::vector<CScriptCheck> vChecks;
21312131
bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
21322132
TxValidationState tx_state;
2133-
if (fScriptChecks && !CheckInputs(tx, tx_state, view, flags, fCacheResults, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : nullptr)) {
2133+
if (fScriptChecks && !CheckInputs(tx, tx_state, view, flags, fCacheResults, fCacheResults, txdata[i], g_parallel_script_checks ? &vChecks : nullptr)) {
21342134
// Any transaction validation failure in ConnectBlock is a block consensus failure
21352135
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
21362136
tx_state.GetRejectReason(), tx_state.GetDebugMessage());

src/validation.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
7676
/** The pre-allocation chunk size for rev?????.dat files (since 0.8) */
7777
static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
7878

79-
/** Maximum number of script-checking threads allowed */
80-
static const int MAX_SCRIPTCHECK_THREADS = 16;
79+
/** Maximum number of dedicated script-checking threads allowed */
80+
static const int MAX_SCRIPTCHECK_THREADS = 15;
8181
/** -par default (number of script-checking threads, 0 = auto) */
8282
static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
8383
/** Number of blocks that can be requested at any given time from a single peer. */
@@ -146,7 +146,10 @@ extern std::condition_variable g_best_block_cv;
146146
extern uint256 g_best_block;
147147
extern std::atomic_bool fImporting;
148148
extern std::atomic_bool fReindex;
149-
extern int nScriptCheckThreads;
149+
/** Whether there are dedicated script-checking threads running.
150+
* False indicates all script checking is done on the main threadMessageHandler thread.
151+
*/
152+
extern bool g_parallel_script_checks;
150153
extern bool fRequireStandard;
151154
extern bool fCheckBlockIndex;
152155
extern bool fCheckpointsEnabled;

0 commit comments

Comments
 (0)