#include "../work/AKRM/src/workplaceagent/common/scopeguard.h" #include #include #include #include #include #include #include "../work/AKRM/extern/variant/include/mpark/variant.hpp" using namespace std; #include #include #include #include #include #include struct CGroupInfo { CGroupInfo(std::string &&controller, std::string &&cgroupPath) : controller { std::move(controller) }, cgroupPath { std::move(cgroupPath) } {} std::string controller; std::string cgroupPath; }; std::vector parseCGroups(std::experimental::string_view view) { std::vector result; std::regex reg(R"(\d+:(?:name=)?([^:]*):\/(.*))"); std::istringstream tokenStream(std::string(view.data(), view.length())); std::string line; while(std::getline(tokenStream, line, '\n')) { std::smatch match; if(!std::regex_search(line, match, reg)) { continue; } if (match.size() == 3) { std::string first = match[1].str(); if (first == "") { first = "unified"; } result.emplace_back(std::move(first), match[2].str()); } } return result; } void setCGroup(std::vector cgroup) { for (auto &g : cgroup) { int fd = open(fmt::sprintf("/sys/fs/cgroup/%s/%s/cgroup.procs", g.controller, g.cgroupPath)); __scope_guard { close(fd); }; } } int main() { std::vector res = parseCgroups (R"(12:perf_event:/ 11:devices:/user.slice 10:freezer:/ 9:cpu,cpuacct:/ 8:hugetlb:/ 7:rdma:/ 6:cpuset:/ 5:blkio:/ 4:net_cls,net_prio:/ 3:memory:/ 2:pids:/user.slice/user-1000.slice/session-2.scope 1:name=systemd:/user.slice/user-1000.slice/session-2.scope 0::/user.slice/user-1000.slice/session-2.scope)"); for (int i = 0; i < res.size(); i++) { std::cout << "controller: " << res[i].controller << " cgroupPath: " << res[i].cgroupPath << std::endl; } return 0; }