From 3f46e6379dcef9217386c50989bef5d36c6fd4e9 Mon Sep 17 00:00:00 2001 From: "fupan.lfp" Date: Tue, 23 Mar 2021 14:35:47 +0800 Subject: [PATCH] cgroups: fix the issue of getting wrong online cpus It's better to get the online cpus from "/sys/devices/system/cpu/online" instead of from cpuset cgroup, cause there would be an latency between one cpu online and present in the root cpuset cgroup. Fixes: #1536 Signed-off-by: fupan.lfp --- src/agent/rustjail/src/cgroups/fs/mod.rs | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/agent/rustjail/src/cgroups/fs/mod.rs b/src/agent/rustjail/src/cgroups/fs/mod.rs index d339289489..cc2e5dcffb 100644 --- a/src/agent/rustjail/src/cgroups/fs/mod.rs +++ b/src/agent/rustjail/src/cgroups/fs/mod.rs @@ -37,6 +37,8 @@ use std::collections::HashMap; use std::fs; use std::path::Path; +const GUEST_CPUS_PATH: &str = "/sys/devices/system/cpu/online"; + // Convenience macro to obtain the scope logger macro_rules! sl { () => { @@ -1027,23 +1029,10 @@ impl Manager { } } +// get the guest's online cpus. pub fn get_guest_cpuset() -> Result { - // for cgroup v2 - if cgroups::hierarchies::is_cgroup2_unified_mode() { - let c = fs::read_to_string("/sys/fs/cgroup/cpuset.cpus.effective")?; - return Ok(c); - } - - // for cgroup v1 - let m = get_mounts()?; - if m.get("cpuset").is_none() { - warn!(sl!(), "no cpuset cgroup!"); - return Err(nix::Error::Sys(Errno::ENOENT).into()); - } - - let p = format!("{}/cpuset.cpus", m.get("cpuset").unwrap()); - let c = fs::read_to_string(p.as_str())?; - Ok(c) + let c = fs::read_to_string(GUEST_CPUS_PATH)?; + Ok(c.trim().to_string()) } // Since the OCI spec is designed for cgroup v1, in some cases