solana_rayon_threadlimit/lib.rs
1#[macro_use]
2extern crate lazy_static;
3
4use std::env;
5//TODO remove this hack when rayon fixes itself
6
7lazy_static! {
8 // reduce the number of threads each pool is allowed to half the cpu core count, to avoid rayon
9 // hogging cpu
10 static ref MAX_RAYON_THREADS: usize =
11 env::var("SOLANA_RAYON_THREADS").ok()
12 .and_then(|num_threads| num_threads.parse().ok())
13 .unwrap_or_else(|| num_cpus::get() / 2)
14 .max(1);
15}
16
17pub fn get_thread_count() -> usize {
18 *MAX_RAYON_THREADS
19}
20
21// Only used in legacy code.
22// Use get_thread_count instead in all new code.
23pub fn get_max_thread_count() -> usize {
24 get_thread_count().saturating_mul(2)
25}