-
Notifications
You must be signed in to change notification settings - Fork 163
/
cg.c
345 lines (282 loc) · 6.28 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Process Isolator -- Control Groups
*
* (c) 2012-2024 Martin Mares <mj@ucw.cz>
* (c) 2012-2014 Bernard Blackham <bernard@blackham.com.au>
*/
#include "isolate.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static char cg_name[256];
#define CG_BUFSIZE 1024
static void
cg_makepath(char *buf, size_t len, const char *attr)
{
int out;
if (attr)
out = snprintf(buf, len, "%s/%s/%s", cf_cg_root, cg_name, attr);
else
out = snprintf(buf, len, "%s/%s", cf_cg_root, cg_name);
assert((size_t) out < len);
}
static int
cg_read(const char *attr, char *buf)
{
int result = 0;
int maybe = 0;
if (attr[0] == '?')
{
attr++;
maybe = 1;
}
char path[PATH_MAX];
cg_makepath(path, sizeof(path), 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,2,3)))
cg_write(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[PATH_MAX];
cg_makepath(path, sizeof(path), 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);
}
static FILE *cg_fopen(const char *attr)
{
char path[PATH_MAX];
cg_makepath(path, sizeof(path), attr);
FILE *f = fopen(path, "r");
if (!f)
die("Cannot open %s: %m", path);
return f;
}
static void cg_fclose(FILE *f)
{
if (ferror(f))
die("Read error on cgroup attributes: %m");
fclose(f);
}
static int cg_fread_kv(FILE *f, char *key, char *val)
{
char line[CG_BUFSIZE];
if (!fgets(line, sizeof(line), f))
return 0;
char *eol = strchr(line, '\n');
if (!eol)
die("Non-terminated or too long line in cgroup key-value file");
*eol = 0;
char *space = strchr(line, ' ');
if (!space)
die("Missing space in cgroup key-value file");
*space = 0;
strcpy(key, line);
strcpy(val, space + 1);
return 1;
}
void
cg_init(void)
{
if (!cg_enable)
return;
if (strlen(cf_cg_root) > 5 && !memcmp(cf_cg_root, "auto:", 5))
{
char *filename = cf_cg_root + 5;
FILE *f = fopen(filename, "r");
if (!f)
die("Cannot open %s: %m", filename);
char *line = NULL;
size_t len;
if (getline(&line, &len, f) < 0)
die("Cannot read from %s: %m", filename);
char *sep = strchr(line, '\n');
if (sep)
*sep = 0;
fclose(f);
cf_cg_root = line;
}
if (!dir_exists(cf_cg_root))
die("Control group root %s does not exist", cf_cg_root);
snprintf(cg_name, sizeof(cg_name), "box-%d", box_id);
msg("Using control group %s under parent %s\n", cg_name, cf_cg_root);
}
void
cg_create(void)
{
if (!cg_enable)
return;
struct stat st;
char path[PATH_MAX];
cg_makepath(path, sizeof(path), NULL);
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))
die("Failed to create control group %s: %m", path);
}
void
cg_enter(void)
{
if (!cg_enable)
return;
msg("Entering control group %s\n", cg_name);
cg_write("cgroup.procs", "%d\n", (int) getpid());
if (cg_memory_limit)
{
cg_write("memory.max", "%lld\n", (long long) cg_memory_limit << 10);
cg_write("?memory.swap.max", "0\n");
}
struct cf_per_box *cf = cf_current_box();
if (cf->cpus)
cg_write("cpuset.cpus", "%s", cf->cpus);
if (cf->mems)
cg_write("cpuset.mems", "%s", cf->mems);
}
static int
raw_get_run_time_ms(void)
{
FILE *f = cg_fopen("cpu.stat");
unsigned long long usec = 0;
bool found_usage = false;
char key[CG_BUFSIZE], val[CG_BUFSIZE];
while (cg_fread_kv(f, key, val))
{
if (!strcmp(key, "usage_usec"))
{
usec = atoll(val);
found_usage = true;
}
}
cg_fclose(f);
if (!found_usage)
die("Missing usage_usec in cpu.stat");
return usec / 1000;
}
static int cg_time_offset;
int
cg_get_run_time_ms(void)
{
if (!cg_enable)
return 0;
return raw_get_run_time_ms() - cg_time_offset;
}
void
cg_setup(void)
{
if (!cg_enable)
return;
/*
* The box CG can be used by multiple invocations of "isolate --run",
* but cpu.stat is cummulative and cannot be reset. So we subtract
* the initial value of cpu.stat.
*/
cg_time_offset = raw_get_run_time_ms();
if (verbose > 1)
msg("CG: Time offset = %d", cg_time_offset);
}
void
cg_stats(void)
{
if (!cg_enable)
return;
char key[CG_BUFSIZE], val[CG_BUFSIZE];
unsigned long long mem=0;
if (cg_read("?memory.peak", val))
mem = atoll(val);
if (mem)
meta_printf("cg-mem:%lld\n", mem >> 10);
// OOM kill detection
FILE *f = cg_fopen("memory.events");
while (cg_fread_kv(f, key, val))
{
if (!strcmp(key, "oom_kill") && atoll(val))
{
meta_printf("cg-oom-killed:1\n");
break;
}
}
cg_fclose(f);
}
void
cg_remove(void)
{
if (!cg_enable)
return;
char path[PATH_MAX];
cg_makepath(path, sizeof(path), NULL);
if (dir_exists(path))
{
msg("Removing control group\n");
cg_write("?cgroup.kill", "1\n");
if (rmdir(path) < 0)
die("Cannot remove control group %s: %m", path);
}
}