gander_core/linux/mod.rs
1//! Linux `/proc` and `/sys` access.
2//!
3//! Split into two layers:
4//! * `parse` — pure functions over `&[u8]`, unit-testable against fixtures
5//! * `scan` — the IO layer, using thread-local scratch buffers
6//!
7//! Both are configured by a `root` path so tests can point at a fixture tree
8//! instead of the live `/proc`.
9
10pub mod delayacct;
11pub mod fsops;
12pub mod parse;
13pub mod scan;
14pub mod system;
15
16pub struct LinuxPlatform;
17
18impl LinuxPlatform {
19 /// Clock ticks per second (`_SC_CLK_TCK`). Never hardcode 100.
20 pub fn clk_tck() -> u64 {
21 // SAFETY: sysconf is thread-safe and has no preconditions.
22 let v = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
23 if v > 0 {
24 v as u64
25 } else {
26 100
27 }
28 }
29
30 /// Memory page size (`_SC_PAGESIZE`).
31 pub fn page_size() -> u64 {
32 // SAFETY: as above.
33 let v = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
34 if v > 0 {
35 v as u64
36 } else {
37 4096
38 }
39 }
40}
41
42/// Number of online CPUs, used to normalize CPU%.
43pub fn cpu_count() -> usize {
44 // SAFETY: `sysconf(_SC_NPROCESSORS_ONLN)` has no preconditions.
45 let v = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) };
46 if v > 0 {
47 v as usize
48 } else {
49 1
50 }
51}