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
|
// SPDX-License-Identifier: GPL-2.0
/*
* memcg_event_listener.c - Simple listener of memcg memory.events
*
* Copyright (c) 2023, SaluteDevices. All Rights Reserved.
*
* Author: Dmitry Rokosov <ddrokosov@salutedevices.com>
*/
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>
#define MEMCG_EVENTS "memory.events"
/* Size of buffer to use when reading inotify events */
#define INOTIFY_BUFFER_SIZE 8192
#define INOTIFY_EVENT_NEXT(event, length) ({ \
(length) -= sizeof(*(event)) + (event)->len; \
(event)++; \
})
#define INOTIFY_EVENT_OK(event, length) ((length) >= (ssize_t)sizeof(*(event)))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
struct memcg_counters {
long low;
long high;
long max;
long oom;
long oom_kill;
long oom_group_kill;
};
struct memcg_events {
struct memcg_counters counters;
char path[PATH_MAX];
int inotify_fd;
int inotify_wd;
};
static void print_memcg_counters(const struct memcg_counters *counters)
{
printf("MEMCG events:\n");
printf("\tlow: %ld\n", counters->low);
printf("\thigh: %ld\n", counters->high);
printf("\tmax: %ld\n", counters->max);
printf("\toom: %ld\n", counters->oom);
printf("\toom_kill: %ld\n", counters->oom_kill);
printf("\toom_group_kill: %ld\n", counters->oom_group_kill);
}
static int get_memcg_counter(char *line, const char *name, long *counter)
{
size_t len = strlen(name);
char *endptr;
long tmp;
if (memcmp(line, name, len)) {
warnx("Counter line %s has wrong name, %s is expected",
line, name);
return -EINVAL;
}
/* skip the whitespace delimiter */
len += 1;
errno = 0;
tmp = strtol(&line[len], &endptr, 10);
if (((tmp == LONG_MAX || tmp == LONG_MIN) && errno == ERANGE) ||
(errno && !tmp)) {
warnx("Failed to parse: %s", &line[len]);
return -ERANGE;
}
if (endptr == &line[len]) {
warnx("Not digits were found in line %s", &line[len]);
return -EINVAL;
}
if (!(*endptr == '\0' || (*endptr == '\n' && *++endptr == '\0'))) {
warnx("Further characters after number: %s", endptr);
return -EINVAL;
}
*counter = tmp;
return 0;
}
static int read_memcg_events(struct memcg_events *events, bool show_diff)
{
FILE *fp = fopen(events->path, "re");
size_t i;
int ret = 0;
bool any_new_events = false;
char *line = NULL;
size_t len = 0;
struct memcg_counters new_counters;
struct memcg_counters *counters = &events->counters;
struct {
const char *name;
long *new;
long *old;
} map[] = {
{
.name = "low",
.new = &new_counters.low,
.old = &counters->low,
},
{
.name = "high",
.new = &new_counters.high,
.old = &counters->high,
},
{
.name = "max",
.new = &new_counters.max,
.old = &counters->max,
},
{
.name = "oom",
.new = &new_counters.oom,
.old = &counters->oom,
},
{
.name = "oom_kill",
.new = &new_counters.oom_kill,
.old = &counters->oom_kill,
},
{
.name = "oom_group_kill",
.new = &new_counters.oom_group_kill,
.old = &counters->oom_group_kill,
},
};
if (!fp) {
warn("Failed to open memcg events file %s", events->path);
return -EBADF;
}
/* Read new values for memcg counters */
for (i = 0; i < ARRAY_SIZE(map); ++i) {
ssize_t nread;
errno = 0;
nread = getline(&line, &len, fp);
if (nread == -1) {
if (errno) {
warn("Failed to read line for counter %s",
map[i].name);
ret = -EIO;
goto exit;
}
break;
}
ret = get_memcg_counter(line, map[i].name, map[i].new);
if (ret) {
warnx("Failed to get counter value from line %s", line);
goto exit;
}
}
for (i = 0; i < ARRAY_SIZE(map); ++i) {
long diff;
if (*map[i].new > *map[i].old) {
diff = *map[i].new - *map[i].old;
if (show_diff)
printf("*** %ld MEMCG %s event%s, "
"change counter %ld => %ld\n",
diff, map[i].name,
(diff == 1) ? "" : "s",
*map[i].old, *map[i].new);
*map[i].old += diff;
any_new_events = true;
}
}
if (show_diff && !any_new_events)
printf("*** No new untracked memcg events available\n");
exit:
free(line);
fclose(fp);
return ret;
}
static void process_memcg_events(struct memcg_events *events,
struct inotify_event *event)
{
int ret;
if (events->inotify_wd != event->wd) {
warnx("Unknown inotify event %d, should be %d", event->wd,
events->inotify_wd);
return;
}
printf("Received event in %s:\n", events->path);
if (!(event->mask & IN_MODIFY)) {
warnx("No IN_MODIFY event, skip it");
return;
}
ret = read_memcg_events(events, /* show_diff = */true);
if (ret)
warnx("Can't read memcg events");
}
static void monitor_events(struct memcg_events *events)
{
struct pollfd fds[1];
int ret;
printf("Started monitoring memory events from '%s'...\n", events->path);
fds[0].fd = events->inotify_fd;
fds[0].events = POLLIN;
for (;;) {
ret = poll(fds, ARRAY_SIZE(fds), -1);
if (ret < 0 && errno != EAGAIN)
err(EXIT_FAILURE, "Can't poll memcg events (%d)", ret);
if (fds[0].revents & POLLERR)
err(EXIT_FAILURE, "Got POLLERR during monitor events");
if (fds[0].revents & POLLIN) {
struct inotify_event *event;
char buffer[INOTIFY_BUFFER_SIZE];
ssize_t length;
length = read(fds[0].fd, buffer, INOTIFY_BUFFER_SIZE);
if (length <= 0)
continue;
event = (struct inotify_event *)buffer;
while (INOTIFY_EVENT_OK(event, length)) {
process_memcg_events(events, event);
event = INOTIFY_EVENT_NEXT(event, length);
}
}
}
}
static int initialize_memcg_events(struct memcg_events *events,
const char *cgroup)
{
int ret;
memset(events, 0, sizeof(struct memcg_events));
ret = snprintf(events->path, PATH_MAX,
"/sys/fs/cgroup/%s/memory.events", cgroup);
if (ret >= PATH_MAX) {
warnx("Path to cgroup memory.events is too long");
return -EMSGSIZE;
} else if (ret < 0) {
warn("Can't generate cgroup event full name");
return ret;
}
ret = read_memcg_events(events, /* show_diff = */false);
if (ret) {
warnx("Failed to read initial memcg events state (%d)", ret);
return ret;
}
events->inotify_fd = inotify_init();
if (events->inotify_fd < 0) {
warn("Failed to setup new inotify device");
return -EMFILE;
}
events->inotify_wd = inotify_add_watch(events->inotify_fd,
events->path, IN_MODIFY);
if (events->inotify_wd < 0) {
warn("Couldn't add monitor in dir %s", events->path);
return -EIO;
}
printf("Initialized MEMCG events with counters:\n");
print_memcg_counters(&events->counters);
return 0;
}
static void cleanup_memcg_events(struct memcg_events *events)
{
inotify_rm_watch(events->inotify_fd, events->inotify_wd);
close(events->inotify_fd);
}
int main(int argc, const char **argv)
{
struct memcg_events events;
ssize_t ret;
if (argc != 2)
errx(EXIT_FAILURE, "Usage: %s <cgroup>", argv[0]);
ret = initialize_memcg_events(&events, argv[1]);
if (ret)
errx(EXIT_FAILURE, "Can't initialize memcg events (%zd)", ret);
monitor_events(&events);
cleanup_memcg_events(&events);
printf("Exiting memcg event listener...\n");
return EXIT_SUCCESS;
}
|