Skip to content

Commit

Permalink
Merge bitcoin#31313: refactor: Clamp worker threads in ChainstateMana…
Browse files Browse the repository at this point in the history
…ger constructor

8f85d36 refactor: Clamp worker threads in ChainstateManager constructor (TheCharlatan)

Pull request description:

  This ensures the options are applied consistently from contexts where they might not pass through the args manager, such as in some tests, or when used through the kernel library.

  This is similar to the patch applied in 09ef322, used to make applying the mempool options consistent.

  ---

  This is part of the libbitcoinkernel project bitcoin#27587

ACKs for top commit:
  maflcko:
    ACK 8f85d36 🛳
  achow101:
    ACK 8f85d36
  furszy:
    Code ACK 8f85d36
  stickies-v:
    ACK 8f85d36

Tree-SHA512: 32d7cc177d6726ee9df62ac9eb43e49ba676f35bfcff47834bd97a1e33f2a9ea7be65d0a8a37be149de04e58c9c500ecef730e498f4e3909042324d3136160e9
  • Loading branch information
achow101 committed Dec 3, 2024
2 parents c9a7418 + 8f85d36 commit ff873a2
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/checkqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BITCOIN_CHECKQUEUE_H
#define BITCOIN_CHECKQUEUE_H

#include <logging.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/threadnames.h>
Expand Down Expand Up @@ -143,6 +144,7 @@ class CCheckQueue
explicit CCheckQueue(unsigned int batch_size, int worker_threads_num)
: nBatchSize(batch_size)
{
LogInfo("Script verification uses %d additional threads", worker_threads_num);
m_worker_threads.reserve(worker_threads_num);
for (int n = 0; n < worker_threads_num; ++n) {
m_worker_threads.emplace_back([this, n]() {
Expand Down
3 changes: 1 addition & 2 deletions src/node/chainstatemanager_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
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);
opts.worker_threads_num = script_threads - 1;

if (auto max_size = args.GetIntArg("-maxsigcachesize")) {
// 1. When supplied with a max_size of 0, both the signature cache and
Expand Down
2 changes: 0 additions & 2 deletions src/node/chainstatemanager_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

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};

Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6294,7 +6294,7 @@ static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
}

ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
: m_script_check_queue{/*batch_size=*/128, options.worker_threads_num},
: m_script_check_queue{/*batch_size=*/128, std::clamp(options.worker_threads_num, 0, MAX_SCRIPTCHECK_THREADS)},
m_interrupt{interrupt},
m_options{Flatten(std::move(options))},
m_blockman{interrupt, std::move(blockman_options)},
Expand Down
3 changes: 3 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ static constexpr int DEFAULT_CHECKLEVEL{3};
// Setting the target to >= 550 MiB will make it likely we can respect the target.
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;

/** Maximum number of dedicated script-checking threads allowed */
static constexpr int MAX_SCRIPTCHECK_THREADS{15};

/** Current sync state passed to tip changed callbacks. */
enum class SynchronizationState {
INIT_REINDEX,
Expand Down

0 comments on commit ff873a2

Please sign in to comment.