procfs_core/process/
clear_refs.rs

1use std::fmt;
2
3/// Clearing the PG_Referenced and ACCESSED/YOUNG bits
4/// provides a method to measure approximately how much memory
5/// a process is using.  One first inspects the values in the
6/// "Referenced" fields for the VMAs shown in
7/// `/proc/[pid]/smaps` to get an idea of the memory footprint
8/// of the process.  One then clears the PG_Referenced and
9/// ACCESSED/YOUNG bits and, after some measured time
10/// interval, once again inspects the values in the
11/// "Referenced" fields to get an idea of the change in memory
12/// footprint of the process during the measured interval.  If
13/// one is interested only in inspecting the selected mapping
14/// types, then the value 2 or 3 can be used instead of 1.
15///
16/// The `/proc/[pid]/clear_refs` file is present only if the
17/// CONFIG_PROC_PAGE_MONITOR kernel configuration option is
18/// enabled.
19///
20/// Only writable by the owner of the process
21///
22/// See `procfs::Process::clear_refs()` and `procfs::Process::pagemap()`
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
24pub enum ClearRefs {
25    /// (since Linux 2.6.22)
26    ///
27    ///  Reset the PG_Referenced and ACCESSED/YOUNG bits for
28    ///  all the pages associated with the process.  (Before
29    ///  kernel 2.6.32, writing any nonzero value to this
30    ///  file had this effect.)
31    PGReferencedAll = 1,
32    /// (since Linux 2.6.32)
33    ///
34    /// Reset the PG_Referenced and ACCESSED/YOUNG bits for
35    /// all anonymous pages associated with the process.
36    PGReferencedAnonymous = 2,
37    /// (since Linux 2.6.32)
38    ///
39    /// Reset the PG_Referenced and ACCESSED/YOUNG bits for
40    /// all file-mapped pages associated with the process.
41    PGReferencedFile = 3,
42    /// (since Linux 3.11)
43    ///
44    /// Clear the soft-dirty bit for all the pages
45    /// associated with the process.  This is used (in
46    /// conjunction with `/proc/[pid]/pagemap`) by the check-
47    /// point restore system to discover which pages of a
48    /// process have been dirtied since the file
49    /// `/proc/[pid]/clear_refs` was written to.
50    SoftDirty = 4,
51    /// (since Linux 4.0)
52    ///
53    /// Reset the peak resident set size ("high water
54    /// mark") to the process's current resident set size
55    /// value.
56    PeakRSS = 5,
57}
58
59impl fmt::Display for ClearRefs {
60    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61        write!(
62            f,
63            "{}",
64            match self {
65                ClearRefs::PGReferencedAll => 1,
66                ClearRefs::PGReferencedAnonymous => 2,
67                ClearRefs::PGReferencedFile => 3,
68                ClearRefs::SoftDirty => 4,
69                ClearRefs::PeakRSS => 5,
70            }
71        )
72    }
73}
74
75impl std::str::FromStr for ClearRefs {
76    type Err = &'static str;
77
78    fn from_str(s: &str) -> Result<Self, Self::Err> {
79        s.parse()
80            .map_err(|_| "Fail to parse clear refs value")
81            .and_then(|n| match n {
82                1 => Ok(ClearRefs::PGReferencedAll),
83                2 => Ok(ClearRefs::PGReferencedAnonymous),
84                3 => Ok(ClearRefs::PGReferencedFile),
85                4 => Ok(ClearRefs::SoftDirty),
86                5 => Ok(ClearRefs::PeakRSS),
87                _ => Err("Unknown clear refs value"),
88            })
89    }
90}