solana_rayon_threadlimit/
lib.rs

1#![cfg_attr(
2    not(feature = "agave-unstable-api"),
3    deprecated(
4        since = "3.1.0",
5        note = "This crate has been marked for formal inclusion in the Agave Unstable API. From \
6                v4.0.0 onward, the `agave-unstable-api` crate feature must be specified to \
7                acknowledge use of an interface that may break without warning."
8    )
9)]
10use {log::warn, std::env};
11//TODO remove this hack when rayon fixes itself
12
13// reduce the number of threads each pool is allowed to half the cpu core count, to avoid rayon
14// hogging cpu
15static MAX_RAYON_THREADS: std::sync::LazyLock<usize> = std::sync::LazyLock::new(|| {
16    env::var("SOLANA_RAYON_THREADS")
17        .ok()
18        .and_then(|num_threads| {
19            warn!(
20                "Use of SOLANA_RAYON_THREADS has been deprecated and will be removed soon. Use \
21                 the individual agave-validator CLI flags to configure threadpool sizes"
22            );
23            num_threads.parse().ok()
24        })
25        .unwrap_or_else(|| num_cpus::get() / 2)
26        .max(1)
27});
28
29pub fn get_thread_count() -> usize {
30    *MAX_RAYON_THREADS
31}
32
33#[deprecated(
34    since = "3.0.0",
35    note = "The solana-rayon-threadlimit crate will be removed, use num_cpus::get() or something \
36            similar instead"
37)]
38pub fn get_max_thread_count() -> usize {
39    #[allow(deprecated)]
40    get_thread_count().saturating_mul(2)
41}