CPU pool: read the tightest cgroup CPU quota up to the mount's root

The container CPU quota that caps the default worker count was read from
the root of the cgroup mount only. That is the process's own group when the
container has its own cgroup namespace, but not for a systemd service or
scope, an older runtime, or a limit set on a parent group: there the mount
shows the host's tree and the limit sits in the group or above it.

container_cpu_quota() now reads /proc/self/cgroup and takes the smallest
quota from the process's group up to the root, for cgroup v2 (cpu.max) and
v1 (cpu.cfs_quota_us / cpu.cfs_period_us), as the memory guard already does.
A mount that is the container's own group reads as before.

cgroup_cpu_test runs it against a fake cgroup tree: own namespace, a limit
on a parent, a tighter and a looser group limit, a missing path, v1, and a
hybrid system.
This commit is contained in:
Niko1221 committed 2026-11-13 17:21:27 +01:00
1 parent 86203d19a6
commit 9e52144fc9
4 files changed
+226 -18

No files matched your search

+5
View File
@@ -457,6 +457,11 @@ if(STRATA_BUILD_TESTS)
target_include_directories(numa_pool_test PRIVATE include)
target_compile_features(numa_pool_test PRIVATE cxx_std_17)
add_test(NAME numa_pool_test COMMAND numa_pool_test)
# the CPU quota a container or a systemd unit sets, read from a fake cgroup mount (own namespace, a parent's limit, cgroup v1, hybrid)
add_executable(cgroup_cpu_test src/kernels/cpu/cgroup_cpu_test.cpp)
target_include_directories(cgroup_cpu_test PRIVATE include)
target_compile_features(cgroup_cpu_test PRIVATE cxx_std_17)
add_test(NAME cgroup_cpu_test COMMAND cgroup_cpu_test)
# #3204: when the HIP start-up warm-up of hipBLAS may run (pure policy, no GPU)
add_executable(warm_policy_test src/prefill/warm_policy_test.cpp)
target_include_directories(warm_policy_test PRIVATE include)
+118
View File
@@ -0,0 +1,118 @@
// include/strata/kernels/cpu/cgroup_cpu.hpp - the CPU time a process may use under cgroup limits (Docker --cpus, a Kubernetes or RunPod
// limit, a systemd CPUQuota=), as a number of CPUs. Header only and independent of the real /sys: the caller passes the cgroup mount
// and the text of /proc/self/cgroup, so a test can build both in a temp directory (cgroup_cpu_test.cpp).
//
// Why not just read <mount>/cpu.max: that file is the process's own group only when the container has its own cgroup namespace (the
// Docker default on a cgroup v2 host). Without one - the host's namespace, a systemd service or scope, an older runtime - the mount
// shows the host's tree and the limit sits in the process's group or in a parent of it, so the root of the mount says nothing. The
// limit that applies is the smallest quota on the way from the process's group up to the root, as for memory (conversation_memory.cpp).
#pragma once
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
namespace strata::kernels::cpu {
namespace cgroup_cpu_detail {
/// "a/b/c" -> {"a","b","c"}; empty parts dropped. False for a path with "..", which no cgroup path has.
inline bool split_path(const std::string& path, std::vector<std::string>& out) {
out.clear();
std::string part;
std::istringstream in(path);
while (std::getline(in, part, '/')) {
if (part.empty() || part == ".") continue;
if (part == "..") return false;
out.push_back(part);
}
return true;
}
inline bool read_line(const std::string& file, std::string& line) {
std::ifstream f(file);
if (!f) return false;
std::getline(f, line);
return true;
}
/// cgroup v2 cpu.max in `dir`: "max 100000" (no limit) or "<quota> <period>" -> CPUs, 0 for no limit or unreadable.
inline double v2_quota(const std::string& dir) {
std::string line;
if (!read_line(dir + "/cpu.max", line)) return 0.0;
std::istringstream in(line);
std::string quota;
long long period = 0;
if (!(in >> quota >> period) || quota == "max" || period <= 0) return 0.0;
char* end = nullptr;
const double q = std::strtod(quota.c_str(), &end);
return end != quota.c_str() && q > 0 ? q / (double) period : 0.0;
}
/// cgroup v1 cpu.cfs_quota_us / cpu.cfs_period_us in `dir`: -1 means no limit.
inline double v1_quota(const std::string& dir) {
std::string a, b;
if (!read_line(dir + "/cpu.cfs_quota_us", a) || !read_line(dir + "/cpu.cfs_period_us", b)) return 0.0;
const long long quota = std::atoll(a.c_str()), period = std::atoll(b.c_str());
return quota > 0 && period > 0 ? (double) quota / (double) period : 0.0;
}
/// The smallest limit in `base`, `base/p1`, `base/p1/p2`, ... `base/p1/.../pn`, read with `one`; 0 when none has one.
template <class Reader>
double tightest_on_the_way(const std::string& base, const std::vector<std::string>& parts, Reader one) {
double best = 0.0;
std::string dir = base;
for (size_t i = 0;; ++i) {
const double q = one(dir);
if (q > 0.0 && (best <= 0.0 || q < best)) best = q;
if (i >= parts.size()) break;
dir += "/" + parts[i];
}
return best;
}
} // namespace cgroup_cpu_detail
/// CPUs the cgroup limits allow this process: 0 when no limit applies. `cgroup_root` is where the cgroup files are mounted (/sys/fs/cgroup),
/// `self_cgroup` the text of /proc/self/cgroup ("0::/path" for cgroup v2, "N:cpu,cpuacct:/path" for v1; both kinds of line may be there).
/// The group's own path is read under the mount and so is every parent of it, down to the mount's root (which is the container's own group
/// when it has a cgroup namespace of its own, and where the old reading stopped). A path that is not there is skipped.
inline double cgroup_cpu_quota_at(const std::string& cgroup_root, const std::string& self_cgroup) {
using namespace cgroup_cpu_detail;
double best = 0.0;
auto take = [&best](double q) { if (q > 0.0 && (best <= 0.0 || q < best)) best = q; };
bool any_line = false;
std::istringstream in(self_cgroup);
std::string line;
while (std::getline(in, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
const size_t c1 = line.find(':');
const size_t c2 = c1 == std::string::npos ? std::string::npos : line.find(':', c1 + 1);
if (c2 == std::string::npos) continue;
const std::string controllers = line.substr(c1 + 1, c2 - c1 - 1);
std::vector<std::string> parts;
if (!split_path(line.substr(c2 + 1), parts)) continue;
any_line = true;
if (controllers.empty()) { // cgroup v2 (the unified hierarchy)
take(tightest_on_the_way(cgroup_root, parts, v2_quota));
} else { // cgroup v1: the line that lists the cpu controller
std::istringstream names(controllers);
std::string name;
bool has_cpu = false;
while (std::getline(names, name, ',')) has_cpu = has_cpu || name == "cpu";
if (!has_cpu) continue;
for (const char* mount : {"/cpu", "/cpu,cpuacct", "/cpuacct,cpu"})
take(tightest_on_the_way(cgroup_root + mount, parts, v1_quota));
}
}
if (!any_line) { // no /proc/self/cgroup: the mount's own root, v2 then v1 (what the engine read before)
take(v2_quota(cgroup_root));
take(v1_quota(cgroup_root + "/cpu"));
}
return best;
}
} // namespace strata::kernels::cpu
+95
View File
@@ -0,0 +1,95 @@
// 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
// fake cgroup mount in a temp directory and a made-up /proc/self/cgroup. No threads, no real cgroups: it runs anywhere.
#include "strata/kernels/cpu/cgroup_cpu.hpp"
#include <cmath>
#include <cstdio>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <string>
namespace cpu = strata::kernels::cpu;
namespace fs = std::filesystem;
static int g_fail = 0;
static void expect(bool ok, const std::string& what) {
if (!ok) {
++g_fail;
std::fprintf(stderr, "FAIL: %s\n", what.c_str());
}
}
static bool near(double a, double b) { return std::fabs(a - b) < 1e-9; }
static void put(const fs::path& file, const std::string& text) {
fs::create_directories(file.parent_path());
std::ofstream(file) << text;
}
int main() {
const fs::path root = fs::temp_directory_path() / ("strata_cgroup_cpu_test_" + std::to_string((long long) std::time(nullptr)));
fs::remove_all(root);
const std::string r = root.string();
// 1. a container with its own cgroup namespace (RunPod, Docker on a cgroup v2 host): the group is the mount's root
put(root / "ns" / "cpu.max", "1360000 100000\n");
expect(near(cpu::cgroup_cpu_quota_at((root / "ns").string(), "0::/\n"), 13.6), "own namespace, v2: 13.6 CPUs");
// 2. no limit: "max"
put(root / "nolimit" / "cpu.max", "max 100000\n");
expect(near(cpu::cgroup_cpu_quota_at((root / "nolimit").string(), "0::/\n"), 0.0), "max: no limit");
// 3. nothing there, garbage, a zero period
expect(near(cpu::cgroup_cpu_quota_at((root / "empty").string(), "0::/\n"), 0.0), "no files: no limit");
put(root / "junk" / "cpu.max", "banana\n");
expect(near(cpu::cgroup_cpu_quota_at((root / "junk").string(), "0::/\n"), 0.0), "garbage: no limit");
put(root / "zero" / "cpu.max", "100000 0\n");
expect(near(cpu::cgroup_cpu_quota_at((root / "zero").string(), "0::/\n"), 0.0), "period 0: no limit");
// 4. the host's namespace: the limit is on a parent of the process's group, the group itself and the root say nothing
const fs::path host = root / "host";
put(host / "cpu.max", "max 100000\n"); // (the real root cgroup has no such file; "max" must not matter either way)
put(host / "system.slice" / "cpu.max", "400000 100000\n");
put(host / "system.slice" / "docker-abc.scope" / "cpu.max", "max 100000\n");
expect(near(cpu::cgroup_cpu_quota_at(host.string(), "0::/system.slice/docker-abc.scope\n"), 4.0), "limit on a parent: 4 CPUs");
// 5. the group's own limit tighter than the parent's, and looser
put(host / "system.slice" / "docker-def.scope" / "cpu.max", "150000 100000\n");
expect(near(cpu::cgroup_cpu_quota_at(host.string(), "0::/system.slice/docker-def.scope\n"), 1.5), "own limit tighter: 1.5 CPUs");
put(host / "system.slice" / "docker-ghi.scope" / "cpu.max", "800000 100000\n");
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");
// 6. a path the mount does not have: the root's own file still counts (what the engine read before)
put(root / "gone" / "cpu.max", "200000 100000\n");
expect(near(cpu::cgroup_cpu_quota_at((root / "gone").string(), "0::/not/here\n"), 2.0), "missing path: the root's limit");
// 7. CRLF, a ".." path, no /proc/self/cgroup at all
expect(near(cpu::cgroup_cpu_quota_at((root / "gone").string(), "0::/not/here\r\n"), 2.0), "CRLF line");
expect(near(cpu::cgroup_cpu_quota_at(host.string(), "0::/../../etc\n"), 0.0), "a path with .. is not followed");
expect(near(cpu::cgroup_cpu_quota_at((root / "ns").string(), ""), 13.6), "no /proc/self/cgroup: the root's file");
// 8. cgroup v1: Docker's group under the cpu controller, the quota on the group or on a parent; -1 is no limit
const fs::path v1 = root / "v1";
put(v1 / "cpu,cpuacct" / "docker" / "abc" / "cpu.cfs_quota_us", "-1\n");
put(v1 / "cpu,cpuacct" / "docker" / "abc" / "cpu.cfs_period_us", "100000\n");
put(v1 / "cpu,cpuacct" / "docker" / "cpu.cfs_quota_us", "300000\n");
put(v1 / "cpu,cpuacct" / "docker" / "cpu.cfs_period_us", "100000\n");
put(v1 / "cpu,cpuacct" / "cpu.cfs_quota_us", "-1\n");
put(v1 / "cpu,cpuacct" / "cpu.cfs_period_us", "100000\n");
const std::string v1_self = "12:memory:/docker/abc\n4:cpu,cpuacct:/docker/abc\n1:name=systemd:/docker/abc\n";
expect(near(cpu::cgroup_cpu_quota_at(v1.string(), v1_self), 3.0), "v1: a parent's 3 CPUs, the group's -1 ignored");
put(v1 / "cpu,cpuacct" / "docker" / "abc" / "cpu.cfs_quota_us", "50000\n");
expect(near(cpu::cgroup_cpu_quota_at(v1.string(), v1_self), 0.5), "v1: the group's own half CPU");
// 9. v1 with a line that does not list the cpu controller: not read
expect(near(cpu::cgroup_cpu_quota_at(v1.string(), "12:memory:/docker/abc\n"), 0.0), "v1: no cpu controller line");
// 10. both kinds of line (a hybrid system): the smaller limit wins
put(root / "hyb" / "cpu.max", "100000 100000\n");
put(root / "hyb" / "cpu" / "x" / "cpu.cfs_quota_us", "600000\n");
put(root / "hyb" / "cpu" / "x" / "cpu.cfs_period_us", "100000\n");
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");
// 11. the real thing runs and gives a sane number (a machine without cgroups gives 0)
expect(cpu::cgroup_cpu_quota_at("/sys/fs/cgroup", "") >= 0.0, "the real mount reads without trouble");
fs::remove_all(root);
if (g_fail) {
std::fprintf(stderr, "cgroup_cpu_test: %d failure(s)\n", g_fail);
return 1;
}
std::printf("cgroup_cpu_test: OK\n");
return 0;
}
+8 -18
View File
@@ -4,6 +4,7 @@
#include "strata/platform/aux_cpus.hpp"
#include "strata/kernels/cpu/expert_layout.hpp"
#include "strata/kernels/cpu/numa_pool.hpp"
#include "strata/kernels/cpu/cgroup_cpu.hpp"
#include <algorithm>
#include <atomic>
@@ -214,26 +215,15 @@ CpuTopology detect_cpu_topology(bool skip_first, PoolAffinity affinity) {
double container_cpu_quota() {
#if defined(__linux__)
// cgroup v2: "max 100000" (no limit) or "<quota> <period>"; v1: cpu.cfs_quota_us (-1: none) / cpu.cfs_period_us
if (std::FILE* f = std::fopen("/sys/fs/cgroup/cpu.max", "r")) {
char a[32] = {0};
long period = 0;
double q = 0;
if (std::fscanf(f, "%31s %ld", a, &period) == 2 && std::strcmp(a, "max") != 0 && period > 0)
q = std::atof(a) / (double) period;
std::fclose(f);
return q > 0 ? q : 0.0;
}
long quota = -1, period = 0;
if (std::FILE* f = std::fopen("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r")) {
if (std::fscanf(f, "%ld", &quota) != 1) quota = -1;
// the smallest cgroup CPU quota from this process's group up to the mount's root (cgroup v2 cpu.max, v1 cfs quota); see cgroup_cpu.hpp
std::string self;
if (std::FILE* f = std::fopen("/proc/self/cgroup", "r")) {
char buf[512];
size_t n;
while ((n = std::fread(buf, 1, sizeof buf, f)) > 0) self.append(buf, n);
std::fclose(f);
}
if (std::FILE* f = std::fopen("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "r")) {
if (std::fscanf(f, "%ld", &period) != 1) period = 0;
std::fclose(f);
}
return quota > 0 && period > 0 ? (double) quota / (double) period : 0.0;
return cgroup_cpu_quota_at("/sys/fs/cgroup", self);
#else
return 0.0;
#endif