include work AKRM src workplaceagent common scopeguard include iostrea

 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "../work/AKRM/src/workplaceagent/common/scopeguard.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <experimental/string_view>
#include<sys/wait.h>
#include "../work/AKRM/extern/variant/include/mpark/variant.hpp"
using namespace std;
#include <experimental/optional>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <regex>
#include <string>
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 setCGroup(std::vector<CGroupInfo> 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<CGroupInfo> 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;
}