Skip to content

Commit 9cf89f7

Browse files
committed
refactor: Make CCheckQueue constructor start worker threads
1 parent d03eaac commit 9cf89f7

File tree

6 files changed

+18
-40
lines changed

6 files changed

+18
-40
lines changed

src/bench/checkqueue.cpp

Lines changed: 3 additions & 2 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);

src/checkqueue.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,11 @@ class CCheckQueue
130130
Mutex m_control_mutex;
131131

132132
//! Create a new check queue
133-
explicit CCheckQueue(unsigned int nBatchSizeIn)
134-
: nBatchSize(nBatchSizeIn)
133+
explicit CCheckQueue(unsigned int batch_size, int worker_threads_num)
134+
: nBatchSize(batch_size)
135135
{
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) {
136+
m_worker_threads.reserve(worker_threads_num);
137+
for (int n = 0; n < worker_threads_num; ++n) {
149138
m_worker_threads.emplace_back([this, n]() {
150139
util::ThreadRename(strprintf("scriptch.%i", n));
151140
Loop(false /* worker thread */);

src/test/checkqueue_tests.cpp

Lines changed: 7 additions & 16 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);
@@ -217,9 +216,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
217216
/** Test that failing checks are caught */
218217
BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
219218
{
220-
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE);
221-
fail_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
222-
219+
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
223220
for (size_t i = 0; i < 1001; ++i) {
224221
CCheckQueueControl<FailingCheck> control(fail_queue.get());
225222
size_t remaining = i;
@@ -244,9 +241,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
244241
// future blocks, ie, the bad state is cleared.
245242
BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
246243
{
247-
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE);
248-
fail_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
249-
244+
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
250245
for (auto times = 0; times < 10; ++times) {
251246
for (const bool end_fails : {true, false}) {
252247
CCheckQueueControl<FailingCheck> control(fail_queue.get());
@@ -267,9 +262,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
267262
// more than once as well
268263
BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
269264
{
270-
auto queue = std::make_unique<Unique_Queue>(QUEUE_BATCH_SIZE);
271-
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
272-
265+
auto queue = std::make_unique<Unique_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
273266
size_t COUNT = 100000;
274267
size_t total = COUNT;
275268
{
@@ -301,8 +294,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
301294
// time could leave the data hanging across a sequence of blocks.
302295
BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
303296
{
304-
auto queue = std::make_unique<Memory_Queue>(QUEUE_BATCH_SIZE);
305-
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
297+
auto queue = std::make_unique<Memory_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
306298
for (size_t i = 0; i < 1000; ++i) {
307299
size_t total = i;
308300
{
@@ -327,9 +319,8 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
327319
// have been destructed
328320
BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
329321
{
330-
auto queue = std::make_unique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE);
322+
auto queue = std::make_unique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
331323
bool fails = false;
332-
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
333324
std::thread t0([&]() {
334325
CCheckQueueControl<FrozenCleanupCheck> control(queue.get());
335326
std::vector<FrozenCleanupCheck> vChecks(1);
@@ -362,7 +353,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
362353
/** Test that CCheckQueueControl is threadsafe */
363354
BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
364355
{
365-
auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE);
356+
auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
366357
{
367358
std::vector<std::thread> tg;
368359
std::atomic<int> nThreads {0};

src/test/fuzz/checkqueue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ FUZZ_TARGET(checkqueue)
3131
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
3232

3333
const unsigned int batch_size = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1024);
34-
CCheckQueue<DumbCheck> check_queue_1{batch_size};
35-
CCheckQueue<DumbCheck> check_queue_2{batch_size};
34+
CCheckQueue<DumbCheck> check_queue_1{batch_size, /*worker_threads_num=*/0};
35+
CCheckQueue<DumbCheck> check_queue_2{batch_size, /*worker_threads_num=*/0};
3636
std::vector<DumbCheck> checks_1;
3737
std::vector<DumbCheck> checks_2;
3838
const int size = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024);

src/test/transaction_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,9 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
530530

531531
// check all inputs concurrently, with the cache
532532
PrecomputedTransactionData txdata(tx);
533-
CCheckQueue<CScriptCheck> scriptcheckqueue(128);
533+
CCheckQueue<CScriptCheck> scriptcheckqueue(/*batch_size=*/128, /*worker_threads_num=*/20);
534534
CCheckQueueControl<CScriptCheck> control(&scriptcheckqueue);
535535

536-
scriptcheckqueue.StartWorkerThreads(20);
537-
538536
std::vector<Coin> coins;
539537
for(uint32_t i = 0; i < mtx.vin.size(); i++) {
540538
Coin coin;

src/validation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5739,12 +5739,11 @@ static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
57395739
}
57405740

57415741
ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
5742-
: m_script_check_queue{/*nBatchSizeIn=*/128},
5742+
: m_script_check_queue{/*batch_size=*/128, options.worker_threads_num},
57435743
m_interrupt{interrupt},
57445744
m_options{Flatten(std::move(options))},
57455745
m_blockman{interrupt, std::move(blockman_options)}
57465746
{
5747-
m_script_check_queue.StartWorkerThreads(m_options.worker_threads_num);
57485747
}
57495748

57505749
ChainstateManager::~ChainstateManager()

0 commit comments

Comments
 (0)