Skip to content

Commit

Permalink
Merge pull request #25 from navidys/develop
Browse files Browse the repository at this point in the history
added /proc/<pid>/cgroup
  • Loading branch information
navidys authored Aug 18, 2024
2 parents 7ad67d5 + e20c0b3 commit f24dba0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Supported Features
*`/proc/<pid>`
* cgroup
* cmdline
* comm
* cwd
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod meminfo;
pub mod net_dev;
pub mod net_protocols;
pub mod process;
pub mod process_cgroup;
pub mod process_io;
pub mod process_limits;
pub mod swaps;
Expand Down
84 changes: 84 additions & 0 deletions src/process_cgroup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use serde::Serialize;

use crate::{
error::{CollectResult, MetricError},
process::Process,
utils,
};

/// ProcessCgroup models one line from /proc/[pid]/cgroup
#[derive(Debug, Serialize, Clone, Default)]
pub struct ProcessCgroup {
pub hierarchy_id: usize,
pub controllers: Vec<String>,
pub path: String,
}

impl ProcessCgroup {
pub fn new() -> Self {
Default::default()
}
}

impl Process {
/// cgroup reads from /proc/<pid>/cgroup and returns cgroup information of the process
pub fn cgroup(&self) -> CollectResult<Vec<ProcessCgroup>> {
let mut proc_cgroups = Vec::new();
let proc_cgroup_path_str = format!("{:?}", self.path());
let proc_cgroup_file = format!("{}/cgroup", proc_cgroup_path_str.replace("\"", ""));

for line in utils::read_file_lines(&proc_cgroup_file)? {
let item_fields: Vec<&str> = line.trim().split(':').collect();
if item_fields.len() != 3 {
return Err(MetricError::InvalidFieldNumberError(
"process cgroup".to_string(),
item_fields.len(),
line,
));
}

let mut proc_cgroup = ProcessCgroup::new();
proc_cgroup.path = item_fields[2].trim().to_string();

match item_fields[0].parse::<usize>() {
Ok(v) => proc_cgroup.hierarchy_id = v,
Err(e) => return Err(MetricError::ParseIntError(item_fields[0].to_string(), e)),
}

if item_fields[1].trim() != "" {
proc_cgroup.controllers = item_fields[1]
.trim()
.split(",")
.map(|c| c.to_string())
.collect::<Vec<String>>()
}

proc_cgroups.push(proc_cgroup);
}

Ok(proc_cgroups)
}
}

#[cfg(test)]
mod tests {
use std::path::Path;

use crate::process::*;

#[test]
fn proc_cgroup() {
let proc_path = Path::new("test_data/fixtures/proc");
let sys_proc = collect_from(proc_path, 26231).expect("running proc 26231");
let sys_proc_cgroup = sys_proc.cgroup().expect("running proc 26231 cgroup stat");

assert_eq!(sys_proc_cgroup.len(), 1);
assert_eq!(sys_proc_cgroup[0].hierarchy_id, 1);
assert_eq!(sys_proc_cgroup[0].controllers.len(), 0);
assert_eq!(sys_proc_cgroup[0].path, "/user.slice/user-1000.slice/[email protected]/app.slice/app-org.gnome.Terminal.slice/vte-spawn-fd5b6c83-c316-470a-9732-4db75febce50.scope");

let sys_proc = collect_from(proc_path, 26232).expect("running proc 26232");
let sys_proc_cgroup = sys_proc.cgroup();
assert_eq!(sys_proc_cgroup.is_err(), true);
}
}
5 changes: 5 additions & 0 deletions test_data/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Mode: 755
Directory: fixtures/proc/26231
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/proc/26231/cgroup
Lines: 1
1::/user.slice/user-1000.slice/[email protected]/app.slice/app-org.gnome.Terminal.slice/vte-spawn-fd5b6c83-c316-470a-9732-4db75febce50.scope
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/proc/26231/cmdline
Lines: 1
vimNULLBYTEtest.goNULLBYTE+10NULLBYTEEOF
Expand Down

0 comments on commit f24dba0

Please sign in to comment.