forked from ioi/isolate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cg.c
327 lines (277 loc) · 6.94 KB
/
cg.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Process Isolator -- Control Groups
*
* (c) 2012-2016 Martin Mares <[email protected]>
* (c) 2012-2014 Bernard Blackham <[email protected]>
*/
#include "isolate.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
struct cg_controller_desc {
const char *name;
int optional;
};
typedef enum {
CG_MEMORY = 0,
CG_CPUACCT,
CG_CPUSET,
CG_NUM_CONTROLLERS,
CG_PARENT = 256,
} cg_controller;
static const struct cg_controller_desc cg_controllers[CG_NUM_CONTROLLERS+1] = {
[CG_MEMORY] = { "memory", 0 },
[CG_CPUACCT] = { "cpuacct", 0 },
[CG_CPUSET] = { "cpuset", 1 },
[CG_NUM_CONTROLLERS] = { NULL, 0 },
};
#define FOREACH_CG_CONTROLLER(_controller) \
for (cg_controller (_controller) = 0; \
(_controller) < CG_NUM_CONTROLLERS; (_controller)++)
static const char *
cg_controller_name(cg_controller c)
{
assert(c < CG_NUM_CONTROLLERS);
return cg_controllers[c].name;
}
static int
cg_controller_optional(cg_controller c)
{
assert(c < CG_NUM_CONTROLLERS);
return cg_controllers[c].optional;
}
static char cg_name[256];
static char cg_parent_name[256];
#define CG_BUFSIZE 1024
static void
cg_makepath(char *buf, size_t len, cg_controller c, const char *attr)
{
snprintf(buf, len, "%s/%s/%s/%s",
cf_cg_root,
cg_controller_name(c & ~CG_PARENT),
(c & CG_PARENT) ? cg_parent_name : cg_name,
attr);
}
static int
cg_read(cg_controller controller, const char *attr, char *buf)
{
int result = 0;
int maybe = 0;
if (attr[0] == '?')
{
attr++;
maybe = 1;
}
char path[256];
cg_makepath(path, sizeof(path), controller, attr);
int fd = open(path, O_RDONLY);
if (fd < 0)
{
if (maybe)
goto fail;
die("Cannot read %s: %m", path);
}
int n = read(fd, buf, CG_BUFSIZE);
if (n < 0)
{
if (maybe)
goto fail_close;
die("Cannot read %s: %m", path);
}
if (n >= CG_BUFSIZE - 1)
die("Attribute %s too long", path);
if (n > 0 && buf[n-1] == '\n')
n--;
buf[n] = 0;
if (verbose > 1)
msg("CG: Read %s = <%s>\n", attr, buf);
result = 1;
fail_close:
close(fd);
fail:
return result;
}
static void __attribute__((format(printf,3,4)))
cg_write(cg_controller controller, const char *attr, const char *fmt, ...)
{
int maybe = 0;
if (attr[0] == '?')
{
attr++;
maybe = 1;
}
va_list args;
va_start(args, fmt);
char buf[CG_BUFSIZE];
int n = vsnprintf(buf, sizeof(buf), fmt, args);
if (n >= CG_BUFSIZE)
die("cg_write: Value for attribute %s is too long", attr);
if (verbose > 1)
msg("CG: Write %s = %s", attr, buf);
char path[256];
cg_makepath(path, sizeof(path), controller, attr);
int fd = open(path, O_WRONLY | O_TRUNC);
if (fd < 0)
{
if (maybe)
goto fail;
else
die("Cannot write %s: %m", path);
}
int written = write(fd, buf, n);
if (written < 0)
{
if (maybe)
goto fail_close;
else
die("Cannot set %s to %s: %m", path, buf);
}
if (written != n)
die("Short write to %s (%d out of %d bytes)", path, written, n);
fail_close:
close(fd);
fail:
va_end(args);
}
void
cg_init(void)
{
if (!cg_enable)
return;
if (!dir_exists(cf_cg_root))
die("Control group filesystem at %s not mounted", cf_cg_root);
if (cf_cg_parent)
{
snprintf(cg_name, sizeof(cg_name), "%s/box-%d", cf_cg_parent, box_id);
snprintf(cg_parent_name, sizeof(cg_parent_name), "%s", cf_cg_parent);
}
else
{
snprintf(cg_name, sizeof(cg_name), "box-%d", box_id);
strcpy(cg_parent_name, ".");
}
msg("Using control group %s under parent %s\n", cg_name, cg_parent_name);
}
void
cg_prepare(void)
{
if (!cg_enable)
return;
struct stat st;
char buf[CG_BUFSIZE];
char path[256];
FOREACH_CG_CONTROLLER(controller)
{
cg_makepath(path, sizeof(path), controller, "");
if (stat(path, &st) >= 0 || errno != ENOENT)
{
msg("Control group %s already exists, trying to empty it.\n", path);
if (rmdir(path) < 0)
die("Failed to reset control group %s: %m", path);
}
if (mkdir(path, 0777) < 0 && !cg_controller_optional(controller))
die("Failed to create control group %s: %m", path);
}
// If the cpuset module is enabled, set up allowed cpus and memory nodes.
// If per-box configuration exists, use it; otherwise, inherit the settings
// from the parent cgroup.
struct cf_per_box *cf = cf_current_box();
if (cg_read(CG_PARENT | CG_CPUSET, "?cpuset.cpus", buf))
cg_write(CG_CPUSET, "cpuset.cpus", "%s", cf->cpus ? cf->cpus : buf);
if (cg_read(CG_PARENT | CG_CPUSET, "?cpuset.mems", buf))
cg_write(CG_CPUSET, "cpuset.mems", "%s", cf->mems ? cf->mems : buf);
}
void
cg_enter(void)
{
if (!cg_enable)
return;
msg("Entering control group %s\n", cg_name);
FOREACH_CG_CONTROLLER(controller)
{
if (cg_controller_optional(controller))
cg_write(controller, "?tasks", "%d\n", (int) getpid());
else
cg_write(controller, "tasks", "%d\n", (int) getpid());
}
if (cg_memory_limit)
{
cg_write(CG_MEMORY, "memory.limit_in_bytes", "%lld\n", (long long) cg_memory_limit << 10);
cg_write(CG_MEMORY, "?memory.memsw.limit_in_bytes", "%lld\n", (long long) cg_memory_limit << 10);
cg_write(CG_MEMORY, "memory.max_usage_in_bytes", "0\n");
cg_write(CG_MEMORY, "?memory.memsw.max_usage_in_bytes", "0\n");
}
if (cg_timing)
cg_write(CG_CPUACCT, "cpuacct.usage", "0\n");
}
int
cg_get_run_time_ms(void)
{
if (!cg_enable)
return 0;
char buf[CG_BUFSIZE];
cg_read(CG_CPUACCT, "cpuacct.usage", buf);
unsigned long long ns = atoll(buf);
return ns / 1000000;
}
void
cg_stats(void)
{
if (!cg_enable)
return;
char buf[CG_BUFSIZE];
// Memory usage statistics
unsigned long long mem=0, memsw=0;
if (cg_read(CG_MEMORY, "?memory.max_usage_in_bytes", buf))
mem = atoll(buf);
if (cg_read(CG_MEMORY, "?memory.memsw.max_usage_in_bytes", buf))
{
memsw = atoll(buf);
if (memsw > mem)
mem = memsw;
}
if (mem)
meta_printf("cg-mem:%lld\n", mem >> 10);
// OOM kill detection
if (cg_read(CG_MEMORY, "?memory.oom_control", buf))
{
int oom_killed = 0;
char *s = buf;
while (s)
{
if (sscanf(s, "oom_kill %d", &oom_killed) == 1 && oom_killed)
{
meta_printf("cg-oom-killed:1\n");
break;
}
s = strchr(s, '\n');
if (s)
s++;
}
}
}
void
cg_remove(void)
{
char buf[CG_BUFSIZE];
if (!cg_enable)
return;
FOREACH_CG_CONTROLLER(controller)
{
// The cgroup can be non-existent at this moment (e.g., --cleanup before the first --init)
if (!cg_read(controller, "?tasks", buf))
continue;
if (buf[0])
die("Some tasks left in controller %s of cgroup %s, failed to remove it",
cg_controller_name(controller), cg_name);
char path[256];
cg_makepath(path, sizeof(path), controller, "");
if (rmdir(path) < 0)
die("Cannot remove control group %s: %m", path);
}
}