Skip to content

Commit 42cb21b

Browse files
committed
cpuset: Fix v2 effective_cpus/mems file names
The cpuset controller was using cgroup v1 file names for effective CPUs and memory nodes on cgroup v2 systems. The file names changed between versions: v1: cpuset.effective_cpus, cpuset.effective_mems v2: cpuset.cpus.effective, cpuset.mems.effective This caused cpuset().effective_cpus to return empty on v2 systems since the files don't exist. The controller already tracks the cgroup version via the v2 flag, so use it to select the correct file names. Fixes: #156 Signed-off-by: Jose Fernandez <[email protected]>
1 parent 69e3897 commit 42cb21b

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/fs/cpuset.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,23 @@ impl CpuSetController {
279279
.unwrap_or_default()
280280
},
281281
effective_cpus: {
282-
self.open_path("cpuset.effective_cpus", false)
282+
let file = if self.v2 {
283+
"cpuset.cpus.effective"
284+
} else {
285+
"cpuset.effective_cpus"
286+
};
287+
self.open_path(file, false)
283288
.and_then(read_string_from)
284289
.and_then(parse_range)
285290
.unwrap_or_default()
286291
},
287292
effective_mems: {
288-
self.open_path("cpuset.effective_mems", false)
293+
let file = if self.v2 {
294+
"cpuset.mems.effective"
295+
} else {
296+
"cpuset.effective_mems"
297+
};
298+
self.open_path(file, false)
289299
.and_then(read_string_from)
290300
.and_then(parse_range)
291301
.unwrap_or_default()

tests/cpuset.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,33 @@ fn test_cpuset_set_cpus_add_task() {
9090

9191
cg.delete().unwrap();
9292
}
93+
94+
#[test]
95+
fn test_cpuset_effective_cpus() {
96+
let h = cgroups_rs::fs::hierarchies::auto();
97+
let cg = Cgroup::new(h, String::from("test_cpuset_effective_cpus")).unwrap();
98+
{
99+
let cpuset: &CpuSetController = cg.controller_of().unwrap();
100+
101+
// Set cpus to 0
102+
let r = cpuset.set_cpus("0");
103+
assert!(r.is_ok());
104+
105+
// Read back and verify effective_cpus is populated
106+
let set = cpuset.cpuset();
107+
108+
if cg.v2() {
109+
// On v2, effective_cpus should be populated after setting cpus
110+
// This tests that we're reading the correct file: cpuset.cpus.effective
111+
assert!(!set.effective_cpus.is_empty(),
112+
"effective_cpus should not be empty on cgroup v2 after setting cpus");
113+
assert_eq!((0, 0), set.effective_cpus[0]);
114+
} else {
115+
// On v1, effective_cpus should also work
116+
// This tests that we're reading the correct file: cpuset.effective_cpus
117+
assert!(!set.effective_cpus.is_empty(),
118+
"effective_cpus should not be empty on cgroup v1");
119+
}
120+
}
121+
cg.delete().unwrap();
122+
}

0 commit comments

Comments
 (0)