Skip to content

Commit

Permalink
Ignore notFound error when collect from pid cgroup
Browse files Browse the repository at this point in the history
When `pids.current` or `pids.max` not exist in cgroup root
(/sys/fs/cgroup/pids/) collect_metrics will print out error level
log "...unable to read a control group file /sys/fs/cgroup/pids/./
pids.current..." continually, this commit will ignore these not
found errors.

As a supplement,if or not set pids.current file in cgroup controller
root directory decided by `CFTYPE_NOT_ON_ROOT` flag, in many commen
environment it is default to enabled. And as a contrast, go version
code will ignore all not found error when collect cgroupv1 metrics:
https://github.com/containerd/containerd/blob/b67a788072abd9671804b1ef1a719e3742867d1c/runtime/v2/runc/task/service.go#L625

Co-authored-by: Yukiteru <[email protected]>

Signed-off-by: Lei Liu <[email protected]>
  • Loading branch information
zhaodiaoer authored and mxpv committed Nov 5, 2024
1 parent 7c01662 commit 2689bbf
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions crates/shim/src/cgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
#![cfg(target_os = "linux")]

use std::{
error::Error as StdError,
fs,
io::Read,
path::{Path, PathBuf},
};

use cgroups_rs::{
cgroup::get_cgroups_relative_paths_by_pid, hierarchies, Cgroup, CgroupPid, MaxValue, Subsystem,
cgroup::get_cgroups_relative_paths_by_pid, error::Result as CgResult, hierarchies, Cgroup,
CgroupPid, MaxValue, Subsystem,
};
use containerd_shim_protos::{
cgroups::metrics::{CPUStat, CPUUsage, MemoryEntry, MemoryStat, Metrics, PidsStat, Throttle},
Expand Down Expand Up @@ -160,21 +162,34 @@ pub fn collect_metrics(pid: u32) -> Result<Metrics> {
metrics.set_memory(mem_stat);
}
Subsystem::Pid(pid_ctr) => {
// ignore cgroup NotFound error
let ignore_err = |cr: CgResult<u64>| -> CgResult<u64> {
cr.or_else(|e| {
if e.source()
.and_then(<dyn StdError>::downcast_ref::<std::io::Error>)
.map(std::io::Error::kind)
== Some(std::io::ErrorKind::NotFound)
{
Ok(0)
} else {
Err(e)
}
})
};

let mut pid_stats = PidsStat::new();
pid_stats.set_current(
pid_ctr
.get_pid_current()
ignore_err(pid_ctr.get_pid_current())
.map_err(other_error!("get current pid"))?,
);

pid_stats.set_limit(
pid_ctr
.get_pid_max()
.map(|val| match val {
// See https://github.com/opencontainers/runc/blob/dbe8434359ca35af1c1e10df42b1f4391c1e1010/libcontainer/cgroups/fs/pids.go#L55
cgroups_rs::MaxValue::Max => 0,
cgroups_rs::MaxValue::Value(val) => val as u64,
})
.map_err(other_error!("get pid limit"))?,
ignore_err(pid_ctr.get_pid_max().map(|val| match val {
// See https://github.com/opencontainers/runc/blob/dbe8434359ca35af1c1e10df42b1f4391c1e1010/libcontainer/cgroups/fs/pids.go#L55
cgroups_rs::MaxValue::Max => 0,
cgroups_rs::MaxValue::Value(val) => val as u64,
}))
.map_err(other_error!("get pid limit"))?,
);
metrics.set_pids(pid_stats)
}
Expand Down

0 comments on commit 2689bbf

Please sign in to comment.