From aac0b00f533854d0396fd8eb9012d69bf994f2f8 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Thu, 21 Mar 2024 15:32:33 +0000 Subject: [PATCH] fix: configure global rayon pool to `available_parallelism() - 2` (#7267) --- Cargo.lock | 1 + crates/node-builder/Cargo.toml | 1 + crates/node-builder/src/builder.rs | 13 +++++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8f15659044..5650c585b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6086,6 +6086,7 @@ dependencies = [ "eyre", "fdlimit", "futures", + "rayon", "reth-auto-seal-consensus", "reth-beacon-consensus", "reth-blockchain-tree", diff --git a/crates/node-builder/Cargo.toml b/crates/node-builder/Cargo.toml index 45ce9331ba..8e389cc6ed 100644 --- a/crates/node-builder/Cargo.toml +++ b/crates/node-builder/Cargo.toml @@ -49,3 +49,4 @@ tokio = { workspace = true, features = [ eyre.workspace = true fdlimit = "0.3.0" confy.workspace = true +rayon.workspace = true diff --git a/crates/node-builder/src/builder.rs b/crates/node-builder/src/builder.rs index 32e6740523..03d99a590f 100644 --- a/crates/node-builder/src/builder.rs +++ b/crates/node-builder/src/builder.rs @@ -14,6 +14,7 @@ use crate::{ }; use eyre::Context; use futures::{future::Either, stream, stream_select, StreamExt}; +use rayon::ThreadPoolBuilder; use reth_beacon_consensus::{ hooks::{EngineHooks, PruneHook, StaticFileHook}, BeaconConsensusEngine, @@ -50,9 +51,9 @@ use reth_revm::EvmProcessorFactory; use reth_rpc_engine_api::EngineApi; use reth_static_file::StaticFileProducer; use reth_tasks::TaskExecutor; -use reth_tracing::tracing::{debug, info}; +use reth_tracing::tracing::{debug, error, info}; use reth_transaction_pool::{PoolConfig, TransactionPool}; -use std::{str::FromStr, sync::Arc}; +use std::{cmp::max, str::FromStr, sync::Arc, thread::available_parallelism}; use tokio::sync::{mpsc::unbounded_channel, oneshot}; /// The builtin provider type of the reth node. @@ -441,6 +442,14 @@ where // Does not do anything on windows. fdlimit::raise_fd_limit()?; + // Limit the global rayon thread pool, reserving 2 cores for the rest of the system + let _ = ThreadPoolBuilder::new() + .num_threads( + available_parallelism().map_or(25, |cpus| max(cpus.get().saturating_sub(2), 2)), + ) + .build_global() + .map_err(|e| error!("Failed to build global thread pool: {:?}", e)); + let provider_factory = ProviderFactory::new( database.clone(), Arc::clone(&config.chain),