Debian Codegraph @

src/collection/linux/cgroups.rs

raw symbols SCIP
1 //! cgroup-related code for Linux.
2 //!
3 //! For info about cgroups, see things like [the kernel docs](https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html)
4 //! and [Kubernetes docs](https://kubernetes.io/docs/concepts/architecture/cgroups/#deprecation-of-cgroup-v1).
5
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#};
7
8 fn read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().(path: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u64> {
9 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().(path).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()
10 }
11
12 fn read_stat_keyrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_stat_key().(path: &str, key: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u64> {
13 // TODO: Maybe check if this is worth it for the files we read.
14 let file = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
15 for line in stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(file).linesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#lines().().map_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map_while().(Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#::okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().) {
16 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(rest) = line.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().(key)
17 && let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(val) = rest.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().(' ')
18 {
19 return val.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
20 }
21 }
22
23 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
24 }
25
26 /// Represents cgroup memory limits.
27 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
28 pub(craterust-analyzer cargo bottom 0.14.9 crate/) enum CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit# {
29 Bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Bytes#(u64),
30 Maxrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Max#,
31 }
32
33 /// Represents cgroup memory usage data.
34 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
35 pub(craterust-analyzer cargo bottom 0.14.9 crate/) struct CgroupMemDatarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData# {
36 pub used_bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#used_bytes.: u64,
37 pub limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#limit.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#>,
38 }
39
40 /// Gathers memory data from cgroup sources.
41 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
42 pub(craterust-analyzer cargo bottom 0.14.9 crate/) struct CgroupMemCollectorrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector# {
43 pub ramrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#ram.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CgroupMemDatarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#>,
44 pub swaprust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#swap.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CgroupMemDatarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#>,
45 }
46
47 /// Computes the cgroup v1 swap limit. Calculated by getting memsw and subtracting the memory limits.
48 #[inline]
49 fn cgroup_v1_swap_limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/cgroup_v1_swap_limit().(
50 memsw_limit: u64, mem_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u64>, total_memory: u64, total_swap: u64,
51 ) -> u64 {
52 let effective_memsw_limit = memsw_limit.minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(total_memory rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().total_swap);
53 let effective_mem_limit = mem_limit.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(0, |mem_limit| mem_limit.minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(total_memory));
54
55 effective_memsw_limit.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().(effective_mem_limit)
56 }
57
58 impl CgroupMemCollectorrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector# {
59 /// Refresh the cgroup memory data.
60 ///
61 /// Based on [docker's CLI](https://github.com/docker/cli/blob/master/cli/command/container/stats_helpers.go#L254).
62 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn refreshrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupMemCollector]refresh().(&mut self, total_memory: u64, total_swap: u64) {
63 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.try_update_memory_cgroup_v1rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupMemCollector]try_update_memory_cgroup_v1().(total_memory, total_swap)
64 && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.try_update_memory_cgroup_v2rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupMemCollector]try_update_memory_cgroup_v2().()
65 {
66 self.ramrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#ram. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
67 self.swaprust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#swap. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
68 }
69 }
70
71 /// Try and update the memory using cgroup v1 semantics. If successful, returns `true`.
72 fn try_update_memory_cgroup_v1rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupMemCollector]try_update_memory_cgroup_v1().(&mut self, total_memory: u64, total_swap: u64) -> bool {
73 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mem_usage) = read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().("/sys/fs/cgroup/memory/memory.usage_in_bytes") {
74 // --- Memory ---
75 let inactive =
76 read_stat_keyrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_stat_key().("/sys/fs/cgroup/memory/memory.stat", "total_inactive_file");
77 let used_bytes = match inactive {
78 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(inactive) if inactive rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().mem_usage => mem_usage rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().inactive,
79 _ => mem_usage,
80 };
81
82 // Technically if it's some insanely high value (https://unix.stackexchange.com/a/421182),
83 // then it's "unlimited", but we can just make it so we take the max of the main and this anyway.
84 let mem_limit_raw = read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().("/sys/fs/cgroup/memory/memory.limit_in_bytes");
85 let mem_limit = mem_limit_raw.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Bytes#);
86
87 self.ramrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#ram. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CgroupMemDatarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData# {
88 used_bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#used_bytes.,
89 limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#limit.: mem_limit,
90 });
91
92 // --- Swap ---
93 // Since swap is dependent on the normal memory usage, we couple it together.
94 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(memsw_usage) = read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().("/sys/fs/cgroup/memory/memory.memsw.usage_in_bytes")
95 {
96 let used_bytes = memsw_usage.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().(mem_usage);
97
98 // Same idea for here.
99 let swap_limit = read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().("/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
100 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|memsw_limit| {
101 cgroup_v1_swap_limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/cgroup_v1_swap_limit().(memsw_limit, mem_limit_raw, total_memory, total_swap)
102 })
103 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Bytes#);
104
105 self.swaprust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#swap. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CgroupMemDatarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData# {
106 used_bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#used_bytes.,
107 limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#limit.: swap_limit,
108 });
109 }
110
111 true
112 } else {
113 false
114 }
115 }
116
117 /// Try and update the memory using cgroup v2 semantics. If successful, returns `true`.
118 fn try_update_memory_cgroup_v2rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupMemCollector]try_update_memory_cgroup_v2().(&mut self) -> bool {
119 let mut could_update = false;
120
121 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mem_current) = read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().("/sys/fs/cgroup/memory.current") {
122 // --- Memory ---
123 let inactive = read_stat_keyrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_stat_key().("/sys/fs/cgroup/memory.stat", "inactive_file");
124 let used_bytes = match inactive {
125 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(inactive) if inactive rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().mem_current => mem_current rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().inactive,
126 _ => mem_current,
127 };
128
129 let limit = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().("/sys/fs/cgroup/memory.max")
130 .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()
131 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| match s.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().() {
132 "max" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Maxrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Max#),
133 v => v.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u64>().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Bytes#).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(),
134 });
135
136 self.ramrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#ram. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CgroupMemDatarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData# { used_bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#used_bytes., limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#limit. });
137
138 could_update = true;
139 }
140
141 // --- Swap ---
142 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(swap_current) = read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().("/sys/fs/cgroup/memory.swap.current") {
143 let limit = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().("/sys/fs/cgroup/memory.swap.max")
144 .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()
145 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| match s.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().() {
146 "max" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Maxrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Max#),
147 v => v.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u64>().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Bytes#).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(),
148 });
149
150 self.swaprust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#swap. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CgroupMemDatarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData# {
151 used_bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#used_bytes.: swap_current,
152 limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#limit.,
153 });
154
155 could_update = true;
156 }
157
158 could_update
159 }
160 }
161
162 #[inline]
163 fn parse_cpu_quotarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/parse_cpu_quota().(cpu_max: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<f64> {
164 let mut parts = cpu_max.split_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_whitespace().();
165 let quota_str = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitWhitespace<'a>`][Iterator]next().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("");
166
167 if quota_str rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne()."max" {
168 let period: u64 = {
169 let period_str = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitWhitespace<'a>`][Iterator]next().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("");
170
171 match period_str.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() {
172 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(v) => v,
173 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
174 }
175 };
176
177 match quota_str.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u64>() {
178 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(quota) if quota rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().0 && period rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().0 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quota as f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().period as f64),
179 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
180 }
181 } else {
182 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# // "max" = unlimited
183 }
184 }
185
186 /// Gathers CPU data from cgroup sources.
187 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
188 pub(craterust-analyzer cargo bottom 0.14.9 crate/) struct CgroupCpuCollectorrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector# {
189 /// A maximum number of CPUs (cores) that can be used, as defined by the cgroup.
190 pub cpu_quotarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#cpu_quota.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<f64>,
191
192 /// Computed average CPU usage percent (only set when a quota is active).
193 pub avg_cpu_percentrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#avg_cpu_percent.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<f32>,
194
195 /// The previous CPU microsecond time (usage) and when it was last updated. Used to compute average cgroup CPU usage.
196 prev_cpurust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#prev_cpu.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(u64, Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#)>,
197 }
198
199 impl CgroupCpuCollectorrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector# {
200 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn refreshrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupCpuCollector]refresh().(&mut self) {
201 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.try_update_cpu_cgroup_v1rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupCpuCollector]try_update_cpu_cgroup_v1().() && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.try_update_cpu_cgroup_v2rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupCpuCollector]try_update_cpu_cgroup_v2().() {
202 self.cpu_quotarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#cpu_quota. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
203 self.avg_cpu_percentrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#avg_cpu_percent. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
204 self.prev_cpurust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#prev_cpu. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
205 }
206 }
207
208 /// Try to update CPU data using cgroup v1 semantics. Returns `true` on success.
209 fn try_update_cpu_cgroup_v1rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupCpuCollector]try_update_cpu_cgroup_v1().(&mut self) -> bool {
210 let quota_raw: i64 = {
211 let quota_str = match fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().("/sys/fs/cgroup/cpu/cpu.cfs_quota_us") {
212 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) => s,
213 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => return false,
214 };
215
216 match quota_str.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() {
217 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(v) => v,
218 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => return false,
219 }
220 };
221
222 self.cpu_quotarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#cpu_quota. = if quota_raw rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt().0 {
223 let period = read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().("/sys/fs/cgroup/cpu/cpu.cfs_period_us");
224
225 period.and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|p| {
226 if p rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().0 {
227 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quota_raw as f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().p as f64)
228 } else {
229 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
230 }
231 })
232 } else {
233 // If it's less than 0 (-1) then it's representing "unlimited" quota (AKA use the full CPU).
234 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
235 };
236
237 // cpuacct.usage is in nanoseconds; convert to microseconds for consistency with v2
238 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(usage_nsec) = read_u64rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_u64().("/sys/fs/cgroup/cpuacct/cpuacct.usage") {
239 self.try_compute_avg_and_updaterust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupCpuCollector]try_compute_avg_and_update().(usage_nsec rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div().1000);
240 } else {
241 self.avg_cpu_percentrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#avg_cpu_percent. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
242 }
243
244 true
245 }
246
247 /// Try to update CPU data using cgroup v2 semantics. Returns `true` on success.
248 fn try_update_cpu_cgroup_v2rust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupCpuCollector]try_update_cpu_cgroup_v2().(&mut self) -> bool {
249 let cpu_max = match fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().("/sys/fs/cgroup/cpu.max") {
250 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) => s,
251 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => return false,
252 };
253
254 self.cpu_quotarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#cpu_quota. = parse_cpu_quotarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/parse_cpu_quota().(cpu_max);
255
256 // cpu.stat usage_usec is in microseconds
257 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(current_microseconds) = read_stat_keyrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/read_stat_key().("/sys/fs/cgroup/cpu.stat", "usage_usec") {
258 self.try_compute_avg_and_updaterust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupCpuCollector]try_compute_avg_and_update().(current_microseconds);
259 } else {
260 self.avg_cpu_percentrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#avg_cpu_percent. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
261 }
262
263 true
264 }
265
266 /// Try and compute average CPU usage based on the current microseconds. Note that this requires _two_ invocations
267 /// to compute a value, as the first invocation just sets the baseline for the next CPU time and timestamp to
268 /// compare with.
269 fn try_compute_avg_and_updaterust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/impl#[CgroupCpuCollector]try_compute_avg_and_update().(&mut self, current_microseconds: u64) {
270 let now = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
271
272 self.avg_cpu_percentrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#avg_cpu_percent. = if let (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cpu_quota), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prev_cpu)) =
273 (self.cpu_quotarust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#cpu_quota., self.prev_cpurust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#prev_cpu.)
274 {
275 let (prev_microseconds, prev_time) = prev_cpu;
276
277 let elapsed_microseconds = now.duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]duration_since().(prev_time).as_microsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_micros().() as u64;
278 if elapsed_microseconds rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().0 && cpu_quota rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().0.0 {
279 let delta = current_microseconds.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().(prev_microseconds);
280 let pct = (delta as f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().(elapsed_microseconds as f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().cpu_quota) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().100.0) as f32;
281
282 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pct.clamprust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]clamp().(0.0, 100.0))
283 } else {
284 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
285 }
286 } else {
287 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
288 };
289
290 self.prev_cpurust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupCpuCollector#prev_cpu. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((current_microseconds, now));
291 }
292 }