mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 15:58:53 +00:00
test: guard the cgroup-quota and affinity core count (PR #14 regression)
This commit is contained in:
1 parent
58ee322772
commit
ff3d1e645d
4 files changed
+274
No files matched your search
@@ -475,6 +475,11 @@ if(STRATA_BUILD_TESTS)
|
||||
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)
|
||||
# the PR#14 composition: a container quota read from a fake cgroup mount, fed straight into the default worker-count plan
|
||||
add_executable(cgroup_quota_plan_test src/kernels/cpu/cgroup_quota_plan_test.cpp)
|
||||
target_include_directories(cgroup_quota_plan_test PRIVATE include)
|
||||
target_compile_features(cgroup_quota_plan_test PRIVATE cxx_std_17)
|
||||
add_test(NAME cgroup_quota_plan_test COMMAND cgroup_quota_plan_test)
|
||||
# when the engine restricts itself to the graphics card's NUMA node (mock machines, a fake sysfs tree, a two-thread restriction)
|
||||
add_executable(card_node_test src/kernels/cpu/card_node_test.cpp)
|
||||
target_include_directories(card_node_test PRIVATE include)
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
// cgroup_quota_plan_test: the PR#14 regression composition. The container CPU quota read from a FAKE cgroup mount
|
||||
// (cgroup_cpu.hpp, path-injectable, the layouts already pinned by cgroup_cpu_test.cpp) wired straight into the
|
||||
// default worker-count plan (pool_plan.hpp) - the same two seams pool.cpp joins in fill_plan_fields /
|
||||
// pool_plan_input. No threads, no real cgroups, no root: it runs anywhere.
|
||||
//
|
||||
// What it guards:
|
||||
// * the quota-bounded worker count is the plan's, and the plan's only: 13.6 CPUs on a 64-core machine gives 12
|
||||
// workers, never the 32 of the cap, the 64 of the cores or the 128 of the logical processors;
|
||||
// * a quota that cannot be read ("max", garbage, no files, no cpu line) reads 0.0 and leaves the count EXACTLY at
|
||||
// the pre-cgroup value - the required failure path when cgroup values are unavailable: no silent collapse to 1;
|
||||
// * the parser-to-plan wiring cannot drift: every clamped row stays strictly below the unclamped baseline,
|
||||
// never below 1, and q == n does not clamp (the comparison is strict).
|
||||
#include "strata/kernels/cpu/cgroup_cpu.hpp"
|
||||
#include "strata/kernels/cpu/pool_plan.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static cpu::PoolPlanInput cpu_of(int logical, std::vector<int> per_package) {
|
||||
cpu::PoolPlanInput in;
|
||||
in.logical = logical;
|
||||
in.cores_per_package = std::move(per_package);
|
||||
return in;
|
||||
}
|
||||
|
||||
// One composition row: read the quota exactly the way container_cpu_quota() will (cgroup_cpu_test.cpp pins these
|
||||
// readings), then feed it to the plan the engine feeds it. `want_quota` is the pin from the parser test - if the
|
||||
// parser ever stops agreeing with it, this names the row that broke before the plan assertion does.
|
||||
static void row(const std::string& name, const std::string& mount, const std::string& self_cgroup,
|
||||
double want_quota, const cpu::PoolPlanInput& base, int want_workers, const char* want_limit) {
|
||||
const double q = cpu::cgroup_cpu_quota_at(mount, self_cgroup);
|
||||
expect(near(q, want_quota), name + ": parser gives the pinned quota");
|
||||
cpu::PoolPlanInput in = base;
|
||||
in.cpu_quota = q;
|
||||
const cpu::PoolPlan p = cpu::plan_pool_workers(in);
|
||||
expect(p.workers == want_workers, name + ": plan -> " + std::to_string(want_workers) + " workers, got " +
|
||||
std::to_string(p.workers));
|
||||
if (want_limit != nullptr)
|
||||
expect(std::string(p.limit) == want_limit, name + ": limit is \"" + want_limit + "\", got \"" +
|
||||
std::string(p.limit) + "\"");
|
||||
const int unclamped = cpu::plan_pool_workers(base).workers;
|
||||
expect(p.workers <= unclamped, name + ": a quota never RAISES the count");
|
||||
expect(p.workers >= 1, name + ": never 0 workers");
|
||||
std::printf(" %-52s quota %6.2f unclamped %2d -> %2d workers [%s]\n", name.c_str(), q, unclamped, p.workers,
|
||||
p.limit);
|
||||
}
|
||||
|
||||
// The "cgroup values unavailable" rows: every shape that yields no limit must leave the count EXACTLY at the
|
||||
// pre-cgroup value (8c/16t -> 7). A silent collapse to 1 worker, a crash, or a cap-sized read all fail here.
|
||||
static void no_limit_row(const std::string& name, const std::string& mount, const std::string& self_cgroup) {
|
||||
const double q = cpu::cgroup_cpu_quota_at(mount, self_cgroup);
|
||||
expect(near(q, 0.0), name + ": unreadable quota reads 0.0, not a limit");
|
||||
cpu::PoolPlanInput in = cpu_of(16, {8});
|
||||
in.cpu_quota = q;
|
||||
const cpu::PoolPlan p = cpu::plan_pool_workers(in);
|
||||
expect(p.workers == 7, name + ": count stays 7 (no silent collapse), got " + std::to_string(p.workers));
|
||||
expect(std::string(p.limit) != "the container's CPU quota", name + ": an absent limit is not claimed");
|
||||
std::printf(" %-52s quota %6.2f -> %2d workers [%s]\n", name.c_str(), q, p.workers, p.limit);
|
||||
}
|
||||
|
||||
int main() {
|
||||
const fs::path root = fs::temp_directory_path() /
|
||||
("strata_cgroup_quota_plan_test_" + std::to_string((long long) std::time(nullptr)));
|
||||
fs::remove_all(root);
|
||||
const std::string r = root.string();
|
||||
std::printf("cgroup_quota_plan_test: quota -> plan composition (PR#14 regression)\n");
|
||||
|
||||
// 1. RunPod / Docker with its own cgroup namespace: 13.6 CPUs on a 64-core box -> 12 workers, far below the
|
||||
// unclamped 32, the cores 64 and the logical 128 (the container report PR#14 came out of)
|
||||
put(root / "ns" / "cpu.max", "1360000 100000\n");
|
||||
row("v2 own namespace 13.6 CPUs on 64c/128t", (root / "ns").string(), "0::/\n", 13.6, cpu_of(128, {64}), 12,
|
||||
"the container's CPU quota");
|
||||
|
||||
// 2. the host's namespace: the limit sits on a PARENT of the process's group (systemd scope) - 4 CPUs on 8c -> 3
|
||||
put(root / "host" / "cpu.max", "max 100000\n");
|
||||
put(root / "host" / "system.slice" / "cpu.max", "400000 100000\n");
|
||||
put(root / "host" / "system.slice" / "docker-abc.scope" / "cpu.max", "max 100000\n");
|
||||
row("v2 parent scope 4.0 CPUs on 8c/16t", (root / "host").string(), "0::/system.slice/docker-abc.scope\n", 4.0,
|
||||
cpu_of(16, {8}), 3, "the container's CPU quota");
|
||||
|
||||
// 3. the group's own limit tighter than the parent's: 1.5 CPUs -> 1 worker (the floor, never 0)
|
||||
put(root / "host" / "system.slice" / "docker-def.scope" / "cpu.max", "150000 100000\n");
|
||||
row("v2 own 1.5 CPUs on 8c/16t", (root / "host").string(), "0::/system.slice/docker-def.scope\n", 1.5,
|
||||
cpu_of(16, {8}), 1, "the container's CPU quota");
|
||||
|
||||
// 4. a group path the mount does not have: the root's own file still counts - 2.0 CPUs -> 1
|
||||
put(root / "gone" / "cpu.max", "200000 100000\n");
|
||||
row("v2 missing path, root 2.0 CPUs on 8c/16t", (root / "gone").string(), "0::/not/here\n", 2.0,
|
||||
cpu_of(16, {8}), 1, "the container's CPU quota");
|
||||
|
||||
// 5-6. cgroup v1: a parent's 3 CPUs -> 2; the group's own half CPU -> 1 (the tree cgroup_cpu_test.cpp pins)
|
||||
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";
|
||||
row("v1 parent 3.0 CPUs on 8c/16t", v1.string(), v1_self, 3.0, cpu_of(16, {8}), 2, "the container's CPU quota");
|
||||
put(v1 / "cpu,cpuacct" / "docker" / "abc" / "cpu.cfs_quota_us", "50000\n");
|
||||
row("v1 own 0.5 CPUs on 8c/16t", v1.string(), v1_self, 0.5, cpu_of(16, {8}), 1, "the container's CPU quota");
|
||||
|
||||
// 7. both kinds of line (a hybrid system): the smaller limit wins - 1.0 CPU -> 1 worker
|
||||
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");
|
||||
row("v2+v1 hybrid, smaller 1.0 CPU wins on 8c/16t", (root / "hyb").string(), "4:cpu:/x\n0::/\n", 1.0,
|
||||
cpu_of(16, {8}), 1, "the container's CPU quota");
|
||||
|
||||
// 8. the failure path: cgroup values unavailable in every shape - "max", garbage, no files at all, no /proc/self
|
||||
// line, a v1 group with no cpu line. All must read 0.0 and leave the plan at exactly its pre-cgroup count.
|
||||
put(root / "nolimit" / "cpu.max", "max 100000\n");
|
||||
no_limit_row("unavailable: cpu.max 'max'", (root / "nolimit").string(), "0::/\n");
|
||||
put(root / "junk" / "cpu.max", "banana\n");
|
||||
no_limit_row("unavailable: cpu.max garbage", (root / "junk").string(), "0::/\n");
|
||||
no_limit_row("unavailable: no cgroup files", (root / "empty").string(), "0::/\n");
|
||||
no_limit_row("unavailable: no /proc/self/cgroup, root unlimited", (root / "nolimit").string(), "");
|
||||
no_limit_row("unavailable: no files and no /proc/self/cgroup", (root / "empty").string(), "");
|
||||
no_limit_row("unavailable: v1 without a cpu line", v1.string(), "12:memory:/docker/abc\n");
|
||||
|
||||
// 9. the q == n boundary: quota 8.0 on the 8c/16t box computes q = 8 - 1 = 7 == n; the comparison is STRICT, so
|
||||
// the count stays 7. A flipped <= would silently drop a worker - and a flipped formula for q would too.
|
||||
{
|
||||
cpu::PoolPlanInput in = cpu_of(16, {8});
|
||||
in.cpu_quota = 8.0;
|
||||
expect(cpu::plan_pool_workers(in).workers == 7, "q == n: quota 8.0 on 8c does not clamp (strict <)");
|
||||
in.cpu_quota = 7.999;
|
||||
expect(cpu::plan_pool_workers(in).workers == 6, "just under: quota 7.999 clamps to 6");
|
||||
}
|
||||
|
||||
// 10. drift guards on the clamp arithmetic itself, no OS at all: 3.999 truncates to 3 (no rounding), 4.5
|
||||
// truncates to 3, a huge quota changes nothing, quota never lifts the count above the affinity bound.
|
||||
{
|
||||
cpu::PoolPlanInput in = cpu_of(16, {8});
|
||||
in.cpu_quota = 3.999;
|
||||
expect(cpu::plan_pool_workers(in).workers == 2, "quota 3.999 truncates to q=2 -> 2 workers");
|
||||
in.cpu_quota = 4.5;
|
||||
expect(cpu::plan_pool_workers(in).workers == 3, "quota 4.5 truncates to q=3 -> 3 workers");
|
||||
in.cpu_quota = 200.0;
|
||||
expect(cpu::plan_pool_workers(in).workers == 7, "a quota above the cores changes nothing");
|
||||
cpu::PoolPlanInput few = cpu_of(4, {4});
|
||||
few.cpu_quota = 8.0;
|
||||
expect(cpu::plan_pool_workers(few).workers == 3, "4 CPUs allowed under an 8-CPU quota: the mask bounds, not the quota");
|
||||
}
|
||||
|
||||
fs::remove_all(root);
|
||||
if (g_fail) {
|
||||
std::fprintf(stderr, "cgroup_quota_plan_test: %d failure(s)\n", g_fail);
|
||||
return 1;
|
||||
}
|
||||
std::printf("cgroup_quota_plan_test: OK\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -250,6 +250,63 @@ static bool failed_pin_preserves_affinity() {
|
||||
return helper_unchanged && !invalid.valid && public_unchanged;
|
||||
}
|
||||
|
||||
static size_t mask_popcount(const std::vector<unsigned long>& words) {
|
||||
size_t bits = 0;
|
||||
for (unsigned long w : words)
|
||||
for (unsigned long x = w; x != 0; x &= x - 1) ++bits;
|
||||
return bits;
|
||||
}
|
||||
|
||||
// PR#14: the mask read once had an unbraced `else` that bound to the INNER `if (CPU_ISSET(...))`, so every CPU id
|
||||
// outside the mask re-appended all hardware_concurrency() ids: a k-of-CPU_SETSIZE mask returned
|
||||
// k + (CPU_SETSIZE - k) * hardware_concurrency() - 1 entries (issue #13: 16143 CPUs on a 16-thread box).
|
||||
// Guard: pin this process to the first few allowed CPUs and require the list read to equal the mask's popcount
|
||||
// exactly - the blow-up shape named, computed and rejected - and everything derived from it (physical_cores,
|
||||
// detect_cpu_topology) to stay inside the mask. No root, no cgroups, no quota: pure affinity.
|
||||
static bool dangling_else_guard(const std::vector<int>& allowed) {
|
||||
if (allowed.size() < 2) {
|
||||
std::puts("SKIP dangling-else guard: fewer than two allowed CPUs");
|
||||
return true;
|
||||
}
|
||||
const size_t k = (std::min)(size_t(4), allowed.size());
|
||||
const std::vector<int> restricted(allowed.begin(), allowed.begin() + k);
|
||||
if (!set_affinity(restricted)) return false;
|
||||
bool ok = true;
|
||||
std::vector<unsigned long> mask;
|
||||
std::vector<int> cpus;
|
||||
if (cpu::detail::get_thread_affinity(mask, &cpus) != 0) {
|
||||
std::fprintf(stderr, "dangling-else guard: get_thread_affinity failed on the restricted mask\n");
|
||||
ok = false;
|
||||
}
|
||||
if (ok) {
|
||||
const size_t bits = mask_popcount(mask);
|
||||
// the exact list AND the two size identities; a re-introduced dangling else fails all three at once
|
||||
ok = cpus == restricted && cpus.size() == bits && bits == k;
|
||||
// the old bug's shape, stated as the number it produced and refused as such (CPU_SETSIZE == 1024 on glibc)
|
||||
const long long blow_up = (long long) k + (long long) (CPU_SETSIZE - k) *
|
||||
(long long) std::thread::hardware_concurrency() - 1;
|
||||
if (!ok)
|
||||
std::fprintf(stderr,
|
||||
"dangling-else guard: mask read gave %zu CPUs, mask holds %lu bits, set wanted %zu (the dangling else gave %lld)\n",
|
||||
cpus.size(), (unsigned long) bits, k, blow_up);
|
||||
ok = ok && (long long) cpus.size() != blow_up;
|
||||
}
|
||||
if (ok) ok = topology_respects_current_mask();
|
||||
if (ok) {
|
||||
const auto topo = cpu::detect_cpu_topology(false);
|
||||
const auto cores = cpu::physical_cores(false);
|
||||
// logical processors seen == the mask's size; the worker-core list is a non-empty subset of it, never the
|
||||
// (CPU_SETSIZE - k) * h blow-up (physical_cores == topo.worker_cores, both checked)
|
||||
ok = topo.logical == (int) k && !cores.empty() && cores.size() <= k && topo.worker_cores.size() <= k;
|
||||
if (!ok)
|
||||
std::fprintf(stderr, "dangling-else guard: topology %d logical, %zu worker cores for a %zu-CPU mask\n",
|
||||
topo.logical, topo.worker_cores.size(), k);
|
||||
}
|
||||
const bool restored = set_affinity(allowed) && expect_affinity(allowed, "restore the full mask after the dangling-else guard");
|
||||
if (ok && restored) std::printf("Dangling-else guard: %zu-CPU self-affinity mask read exactly (%zu CPUs), topology stayed inside it\n", k, k);
|
||||
return ok && restored;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
bool require_high = false;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
@@ -272,6 +329,7 @@ int main(int argc, char** argv) {
|
||||
if (ok) ok = round_trip_high_cpu(allowed, require_high);
|
||||
if (ok) ok = negative_cpu_is_noop();
|
||||
if (ok) ok = failed_pin_preserves_affinity();
|
||||
if (ok) ok = dangling_else_guard(allowed);
|
||||
const bool restored = original.restore() && expect_affinity(allowed, "final original affinity cleanup");
|
||||
if (!ok || !restored) return 1;
|
||||
|
||||
|
||||
@@ -129,6 +129,37 @@ int main() {
|
||||
expect(cpu::plan_pool_workers(q).workers == 3, "quota 4 CPUs -> 3 workers");
|
||||
expect(cpu::plan_pool_workers(cpu_of(128, {64}, 200.0)).workers == cpu::kPoolWorkersCap, "a big quota changes nothing");
|
||||
|
||||
// ---- PR#14 regression: the quota against the affinity and the other bounds (the container reports) ----
|
||||
{
|
||||
// a quota below the cores wins; it truncates toward zero (never rounds), and a generous one changes nothing
|
||||
const cpu::PoolPlan q136 = cpu::plan_pool_workers(cpu_of(128, {64}, 13.6));
|
||||
expect(q136.workers == 12, "64c/128t under a 13.6-CPU quota -> 12 (never the 32 of the cap or the 64 of the cores)");
|
||||
expect(std::string(q136.limit) == "the container's CPU quota", "13.6 names the quota as the bound");
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 4.5)).workers == 3, "quota 4.5 truncates to 3 workers (no rounding up)");
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 3.999)).workers == 2, "quota 3.999 truncates to 2 workers");
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 7.999)).workers == 6, "quota 7.999 clamps 8c/16t to 6 workers");
|
||||
// the q == n boundary: the comparison is STRICT, so a quota equal to the cores does not drop another worker
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 8.0)).workers == 7, "quota 8.0 on 8 cores -> 7, NOT clamped (q == 8 - 1 == n, strict <)");
|
||||
// sub-core quotas floor at 1 worker, never 0
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 0.5)).workers == 1, "quota 0.5 -> 1 worker (floor)");
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 1.0)).workers == 1, "quota 1.0 -> 1 worker (floor)");
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 1.9)).workers == 1, "quota 1.9 -> 1 worker (floor)");
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 2.0)).workers == 1, "quota 2.0 -> 1 worker");
|
||||
// affinity below the quota: the mask bounds, the quota is inert
|
||||
expect(cpu::plan_pool_workers(cpu_of(8, {4}, 8.0)).workers == 3, "4 cores under an 8-CPU quota -> 3 (quota inert)");
|
||||
expect(cpu::plan_pool_workers(cpu_of(8, {4}, 200.0)).workers == 3, "4 cores under a 200-CPU quota -> 3");
|
||||
// no quota (0.0: what an unreadable cgroup gives, see cgroup_quota_plan_test) leaves the count EXACTLY at the
|
||||
// pre-cgroup value - the anti silent-collapse pins
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {8}, 0.0)).workers == 7, "no quota: 8c/16t stays 7");
|
||||
expect(cpu::plan_pool_workers(cpu_of(32, {16}, 0.0)).workers == 15,
|
||||
"no quota: 16c/32t stays 15 (the reserve applies to n after the host's core is dropped: 15 < kPoolReserveFrom)");
|
||||
expect(cpu::plan_pool_workers(cpu_of(40, {20}, 0.0)).workers == 18, "no quota: 20c keeps one core free (19 -> 18)");
|
||||
// an unknown core layout: the half-logical bound first, then the quota over it
|
||||
expect(cpu::plan_pool_workers(cpu_of(16, {}, 4.0)).workers == 3, "16 logical, no layout, quota 4 -> 3");
|
||||
expect(cpu::plan_pool_workers(cpu_of(256, {}, 0.0)).workers == cpu::kPoolWorkersCap, "256 logical, no layout, no quota: the cap");
|
||||
expect(cpu::plan_pool_workers(cpu_of(8, {}, 0.0)).workers == 7, "8 logical, no layout, no quota -> 7");
|
||||
}
|
||||
|
||||
// warnings: only above the plan, and each says what to do
|
||||
const cpu::PoolPlan p2s = cpu::plan_pool_workers(c2s);
|
||||
expect(cpu::pool_workers_warning(0, c2s, p2s).empty(), "0 (the default) never warns");
|
||||
|
||||
Reference in new issue
Block a user