okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fb8d3dbfa
main
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 ) { 18if (!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 () { 32const fs::path root = fs::temp_directory_path () / ("strata_cgroup_cpu_test_" + std::to_string ((long long ) std::time (nullptr ))); 33 fs::remove_all (root ); 34const 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 37put (root /"ns" /"cpu.max" ,"1360000 100000\n" ); 38expect (near (cpu::cgroup_cpu_quota_at ((root /"ns" ).string (),"0::/\n" ),13.6 ),"own namespace, v2: 13.6 CPUs" ); 39// 2. no limit: "max" 40put (root /"nolimit" /"cpu.max" ,"max 100000\n" ); 41expect (near (cpu::cgroup_cpu_quota_at ((root /"nolimit" ).string (),"0::/\n" ),0.0 ),"max: no limit" ); 42// 3. nothing there, garbage, a zero period 43expect (near (cpu::cgroup_cpu_quota_at ((root /"empty" ).string (),"0::/\n" ),0.0 ),"no files: no limit" ); 44put (root /"junk" /"cpu.max" ,"banana\n" ); 45expect (near (cpu::cgroup_cpu_quota_at ((root /"junk" ).string (),"0::/\n" ),0.0 ),"garbage: no limit" ); 46put (root /"zero" /"cpu.max" ,"100000 0\n" ); 47expect (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 49const fs::path host = root /"host" ; 50put (host /"cpu.max" ,"max 100000\n" );// (the real root cgroup has no such file; "max" must not matter either way) 51put (host /"system.slice" /"cpu.max" ,"400000 100000\n" ); 52put (host /"system.slice" /"docker-abc.scope" /"cpu.max" ,"max 100000\n" ); 53expect (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 55put (host /"system.slice" /"docker-def.scope" /"cpu.max" ,"150000 100000\n" ); 56expect (near (cpu::cgroup_cpu_quota_at (host .string (),"0::/system.slice/docker-def.scope\n" ),1.5 ),"own limit tighter: 1.5 CPUs" ); 57put (host /"system.slice" /"docker-ghi.scope" /"cpu.max" ,"800000 100000\n" ); 58expect (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) 60put (root /"gone" /"cpu.max" ,"200000 100000\n" ); 61expect (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 63expect (near (cpu::cgroup_cpu_quota_at ((root /"gone" ).string (),"0::/not/here\r\n" ),2.0 ),"CRLF line" ); 64expect (near (cpu::cgroup_cpu_quota_at (host .string (),"0::/../../etc\n" ),0.0 ),"a path with .. is not followed" ); 65expect (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 67const fs::path v1 = root /"v1" ; 68put (v1 /"cpu,cpuacct" /"docker" /"abc" /"cpu.cfs_quota_us" ,"-1\n" ); 69put (v1 /"cpu,cpuacct" /"docker" /"abc" /"cpu.cfs_period_us" ,"100000\n" ); 70put (v1 /"cpu,cpuacct" /"docker" /"cpu.cfs_quota_us" ,"300000\n" ); 71put (v1 /"cpu,cpuacct" /"docker" /"cpu.cfs_period_us" ,"100000\n" ); 72put (v1 /"cpu,cpuacct" /"cpu.cfs_quota_us" ,"-1\n" ); 73put (v1 /"cpu,cpuacct" /"cpu.cfs_period_us" ,"100000\n" ); 74const std::string v1_self = "12:memory:/docker/abc\n4:cpu,cpuacct:/docker/abc\n1:name=systemd:/docker/abc\n" ; 75expect (near (cpu::cgroup_cpu_quota_at (v1 .string (),v1_self ),3.0 ),"v1: a parent's 3 CPUs, the group's -1 ignored" ); 76put (v1 /"cpu,cpuacct" /"docker" /"abc" /"cpu.cfs_quota_us" ,"50000\n" ); 77expect (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 79expect (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 81put (root /"hyb" /"cpu.max" ,"100000 100000\n" ); 82put (root /"hyb" /"cpu" /"x" /"cpu.cfs_quota_us" ,"600000\n" ); 83put (root /"hyb" /"cpu" /"x" /"cpu.cfs_period_us" ,"100000\n" ); 84expect (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) 86expect (cpu::cgroup_cpu_quota_at ("/sys/fs/cgroup" ,"" ) >=0.0 ,"the real mount reads without trouble" ); 87 88 fs::remove_all (root ); 89if (g_fail ) { 90 std::fprintf (stderr ,"cgroup_cpu_test: %d failure(s)\n" ,g_fail ); 91return 1 ; 92 } 93 std::printf ("cgroup_cpu_test: OK\n" ); 94return 0 ; 95}