Skip to main content

agave_validator/cli/
thread_args.rs

1//! Arguments for controlling the number of threads allocated for various tasks
2
3use {
4    clap::{Arg, ArgMatches, value_t_or_exit},
5    solana_accounts_db::{accounts_db, accounts_index},
6    solana_clap_utils::{hidden_unless_forced, input_validators::is_within_range},
7    solana_core::banking_stage::BankingStage,
8    solana_rayon_threadlimit::get_thread_count,
9    std::{num::NonZeroUsize, ops::RangeInclusive},
10};
11
12// Need this struct to provide &str whose lifetime matches that of the CLAP Arg's
13pub struct DefaultThreadArgs {
14    pub accounts_db_background_threads: String,
15    pub accounts_db_foreground_threads: String,
16    pub accounts_index_flush_threads: String,
17    pub block_production_num_workers: String,
18    pub ip_echo_server_threads: String,
19    pub rayon_global_threads: String,
20    pub replay_forks_threads: String,
21    pub replay_transactions_threads: String,
22    pub tpu_sigverify_threads: String,
23    pub tpu_transaction_forward_receive_threads: String,
24    pub tpu_transaction_receive_threads: String,
25    pub tpu_vote_transaction_receive_threads: String,
26    pub tvu_receive_threads: String,
27    pub tvu_retransmit_threads: String,
28    pub tvu_sigverify_threads: String,
29}
30
31impl Default for DefaultThreadArgs {
32    fn default() -> Self {
33        Self {
34            accounts_db_background_threads: AccountsDbBackgroundThreadsArg::bounded_default()
35                .to_string(),
36            accounts_db_foreground_threads: AccountsDbForegroundThreadsArg::bounded_default()
37                .to_string(),
38            accounts_index_flush_threads: AccountsIndexFlushThreadsArg::bounded_default()
39                .to_string(),
40            block_production_num_workers: BankingStage::default_num_workers().to_string(),
41            ip_echo_server_threads: IpEchoServerThreadsArg::bounded_default().to_string(),
42            rayon_global_threads: RayonGlobalThreadsArg::bounded_default().to_string(),
43            replay_forks_threads: ReplayForksThreadsArg::bounded_default().to_string(),
44            replay_transactions_threads: ReplayTransactionsThreadsArg::bounded_default()
45                .to_string(),
46            tpu_sigverify_threads: TpuSigverifyThreadsArg::bounded_default().to_string(),
47            tpu_transaction_forward_receive_threads:
48                TpuTransactionForwardReceiveThreadArgs::bounded_default().to_string(),
49            tpu_transaction_receive_threads: TpuTransactionReceiveThreads::bounded_default()
50                .to_string(),
51            tpu_vote_transaction_receive_threads:
52                TpuVoteTransactionReceiveThreads::bounded_default().to_string(),
53            tvu_receive_threads: TvuReceiveThreadsArg::bounded_default().to_string(),
54            tvu_retransmit_threads: TvuRetransmitThreadsArg::bounded_default().to_string(),
55            tvu_sigverify_threads: TvuShredSigverifyThreadsArg::bounded_default().to_string(),
56        }
57    }
58}
59
60pub fn thread_args<'a>(defaults: &DefaultThreadArgs) -> Vec<Arg<'_, 'a>> {
61    vec![
62        new_thread_arg::<AccountsDbBackgroundThreadsArg>(&defaults.accounts_db_background_threads),
63        new_thread_arg::<AccountsDbForegroundThreadsArg>(&defaults.accounts_db_foreground_threads),
64        new_thread_arg::<AccountsIndexFlushThreadsArg>(&defaults.accounts_index_flush_threads),
65        new_thread_arg::<BlockProductionNumWorkersArg>(&defaults.block_production_num_workers),
66        new_thread_arg::<IpEchoServerThreadsArg>(&defaults.ip_echo_server_threads),
67        new_thread_arg::<RayonGlobalThreadsArg>(&defaults.rayon_global_threads),
68        new_thread_arg::<ReplayForksThreadsArg>(&defaults.replay_forks_threads),
69        new_thread_arg::<ReplayTransactionsThreadsArg>(&defaults.replay_transactions_threads),
70        new_thread_arg::<TpuSigverifyThreadsArg>(&defaults.tpu_sigverify_threads),
71        new_thread_arg::<TpuTransactionForwardReceiveThreadArgs>(
72            &defaults.tpu_transaction_forward_receive_threads,
73        ),
74        new_thread_arg::<TpuTransactionReceiveThreads>(&defaults.tpu_transaction_receive_threads),
75        new_thread_arg::<TpuVoteTransactionReceiveThreads>(
76            &defaults.tpu_vote_transaction_receive_threads,
77        ),
78        new_thread_arg::<TvuReceiveThreadsArg>(&defaults.tvu_receive_threads),
79        new_thread_arg::<TvuRetransmitThreadsArg>(&defaults.tvu_retransmit_threads),
80        new_thread_arg::<TvuShredSigverifyThreadsArg>(&defaults.tvu_sigverify_threads),
81    ]
82}
83
84pub(crate) fn new_thread_arg<'a, T: ThreadArg>(default: &str) -> Arg<'_, 'a> {
85    Arg::with_name(T::NAME)
86        .long(T::LONG_NAME)
87        .takes_value(true)
88        .value_name("NUMBER")
89        .default_value(default)
90        .validator(|num| is_within_range(num, T::range()))
91        .hidden(hidden_unless_forced())
92        .help(T::HELP)
93}
94
95pub struct NumThreadConfig {
96    pub accounts_db_background_threads: NonZeroUsize,
97    pub accounts_db_foreground_threads: NonZeroUsize,
98    pub accounts_index_flush_threads: NonZeroUsize,
99    pub block_production_num_workers: NonZeroUsize,
100    pub ip_echo_server_threads: NonZeroUsize,
101    pub rayon_global_threads: NonZeroUsize,
102    pub replay_forks_threads: NonZeroUsize,
103    pub replay_transactions_threads: NonZeroUsize,
104    pub tpu_sigverify_threads: NonZeroUsize,
105    pub tpu_transaction_forward_receive_threads: NonZeroUsize,
106    pub tpu_transaction_receive_threads: NonZeroUsize,
107    pub tpu_vote_transaction_receive_threads: NonZeroUsize,
108    pub tvu_receive_threads: NonZeroUsize,
109    pub tvu_retransmit_threads: NonZeroUsize,
110    pub tvu_sigverify_threads: NonZeroUsize,
111}
112
113pub fn parse_num_threads_args(matches: &ArgMatches) -> NumThreadConfig {
114    NumThreadConfig {
115        accounts_db_background_threads: value_t_or_exit!(
116            matches,
117            AccountsDbBackgroundThreadsArg::NAME,
118            NonZeroUsize
119        ),
120        accounts_db_foreground_threads: value_t_or_exit!(
121            matches,
122            AccountsDbForegroundThreadsArg::NAME,
123            NonZeroUsize
124        ),
125        accounts_index_flush_threads: value_t_or_exit!(
126            matches,
127            AccountsIndexFlushThreadsArg::NAME,
128            NonZeroUsize
129        ),
130        block_production_num_workers: value_t_or_exit!(
131            matches,
132            BlockProductionNumWorkersArg::NAME,
133            NonZeroUsize
134        ),
135        ip_echo_server_threads: value_t_or_exit!(
136            matches,
137            IpEchoServerThreadsArg::NAME,
138            NonZeroUsize
139        ),
140        rayon_global_threads: value_t_or_exit!(matches, RayonGlobalThreadsArg::NAME, NonZeroUsize),
141        replay_forks_threads: value_t_or_exit!(matches, ReplayForksThreadsArg::NAME, NonZeroUsize),
142        replay_transactions_threads: value_t_or_exit!(
143            matches,
144            ReplayTransactionsThreadsArg::NAME,
145            NonZeroUsize
146        ),
147        tpu_sigverify_threads: value_t_or_exit!(
148            matches,
149            TpuSigverifyThreadsArg::NAME,
150            NonZeroUsize
151        ),
152        tpu_transaction_forward_receive_threads: value_t_or_exit!(
153            matches,
154            TpuTransactionForwardReceiveThreadArgs::NAME,
155            NonZeroUsize
156        ),
157        tpu_transaction_receive_threads: value_t_or_exit!(
158            matches,
159            TpuTransactionReceiveThreads::NAME,
160            NonZeroUsize
161        ),
162        tpu_vote_transaction_receive_threads: value_t_or_exit!(
163            matches,
164            TpuVoteTransactionReceiveThreads::NAME,
165            NonZeroUsize
166        ),
167        tvu_receive_threads: value_t_or_exit!(matches, TvuReceiveThreadsArg::NAME, NonZeroUsize),
168        tvu_retransmit_threads: value_t_or_exit!(
169            matches,
170            TvuRetransmitThreadsArg::NAME,
171            NonZeroUsize
172        ),
173        tvu_sigverify_threads: value_t_or_exit!(
174            matches,
175            TvuShredSigverifyThreadsArg::NAME,
176            NonZeroUsize
177        ),
178    }
179}
180
181/// Configuration for CLAP arguments that control the number of threads for various functions
182pub trait ThreadArg {
183    /// The argument's name
184    const NAME: &'static str;
185    /// The argument's long name
186    const LONG_NAME: &'static str;
187    /// The argument's help message
188    const HELP: &'static str;
189
190    /// The default number of threads
191    fn default() -> usize;
192    /// The default number of threads, bounded by Self::max()
193    /// This prevents potential CLAP issues on low core count machines where
194    /// a fixed value in Self::default() could be greater than Self::max()
195    fn bounded_default() -> usize {
196        std::cmp::min(Self::default(), Self::max())
197    }
198    /// The minimum allowed number of threads (inclusive)
199    fn min() -> usize {
200        1
201    }
202    /// The maximum allowed number of threads (inclusive)
203    fn max() -> usize {
204        // By default, no thread pool should scale over the number of the machine's threads
205        num_cpus::get()
206    }
207    /// The range of allowed number of threads (inclusive on both ends)
208    fn range() -> RangeInclusive<usize> {
209        RangeInclusive::new(Self::min(), Self::max())
210    }
211}
212
213struct AccountsDbBackgroundThreadsArg;
214impl ThreadArg for AccountsDbBackgroundThreadsArg {
215    const NAME: &'static str = "accounts_db_background_threads";
216    const LONG_NAME: &'static str = "accounts-db-background-threads";
217    const HELP: &'static str = "Number of threads to use for AccountsDb background tasks";
218
219    fn default() -> usize {
220        accounts_db::quarter_thread_count()
221    }
222}
223
224struct AccountsDbForegroundThreadsArg;
225impl ThreadArg for AccountsDbForegroundThreadsArg {
226    const NAME: &'static str = "accounts_db_foreground_threads";
227    const LONG_NAME: &'static str = "accounts-db-foreground-threads";
228    const HELP: &'static str =
229        "Number of threads to use for AccountsDb foreground tasks, e.g. transaction processing";
230
231    fn default() -> usize {
232        accounts_db::default_num_foreground_threads()
233    }
234}
235
236struct AccountsIndexFlushThreadsArg;
237impl ThreadArg for AccountsIndexFlushThreadsArg {
238    const NAME: &'static str = "accounts_index_flush_threads";
239    const LONG_NAME: &'static str = "accounts-index-flush-threads";
240    const HELP: &'static str = "Number of threads to use for flushing the accounts index";
241
242    fn default() -> usize {
243        accounts_index::default_num_flush_threads().get()
244    }
245}
246
247struct BlockProductionNumWorkersArg;
248impl ThreadArg for BlockProductionNumWorkersArg {
249    const NAME: &'static str = "block_production_num_workers";
250    const LONG_NAME: &'static str = "block-production-num-workers";
251    const HELP: &'static str = "Number of worker threads to use for block production";
252
253    fn default() -> usize {
254        BankingStage::default_num_workers().get()
255    }
256
257    fn min() -> usize {
258        1
259    }
260
261    fn max() -> usize {
262        BankingStage::max_num_workers().get()
263    }
264}
265
266struct IpEchoServerThreadsArg;
267impl ThreadArg for IpEchoServerThreadsArg {
268    const NAME: &'static str = "ip_echo_server_threads";
269    const LONG_NAME: &'static str = "ip-echo-server-threads";
270    const HELP: &'static str = "Number of threads to use for the IP echo server";
271
272    fn default() -> usize {
273        solana_net_utils::DEFAULT_IP_ECHO_SERVER_THREADS.get()
274    }
275}
276
277struct RayonGlobalThreadsArg;
278impl ThreadArg for RayonGlobalThreadsArg {
279    const NAME: &'static str = "rayon_global_threads";
280    const LONG_NAME: &'static str = "rayon-global-threads";
281    const HELP: &'static str = "Number of threads to use for the global rayon thread pool";
282
283    fn default() -> usize {
284        num_cpus::get()
285    }
286}
287
288struct ReplayForksThreadsArg;
289impl ThreadArg for ReplayForksThreadsArg {
290    const NAME: &'static str = "replay_forks_threads";
291    const LONG_NAME: &'static str = "replay-forks-threads";
292    const HELP: &'static str = "Number of threads to use for replay of blocks on different forks";
293
294    fn default() -> usize {
295        // Default to single threaded fork execution
296        1
297    }
298    fn max() -> usize {
299        // Choose a value that is small enough to limit the overhead of having a large thread pool
300        // while also being large enough to allow replay of all active forks in most scenarios
301        4
302    }
303}
304
305struct ReplayTransactionsThreadsArg;
306impl ThreadArg for ReplayTransactionsThreadsArg {
307    const NAME: &'static str = "replay_transactions_threads";
308    const LONG_NAME: &'static str = "replay-transactions-threads";
309    const HELP: &'static str = "Number of threads to use for transaction replay";
310
311    fn default() -> usize {
312        num_cpus::get()
313    }
314}
315
316struct TpuSigverifyThreadsArg;
317impl ThreadArg for TpuSigverifyThreadsArg {
318    const NAME: &'static str = "tpu_sigverify_threads";
319    const LONG_NAME: &'static str = "tpu-sigverify-threads";
320    const HELP: &'static str =
321        "Number of threads to use for performing signature verification of received transactions";
322
323    fn default() -> usize {
324        get_thread_count()
325    }
326}
327
328struct TpuTransactionForwardReceiveThreadArgs;
329impl ThreadArg for TpuTransactionForwardReceiveThreadArgs {
330    const NAME: &'static str = "tpu_transaction_forward_receive_threads";
331    const LONG_NAME: &'static str = "tpu-transaction-forward-receive-threads";
332    const HELP: &'static str =
333        "Number of threads to use for receiving transactions on the TPU forwards port";
334
335    fn default() -> usize {
336        solana_streamer::quic::default_num_tpu_transaction_forward_receive_threads()
337    }
338}
339
340struct TpuTransactionReceiveThreads;
341impl ThreadArg for TpuTransactionReceiveThreads {
342    const NAME: &'static str = "tpu_transaction_receive_threads";
343    const LONG_NAME: &'static str = "tpu-transaction-receive-threads";
344    const HELP: &'static str =
345        "Number of threads to use for receiving transactions on the TPU port";
346
347    fn default() -> usize {
348        solana_streamer::quic::default_num_tpu_transaction_receive_threads()
349    }
350}
351
352struct TpuVoteTransactionReceiveThreads;
353impl ThreadArg for TpuVoteTransactionReceiveThreads {
354    const NAME: &'static str = "tpu_vote_transaction_receive_threads";
355    const LONG_NAME: &'static str = "tpu-vote-transaction-receive-threads";
356    const HELP: &'static str =
357        "Number of threads to use for receiving transactions on the TPU vote port";
358
359    fn default() -> usize {
360        solana_streamer::quic::default_num_tpu_vote_transaction_receive_threads()
361    }
362}
363
364struct TvuReceiveThreadsArg;
365impl ThreadArg for TvuReceiveThreadsArg {
366    const NAME: &'static str = "tvu_receive_threads";
367    const LONG_NAME: &'static str = "tvu-receive-threads";
368    const HELP: &'static str =
369        "Number of threads (and sockets) to use for receiving shreds on the TVU port";
370
371    fn default() -> usize {
372        solana_gossip::cluster_info::DEFAULT_NUM_TVU_RECEIVE_SOCKETS.get()
373    }
374    fn min() -> usize {
375        solana_gossip::cluster_info::MINIMUM_NUM_TVU_RECEIVE_SOCKETS.get()
376    }
377}
378
379struct TvuRetransmitThreadsArg;
380impl ThreadArg for TvuRetransmitThreadsArg {
381    const NAME: &'static str = "tvu_retransmit_threads";
382    const LONG_NAME: &'static str = "tvu-retransmit-threads";
383    const HELP: &'static str = "Number of threads (and sockets) to use for retransmitting shreds";
384
385    fn default() -> usize {
386        solana_gossip::cluster_info::DEFAULT_NUM_TVU_RETRANSMIT_SOCKETS.get()
387    }
388
389    fn min() -> usize {
390        solana_gossip::cluster_info::MINIMUM_NUM_TVU_RETRANSMIT_SOCKETS.get()
391    }
392}
393
394struct TvuShredSigverifyThreadsArg;
395impl ThreadArg for TvuShredSigverifyThreadsArg {
396    const NAME: &'static str = "tvu_shred_sigverify_threads";
397    const LONG_NAME: &'static str = "tvu-shred-sigverify-threads";
398    const HELP: &'static str =
399        "Number of threads to use for performing signature verification of received shreds";
400
401    fn default() -> usize {
402        get_thread_count()
403    }
404}