| 1 |
procfs |
| 2 |
====== |
| 3 |
|
| 4 |
[](https://crates.io/crates/procfs) |
| 5 |
[](https://docs.rs/procfs) |
| 6 |
[](https://github.com/eminence/procfs#minimum-rust-version) |
| 7 |
|
| 8 |
|
| 9 |
This crate is an interface to the `proc` pseudo-filesystem on linux, which is normally mounted as `/proc`. |
| 10 |
Long-term, this crate aims to be fairly feature complete, but at the moment not all files are exposed. |
| 11 |
See the docs for info on what's supported, or view the [support.md](https://github.com/eminence/procfs/blob/master/support.md) |
| 12 |
file in the code repository. |
| 13 |
|
| 14 |
## Examples |
| 15 |
There are several examples in the docs and in the [examples folder](https://github.com/eminence/procfs/tree/master/procfs/examples) |
| 16 |
of the code repository. |
| 17 |
|
| 18 |
Here's a small example that prints out all processes that are running on the same tty as the calling |
| 19 |
process. This is very similar to what "ps" does in its default mode: |
| 20 |
|
| 21 |
```rust |
| 22 |
fn main() { |
| 23 |
let me = procfs::process::Process::myself().unwrap(); |
| 24 |
let me_stat = me.stat().unwrap(); |
| 25 |
let tps = procfs::ticks_per_second().unwrap(); |
| 26 |
|
| 27 |
println!("{: >5} {: <8} {: >8} {}", "PID", "TTY", "TIME", "CMD"); |
| 28 |
|
| 29 |
let tty = format!("pty/{}", me_stat.tty_nr().1); |
| 30 |
for prc in procfs::process::all_processes().unwrap() { |
| 31 |
let prc = prc.unwrap(); |
| 32 |
let stat = prc.stat().unwrap(); |
| 33 |
if stat.tty_nr == me_stat.tty_nr { |
| 34 |
// total_time is in seconds |
| 35 |
let total_time = |
| 36 |
(stat.utime + stat.stime) as f32 / (tps as f32); |
| 37 |
println!( |
| 38 |
"{: >5} {: <8} {: >8} {}", |
| 39 |
stat.pid, tty, total_time, stat.comm |
| 40 |
); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
``` |
| 46 |
|
| 47 |
Here's another example that shows how to get the current memory usage of the current process: |
| 48 |
|
| 49 |
```rust |
| 50 |
use procfs::process::Process; |
| 51 |
|
| 52 |
fn main() { |
| 53 |
let me = Process::myself().unwrap(); |
| 54 |
let me_stat = me.stat().unwrap(); |
| 55 |
println!("PID: {}", me.pid); |
| 56 |
|
| 57 |
let page_size = procfs::page_size(); |
| 58 |
println!("Memory page size: {}", page_size); |
| 59 |
|
| 60 |
println!("== Data from /proc/self/stat:"); |
| 61 |
println!("Total virtual memory used: {} bytes", me_stat.vsize); |
| 62 |
println!( |
| 63 |
"Total resident set: {} pages ({} bytes)", |
| 64 |
me_stat.rss, |
| 65 |
me_stat.rss * page_size |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
``` |
| 70 |
|
| 71 |
There are a few ways to get this data, so also checkout the longer |
| 72 |
[self_memory](https://github.com/eminence/procfs/blob/master/procfs/examples/self_memory.rs) example for more |
| 73 |
details. |
| 74 |
|
| 75 |
## Cargo features |
| 76 |
|
| 77 |
The following cargo features are available: |
| 78 |
|
| 79 |
* `chrono` -- Default. Optional. This feature enables a few methods that return values as `DateTime` objects. |
| 80 |
* `flate2` -- Default. Optional. This feature enables parsing gzip compressed `/proc/config.gz` file via the `procfs::kernel_config` method. |
| 81 |
* `backtrace` -- Optional. This feature lets you get a stack trace whenever an `InternalError` is raised. |
| 82 |
* `serde1` -- Optional. This feature allows most structs to be serialized and deserialized using serde 1.0. Note, this |
| 83 |
feature requires a version of rust newer than 1.48.0 (which is the MSRV for procfs). The exact version required is not |
| 84 |
specified here, since serde does not not have an MSRV policy. |
| 85 |
|
| 86 |
## Minimum Rust Version |
| 87 |
|
| 88 |
This crate is only tested against the latest stable rustc compiler, but may |
| 89 |
work with older compilers. See [msrv.md](msrv.md) for more details. |
| 90 |
|
| 91 |
## License |
| 92 |
|
| 93 |
The procfs library is licensed under either of |
| 94 |
|
| 95 |
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 96 |
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 97 |
|
| 98 |
at your option. |
| 99 |
|
| 100 |
For additional copyright information regarding documentation, please also see the COPYRIGHT.txt file. |
| 101 |
|
| 102 |
### Contribution |
| 103 |
|
| 104 |
Contributions are welcome, especially in the areas of documentation and testing on older kernels. |
| 105 |
|
| 106 |
Unless you explicitly state otherwise, any contribution intentionally |
| 107 |
submitted for inclusion in the work by you, as defined in the Apache-2.0 |
| 108 |
license, shall be dual licensed as above, without any additional terms or |
| 109 |
conditions. |