Skip to content

Commit

Permalink
Use /proc/self/stat on Linux (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Freaky authored Nov 16, 2021
1 parent e8c1037 commit 5b8d144
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use std::fs;
use std::num::NonZeroUsize;

pub(crate) fn num_threads() -> Option<NonZeroUsize> {
fs::read_dir("/proc/self/task")
// If we can't read the directory, return `None`.
fs::read_to_string("/proc/self/stat")
.ok()
// The number of files in the directory is the number of threads.
.and_then(|tasks| NonZeroUsize::new(tasks.count()))
.as_ref()
// Skip past the pid and (process name) fields
.and_then(|stat| stat.rsplit(')').next())
// 20th field, less the two we skipped
.and_then(|rstat| rstat.split_whitespace().nth(17))
.and_then(|num_threads| num_threads.parse::<usize>().ok())
.and_then(NonZeroUsize::new)
}

0 comments on commit 5b8d144

Please sign in to comment.