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();
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() {
let Ok(prc) = prc else {
// process vanished
continue;
};
let Ok(stat) = prc.stat() else {
// process vanished
continue;
};
if let Ok(stat) = prc.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();
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§
- Coredump
Flags - See the coredump_filter() method.
- FDInfo
- See the Process::fd() method
- FDPermissions
- The mode (read/write permissions) for an open file descriptor
- FDsIter
- The result of
Process::fd
, iterates over all fds in a process - Io
- This struct contains I/O statistics for the process, built from
/proc/<pid>/io
- Limit
- Limits
- Process limits
- MMPermissions
- The permissions a process has on memory map entries.
- MMap
Extension - Represents the information about a specific mapping as presented in /proc/<pid>/smaps
- Memory
Map - Represents an entry in a
/proc/<pid>/maps
or/proc/<pid>/smaps
file. - Memory
Maps - Represents all entries in a
/proc/<pid>/maps
or/proc/<pid>/smaps
file. - Memory
Page Flags - Represents the fields and flags in a page table entry for a memory page.
- Mount
Info - Information about a specific mount in a process’s mount namespace.
- Mount
Infos - Information about a all mounts in a process’s mount namespace.
- MountNFS
Statistics - Only NFS mounts provide additional statistics in
MountStat
entries. - Mount
Stat - A single entry in MountStats.
- Mount
Stats - Mount information from
/proc/<pid>/mountstats
. - NFSByte
Counter - Represents NFS data from
/proc/<pid>/mountstats
under the sectionbytes
. - NFSEvent
Counter - Represents NFS data from
/proc/<pid>/mountstats
under the sectionevents
. - NFSOperation
Stat - Represents NFS data from
/proc/<pid>/mountstats
under the section ofper-op statistics
. - NFSServer
Caps - Namespace
- Information about a namespace
- Namespaces
- All namespaces of a process.
- PageMap
- Parses page table entries accessing
/proc/<pid>/pagemap
. - Pfn
- A Page Frame Number, representing a 4 kiB physical memory page
- Process
- Represents a process in
/proc/<pid>
. - Processes
Iter - An iterator over all processes in the system.
- Schedstat
- Provides scheduler statistics of the process, based on the
/proc/<pid>/schedstat
file. - Smaps
Rollup - Stat
- Status information about the process, based on the
/proc/<pid>/stat
file. - Stat
Flags - Kernel flags for a process
- StatM
- Provides information about memory usage, measured in pages.
- Status
- Status information about the process, based on the
/proc/<pid>/status
file. - Swap
Page Flags - Represents the fields and flags in a page table entry for a swapped page.
- Task
- A task (aka Thread) inside of a
Process
- Tasks
Iter - The result of
Process::tasks
, iterates over all tasks in a process - VmFlags
- Represents the kernel flags associated with the virtual memory area. The names of these flags are just those you’ll find in the man page, but in upper case.
Enums§
- Clear
Refs - Clearing the PG_Referenced and ACCESSED/YOUNG bits
provides a method to measure approximately how much memory
a process is using. One first inspects the values in the
“Referenced” fields for the VMAs shown in
/proc/[pid]/smaps
to get an idea of the memory footprint of the process. One then clears the PG_Referenced and ACCESSED/YOUNG bits and, after some measured time interval, once again inspects the values in the “Referenced” fields to get an idea of the change in memory footprint of the process during the measured interval. If one is interested only in inspecting the selected mapping types, then the value 2 or 3 can be used instead of 1. - FDTarget
- Describes a file descriptor opened by a process.
- Limit
Value - MMap
Path - Mount
OptFields - Optional fields used in MountInfo
- Page
Info - Represents a page table entry in
/proc/<pid>/pagemap
. - Proc
State - Represents the state of a process.
Functions§
- all_
processes - Return a iterator of all processes
- all_
processes_ with_ root - Return a list of all processes based on a specified
/proc
path