|
| 1 | +#include <iostream> |
| 2 | +#include "sandbox/cgroups.h" |
| 3 | + |
| 4 | +int main() { |
| 5 | + std::cout << "cgroups_limits_test: starting" << std::endl; |
| 6 | + if (!sandbox::is_cgroup_v2_available()) { |
| 7 | + std::cout << "cgroup v2 not available; skipping test" << std::endl; |
| 8 | + return 0; |
| 9 | + } |
| 10 | + |
| 11 | + const std::string name = "native_node_limits_test_12345"; |
| 12 | + std::string path = sandbox::create_transient_cgroup(name); |
| 13 | + if (path.empty()) { |
| 14 | + std::cerr << "failed to create transient cgroup" << std::endl; |
| 15 | + return 2; |
| 16 | + } |
| 17 | + |
| 18 | + // Set limits |
| 19 | + if (!sandbox::set_cgroup_cpu_max(path, "100000 100000")) { |
| 20 | + std::cerr << "failed to set cpu.max" << std::endl; |
| 21 | + sandbox::remove_transient_cgroup(path); |
| 22 | + return 2; |
| 23 | + } |
| 24 | + |
| 25 | + if (!sandbox::set_cgroup_memory_max(path, "33554432")) { // 32MB |
| 26 | + std::cerr << "failed to set memory.max" << std::endl; |
| 27 | + sandbox::remove_transient_cgroup(path); |
| 28 | + return 2; |
| 29 | + } |
| 30 | + |
| 31 | + if (!sandbox::set_cgroup_pids_max(path, "10")) { |
| 32 | + std::cerr << "failed to set pids.max" << std::endl; |
| 33 | + sandbox::remove_transient_cgroup(path); |
| 34 | + return 2; |
| 35 | + } |
| 36 | + |
| 37 | + // Read back and verify (best-effort) |
| 38 | + auto cpu = sandbox::read_cgroup_file(path + "/cpu.max"); |
| 39 | + auto mem = sandbox::read_cgroup_file(path + "/memory.max"); |
| 40 | + auto pids = sandbox::read_cgroup_file(path + "/pids.max"); |
| 41 | + |
| 42 | + std::cout << "cpu.max='" << cpu << "' memory.max='" << mem << "' pids.max='" << pids << "'" << std::endl; |
| 43 | + |
| 44 | + if (cpu.empty() || mem.empty() || pids.empty()) { |
| 45 | + std::cerr << "failed to read back one or more limits" << std::endl; |
| 46 | + sandbox::remove_transient_cgroup(path); |
| 47 | + return 2; |
| 48 | + } |
| 49 | + |
| 50 | + // Cleanup |
| 51 | + if (!sandbox::remove_transient_cgroup(path)) { |
| 52 | + std::cerr << "failed to remove cgroup" << std::endl; |
| 53 | + return 2; |
| 54 | + } |
| 55 | + |
| 56 | + std::cout << "cgroups_limits_test: succeeded" << std::endl; |
| 57 | + return 0; |
| 58 | +} |
0 commit comments