num_threads/
linux.rs

1use std::fs;
2use std::num::NonZeroUsize;
3
4pub(crate) fn num_threads() -> Option<NonZeroUsize> {
5    fs::read_to_string("/proc/self/stat")
6        .ok()
7        .as_ref()
8        // Skip past the pid and (process name) fields
9        .and_then(|stat| stat.rsplit(')').next())
10        // 20th field, less the two we skipped
11        .and_then(|rstat| rstat.split_whitespace().nth(17))
12        .and_then(|num_threads| num_threads.parse::<usize>().ok())
13        .and_then(NonZeroUsize::new)
14}