Expand description
Functions and structs related to process information
The primary source of data for functions in this module is the files in a /proc/<pid>/
directory. If you have a process ID, you can use
Process::new(pid)
, otherwise you can get a
list of all running processes using all_processes()
.
In case you have procfs filesystem mounted to a location other than /proc
,
use Process::new_with_root()
.
Examples
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. You can run this example yourself with:
cargo run –example=ps
let me = procfs::process::Process::myself().unwrap();
let me_stat = me.stat().unwrap();
let tps = procfs::ticks_per_second().unwrap();
println!("{: >10} {: <8} {: >8} {}", "PID", "TTY", "TIME", "CMD");
let tty = format!("pty/{}", me_stat.tty_nr().1);
for prc in procfs::process::all_processes().unwrap() {
if let Ok(stat) = prc.unwrap().stat() {
if stat.tty_nr == me_stat.tty_nr {
// total_time is in seconds
let total_time =
(stat.utime + stat.stime) as f32 / (tps as f32);
println!(
"{: >10} {: <8} {: >8} {}",
stat.pid, tty, total_time, stat.comm
);
}
}
}
Here’s a simple example of how you could get the total memory used by the current process.
There are several ways to do this. For a longer example, see the examples/self_memory.rs
file in the git repository. You can run this example with:
cargo run –example=self_memory
let me = Process::myself().unwrap();
let me_stat = me.stat().unwrap();
let page_size = procfs::page_size().unwrap() as u64;
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);
Structs
Process::fd
, iterates over all fds in a process/proc/<pid>/io
/proc/<pid>/maps
file.MountStat
entries./proc/<pid>/mountstats
./proc/<pid>/mountstats
under the section bytes
./proc/<pid>/mountstats
under the section events
./proc/<pid>/mountstats
under the section of per-op statistics
./proc/<pid>/pagemap
./proc/<pid>
./proc/<pid>/schedstat
file./proc/<pid>/stat
file./proc/<pid>/status
file.Process::tasks
, iterates over all tasks in a processEnums
/proc/<pid>/pagemap
.Functions
/proc
path