perf_event_data/records/
cgroup.rs

1use std::borrow::Cow;
2use std::fmt;
3
4use crate::prelude::*;
5
6/// CGROUP records indicate when a new cgroup is created and activated.
7///
8/// This struct corresponds to `PERF_RECORD_CGROUP`. See the [manpage] for more
9/// documentation.
10///
11/// [manpage]: http://man7.org/linux/man-pages/man2/perf_event_open.2.html
12#[derive(Clone)]
13pub struct CGroup<'a> {
14    /// The cgroup ID.
15    pub id: u64,
16
17    /// Path of the cgroup from the root.
18    pub path: Cow<'a, [u8]>,
19}
20
21impl<'a> CGroup<'a> {
22    /// Get `path` as a [`Path`](std::path::Path).
23    #[cfg(unix)]
24    pub fn path_os(&self) -> &std::path::Path {
25        use std::ffi::OsStr;
26        use std::os::unix::ffi::OsStrExt;
27        use std::path::Path;
28
29        Path::new(OsStr::from_bytes(&self.path))
30    }
31
32    /// Convert all the borrowed data in this `CGroup` into owned data.
33    pub fn into_owned(self) -> CGroup<'static> {
34        CGroup {
35            path: self.path.into_owned().into(),
36            ..self
37        }
38    }
39}
40
41impl<'p> Parse<'p> for CGroup<'p> {
42    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
43    where
44        E: Endian,
45        B: ParseBuf<'p>,
46    {
47        Ok(Self {
48            id: p.parse()?,
49            path: p.parse_rest_trim_nul()?,
50        })
51    }
52}
53
54impl fmt::Debug for CGroup<'_> {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        f.debug_struct("CGroup")
57            .field("id", &self.id)
58            .field("path", &crate::util::fmt::ByteStr(&self.path))
59            .finish()
60    }
61}