Skip to content

Commit

Permalink
Add hidden CLI arg to control number of AccountsDb hash threads
Browse files Browse the repository at this point in the history
  • Loading branch information
steviez committed Oct 24, 2024
1 parent 204fe9d commit 4f2e788
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
22 changes: 14 additions & 8 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ pub const ACCOUNTS_DB_CONFIG_FOR_TESTING: AccountsDbConfig = AccountsDbConfig {
scan_filter_for_shrinking: ScanFilter::OnlyAbnormalWithVerify,
enable_experimental_accumulator_hash: false,
num_clean_threads: None,
num_hash_threads: None,
};
pub const ACCOUNTS_DB_CONFIG_FOR_BENCHMARKS: AccountsDbConfig = AccountsDbConfig {
index: Some(ACCOUNTS_INDEX_CONFIG_FOR_BENCHMARKS),
Expand All @@ -529,6 +530,7 @@ pub const ACCOUNTS_DB_CONFIG_FOR_BENCHMARKS: AccountsDbConfig = AccountsDbConfig
scan_filter_for_shrinking: ScanFilter::OnlyAbnormalWithVerify,
enable_experimental_accumulator_hash: false,
num_clean_threads: None,
num_hash_threads: None,
};

pub type BinnedHashData = Vec<Vec<CalculateHashIntermediate>>;
Expand Down Expand Up @@ -641,6 +643,7 @@ pub struct AccountsDbConfig {
pub scan_filter_for_shrinking: ScanFilter,
pub enable_experimental_accumulator_hash: bool,
pub num_clean_threads: Option<NonZeroUsize>,
pub num_hash_threads: Option<NonZeroUsize>,
}

#[cfg(not(test))]
Expand Down Expand Up @@ -1749,14 +1752,9 @@ pub fn make_min_priority_thread_pool() -> ThreadPool {
.unwrap()
}

pub fn make_hash_thread_pool() -> ThreadPool {
pub fn default_num_hash_threads() -> usize {
// 1/8 of the number of cpus and up to 6 threads gives good balance for the system.
let num_threads = (num_cpus::get() / 8).clamp(2, 6);
rayon::ThreadPoolBuilder::new()
.thread_name(|i| format!("solAcctHash{i:02}"))
.num_threads(num_threads)
.build()
.unwrap()
(num_cpus::get() / 8).clamp(2, 6)
}

#[cfg(feature = "frozen-abi")]
Expand Down Expand Up @@ -1906,7 +1904,15 @@ impl AccountsDb {
.build()
.expect("new rayon threadpool");

let thread_pool_hash = make_hash_thread_pool();
let num_hash_threads = accounts_db_config
.num_hash_threads
.map(Into::into)
.unwrap_or_else(default_num_hash_threads);
let thread_pool_hash = rayon::ThreadPoolBuilder::new()
.thread_name(|i| format!("solAcctHash{i:02}"))
.num_threads(num_hash_threads)
.build()
.expect("new rayon threadpool");

let mut new = Self {
accounts_index,
Expand Down
20 changes: 20 additions & 0 deletions validator/src/cli/thread_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
// Need this struct to provide &str whose lifetime matches that of the CLAP Arg's
pub struct DefaultThreadArgs {
pub accounts_db_clean_threads: String,
pub accounts_db_hash_threads: String,
pub ip_echo_server_threads: String,
pub replay_forks_threads: String,
pub replay_transactions_threads: String,
Expand All @@ -22,6 +23,7 @@ impl Default for DefaultThreadArgs {
fn default() -> Self {
Self {
accounts_db_clean_threads: AccountsDbCleanThreadsArg::bounded_default().to_string(),
accounts_db_hash_threads: AccountsDbHashThreadsArg::bounded_default().to_string(),
ip_echo_server_threads: IpEchoServerThreadsArg::bounded_default().to_string(),
replay_forks_threads: ReplayForksThreadsArg::bounded_default().to_string(),
replay_transactions_threads: ReplayTransactionsThreadsArg::bounded_default()
Expand All @@ -35,6 +37,7 @@ impl Default for DefaultThreadArgs {
pub fn thread_args<'a>(defaults: &DefaultThreadArgs) -> Vec<Arg<'_, 'a>> {
vec![
new_thread_arg::<AccountsDbCleanThreadsArg>(&defaults.accounts_db_clean_threads),
new_thread_arg::<AccountsDbHashThreadsArg>(&defaults.accounts_db_hash_threads),
new_thread_arg::<IpEchoServerThreadsArg>(&defaults.ip_echo_server_threads),
new_thread_arg::<ReplayForksThreadsArg>(&defaults.replay_forks_threads),
new_thread_arg::<ReplayTransactionsThreadsArg>(&defaults.replay_transactions_threads),
Expand All @@ -56,6 +59,7 @@ fn new_thread_arg<'a, T: ThreadArg>(default: &str) -> Arg<'_, 'a> {

pub struct NumThreadConfig {
pub accounts_db_clean_threads: NonZeroUsize,
pub accounts_db_hash_threads: NonZeroUsize,
pub ip_echo_server_threads: NonZeroUsize,
pub replay_forks_threads: NonZeroUsize,
pub replay_transactions_threads: NonZeroUsize,
Expand All @@ -70,6 +74,11 @@ pub fn parse_num_threads_args(matches: &ArgMatches) -> NumThreadConfig {
AccountsDbCleanThreadsArg::NAME,
NonZeroUsize
),
accounts_db_hash_threads: value_t_or_exit!(
matches,
AccountsDbHashThreadsArg::NAME,
NonZeroUsize
),
ip_echo_server_threads: value_t_or_exit!(
matches,
IpEchoServerThreadsArg::NAME,
Expand Down Expand Up @@ -137,6 +146,17 @@ impl ThreadArg for AccountsDbCleanThreadsArg {
}
}

struct AccountsDbHashThreadsArg;
impl ThreadArg for AccountsDbHashThreadsArg {
const NAME: &'static str = "accounts_db_hash_threads";
const LONG_NAME: &'static str = "accounts-db-hash-threads";
const HELP: &'static str = "Number of threads to use for background AccountsDb hashing";

fn default() -> usize {
accounts_db::default_num_hash_threads()
}
}

struct IpEchoServerThreadsArg;
impl ThreadArg for IpEchoServerThreadsArg {
const NAME: &'static str = "ip_echo_server_threads";
Expand Down
2 changes: 2 additions & 0 deletions validator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,7 @@ pub fn main() {

let cli::thread_args::NumThreadConfig {
accounts_db_clean_threads,
accounts_db_hash_threads,
ip_echo_server_threads,
replay_forks_threads,
replay_transactions_threads,
Expand Down Expand Up @@ -1313,6 +1314,7 @@ pub fn main() {
enable_experimental_accumulator_hash: matches
.is_present("accounts_db_experimental_accumulator_hash"),
num_clean_threads: Some(accounts_db_clean_threads),
num_hash_threads: Some(accounts_db_hash_threads),
..AccountsDbConfig::default()
};

Expand Down

0 comments on commit 4f2e788

Please sign in to comment.