mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-21 14:34:49 +01:00
Merge bitcoin/bitcoin#26762: bugfix: Make CCheckQueue
RAII-styled (attempt 2)
5b3ea5fa2e
refactor: Move `{MAX,DEFAULT}_SCRIPTCHECK_THREADS` constants (Hennadii Stepanov)6e17b31680
refactor: Make `CCheckQueue` non-copyable and non-movable explicitly (Hennadii Stepanov)8111e74653
refactor: Drop unneeded declaration (Hennadii Stepanov)9cf89f7a5b
refactor: Make `CCheckQueue` constructor start worker threads (Hennadii Stepanov)d03eaacbcf
Make `CCheckQueue` destructor stop worker threads (Hennadii Stepanov)be4ff3060b
Move global `scriptcheckqueue` into `ChainstateManager` class (Hennadii Stepanov) Pull request description: This PR: - makes `CCheckQueue` RAII-styled - gets rid of the global `scriptcheckqueue` - fixes https://github.com/bitcoin/bitcoin/issues/25448 The previous attempt was in https://github.com/bitcoin/bitcoin/pull/18731. ACKs for top commit: martinus: ACK5b3ea5fa2e
achow101: ACK5b3ea5fa2e
TheCharlatan: ACK5b3ea5fa2e
Tree-SHA512: 45cca846e7ed107e3930149f0b616ddbaf2648d6cde381f815331b861b5d67ab39e154883ae174b8abb1dae485bc904318c50c51e5d6b46923d89de51c5eadb0
This commit is contained in:
commit
498994b6f5
15 changed files with 62 additions and 110 deletions
|
@ -37,10 +37,11 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
CCheckQueue<PrevectorJob> queue {QUEUE_BATCH_SIZE};
|
|
||||||
// The main thread should be counted to prevent thread oversubscription, and
|
// The main thread should be counted to prevent thread oversubscription, and
|
||||||
// to decrease the variance of benchmark results.
|
// to decrease the variance of benchmark results.
|
||||||
queue.StartWorkerThreads(GetNumCores() - 1);
|
int worker_threads_num{GetNumCores() - 1};
|
||||||
|
CCheckQueue<PrevectorJob> queue{QUEUE_BATCH_SIZE, worker_threads_num};
|
||||||
|
|
||||||
// create all the data once, then submit copies in the benchmark.
|
// create all the data once, then submit copies in the benchmark.
|
||||||
FastRandomContext insecure_rand(true);
|
FastRandomContext insecure_rand(true);
|
||||||
|
@ -61,7 +62,6 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
|
||||||
// it is done explicitly here for clarity
|
// it is done explicitly here for clarity
|
||||||
control.Wait();
|
control.Wait();
|
||||||
});
|
});
|
||||||
queue.StopWorkerThreads();
|
|
||||||
ECC_Stop();
|
ECC_Stop();
|
||||||
}
|
}
|
||||||
BENCHMARK(CCheckQueueSpeedPrevectorJob, benchmark::PriorityLevel::HIGH);
|
BENCHMARK(CCheckQueueSpeedPrevectorJob, benchmark::PriorityLevel::HIGH);
|
||||||
|
|
|
@ -290,7 +290,6 @@ epilogue:
|
||||||
// dereferencing and UB.
|
// dereferencing and UB.
|
||||||
scheduler.stop();
|
scheduler.stop();
|
||||||
if (chainman.m_thread_load.joinable()) chainman.m_thread_load.join();
|
if (chainman.m_thread_load.joinable()) chainman.m_thread_load.join();
|
||||||
StopScriptCheckWorkerThreads();
|
|
||||||
|
|
||||||
GetMainSignals().FlushBackgroundCallbacks();
|
GetMainSignals().FlushBackgroundCallbacks();
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,9 +13,6 @@
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class CCheckQueueControl;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue for verifications that have to be performed.
|
* Queue for verifications that have to be performed.
|
||||||
* The verifications are represented by a type T, which must provide an
|
* The verifications are represented by a type T, which must provide an
|
||||||
|
@ -130,22 +127,11 @@ public:
|
||||||
Mutex m_control_mutex;
|
Mutex m_control_mutex;
|
||||||
|
|
||||||
//! Create a new check queue
|
//! Create a new check queue
|
||||||
explicit CCheckQueue(unsigned int nBatchSizeIn)
|
explicit CCheckQueue(unsigned int batch_size, int worker_threads_num)
|
||||||
: nBatchSize(nBatchSizeIn)
|
: nBatchSize(batch_size)
|
||||||
{
|
{
|
||||||
}
|
m_worker_threads.reserve(worker_threads_num);
|
||||||
|
for (int n = 0; n < worker_threads_num; ++n) {
|
||||||
//! Create a pool of new worker threads.
|
|
||||||
void StartWorkerThreads(const int threads_num) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
|
||||||
{
|
|
||||||
{
|
|
||||||
LOCK(m_mutex);
|
|
||||||
nIdle = 0;
|
|
||||||
nTotal = 0;
|
|
||||||
fAllOk = true;
|
|
||||||
}
|
|
||||||
assert(m_worker_threads.empty());
|
|
||||||
for (int n = 0; n < threads_num; ++n) {
|
|
||||||
m_worker_threads.emplace_back([this, n]() {
|
m_worker_threads.emplace_back([this, n]() {
|
||||||
util::ThreadRename(strprintf("scriptch.%i", n));
|
util::ThreadRename(strprintf("scriptch.%i", n));
|
||||||
Loop(false /* worker thread */);
|
Loop(false /* worker thread */);
|
||||||
|
@ -153,6 +139,13 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Since this class manages its own resources, which is a thread
|
||||||
|
// pool `m_worker_threads`, copy and move operations are not appropriate.
|
||||||
|
CCheckQueue(const CCheckQueue&) = delete;
|
||||||
|
CCheckQueue& operator=(const CCheckQueue&) = delete;
|
||||||
|
CCheckQueue(CCheckQueue&&) = delete;
|
||||||
|
CCheckQueue& operator=(CCheckQueue&&) = delete;
|
||||||
|
|
||||||
//! Wait until execution finishes, and return whether all evaluations were successful.
|
//! Wait until execution finishes, and return whether all evaluations were successful.
|
||||||
bool Wait() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
bool Wait() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||||
{
|
{
|
||||||
|
@ -179,24 +172,16 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Stop all of the worker threads.
|
~CCheckQueue()
|
||||||
void StopWorkerThreads() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
|
||||||
{
|
{
|
||||||
WITH_LOCK(m_mutex, m_request_stop = true);
|
WITH_LOCK(m_mutex, m_request_stop = true);
|
||||||
m_worker_cv.notify_all();
|
m_worker_cv.notify_all();
|
||||||
for (std::thread& t : m_worker_threads) {
|
for (std::thread& t : m_worker_threads) {
|
||||||
t.join();
|
t.join();
|
||||||
}
|
}
|
||||||
m_worker_threads.clear();
|
|
||||||
WITH_LOCK(m_mutex, m_request_stop = false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HasThreads() const { return !m_worker_threads.empty(); }
|
bool HasThreads() const { return !m_worker_threads.empty(); }
|
||||||
|
|
||||||
~CCheckQueue()
|
|
||||||
{
|
|
||||||
assert(m_worker_threads.empty());
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
21
src/init.cpp
21
src/init.cpp
|
@ -268,10 +268,9 @@ void Shutdown(NodeContext& node)
|
||||||
StopTorControl();
|
StopTorControl();
|
||||||
|
|
||||||
// After everything has been shut down, but before things get flushed, stop the
|
// After everything has been shut down, but before things get flushed, stop the
|
||||||
// CScheduler/checkqueue, scheduler and load block thread.
|
// scheduler and load block thread.
|
||||||
if (node.scheduler) node.scheduler->stop();
|
if (node.scheduler) node.scheduler->stop();
|
||||||
if (node.chainman && node.chainman->m_thread_load.joinable()) node.chainman->m_thread_load.join();
|
if (node.chainman && node.chainman->m_thread_load.joinable()) node.chainman->m_thread_load.join();
|
||||||
StopScriptCheckWorkerThreads();
|
|
||||||
|
|
||||||
// After the threads that potentially access these pointers have been stopped,
|
// After the threads that potentially access these pointers have been stopped,
|
||||||
// destruct and reset all to nullptr.
|
// destruct and reset all to nullptr.
|
||||||
|
@ -1114,24 +1113,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
return InitError(strprintf(_("Unable to allocate memory for -maxsigcachesize: '%s' MiB"), args.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_BYTES >> 20)));
|
return InitError(strprintf(_("Unable to allocate memory for -maxsigcachesize: '%s' MiB"), args.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_BYTES >> 20)));
|
||||||
}
|
}
|
||||||
|
|
||||||
int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
|
|
||||||
if (script_threads <= 0) {
|
|
||||||
// -par=0 means autodetect (number of cores - 1 script threads)
|
|
||||||
// -par=-n means "leave n cores free" (number of cores - n - 1 script threads)
|
|
||||||
script_threads += GetNumCores();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subtract 1 because the main thread counts towards the par threads
|
|
||||||
script_threads = std::max(script_threads - 1, 0);
|
|
||||||
|
|
||||||
// Number of script-checking threads <= MAX_SCRIPTCHECK_THREADS
|
|
||||||
script_threads = std::min(script_threads, MAX_SCRIPTCHECK_THREADS);
|
|
||||||
|
|
||||||
LogPrintf("Script verification uses %d additional threads\n", script_threads);
|
|
||||||
if (script_threads >= 1) {
|
|
||||||
StartScriptCheckWorkerThreads(script_threads);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(!node.scheduler);
|
assert(!node.scheduler);
|
||||||
node.scheduler = std::make_unique<CScheduler>();
|
node.scheduler = std::make_unique<CScheduler>();
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,8 @@ struct ChainstateManagerOpts {
|
||||||
DBOptions coins_db{};
|
DBOptions coins_db{};
|
||||||
CoinsViewOptions coins_view{};
|
CoinsViewOptions coins_view{};
|
||||||
Notifications& notifications;
|
Notifications& notifications;
|
||||||
|
//! Number of script check worker threads. Zero means no parallel verification.
|
||||||
|
int worker_threads_num{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace kernel
|
} // namespace kernel
|
||||||
|
|
|
@ -6,7 +6,8 @@
|
||||||
|
|
||||||
#include <arith_uint256.h>
|
#include <arith_uint256.h>
|
||||||
#include <common/args.h>
|
#include <common/args.h>
|
||||||
#include <kernel/chainstatemanager_opts.h>
|
#include <common/system.h>
|
||||||
|
#include <logging.h>
|
||||||
#include <node/coins_view_args.h>
|
#include <node/coins_view_args.h>
|
||||||
#include <node/database_args.h>
|
#include <node/database_args.h>
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
|
@ -16,6 +17,7 @@
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
@ -41,6 +43,16 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
|
||||||
ReadDatabaseArgs(args, opts.coins_db);
|
ReadDatabaseArgs(args, opts.coins_db);
|
||||||
ReadCoinsViewArgs(args, opts.coins_view);
|
ReadCoinsViewArgs(args, opts.coins_view);
|
||||||
|
|
||||||
|
int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
|
||||||
|
if (script_threads <= 0) {
|
||||||
|
// -par=0 means autodetect (number of cores - 1 script threads)
|
||||||
|
// -par=-n means "leave n cores free" (number of cores - n - 1 script threads)
|
||||||
|
script_threads += GetNumCores();
|
||||||
|
}
|
||||||
|
// Subtract 1 because the main thread counts towards the par threads.
|
||||||
|
opts.worker_threads_num = std::clamp(script_threads - 1, 0, MAX_SCRIPTCHECK_THREADS);
|
||||||
|
LogPrintf("Script verification uses %d additional threads\n", opts.worker_threads_num);
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
|
|
@ -10,6 +10,11 @@
|
||||||
|
|
||||||
class ArgsManager;
|
class ArgsManager;
|
||||||
|
|
||||||
|
/** Maximum number of dedicated script-checking threads allowed */
|
||||||
|
static constexpr int MAX_SCRIPTCHECK_THREADS{15};
|
||||||
|
/** -par default (number of script-checking threads, 0 = auto) */
|
||||||
|
static constexpr int DEFAULT_SCRIPTCHECK_THREADS{0};
|
||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
[[nodiscard]] util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts);
|
[[nodiscard]] util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts);
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
|
|
@ -17,9 +17,9 @@
|
||||||
|
|
||||||
#include <common/system.h>
|
#include <common/system.h>
|
||||||
#include <interfaces/node.h>
|
#include <interfaces/node.h>
|
||||||
|
#include <node/chainstatemanager_args.h>
|
||||||
#include <netbase.h>
|
#include <netbase.h>
|
||||||
#include <txdb.h>
|
#include <txdb.h>
|
||||||
#include <validation.h>
|
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include <mapport.h>
|
#include <mapport.h>
|
||||||
#include <net.h>
|
#include <net.h>
|
||||||
#include <netbase.h>
|
#include <netbase.h>
|
||||||
|
#include <node/chainstatemanager_args.h>
|
||||||
#include <txdb.h> // for -dbcache defaults
|
#include <txdb.h> // for -dbcache defaults
|
||||||
#include <util/string.h>
|
#include <util/string.h>
|
||||||
#include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS
|
#include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS
|
||||||
|
|
|
@ -158,8 +158,7 @@ typedef CCheckQueue<FrozenCleanupCheck> FrozenCleanup_Queue;
|
||||||
*/
|
*/
|
||||||
static void Correct_Queue_range(std::vector<size_t> range)
|
static void Correct_Queue_range(std::vector<size_t> range)
|
||||||
{
|
{
|
||||||
auto small_queue = std::make_unique<Correct_Queue>(QUEUE_BATCH_SIZE);
|
auto small_queue = std::make_unique<Correct_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
|
||||||
small_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
|
|
||||||
// Make vChecks here to save on malloc (this test can be slow...)
|
// Make vChecks here to save on malloc (this test can be slow...)
|
||||||
std::vector<FakeCheckCheckCompletion> vChecks;
|
std::vector<FakeCheckCheckCompletion> vChecks;
|
||||||
vChecks.reserve(9);
|
vChecks.reserve(9);
|
||||||
|
@ -176,7 +175,6 @@ static void Correct_Queue_range(std::vector<size_t> range)
|
||||||
BOOST_REQUIRE(control.Wait());
|
BOOST_REQUIRE(control.Wait());
|
||||||
BOOST_REQUIRE_EQUAL(FakeCheckCheckCompletion::n_calls, i);
|
BOOST_REQUIRE_EQUAL(FakeCheckCheckCompletion::n_calls, i);
|
||||||
}
|
}
|
||||||
small_queue->StopWorkerThreads();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Test that 0 checks is correct
|
/** Test that 0 checks is correct
|
||||||
|
@ -218,9 +216,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
|
||||||
/** Test that failing checks are caught */
|
/** Test that failing checks are caught */
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
|
||||||
{
|
{
|
||||||
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE);
|
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
|
||||||
fail_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < 1001; ++i) {
|
for (size_t i = 0; i < 1001; ++i) {
|
||||||
CCheckQueueControl<FailingCheck> control(fail_queue.get());
|
CCheckQueueControl<FailingCheck> control(fail_queue.get());
|
||||||
size_t remaining = i;
|
size_t remaining = i;
|
||||||
|
@ -240,15 +236,12 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
|
||||||
BOOST_REQUIRE(success);
|
BOOST_REQUIRE(success);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fail_queue->StopWorkerThreads();
|
|
||||||
}
|
}
|
||||||
// Test that a block validation which fails does not interfere with
|
// Test that a block validation which fails does not interfere with
|
||||||
// future blocks, ie, the bad state is cleared.
|
// future blocks, ie, the bad state is cleared.
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
|
||||||
{
|
{
|
||||||
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE);
|
auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
|
||||||
fail_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
|
|
||||||
|
|
||||||
for (auto times = 0; times < 10; ++times) {
|
for (auto times = 0; times < 10; ++times) {
|
||||||
for (const bool end_fails : {true, false}) {
|
for (const bool end_fails : {true, false}) {
|
||||||
CCheckQueueControl<FailingCheck> control(fail_queue.get());
|
CCheckQueueControl<FailingCheck> control(fail_queue.get());
|
||||||
|
@ -262,7 +255,6 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
|
||||||
BOOST_REQUIRE(r != end_fails);
|
BOOST_REQUIRE(r != end_fails);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fail_queue->StopWorkerThreads();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that unique checks are actually all called individually, rather than
|
// Test that unique checks are actually all called individually, rather than
|
||||||
|
@ -270,9 +262,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
|
||||||
// more than once as well
|
// more than once as well
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
|
||||||
{
|
{
|
||||||
auto queue = std::make_unique<Unique_Queue>(QUEUE_BATCH_SIZE);
|
auto queue = std::make_unique<Unique_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
|
||||||
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
|
|
||||||
|
|
||||||
size_t COUNT = 100000;
|
size_t COUNT = 100000;
|
||||||
size_t total = COUNT;
|
size_t total = COUNT;
|
||||||
{
|
{
|
||||||
|
@ -294,7 +284,6 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
|
||||||
}
|
}
|
||||||
BOOST_REQUIRE(r);
|
BOOST_REQUIRE(r);
|
||||||
}
|
}
|
||||||
queue->StopWorkerThreads();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -305,8 +294,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
|
||||||
// time could leave the data hanging across a sequence of blocks.
|
// time could leave the data hanging across a sequence of blocks.
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
|
||||||
{
|
{
|
||||||
auto queue = std::make_unique<Memory_Queue>(QUEUE_BATCH_SIZE);
|
auto queue = std::make_unique<Memory_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
|
||||||
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
|
|
||||||
for (size_t i = 0; i < 1000; ++i) {
|
for (size_t i = 0; i < 1000; ++i) {
|
||||||
size_t total = i;
|
size_t total = i;
|
||||||
{
|
{
|
||||||
|
@ -325,16 +313,14 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
|
||||||
}
|
}
|
||||||
BOOST_REQUIRE_EQUAL(MemoryCheck::fake_allocated_memory, 0U);
|
BOOST_REQUIRE_EQUAL(MemoryCheck::fake_allocated_memory, 0U);
|
||||||
}
|
}
|
||||||
queue->StopWorkerThreads();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that a new verification cannot occur until all checks
|
// Test that a new verification cannot occur until all checks
|
||||||
// have been destructed
|
// have been destructed
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
|
||||||
{
|
{
|
||||||
auto queue = std::make_unique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE);
|
auto queue = std::make_unique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
|
||||||
bool fails = false;
|
bool fails = false;
|
||||||
queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
|
|
||||||
std::thread t0([&]() {
|
std::thread t0([&]() {
|
||||||
CCheckQueueControl<FrozenCleanupCheck> control(queue.get());
|
CCheckQueueControl<FrozenCleanupCheck> control(queue.get());
|
||||||
std::vector<FrozenCleanupCheck> vChecks(1);
|
std::vector<FrozenCleanupCheck> vChecks(1);
|
||||||
|
@ -361,14 +347,13 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
|
||||||
// Wait for control to finish
|
// Wait for control to finish
|
||||||
t0.join();
|
t0.join();
|
||||||
BOOST_REQUIRE(!fails);
|
BOOST_REQUIRE(!fails);
|
||||||
queue->StopWorkerThreads();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Test that CCheckQueueControl is threadsafe */
|
/** Test that CCheckQueueControl is threadsafe */
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
|
BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
|
||||||
{
|
{
|
||||||
auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE);
|
auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
|
||||||
{
|
{
|
||||||
std::vector<std::thread> tg;
|
std::vector<std::thread> tg;
|
||||||
std::atomic<int> nThreads {0};
|
std::atomic<int> nThreads {0};
|
||||||
|
|
|
@ -31,8 +31,8 @@ FUZZ_TARGET(checkqueue)
|
||||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||||
|
|
||||||
const unsigned int batch_size = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1024);
|
const unsigned int batch_size = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1024);
|
||||||
CCheckQueue<DumbCheck> check_queue_1{batch_size};
|
CCheckQueue<DumbCheck> check_queue_1{batch_size, /*worker_threads_num=*/0};
|
||||||
CCheckQueue<DumbCheck> check_queue_2{batch_size};
|
CCheckQueue<DumbCheck> check_queue_2{batch_size, /*worker_threads_num=*/0};
|
||||||
std::vector<DumbCheck> checks_1;
|
std::vector<DumbCheck> checks_1;
|
||||||
std::vector<DumbCheck> checks_2;
|
std::vector<DumbCheck> checks_2;
|
||||||
const int size = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024);
|
const int size = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024);
|
||||||
|
|
|
@ -529,11 +529,9 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
|
||||||
|
|
||||||
// check all inputs concurrently, with the cache
|
// check all inputs concurrently, with the cache
|
||||||
PrecomputedTransactionData txdata(tx);
|
PrecomputedTransactionData txdata(tx);
|
||||||
CCheckQueue<CScriptCheck> scriptcheckqueue(128);
|
CCheckQueue<CScriptCheck> scriptcheckqueue(/*batch_size=*/128, /*worker_threads_num=*/20);
|
||||||
CCheckQueueControl<CScriptCheck> control(&scriptcheckqueue);
|
CCheckQueueControl<CScriptCheck> control(&scriptcheckqueue);
|
||||||
|
|
||||||
scriptcheckqueue.StartWorkerThreads(20);
|
|
||||||
|
|
||||||
std::vector<Coin> coins;
|
std::vector<Coin> coins;
|
||||||
for(uint32_t i = 0; i < mtx.vin.size(); i++) {
|
for(uint32_t i = 0; i < mtx.vin.size(); i++) {
|
||||||
Coin coin;
|
Coin coin;
|
||||||
|
@ -552,7 +550,6 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
|
||||||
|
|
||||||
bool controlCheck = control.Wait();
|
bool controlCheck = control.Wait();
|
||||||
assert(controlCheck);
|
assert(controlCheck);
|
||||||
scriptcheckqueue.StopWorkerThreads();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SignatureData CombineSignatures(const CMutableTransaction& input1, const CMutableTransaction& input2, const CTransactionRef tx)
|
SignatureData CombineSignatures(const CMutableTransaction& input1, const CMutableTransaction& input2, const CTransactionRef tx)
|
||||||
|
|
|
@ -187,6 +187,7 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
|
||||||
.adjusted_time_callback = GetAdjustedTime,
|
.adjusted_time_callback = GetAdjustedTime,
|
||||||
.check_block_index = true,
|
.check_block_index = true,
|
||||||
.notifications = *m_node.notifications,
|
.notifications = *m_node.notifications,
|
||||||
|
.worker_threads_num = 2,
|
||||||
};
|
};
|
||||||
const BlockManager::Options blockman_opts{
|
const BlockManager::Options blockman_opts{
|
||||||
.chainparams = chainman_opts.chainparams,
|
.chainparams = chainman_opts.chainparams,
|
||||||
|
@ -198,15 +199,11 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
|
||||||
.path = m_args.GetDataDirNet() / "blocks" / "index",
|
.path = m_args.GetDataDirNet() / "blocks" / "index",
|
||||||
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),
|
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),
|
||||||
.memory_only = true});
|
.memory_only = true});
|
||||||
|
|
||||||
constexpr int script_check_threads = 2;
|
|
||||||
StartScriptCheckWorkerThreads(script_check_threads);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ChainTestingSetup::~ChainTestingSetup()
|
ChainTestingSetup::~ChainTestingSetup()
|
||||||
{
|
{
|
||||||
if (m_node.scheduler) m_node.scheduler->stop();
|
if (m_node.scheduler) m_node.scheduler->stop();
|
||||||
StopScriptCheckWorkerThreads();
|
|
||||||
GetMainSignals().FlushBackgroundCallbacks();
|
GetMainSignals().FlushBackgroundCallbacks();
|
||||||
GetMainSignals().UnregisterBackgroundSignalScheduler();
|
GetMainSignals().UnregisterBackgroundSignalScheduler();
|
||||||
m_node.connman.reset();
|
m_node.connman.reset();
|
||||||
|
|
|
@ -2079,18 +2079,6 @@ DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIn
|
||||||
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
|
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
|
|
||||||
|
|
||||||
void StartScriptCheckWorkerThreads(int threads_num)
|
|
||||||
{
|
|
||||||
scriptcheckqueue.StartWorkerThreads(threads_num);
|
|
||||||
}
|
|
||||||
|
|
||||||
void StopScriptCheckWorkerThreads()
|
|
||||||
{
|
|
||||||
scriptcheckqueue.StopWorkerThreads();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Threshold condition checker that triggers when unknown versionbits are seen on the network.
|
* Threshold condition checker that triggers when unknown versionbits are seen on the network.
|
||||||
*/
|
*/
|
||||||
|
@ -2179,7 +2167,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
|
||||||
|
|
||||||
uint256 block_hash{block.GetHash()};
|
uint256 block_hash{block.GetHash()};
|
||||||
assert(*pindex->phashBlock == block_hash);
|
assert(*pindex->phashBlock == block_hash);
|
||||||
const bool parallel_script_checks{scriptcheckqueue.HasThreads()};
|
const bool parallel_script_checks{m_chainman.GetCheckQueue().HasThreads()};
|
||||||
|
|
||||||
const auto time_start{SteadyClock::now()};
|
const auto time_start{SteadyClock::now()};
|
||||||
const CChainParams& params{m_chainman.GetParams()};
|
const CChainParams& params{m_chainman.GetParams()};
|
||||||
|
@ -2368,7 +2356,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
|
||||||
// in multiple threads). Preallocate the vector size so a new allocation
|
// in multiple threads). Preallocate the vector size so a new allocation
|
||||||
// doesn't invalidate pointers into the vector, and keep txsdata in scope
|
// doesn't invalidate pointers into the vector, and keep txsdata in scope
|
||||||
// for as long as `control`.
|
// for as long as `control`.
|
||||||
CCheckQueueControl<CScriptCheck> control(fScriptChecks && parallel_script_checks ? &scriptcheckqueue : nullptr);
|
CCheckQueueControl<CScriptCheck> control(fScriptChecks && parallel_script_checks ? &m_chainman.GetCheckQueue() : nullptr);
|
||||||
std::vector<PrecomputedTransactionData> txsdata(block.vtx.size());
|
std::vector<PrecomputedTransactionData> txsdata(block.vtx.size());
|
||||||
|
|
||||||
std::vector<int> prevheights;
|
std::vector<int> prevheights;
|
||||||
|
@ -5792,9 +5780,12 @@ static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
|
ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
|
||||||
: m_interrupt{interrupt},
|
: m_script_check_queue{/*batch_size=*/128, options.worker_threads_num},
|
||||||
|
m_interrupt{interrupt},
|
||||||
m_options{Flatten(std::move(options))},
|
m_options{Flatten(std::move(options))},
|
||||||
m_blockman{interrupt, std::move(blockman_options)} {}
|
m_blockman{interrupt, std::move(blockman_options)}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
ChainstateManager::~ChainstateManager()
|
ChainstateManager::~ChainstateManager()
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <arith_uint256.h>
|
#include <arith_uint256.h>
|
||||||
#include <attributes.h>
|
#include <attributes.h>
|
||||||
#include <chain.h>
|
#include <chain.h>
|
||||||
|
#include <checkqueue.h>
|
||||||
#include <kernel/chain.h>
|
#include <kernel/chain.h>
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
#include <deploymentstatus.h>
|
#include <deploymentstatus.h>
|
||||||
|
@ -65,10 +66,6 @@ namespace util {
|
||||||
class SignalInterrupt;
|
class SignalInterrupt;
|
||||||
} // namespace util
|
} // namespace util
|
||||||
|
|
||||||
/** Maximum number of dedicated script-checking threads allowed */
|
|
||||||
static const int MAX_SCRIPTCHECK_THREADS = 15;
|
|
||||||
/** -par default (number of script-checking threads, 0 = auto) */
|
|
||||||
static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
|
|
||||||
/** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pruned. */
|
/** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pruned. */
|
||||||
static const unsigned int MIN_BLOCKS_TO_KEEP = 288;
|
static const unsigned int MIN_BLOCKS_TO_KEEP = 288;
|
||||||
static const signed int DEFAULT_CHECKBLOCKS = 6;
|
static const signed int DEFAULT_CHECKBLOCKS = 6;
|
||||||
|
@ -98,11 +95,6 @@ extern uint256 g_best_block;
|
||||||
/** Documentation for argument 'checklevel'. */
|
/** Documentation for argument 'checklevel'. */
|
||||||
extern const std::vector<std::string> CHECKLEVEL_DOC;
|
extern const std::vector<std::string> CHECKLEVEL_DOC;
|
||||||
|
|
||||||
/** Run instances of script checking worker threads */
|
|
||||||
void StartScriptCheckWorkerThreads(int threads_num);
|
|
||||||
/** Stop all of the script checking worker threads */
|
|
||||||
void StopScriptCheckWorkerThreads();
|
|
||||||
|
|
||||||
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
|
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
|
||||||
|
|
||||||
bool FatalError(kernel::Notifications& notifications, BlockValidationState& state, const std::string& strMessage, const bilingual_str& userMessage = {});
|
bool FatalError(kernel::Notifications& notifications, BlockValidationState& state, const std::string& strMessage, const bilingual_str& userMessage = {});
|
||||||
|
@ -926,6 +918,9 @@ private:
|
||||||
return cs && !cs->m_disabled;
|
return cs && !cs->m_disabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! A queue for script verifications that have to be performed by worker threads.
|
||||||
|
CCheckQueue<CScriptCheck> m_script_check_queue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Options = kernel::ChainstateManagerOpts;
|
using Options = kernel::ChainstateManagerOpts;
|
||||||
|
|
||||||
|
@ -1276,6 +1271,8 @@ public:
|
||||||
//! nullopt.
|
//! nullopt.
|
||||||
std::optional<int> GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
std::optional<int> GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||||
|
|
||||||
|
CCheckQueue<CScriptCheck>& GetCheckQueue() { return m_script_check_queue; }
|
||||||
|
|
||||||
~ChainstateManager();
|
~ChainstateManager();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue