struct CGroupInfo CGroupInfo std string controller std string cgroupPa

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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<CGroupInfo> parseCGroups(std::experimental::string_view view) {
std::vector<CGroupInfo> 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 setCGroups(std::vector<CGroupInfo> const &cgroups) {
std::string pid = fmt::sprintf("%d", getpid());
for (auto && cgroup : cgroups){
std::string path = fmt::sprintf("/sys/fs/cgroup/%s/%s/cgroup.procs", cgroup.controller, cgroup.cgroupPath);
std::cout << path << std::endl;
int fd = open(path.c_str(), O_WRONLY | O_APPEND);
if (fd < 0) {
throw std::runtime_error(fmt::sprintf("file is not exist %s", path));
}
__scope_guard {
close(fd);
};
write(fd, pid.c_str(), pid.length() + 1);
}
}