okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-container-cgroup 9e52144fc into the batch 18 candidate (CPU pool: read the tightest cgroup CPU quota up to the mount's root)fb8d3dbfa

main
5.7 KiB95 linesraw
1// cgroup_cpu_test: the CPU quota a container or a systemd unit puts on the process (include/strata/kernels/cpu/cgroup_cpu.hpp), read from a
2// fake cgroup mount in a temp directory and a made-up /proc/self/cgroup.  No threads, no real cgroups: it runs anywhere.
3#include "strata/kernels/cpu/cgroup_cpu.hpp"
4
5#include <cmath>
6#include <cstdio>
7#include <ctime>
8#include <filesystem>
9#include <fstream>
10#include <string>
11
12namespace cpu = strata::kernels::cpu;
13namespace fs = std::filesystem;
14
15static int g_fail = 0;
16
17static void expect(bool ok, const std::string& what) {
18    if (!ok) {
19        ++g_fail;
20        std::fprintf(stderr, "FAIL: %s\n", what.c_str());
21    }
22}
23
24static bool near(double a, double b) { return std::fabs(a - b) < 1e-9; }
25
26static void put(const fs::path& file, const std::string& text) {
27    fs::create_directories(file.parent_path());
28    std::ofstream(file) << text;
29}
30
31int main() {
32    const fs::path root = fs::temp_directory_path() / ("strata_cgroup_cpu_test_" + std::to_string((long long) std::time(nullptr)));
33    fs::remove_all(root);
34    const std::string r = root.string();
35
36    // 1. a container with its own cgroup namespace (RunPod, Docker on a cgroup v2 host): the group is the mount's root
37    put(root / "ns" / "cpu.max", "1360000 100000\n");
38    expect(near(cpu::cgroup_cpu_quota_at((root / "ns").string(), "0::/\n"), 13.6), "own namespace, v2: 13.6 CPUs");
39    // 2. no limit: "max"
40    put(root / "nolimit" / "cpu.max", "max 100000\n");
41    expect(near(cpu::cgroup_cpu_quota_at((root / "nolimit").string(), "0::/\n"), 0.0), "max: no limit");
42    // 3. nothing there, garbage, a zero period
43    expect(near(cpu::cgroup_cpu_quota_at((root / "empty").string(), "0::/\n"), 0.0), "no files: no limit");
44    put(root / "junk" / "cpu.max", "banana\n");
45    expect(near(cpu::cgroup_cpu_quota_at((root / "junk").string(), "0::/\n"), 0.0), "garbage: no limit");
46    put(root / "zero" / "cpu.max", "100000 0\n");
47    expect(near(cpu::cgroup_cpu_quota_at((root / "zero").string(), "0::/\n"), 0.0), "period 0: no limit");
48    // 4. the host's namespace: the limit is on a parent of the process's group, the group itself and the root say nothing
49    const fs::path host = root / "host";
50    put(host / "cpu.max", "max 100000\n");   // (the real root cgroup has no such file; "max" must not matter either way)
51    put(host / "system.slice" / "cpu.max", "400000 100000\n");
52    put(host / "system.slice" / "docker-abc.scope" / "cpu.max", "max 100000\n");
53    expect(near(cpu::cgroup_cpu_quota_at(host.string(), "0::/system.slice/docker-abc.scope\n"), 4.0), "limit on a parent: 4 CPUs");
54    // 5. the group's own limit tighter than the parent's, and looser
55    put(host / "system.slice" / "docker-def.scope" / "cpu.max", "150000 100000\n");
56    expect(near(cpu::cgroup_cpu_quota_at(host.string(), "0::/system.slice/docker-def.scope\n"), 1.5), "own limit tighter: 1.5 CPUs");
57    put(host / "system.slice" / "docker-ghi.scope" / "cpu.max", "800000 100000\n");
58    expect(near(cpu::cgroup_cpu_quota_at(host.string(), "0::/system.slice/docker-ghi.scope\n"), 4.0), "own limit looser: the parent's 4 CPUs");
59    // 6. a path the mount does not have: the root's own file still counts (what the engine read before)
60    put(root / "gone" / "cpu.max", "200000 100000\n");
61    expect(near(cpu::cgroup_cpu_quota_at((root / "gone").string(), "0::/not/here\n"), 2.0), "missing path: the root's limit");
62    // 7. CRLF, a ".." path, no /proc/self/cgroup at all
63    expect(near(cpu::cgroup_cpu_quota_at((root / "gone").string(), "0::/not/here\r\n"), 2.0), "CRLF line");
64    expect(near(cpu::cgroup_cpu_quota_at(host.string(), "0::/../../etc\n"), 0.0), "a path with .. is not followed");
65    expect(near(cpu::cgroup_cpu_quota_at((root / "ns").string(), ""), 13.6), "no /proc/self/cgroup: the root's file");
66    // 8. cgroup v1: Docker's group under the cpu controller, the quota on the group or on a parent; -1 is no limit
67    const fs::path v1 = root / "v1";
68    put(v1 / "cpu,cpuacct" / "docker" / "abc" / "cpu.cfs_quota_us", "-1\n");
69    put(v1 / "cpu,cpuacct" / "docker" / "abc" / "cpu.cfs_period_us", "100000\n");
70    put(v1 / "cpu,cpuacct" / "docker" / "cpu.cfs_quota_us", "300000\n");
71    put(v1 / "cpu,cpuacct" / "docker" / "cpu.cfs_period_us", "100000\n");
72    put(v1 / "cpu,cpuacct" / "cpu.cfs_quota_us", "-1\n");
73    put(v1 / "cpu,cpuacct" / "cpu.cfs_period_us", "100000\n");
74    const std::string v1_self = "12:memory:/docker/abc\n4:cpu,cpuacct:/docker/abc\n1:name=systemd:/docker/abc\n";
75    expect(near(cpu::cgroup_cpu_quota_at(v1.string(), v1_self), 3.0), "v1: a parent's 3 CPUs, the group's -1 ignored");
76    put(v1 / "cpu,cpuacct" / "docker" / "abc" / "cpu.cfs_quota_us", "50000\n");
77    expect(near(cpu::cgroup_cpu_quota_at(v1.string(), v1_self), 0.5), "v1: the group's own half CPU");
78    // 9. v1 with a line that does not list the cpu controller: not read
79    expect(near(cpu::cgroup_cpu_quota_at(v1.string(), "12:memory:/docker/abc\n"), 0.0), "v1: no cpu controller line");
80    // 10. both kinds of line (a hybrid system): the smaller limit wins
81    put(root / "hyb" / "cpu.max", "100000 100000\n");
82    put(root / "hyb" / "cpu" / "x" / "cpu.cfs_quota_us", "600000\n");
83    put(root / "hyb" / "cpu" / "x" / "cpu.cfs_period_us", "100000\n");
84    expect(near(cpu::cgroup_cpu_quota_at((root / "hyb").string(), "4:cpu:/x\n0::/\n"), 1.0), "hybrid: the smaller of 1 and 6 CPUs");
85    // 11. the real thing runs and gives a sane number (a machine without cgroups gives 0)
86    expect(cpu::cgroup_cpu_quota_at("/sys/fs/cgroup", "") >= 0.0, "the real mount reads without trouble");
87
88    fs::remove_all(root);
89    if (g_fail) {
90        std::fprintf(stderr, "cgroup_cpu_test: %d failure(s)\n", g_fail);
91        return 1;
92    }
93    std::printf("cgroup_cpu_test: OK\n");
94    return 0;
95}