proc_status/lib.rs
1//!
2//! basic process information
3//!
4//! The data comes from `/proc/<pid>/process` and is only
5//! available on unix-like systems.
6//!
7//! This crate aims at keeping very simple.
8//! If it doesn't cover your needs, you should probably have a look
9//! at the much more complete [procfs](https://crates.io/crates/procfs).
10//!
11//! # Examples:
12//!
13//! ## Dump memory info about the current process:
14//!
15//! ```
16//! let mem = proc_status::mem_usage().unwrap();
17//! println!("Mem usage in bytes: current={}, peak={}", mem.current, mem.peak);
18//! ```
19//! This prints something like
20//!
21//! ```stdout
22//! Mem usage in bytes: current=1232896, peak=141430784
23//! ```
24//!
25//!
26//! ## Print all the fields of the current process' status:
27//!
28//! ```
29//! use proc_status::ProcStatus;
30//!
31//! let ps = ProcStatus::read().unwrap();
32//! for entry in ps.entries() {
33//! let entry = entry.unwrap();
34//! println!("{} = {:?}", entry.key, entry.value);
35//! }
36//! ```
37//!
38//! ## Get the raw value of specific entries
39//!
40//! ```
41//! use proc_status::ProcStatus;
42//!
43//! let ps = ProcStatus::read().unwrap();
44//! println!("State: {:?}", ps.value("State").unwrap());
45//! println!("VmPeak in bytes: {:?}", ps.value_KiB("VmPeak").unwrap() * 1024);
46//! ```
47//!
48
49
50mod entry;
51mod entries;
52mod errors;
53mod mem_usage;
54mod proc_ref;
55mod status;
56
57pub use {
58 entry::ProcEntry,
59 entries::ProcEntries,
60 errors::ProcStatusError,
61 mem_usage::MemUsage,
62 proc_ref::ProcRef,
63 status::ProcStatus,
64};
65
66/// load information about the current en peak memory
67/// usage of the current process
68pub fn mem_usage() -> Result<MemUsage, ProcStatusError> {
69 ProcStatus::read()
70 .and_then(|ps| ps.mem_usage())
71}
72
73/// get the value of an entry by name
74///
75///
76/// If you want to read several entries and you
77/// want them to be consistent, you should read
78/// the whole ProcStatus and call the `value`
79/// function on that instance. This would also
80/// be more efficient.
81pub fn value(key: &str) -> Result<String, ProcStatusError> {
82 ProcStatus::read()
83 .and_then(|ps| ps.value(key).map(|v| v.to_string()))
84}
85
86/// get the value of an entry by name if it is
87/// a size in KiB.
88///
89///
90/// If you want to read several entries and you
91/// want them to be consistent, you should read
92/// the whole ProcStatus and call the `value_KiB`
93/// function on that instance. This would also
94/// be more efficient.
95#[allow(non_snake_case)]
96pub fn value_KiB(key: &str) -> Result<usize, ProcStatusError> {
97 ProcStatus::read()
98 .and_then(|ps| ps.value_KiB(key))
99}
100
101