Skip to content

Commit 498994b

Browse files
committed
Merge bitcoin#26762: bugfix: Make CCheckQueue RAII-styled (attempt 2)
5b3ea5f refactor: Move `{MAX,DEFAULT}_SCRIPTCHECK_THREADS` constants (Hennadii Stepanov) 6e17b31 refactor: Make `CCheckQueue` non-copyable and non-movable explicitly (Hennadii Stepanov) 8111e74 refactor: Drop unneeded declaration (Hennadii Stepanov) 9cf89f7 refactor: Make `CCheckQueue` constructor start worker threads (Hennadii Stepanov) d03eaac Make `CCheckQueue` destructor stop worker threads (Hennadii Stepanov) be4ff30 Move global `scriptcheckqueue` into `ChainstateManager` class (Hennadii Stepanov) Pull request description: This PR: - makes `CCheckQueue` RAII-styled - gets rid of the global `scriptcheckqueue` - fixes bitcoin#25448 The previous attempt was in bitcoin#18731. ACKs for top commit: martinus: ACK 5b3ea5f achow101: ACK 5b3ea5f TheCharlatan: ACK 5b3ea5f Tree-SHA512: 45cca846e7ed107e3930149f0b616ddbaf2648d6cde381f815331b861b5d67ab39e154883ae174b8abb1dae485bc904318c50c51e5d6b46923d89de51c5eadb0
2 parents ffb0216 + 5b3ea5f commit 498994b

15 files changed

+62
-110
lines changed

src/bench/checkqueue.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
3737
return true;
3838
}
3939
};
40-
CCheckQueue<PrevectorJob> queue {QUEUE_BATCH_SIZE};
40+
4141
// The main thread should be counted to prevent thread oversubscription, and
4242
// to decrease the variance of benchmark results.
43-
queue.StartWorkerThreads(GetNumCores() - 1);
43+
int worker_threads_num{GetNumCores() - 1};
44+
CCheckQueue<PrevectorJob> queue{QUEUE_BATCH_SIZE, worker_threads_num};
4445

4546
// create all the data once, then submit copies in the benchmark.
4647
FastRandomContext insecure_rand(true);
@@ -61,7 +62,6 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
6162
// it is done explicitly here for clarity
6263
control.Wait();
6364
});
64-
queue.StopWorkerThreads();
6565
ECC_Stop();
6666
}
6767
BENCHMARK(CCheckQueueSpeedPrevectorJob, benchmark::PriorityLevel::HIGH);

src/bitcoin-chainstate.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ 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();
294293

295294
GetMainSignals().FlushBackgroundCallbacks();
296295
{

src/checkqueue.h

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
#include <iterator>
1414
#include <vector>
1515

16-
template <typename T>
17-
class CCheckQueueControl;
18-
1916
/**
2017
* Queue for verifications that have to be performed.
2118
* The verifications are represented by a type T, which must provide an
@@ -130,29 +127,25 @@ class CCheckQueue
130127
Mutex m_control_mutex;
131128

132129
//! Create a new check queue
133-
explicit CCheckQueue(unsigned int nBatchSizeIn)
134-
: nBatchSize(nBatchSizeIn)
130+
explicit CCheckQueue(unsigned int batch_size, int worker_threads_num)
131+
: nBatchSize(batch_size)
135132
{
136-
}
137-
138-
//! Create a pool of new worker threads.
139-
void StartWorkerThreads(const int threads_num) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
140-
{
141-
{
142-
LOCK(m_mutex);
143-
nIdle = 0;
144-
nTotal = 0;
145-
fAllOk = true;
146-
}
147-
assert(m_worker_threads.empty());
148-
for (int n = 0; n < threads_num; ++n) {
133+
m_worker_threads.reserve(worker_threads_num);
134+
for (int n = 0; n < worker_threads_num; ++n) {
149135
m_worker_threads.emplace_back([this, n]() {
150136
util::ThreadRename(strprintf("scriptch.%i", n));
151137
Loop(false /* worker thread */);
152138
});
153139
}
154140
}
155141

142+
// Since this class manages its own resources, which is a thread
143+
// pool `m_worker_threads`, copy and move operations are not appropriate.
144+
CCheckQueue(const CCheckQueue&) = delete;
145+
CCheckQueue& operator=(const CCheckQueue&) = delete;
146+
CCheckQueue(CCheckQueue&&) = delete;
147+
CCheckQueue& operator=(CCheckQueue&&) = delete;
148+
156149
//! Wait until execution finishes, and return whether all evaluations were successful.
157150
bool Wait() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
158151
{
@@ -179,24 +172,16 @@ class CCheckQueue
179172
}
180173
}
181174

182-
//! Stop all of the worker threads.
183-
void StopWorkerThreads() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
175+
~CCheckQueue()
184176
{
185177
WITH_LOCK(m_mutex, m_request_stop = true);
186178
m_worker_cv.notify_all();
187179
for (std::thread& t : m_worker_threads) {
188180
t.join();
189181
}
190-
m_worker_threads.clear();
191-
WITH_LOCK(m_mutex, m_request_stop = false);
192182
}
193183

194184
bool HasThreads() const { return !m_worker_threads.empty(); }
195-
196-
~CCheckQueue()
197-
{
198-
assert(m_worker_threads.empty());
199-
}
200185
};
201186

