#procfs #proc #linux #process

procfs

Interface to the linux procfs pseudo-filesystem

37 releases

0.12.0 Dec 10, 2021
0.11.1 Nov 26, 2021
0.11.0 Oct 14, 2021
0.9.1 Dec 5, 2020
0.3.0 Jul 29, 2018

#6 in Unix APIs

Download history 84673/week @ 2022-01-24 62663/week @ 2022-01-31 45070/week @ 2022-02-07 49153/week @ 2022-02-14 37320/week @ 2022-02-21 41102/week @ 2022-02-28 35468/week @ 2022-03-07 36255/week @ 2022-03-14 51944/week @ 2022-03-21 55282/week @ 2022-03-28 50871/week @ 2022-04-04 49351/week @ 2022-04-11 47451/week @ 2022-04-18 52609/week @ 2022-04-25 45860/week @ 2022-05-02 49435/week @ 2022-05-09

197,614 downloads per month
Used in 77 crates (42 directly)

MIT/Apache

335KB
6K SLoC

procfs

Crate Docs Minimum rustc version

This crate is an interface to the proc pseudo-filesystem on linux, which is normally mounted as /proc. Long-term, this crate aims to be fairly feature complete, but at the moment not all files are exposed. See the docs for info on what's supported, or view the support.md file in the code repository.

Examples

There are several examples in the docs and in the examples folder of the code repository.

Here's a small example that prints out all processes that are running on the same tty as the calling process. This is very similar to what "ps" does in its default mode:

fn main() {
    let me = procfs::process::Process::myself().unwrap();
    let tps = procfs::ticks_per_second().unwrap();

    println!("{: >5} {: <8} {: >8} {}", "PID", "TTY", "TIME", "CMD");

    let tty = format!("pty/{}", me.stat.tty_nr().1);
    for prc in procfs::process::all_processes().unwrap() {
        if prc.stat.tty_nr == me.stat.tty_nr {
            // total_time is in seconds
            let total_time =
                (prc.stat.utime + prc.stat.stime) as f32 / (tps as f32);
            println!(
                "{: >5} {: <8} {: >8} {}",
                prc.stat.pid, tty, total_time, prc.stat.comm
            );
        }
    }
}

Here's another example that shows how to get the current memory usage of the current process:

use procfs::process::Process;

fn main() {
    let me = Process::myself().unwrap();
    println!("PID: {}", me.pid);

    let page_size = procfs::page_size().unwrap() as u64;
    println!("Memory page size: {}", page_size);

    println!("== Data from /proc/self/stat:");
    println!("Total virtual memory used: {} bytes", me.stat.vsize);
    println!("Total resident set: {} pages ({} bytes)", me.stat.rss, me.stat.rss as u64 * page_size);
}

There are a few ways to get this data, so also checkout the longer self_memory example for more details.

Cargo features

The following cargo features are available:

  • chrono -- Default. Optional. This feature enables a few methods that return values as DateTime objects.
  • flate2 -- Default. Optional. This feature enables parsing gzip compressed /proc/config.gz file via the procfs::kernel_config method.
  • backtrace -- Optional. This feature lets you get a stack trace whenever an InternalError is raised.

Minimum Rust Version

This crate requires a minimum rust version of 1.34.0 (2019-04-11), though if you use the optional backtrace feature, rust 1.42.0 is required (2020-03-12). However, three dependencies of this crate (hex, bitflags, and flate2) require a newer version of rust, and must be manually pinned to an older version in order to use rust 1.34. You can do this by running:

cargo update -p hex --precise 0.4.2
cargo update -p bitflags --precise 1.2.1
cargo update -p flate2 --precise 1.0.21

License

The procfs library is licensed under either of

at your option.

For additional copyright information regarding documentation, please also see the COPYRIGHT.txt file.

Contribution

Contributions are welcome, especially in the areas of documentation and testing on older kernels.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1–1.9MB
~32K SLoC