cgroups_rs/fs/
perf_event.rs

1// Copyright (c) 2018 Levente Kurusa
2//
3// SPDX-License-Identifier: Apache-2.0 or MIT
4//
5
6//! This module contains the implementation of the `perf_event` cgroup subsystem.
7//!
8//! See the Kernel's documentation for more information about this subsystem, found at:
9//!  [tools/perf/Documentation/perf-record.txt](https://raw.githubusercontent.com/torvalds/linux/master/tools/perf/Documentation/perf-record.txt)
10use std::path::PathBuf;
11
12use crate::fs::error::*;
13
14use crate::fs::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
15
16/// A controller that allows controlling the `perf_event` subsystem of a Cgroup.
17///
18/// In essence, when processes belong to the same `perf_event` controller, they can be monitored
19/// together using the `perf` performance monitoring and reporting tool.
20#[derive(Debug, Clone)]
21pub struct PerfEventController {
22    base: PathBuf,
23    path: PathBuf,
24}
25
26impl ControllerInternal for PerfEventController {
27    fn control_type(&self) -> Controllers {
28        Controllers::PerfEvent
29    }
30    fn get_path(&self) -> &PathBuf {
31        &self.path
32    }
33    fn get_path_mut(&mut self) -> &mut PathBuf {
34        &mut self.path
35    }
36    fn get_base(&self) -> &PathBuf {
37        &self.base
38    }
39
40    fn apply(&self, _res: &Resources) -> Result<()> {
41        Ok(())
42    }
43}
44
45impl ControllIdentifier for PerfEventController {
46    fn controller_type() -> Controllers {
47        Controllers::PerfEvent
48    }
49}
50
51impl<'a> From<&'a Subsystem> for &'a PerfEventController {
52    fn from(sub: &'a Subsystem) -> &'a PerfEventController {
53        unsafe {
54            match sub {
55                Subsystem::PerfEvent(c) => c,
56                _ => {
57                    assert_eq!(1, 0);
58                    let v = std::mem::MaybeUninit::uninit();
59                    v.assume_init()
60                }
61            }
62        }
63    }
64}
65
66impl PerfEventController {
67    /// Constructs a new `PerfEventController` with `root` serving as the root of the control group.
68    pub fn new(point: PathBuf, root: PathBuf) -> Self {
69        Self {
70            base: root,
71            path: point,
72        }
73    }
74}