202187
/**

src/init.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,9 @@ void Shutdown(NodeContext& node)
268268
StopTorControl();
269269

270270
// After everything has been shut down, but before things get flushed, stop the
271-
// CScheduler/checkqueue, scheduler and load block thread.
271+
// 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();
275274

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

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

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
#include <arith_uint256.h>
88
#include <common/args.h>
9-
#include <kernel/chainstatemanager_opts.h>
9+
#include <common/system.h>
10+
#include <logging.h>
1011
#include <node/coins_view_args.h>
1112
#include <node/database_args.h>
1213
#include <tinyformat.h>
@@ -16,6 +17,7 @@
1617
#include <util/translation.h>
1718
#include <validation.h>
1819

20+
#include <algorithm>
1921
#include <chrono>
2022
#include <string>
2123

@@ -41,6 +43,16 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
4143
ReadDatabaseArgs(args, opts.coins_db);
4244
ReadCoinsViewArgs(args, opts.coins_view);
4345

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

src/node/chainstatemanager_args.h

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

1111
class ArgsManager;
1212

13+
/** Maximum number of dedicated script-checking threads allowed */
14+
static constexpr int MAX_SCRIPTCHECK_THREADS{15};
15+
/** -par default (number of script-checking threads, 0 = auto) */
16+
static constexpr int DEFAULT_SCRIPTCHECK_THREADS{0};
17+
1318
namespace node {
1419
[[nodiscard]] util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts);
1520
} // namespace node

src/qt/optionsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
#include <common/system.h>
1919
#include <interfaces/node.h>
20+
#include <node/chainstatemanager_args.h>
2021
#include <netbase.h>
2122
#include <txdb.h>
22-
#include <validation.h>
2323

2424
#include <chrono>
2525

src/qt/optionsmodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <mapport.h>
1818
#include <net.h>
1919
#include <netbase.h>
20+
#include <node/chainstatemanager_args.h>
2021
#include <txdb.h> // for -dbcache defaults
2122
#include <util/string.h>
2223
#include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS

src/test/checkqueue_tests.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ typedef CCheckQueue<FrozenCleanupCheck> FrozenCleanup_Queue;
158158
*/
159159
static void Correct_Queue_range(std::vector<size_t> range)
160160
{
161-
auto small_queue = std::make_unique<Correct_Queue>(QUEUE_BATCH_SIZE);
162-
small_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
161+
auto small_queue = std::make_unique<Correct_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
163162
// Make vChecks here to save on malloc (this test can be slow...)
164163
std::vector<FakeCheckCheckCompletion> vChecks;
165164
vChecks.reserve(9);
@@ -176,7 +175,6 @@ static void Correct_Queue_range(std::vector<size_t> range)
176175
BOOST_REQUIRE(control.Wait());
177176
BOOST_REQUIRE_EQUAL(FakeCheckCheckCompletion::n_calls, i);
178177
}
179-
small_queue->StopWorkerThreads();
180178
}
181179

182180
/** Test that 0 checks is correct
@@ -218,9 +216,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
218216
/** Test that failing checks are caught */
219217
BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
220218
{
221-
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE);
222-
fail_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
223-
219+
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
224220
for (size_t i = 0; i < 1001; ++i) {
225221
CCheckQueueControl<FailingCheck> control(fail_queue.get());
226222
size_t remaining = i;
@@ -240,15 +236,12 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
240236
BOOST_REQUIRE(success);
241237
}
242238
}
243-
fail_queue->StopWorkerThreads();
244239
}
245240
// Test that a block validation which fails does not interfere with
246241
// future blocks, ie, the bad state is cleared.
247242
BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
248243
{
249-
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE);
250-
fail_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
251-
244+
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
252245
for (auto times = 0; times < 10; ++times) {
253246
for (const bool end_fails : {true, false}) {
254247
CCheckQueueControl<FailingCheck> control(fail_queue.get());
@@ -262,17 +255,14 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
262255
BOOST_REQUIRE(r != end_fails);
263256
}
264257
}
265-
fail_queue->StopWorkerThreads();
266258
}
267259

268260
// Test that unique checks are actually all called individually, rather than
269261
// just one check being called repeatedly. Test that checks are not called
270262
// more than once as well
271263
BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
272264
{
273-
auto queue = std::make_unique<Unique_Queue>(QUEUE_BATCH_SIZE);
274-
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
275-
265+
auto queue = std::make_unique<Unique_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
276266
size_t COUNT = 100000;
277267
size_t total = COUNT;
278268
{
@@ -294,7 +284,6 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
294284
}
295285
BOOST_REQUIRE(r);
296286
}
297-
queue->StopWorkerThreads();
298287
}
299288

300289

@@ -305,8 +294,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
305294
// time could leave the data hanging across a sequence of blocks.
306295
BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
307296
{
308-
auto queue = std::make_unique<Memory_Queue>(QUEUE_BATCH_SIZE);
309-
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
297+
auto queue = std::make_unique<Memory_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
310298
for (size_t i = 0; i < 1000; ++i) {
311299
size_t total = i;
312300
{
@@ -325,16 +313,14 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
325313
}
326314
BOOST_REQUIRE_EQUAL(MemoryCheck::fake_allocated_memory, 0U);
327315
}
328-
queue->StopWorkerThreads();
329316
}
330317

331318
// Test that a new verification cannot occur until all checks
332319
// have been destructed
333320
BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
334321
{
335-
auto queue = std::make_unique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE);
322+
auto queue = std::make_unique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
336323
bool fails = false;
337-
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
338324
std::thread t0([&]() {
339325
CCheckQueueControl<FrozenCleanupCheck> control(queue.get());
340326
std::vector<FrozenCleanupCheck> vChecks(1);
@@ -361,14 +347,13 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
361347
// Wait for control to finish
362348
t0.join();
363349
BOOST_REQUIRE(!fails);
364-
queue->StopWorkerThreads();
365350
}
366351

367352

368353
/** Test that CCheckQueueControl is threadsafe */
369354
BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
370355
{
371-
auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE);
356+
auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
372357
{
373358
std::vector<std::thread> tg;
374359
std::atomic<int> nThreads {0};

0 commit comments

Comments
 (0)