1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
//! Cgroup-fs is a minimal wrapper around Linux Control Groups (cgroups) filesystem (usually
//! mounted as `/sys/fs/cgroup`).
//!
//! # Examples
//!
//! ## Get memory usage from root cgroup
//!
//! ```
//! let root_cgroup = cgroups_fs::CgroupName::new("");
//! let root_memory_cgroup = cgroups_fs::Cgroup::new(&root_cgroup, "memory");
//! println!(
//! "Current memory usage is {} bytes",
//! root_memory_cgroup.get_value::<u64>("memory.usage_in_bytes").unwrap()
//! );
//! ```
//!
//! ## Measure memory usage of a child process
//!
//! Read [the `CgroupsCommandExt` documentation].
//!
//! [the `CgroupsCommandExt` documentation]: trait.CgroupsCommandExt.html#impl-CgroupsCommandExt
#![cfg(target_os = "linux")]
use std::fs;
use std::io;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
/// A common structure holding a cgroups name (path).
#[derive(Debug)]
pub struct CgroupName {
mount_point: PathBuf,
name: PathBuf,
}
impl CgroupName {
/// Defines a new cgroups name.
///
/// Notes:
/// * It does not create any cgroups. It is just an API abstraction layer. Learn more about
/// [`Cgroup::new`], [`Cgroup::create`], [`Cgroup::remove`], and [`AutomanagedCgroup::init`]
/// methods.
///
/// [`Cgroup::new`]: struct.Cgroup.html#method.new
/// [`Cgroup::create`]: struct.Cgroup.html#method.create
/// [`Cgroup::remove`]: struct.Cgroup.html#method.remove
/// [`AutomanagedCgroup::init`]: struct.AutomanagedCgroup.html#method.init
pub fn new<P>(name: P) -> Self
where
P: AsRef<Path>,
{
Self {
// TODO: auto-discover the cgroups FS mount-point
mount_point: "/sys/fs/cgroup".into(),
name: name.as_ref().to_path_buf(),
}
}
}
/// A controller of a specific cgroups namespace.
///
/// This type supports a number of operations for manipulating with a cgroups namespace.
#[derive(Debug)]
pub struct Cgroup {
root: PathBuf,
}
impl Cgroup {
/// Defines a cgroup relation.
///
/// Notes:
/// * It does not create any cgroups. It is just an API abstraction layer. Learn more about
/// [`Cgroup::create`], [`Cgroup::remove`], and [`AutomanagedCgroup::init`] methods.
///
/// [`Cgroup::create`]: #method.create
/// [`Cgroup::remove`]: #method.remove
/// [`AutomanagedCgroup::init`]: struct.AutomanagedCgroup.html#method.init
pub fn new(cgroup_name: &CgroupName, subsystem: &str) -> Self {
Self {
root: cgroup_name
.mount_point
.join(subsystem)
.join(&cgroup_name.name),
}
}
/// Creates a cgroups namespace.
///
/// Notes:
/// * Keep in mind the usual filesystem permissions (owner, group, and mode bits).
pub fn create(&self) -> io::Result<()> {
fs::create_dir(&self.root)
}
/// Removes a cgroups namespace.
///
/// Notes:
/// * This method will fail if there are nested cgroups.
/// * Keep in mind the usual filesystem permissions (owner, group, and mode bits).
pub fn remove(&self) -> io::Result<()> {
fs::remove_dir(&self.root)
}
/// Sets a binary or string value to the cgroup control file.
pub fn set_raw_value<V>(&self, key: &str, value: V) -> io::Result<()>
where
V: AsRef<[u8]>,
{
fs::write(self.root.join(key), value)
}
/// Sets a value to the cgroup control file.
pub fn set_value<V>(&self, key: &str, value: V) -> io::Result<()>
where
V: Copy + ToString,
{
self.set_raw_value(key, value.to_string())
}
/// Gets a string value from cgroup control file.
pub fn get_raw_value(&self, key: &str) -> io::Result<String> {
fs::read_to_string(self.root.join(key))
}
/// Gets a value from cgroup control file.
pub fn get_value<T>(&self, key: &str) -> io::Result<T>
where
T: std::str::FromStr,
{
self.get_raw_value(key)?
.trim_end()
.parse()
.map_err(|_| io::Error::new(io::ErrorKind::Other, "could not parse the value"))
}
fn tasks_absolute_path(&self) -> PathBuf {
self.root.join("tasks")
}
/// Attaches a task (thread) to the cgroup.
pub fn add_task(&self, pid: nix::unistd::Pid) -> io::Result<()> {
fs::write(self.tasks_absolute_path(), pid.to_string())
}
/// Lists tasks (threads) attached to the cgroup.
pub fn get_tasks(&self) -> io::Result<Vec<nix::unistd::Pid>> {
Ok(fs::read_to_string(self.tasks_absolute_path())?
.split_whitespace()
.map(|pid| nix::unistd::Pid::from_raw(pid.parse().unwrap()))
.collect())
}
/// Kills (SIGKILL) all the attached to the cgroup tasks.
pub fn kill_all_tasks(&self) -> io::Result<()> {
for _ in 0..100 {
let tasks = self.get_tasks()?;
if tasks.is_empty() {
return Ok(());
}
for task in tasks {
nix::sys::signal::kill(task, nix::sys::signal::Signal::SIGKILL).is_ok();
}
std::thread::sleep(std::time::Duration::from_micros(1));
}
Err(io::Error::new(
io::ErrorKind::Other,
"child subprocess(es) survived SIGKILL",
))
}
}
/// An automatically managed controller of a specific cgroups subsystem.
///
/// It is a wrapper around [`Cgroup`] type which automatically creates (on [`init`]) and removes
/// (on [`drop`]) a cgroup in a given subsystem.
///
/// Since it is a wrapper, all the methods from [`Cgroup`] type are directly available for
/// `AutomanagedCgroup` instances.
///
/// [`Cgroup`]: struct.Cgroup.html
/// [`init`]: struct.AutomanagedCgroup.html#method.init
/// [`drop`]: struct.AutomanagedCgroup.html#impl-Drop
pub struct AutomanagedCgroup {
inner: Cgroup,
}
impl AutomanagedCgroup {
/// Inits a cgroup, which means that it creates a cgroup in a given subsystem.
///
/// Notes:
/// * If there is an existing cgroup, it will be recreated from scratch. If that is not what
/// you what, consider using [`Cgroup`] type instead.
/// * The cgroup will be automatically removed once the `AutomanagedCgroup` instance is
/// dropped.
///
/// [`Cgroup`]: struct.Cgroup.html
pub fn init(cgroup_name: &CgroupName, subsystem: &str) -> io::Result<Self> {
let inner = Cgroup::new(cgroup_name, subsystem);
if let Err(error) = inner.create() {
match inner.get_tasks() {
Err(_) => return Err(error),
Ok(tasks) => {
if !tasks.is_empty() {
return Err(error);
}
}
}
inner.remove().is_ok();
inner.create()?;
}
Ok(Self { inner })
}
}
impl std::ops::Deref for AutomanagedCgroup {
type Target = Cgroup;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl AsRef<Cgroup> for AutomanagedCgroup {
fn as_ref(&self) -> &Cgroup {
&self
}
}
impl Drop for AutomanagedCgroup {
fn drop(&mut self) {
drop(self.inner.remove());
}
}
pub trait CgroupsCommandExt {
fn cgroups(&mut self, cgroups: &[impl AsRef<Cgroup>]) -> &mut Self;
}
/// `std::process::Command` extension which adds `cgroups` helper method.
///
/// # Example
///
/// ```no_run
/// let my_cgroup = cgroups_fs::CgroupName::new("my-cgroup");
/// let my_memory_cgroup = cgroups_fs::AutomanagedCgroup::init(&my_cgroup, "memory").unwrap();
///
/// use cgroups_fs::CgroupsCommandExt;
/// let output = std::process::Command::new("echo")
/// .arg("Hello world")
/// .cgroups(&[&my_memory_cgroup])
/// .output()
/// .expect("Failed to execute command");
///
/// println!(
/// "The echo process used {} bytes of RAM.",
/// my_memory_cgroup.get_value::<u64>("memory.max_usage_in_bytes").unwrap()
/// );
/// ```
impl CgroupsCommandExt for std::process::Command {
fn cgroups(&mut self, cgroups: &[impl AsRef<Cgroup>]) -> &mut Self {
let tasks_paths = cgroups
.iter()
.map(|cgroup| cgroup.as_ref().tasks_absolute_path())
.collect::<Vec<PathBuf>>();
self.before_exec(move || {
let pid = std::process::id().to_string();
for tasks_path in &tasks_paths {
fs::write(tasks_path, &pid)?;
}
Ok(())
})
}
}