Skip to content

Commit 6b134a6

Browse files
jnewberymajcosta
authored andcommitted
[backport#17342][refactor] Remove global int nScriptCheckThreads
Summary: The global nScriptCheckThreads int is confusing and is only needed for its int-ness in AppInitMain. Move all `-par` parsing logic there and ~~replace~~ remove the int nScriptCheckThreads ~~with a bool g_parallel_script_checks.~~ (see D521) Also tidy up logic and improve comments. bitcoin/bitcoin@5506ecf --- Concludes backport of Core [[bitcoin/bitcoin#17342 | PR17342]] Depends on D7274 Test Plan: ninja check check-functional Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D7275
1 parent 02aca79 commit 6b134a6

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

src/init.cpp

+18-15
Original file line numberDiff line numberDiff line change
@@ -1821,17 +1821,6 @@ bool AppInitParameterInteraction(Config &config) {
18211821
std::ceil(nMempoolSizeMin / 1000000.0)));
18221822
}
18231823

1824-
// -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency
1825-
nScriptCheckThreads = gArgs.GetArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
1826-
if (nScriptCheckThreads <= 0) {
1827-
nScriptCheckThreads += GetNumCores();
1828-
}
1829-
if (nScriptCheckThreads <= 1) {
1830-
nScriptCheckThreads = 0;
1831-
} else if (nScriptCheckThreads > MAX_SCRIPTCHECK_THREADS) {
1832-
nScriptCheckThreads = MAX_SCRIPTCHECK_THREADS;
1833-
}
1834-
18351824
// Configure excessive block size.
18361825
const uint64_t nProposedExcessiveBlockSize =
18371826
gArgs.GetArg("-excessiveblocksize", DEFAULT_MAX_BLOCK_SIZE);
@@ -2104,10 +2093,24 @@ bool AppInitMain(Config &config, RPCServer &rpcServer,
21042093
InitSignatureCache();
21052094
InitScriptExecutionCache();
21062095

2107-
LogPrintf("Using %u threads for script verification\n",
2108-
nScriptCheckThreads);
2109-
if (nScriptCheckThreads) {
2110-
for (int i = 0; i < nScriptCheckThreads - 1; i++) {
2096+
int script_threads = gArgs.GetArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
2097+
if (script_threads <= 0) {
2098+
// -par=0 means autodetect (number of cores - 1 script threads)
2099+
// -par=-n means "leave n cores free" (number of cores - n - 1 script
2100+
// threads)
2101+
script_threads += GetNumCores();
2102+
}
2103+
2104+
// Subtract 1 because the main thread counts towards the par threads
2105+
script_threads = std::max(script_threads - 1, 0);
2106+
2107+
// Number of script-checking threads <= MAX_SCRIPTCHECK_THREADS
2108+
script_threads = std::min(script_threads, MAX_SCRIPTCHECK_THREADS);
2109+
2110+
LogPrintf("Script verification uses %d additional threads\n",
2111+
script_threads);
2112+
if (script_threads >= 1) {
2113+
for (int i = 0; i < script_threads; ++i) {
21112114
threadGroup.create_thread([i]() { return ThreadScriptCheck(i); });
21122115
}
21132116
}

src/test/util/setup_common.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ TestingSetup::TestingSetup(const std::string &chainName)
124124
FormatStateMessage(state)));
125125
}
126126
}
127-
nScriptCheckThreads = 3;
128-
for (int i = 0; i < nScriptCheckThreads - 1; i++) {
127+
constexpr int script_check_threads = 2;
128+
for (int i = 0; i < script_check_threads; ++i) {
129129
threadGroup.create_thread([i]() { return ThreadScriptCheck(i); });
130130
}
131131

src/validation.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ CBlockIndex *pindexBestHeader = nullptr;
8787
Mutex g_best_block_mutex;
8888
std::condition_variable g_best_block_cv;
8989
uint256 g_best_block;
90-
int nScriptCheckThreads = 0;
9190
std::atomic_bool fImporting(false);
9291
std::atomic_bool fReindex(false);
9392
bool fHavePruned = false;

src/validation.h

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

81-
/** Maximum number of script-checking threads allowed */
82-
static const int MAX_SCRIPTCHECK_THREADS = 16;
81+
/** Maximum number of dedicated script-checking threads allowed */
82+
static const int MAX_SCRIPTCHECK_THREADS = 15;
8383
/** -par default (number of script-checking threads, 0 = auto) */
8484
static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
8585
/**
@@ -176,7 +176,6 @@ extern std::condition_variable g_best_block_cv;
176176
extern uint256 g_best_block;
177177
extern std::atomic_bool fImporting;
178178
extern std::atomic_bool fReindex;
179-
extern int nScriptCheckThreads;
180179
extern bool fRequireStandard;
181180
extern bool fCheckBlockIndex;
182181
extern bool fCheckpointsEnabled;

0 commit comments

Comments
 (0)