cgroups_rs/fs/
cpu.rs

1// Copyright (c) 2018 Levente Kurusa
2// Copyright (c) 2020 Ant Group
3//
4// SPDX-License-Identifier: Apache-2.0 or MIT
5//
6
7//! This module contains the implementation of the `cpu` cgroup subsystem.
8//!
9//! See the Kernel's documentation for more information about this subsystem, found at:
10//!  [Documentation/scheduler/sched-design-CFS.txt](https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt)
11//!  paragraph 7 ("GROUP SCHEDULER EXTENSIONS TO CFS").
12use std::fs::File;
13use std::io::{Read, Write};
14use std::path::PathBuf;
15
16use crate::fs::error::ErrorKind::*;
17use crate::fs::error::*;
18use crate::fs::{parse_max_value, read_i64_from, read_u64_from};
19
20use crate::fs::{
21    ControllIdentifier, ControllerInternal, Controllers, CpuResources, CustomizedAttribute,
22    MaxValue, Resources, Subsystem,
23};
24
25/// A controller that allows controlling the `cpu` subsystem of a Cgroup.
26///
27/// In essence, it allows gathering information about how much the tasks inside the control group
28/// are using the CPU and creating rules that limit their usage. Note that this crate does not yet
29/// support managing realtime tasks.
30#[derive(Debug, Clone)]
31pub struct CpuController {
32    base: PathBuf,
33    path: PathBuf,
34    v2: bool,
35}
36
37/// The current state of the control group and its processes.
38#[derive(Debug)]
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40pub struct Cpu {
41    /// Reports CPU time statistics.
42    ///
43    /// Corresponds the `cpu.stat` file in `cpu` control group.
44    pub stat: String,
45}
46
47/// The current state of the control group and its processes.
48#[derive(Debug)]
49struct CfsQuotaAndPeriod {
50    quota: MaxValue,
51    period: u64,
52}
53
54impl ControllerInternal for CpuController {
55    fn control_type(&self) -> Controllers {
56        Controllers::Cpu
57    }
58
59    fn get_path(&self) -> &PathBuf {
60        &self.path
61    }
62    fn get_path_mut(&mut self) -> &mut PathBuf {
63        &mut self.path
64    }
65
66    fn get_base(&self) -> &PathBuf {
67        &self.base
68    }
69
70    fn is_v2(&self) -> bool {
71        self.v2
72    }
73
74    fn apply(&self, res: &Resources) -> Result<()> {
75        // get the resources that apply to this controller
76        let res: &CpuResources = &res.cpu;
77
78        update_and_test!(self, set_shares, res.shares, shares);
79        update_and_test!(self, set_cfs_period, res.period, cfs_period);
80        update_and_test!(self, set_cfs_quota, res.quota, cfs_quota);
81
82        res.attrs.iter().for_each(|(k, v)| {
83            let _ = self.set(k, v);
84        });
85
86        // TODO: rt properties (CONFIG_RT_GROUP_SCHED) are not yet supported
87
88        Ok(())
89    }
90}
91
92impl ControllIdentifier for CpuController {
93    fn controller_type() -> Controllers {
94        Controllers::Cpu
95    }
96}
97
98impl<'a> From<&'a Subsystem> for &'a CpuController {
99    fn from(sub: &'a Subsystem) -> &'a CpuController {
100        unsafe {
101            match sub {
102                Subsystem::Cpu(c) => c,
103                _ => {
104                    assert_eq!(1, 0);
105                    let v = std::mem::MaybeUninit::uninit();
106                    v.assume_init()
107                }
108            }
109        }
110    }
111}
112
113impl CpuController {
114    /// Contructs a new `CpuController` with `root` serving as the root of the control group.
115    pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
116        Self {
117            base: root,
118            path: point,
119            v2,
120        }
121    }
122
123    /// Returns CPU time statistics based on the processes in the control group.
124    pub fn cpu(&self) -> Cpu {
125        Cpu {
126            stat: self
127                .open_path("cpu.stat", false)
128                .and_then(|mut file| {
129                    let mut s = String::new();
130                    let res = file.read_to_string(&mut s);
131                    match res {
132                        Ok(_) => Ok(s),
133                        Err(e) => Err(Error::with_cause(ReadFailed("cpu.stat".to_string()), e)),
134                    }
135                })
136                .unwrap_or_default(),
137        }
138    }
139
140    /// Configures the CPU bandwidth (in relative relation to other control groups and this control
141    /// group's parent).
142    ///
143    /// For example, setting control group `A`'s `shares` to `100`, and control group `B`'s
144    /// `shares` to `200` ensures that control group `B` receives twice as much as CPU bandwidth.
145    /// (Assuming both `A` and `B` are of the same parent)
146    pub fn set_shares(&self, shares: u64) -> Result<()> {
147        let mut file_name = "cpu.shares";
148        if self.v2 {
149            file_name = "cpu.weight";
150        }
151        // NOTE: .CpuShares is not used here. Conversion is the caller's responsibility.
152        self.open_path(file_name, true).and_then(|mut file| {
153            file.write_all(shares.to_string().as_ref()).map_err(|e| {
154                Error::with_cause(WriteFailed(file_name.to_string(), shares.to_string()), e)
155            })
156        })
157    }
158
159    /// Retrieve the CPU bandwidth that this control group (relative to other control groups and
160    /// this control group's parent) can use.
161    pub fn shares(&self) -> Result<u64> {
162        let mut file = "cpu.shares";
163        if self.v2 {
164            file = "cpu.weight";
165        }
166        self.open_path(file, false).and_then(read_u64_from)
167    }
168
169    /// Specify a period (when using the CFS scheduler) of time in microseconds for how often this
170    /// control group's access to the CPU should be reallocated.
171    pub fn set_cfs_period(&self, us: u64) -> Result<()> {
172        if self.v2 {
173            return self.set_cfs_quota_and_period(None, Some(us));
174        }
175        self.open_path("cpu.cfs_period_us", true)
176            .and_then(|mut file| {
177                file.write_all(us.to_string().as_ref()).map_err(|e| {
178                    Error::with_cause(
179                        WriteFailed("cpu.cfs_period_us".to_string(), us.to_string()),
180                        e,
181                    )
182                })
183            })
184    }
185
186    /// Retrieve the period of time of how often this cgroup's access to the CPU should be
187    /// reallocated in microseconds.
188    pub fn cfs_period(&self) -> Result<u64> {
189        if self.v2 {
190            let current_value = self
191                .open_path("cpu.max", false)
192                .and_then(parse_cfs_quota_and_period)?;
193            return Ok(current_value.period);
194        }
195        self.open_path("cpu.cfs_period_us", false)
196            .and_then(read_u64_from)
197    }
198
199    /// Specify a quota (when using the CFS scheduler) of time in microseconds for which all tasks
200    /// in this control group can run during one period (see: `set_cfs_period()`).
201    pub fn set_cfs_quota(&self, us: i64) -> Result<()> {
202        if self.v2 {
203            return self.set_cfs_quota_and_period(Some(us), None);
204        }
205        self.open_path("cpu.cfs_quota_us", true)
206            .and_then(|mut file| {
207                file.write_all(us.to_string().as_ref()).map_err(|e| {
208                    Error::with_cause(
209                        WriteFailed("cpu.cfs_quota_us".to_string(), us.to_string()),
210                        e,
211                    )
212                })
213            })
214    }
215
216    /// Retrieve the quota of time for which all tasks in this cgroup can run during one period, in
217    /// microseconds.
218    pub fn cfs_quota(&self) -> Result<i64> {
219        if self.v2 {
220            let current_value = self
221                .open_path("cpu.max", false)
222                .and_then(parse_cfs_quota_and_period)?;
223            return Ok(current_value.quota.to_i64());
224        }
225
226        self.open_path("cpu.cfs_quota_us", false)
227            .and_then(read_i64_from)
228    }
229
230    pub fn set_cfs_quota_and_period(&self, quota: Option<i64>, period: Option<u64>) -> Result<()> {
231        if !self.v2 {
232            if let Some(q) = quota {
233                self.set_cfs_quota(q)?;
234            }
235            if let Some(p) = period {
236                self.set_cfs_period(p)?;
237            }
238            return Ok(());
239        }
240
241        // https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
242
243        // cpu.max
244        // A read-write two value file which exists on non-root cgroups. The default is “max 100000”.
245        // The maximum bandwidth limit. It’s in the following format:
246        // $MAX $PERIOD
247        // which indicates that the group may consume upto $MAX in each $PERIOD duration.
248        // “max” for $MAX indicates no limit. If only one number is written, $MAX is updated.
249
250        let current_value = self
251            .open_path("cpu.max", false)
252            .and_then(parse_cfs_quota_and_period)?;
253
254        let new_quota = if let Some(q) = quota {
255            if q > 0 {
256                q.to_string()
257            } else {
258                "max".to_string()
259            }
260        } else {
261            current_value.quota.to_string()
262        };
263
264        let new_period = if let Some(p) = period {
265            p.to_string()
266        } else {
267            current_value.period.to_string()
268        };
269
270        let line = format!("{} {}", new_quota, new_period);
271        self.open_path("cpu.max", true).and_then(|mut file| {
272            file.write_all(line.as_ref())
273                .map_err(|e| Error::with_cause(WriteFailed("cpu.max".to_string(), line), e))
274        })
275    }
276
277    pub fn set_rt_runtime(&self, us: i64) -> Result<()> {
278        self.open_path("cpu.rt_runtime_us", true)
279            .and_then(|mut file| {
280                file.write_all(us.to_string().as_ref()).map_err(|e| {
281                    Error::with_cause(
282                        WriteFailed("cpu.rt_runtime_us".to_string(), us.to_string()),
283                        e,
284                    )
285                })
286            })
287    }
288
289    pub fn set_rt_period_us(&self, us: u64) -> Result<()> {
290        self.open_path("cpu.rt_period_us", true)
291            .and_then(|mut file| {
292                file.write_all(us.to_string().as_ref()).map_err(|e| {
293                    Error::with_cause(
294                        WriteFailed("cpu.rt_period_us".to_string(), us.to_string()),
295                        e,
296                    )
297                })
298            })
299    }
300}
301
302impl CustomizedAttribute for CpuController {}
303
304fn parse_cfs_quota_and_period(mut file: File) -> Result<CfsQuotaAndPeriod> {
305    let mut content = String::new();
306    file.read_to_string(&mut content)
307        .map_err(|e| Error::with_cause(ReadFailed("cpu.max".to_string()), e))?;
308
309    let fields = content.trim().split(' ').collect::<Vec<&str>>();
310    if fields.len() != 2 {
311        return Err(Error::from_string(format!("invaild format: {}", content)));
312    }
313
314    let quota = parse_max_value(fields[0])?;
315    let period = fields[1]
316        .parse::<u64>()
317        .map_err(|e| Error::with_cause(ParseError, e))?;
318
319    Ok(CfsQuotaAndPeriod { quota, period })
320}