Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
4b6ab94e | 2 | #include <subcmd/parse-options.h> |
023695d9 SE |
3 | #include "evsel.h" |
4 | #include "cgroup.h" | |
023695d9 | 5 | #include "evlist.h" |
b214ba8c NK |
6 | #include "rblist.h" |
7 | #include "metricgroup.h" | |
8 | #include "stat.h" | |
7f7c536f | 9 | #include <linux/zalloc.h> |
bafae98e ACM |
10 | #include <sys/types.h> |
11 | #include <sys/stat.h> | |
21bcc726 | 12 | #include <sys/statfs.h> |
bafae98e | 13 | #include <fcntl.h> |
f2a39fe8 ACM |
14 | #include <stdlib.h> |
15 | #include <string.h> | |
7982a898 | 16 | #include <api/fs/fs.h> |
bb1c15b6 NK |
17 | #include <ftw.h> |
18 | #include <regex.h> | |
023695d9 SE |
19 | |
20 | int nr_cgroups; | |
944138f0 | 21 | bool cgrp_event_expanded; |
023695d9 | 22 | |
bb1c15b6 NK |
23 | /* used to match cgroup name with patterns */ |
24 | struct cgroup_name { | |
25 | struct list_head list; | |
26 | bool used; | |
27 | char name[]; | |
28 | }; | |
29 | static LIST_HEAD(cgroup_list); | |
30 | ||
3b569286 | 31 | static int open_cgroup(const char *name) |
023695d9 | 32 | { |
c168fbfb ACM |
33 | char path[PATH_MAX + 1]; |
34 | char mnt[PATH_MAX + 1]; | |
023695d9 SE |
35 | int fd; |
36 | ||
37 | ||
7982a898 | 38 | if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event")) |
023695d9 SE |
39 | return -1; |
40 | ||
77f18153 | 41 | scnprintf(path, PATH_MAX, "%s/%s", mnt, name); |
023695d9 SE |
42 | |
43 | fd = open(path, O_RDONLY); | |
44 | if (fd == -1) | |
45 | fprintf(stderr, "no access to cgroup %s\n", path); | |
46 | ||
47 | return fd; | |
48 | } | |
49 | ||
69e874db | 50 | #ifdef HAVE_FILE_HANDLE |
2bc12abc | 51 | static u64 __read_cgroup_id(const char *path) |
69e874db | 52 | { |
69e874db NK |
53 | struct { |
54 | struct file_handle fh; | |
55 | uint64_t cgroup_id; | |
56 | } handle; | |
57 | int mount_id; | |
58 | ||
2bc12abc NK |
59 | handle.fh.handle_bytes = sizeof(handle.cgroup_id); |
60 | if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) | |
61 | return -1ULL; | |
62 | ||
63 | return handle.cgroup_id; | |
64 | } | |
65 | ||
66 | int read_cgroup_id(struct cgroup *cgrp) | |
67 | { | |
68 | char path[PATH_MAX + 1]; | |
69 | char mnt[PATH_MAX + 1]; | |
70 | ||
69e874db NK |
71 | if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event")) |
72 | return -1; | |
73 | ||
74 | scnprintf(path, PATH_MAX, "%s/%s", mnt, cgrp->name); | |
75 | ||
2bc12abc | 76 | cgrp->id = __read_cgroup_id(path); |
69e874db NK |
77 | return 0; |
78 | } | |
2bc12abc NK |
79 | #else |
80 | static inline u64 __read_cgroup_id(const char *path __maybe_unused) { return -1ULL; } | |
69e874db NK |
81 | #endif /* HAVE_FILE_HANDLE */ |
82 | ||
21bcc726 NK |
83 | #ifndef CGROUP2_SUPER_MAGIC |
84 | #define CGROUP2_SUPER_MAGIC 0x63677270 | |
85 | #endif | |
86 | ||
87 | int cgroup_is_v2(const char *subsys) | |
88 | { | |
89 | char mnt[PATH_MAX + 1]; | |
90 | struct statfs stbuf; | |
91 | ||
92 | if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, subsys)) | |
93 | return -1; | |
94 | ||
95 | if (statfs(mnt, &stbuf) < 0) | |
96 | return -1; | |
97 | ||
98 | return (stbuf.f_type == CGROUP2_SUPER_MAGIC); | |
99 | } | |
100 | ||
63503dba | 101 | static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str) |
023695d9 | 102 | { |
32dcd021 | 103 | struct evsel *counter; |
023695d9 SE |
104 | /* |
105 | * check if cgrp is already defined, if so we reuse it | |
106 | */ | |
e5cadb93 | 107 | evlist__for_each_entry(evlist, counter) { |
fc9ffb9c | 108 | if (!counter->cgrp) |
023695d9 | 109 | continue; |
5dbe23e8 ACM |
110 | if (!strcmp(counter->cgrp->name, str)) |
111 | return cgroup__get(counter->cgrp); | |
023695d9 SE |
112 | } |
113 | ||
5dbe23e8 | 114 | return NULL; |
b80271f7 ACM |
115 | } |
116 | ||
4fd06bd2 | 117 | struct cgroup *cgroup__new(const char *name, bool do_open) |
923a0fb3 ACM |
118 | { |
119 | struct cgroup *cgroup = zalloc(sizeof(*cgroup)); | |
120 | ||
121 | if (cgroup != NULL) { | |
923a0fb3 ACM |
122 | refcount_set(&cgroup->refcnt, 1); |
123 | ||
3b569286 ACM |
124 | cgroup->name = strdup(name); |
125 | if (!cgroup->name) | |
126 | goto out_err; | |
89fb1ca2 NK |
127 | |
128 | if (do_open) { | |
129 | cgroup->fd = open_cgroup(name); | |
130 | if (cgroup->fd == -1) | |
131 | goto out_free_name; | |
132 | } else { | |
133 | cgroup->fd = -1; | |
134 | } | |
923a0fb3 ACM |
135 | } |
136 | ||
137 | return cgroup; | |
3b569286 ACM |
138 | |
139 | out_free_name: | |
d8f9da24 | 140 | zfree(&cgroup->name); |
923a0fb3 ACM |
141 | out_err: |
142 | free(cgroup); | |
143 | return NULL; | |
144 | } | |
145 | ||
63503dba | 146 | struct cgroup *evlist__findnew_cgroup(struct evlist *evlist, const char *name) |
69239ec8 ACM |
147 | { |
148 | struct cgroup *cgroup = evlist__find_cgroup(evlist, name); | |
149 | ||
89fb1ca2 | 150 | return cgroup ?: cgroup__new(name, true); |
69239ec8 ACM |
151 | } |
152 | ||
63503dba | 153 | static int add_cgroup(struct evlist *evlist, const char *str) |
b80271f7 | 154 | { |
32dcd021 | 155 | struct evsel *counter; |
69239ec8 | 156 | struct cgroup *cgrp = evlist__findnew_cgroup(evlist, str); |
b80271f7 ACM |
157 | int n; |
158 | ||
69239ec8 ACM |
159 | if (!cgrp) |
160 | return -1; | |
023695d9 SE |
161 | /* |
162 | * find corresponding event | |
163 | * if add cgroup N, then need to find event N | |
164 | */ | |
165 | n = 0; | |
e5cadb93 | 166 | evlist__for_each_entry(evlist, counter) { |
023695d9 SE |
167 | if (n == nr_cgroups) |
168 | goto found; | |
169 | n++; | |
170 | } | |
023695d9 | 171 | |
a53b6460 | 172 | cgroup__put(cgrp); |
023695d9 SE |
173 | return -1; |
174 | found: | |
023695d9 SE |
175 | counter->cgrp = cgrp; |
176 | return 0; | |
177 | } | |
178 | ||
9450d0d4 ACM |
179 | static void cgroup__delete(struct cgroup *cgroup) |
180 | { | |
d2e7d863 TR |
181 | if (cgroup->fd >= 0) |
182 | close(cgroup->fd); | |
9450d0d4 ACM |
183 | zfree(&cgroup->name); |
184 | free(cgroup); | |
185 | } | |
186 | ||
a53b6460 | 187 | void cgroup__put(struct cgroup *cgrp) |
023695d9 | 188 | { |
79c5fe6d | 189 | if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) { |
9450d0d4 | 190 | cgroup__delete(cgrp); |
023695d9 SE |
191 | } |
192 | } | |
193 | ||
fc9ffb9c ACM |
194 | struct cgroup *cgroup__get(struct cgroup *cgroup) |
195 | { | |
196 | if (cgroup) | |
197 | refcount_inc(&cgroup->refcnt); | |
198 | return cgroup; | |
199 | } | |
200 | ||
32dcd021 | 201 | static void evsel__set_default_cgroup(struct evsel *evsel, struct cgroup *cgroup) |
483322dd ACM |
202 | { |
203 | if (evsel->cgrp == NULL) | |
204 | evsel->cgrp = cgroup__get(cgroup); | |
205 | } | |
206 | ||
63503dba | 207 | void evlist__set_default_cgroup(struct evlist *evlist, struct cgroup *cgroup) |
483322dd | 208 | { |
32dcd021 | 209 | struct evsel *evsel; |
483322dd ACM |
210 | |
211 | evlist__for_each_entry(evlist, evsel) | |
212 | evsel__set_default_cgroup(evsel, cgroup); | |
213 | } | |
214 | ||
bb1c15b6 NK |
215 | /* helper function for ftw() in match_cgroups and list_cgroups */ |
216 | static int add_cgroup_name(const char *fpath, const struct stat *sb __maybe_unused, | |
a81fbb87 | 217 | int typeflag, struct FTW *ftwbuf __maybe_unused) |
bb1c15b6 NK |
218 | { |
219 | struct cgroup_name *cn; | |
220 | ||
221 | if (typeflag != FTW_D) | |
222 | return 0; | |
223 | ||
224 | cn = malloc(sizeof(*cn) + strlen(fpath) + 1); | |
225 | if (cn == NULL) | |
226 | return -1; | |
227 | ||
228 | cn->used = false; | |
229 | strcpy(cn->name, fpath); | |
230 | ||
231 | list_add_tail(&cn->list, &cgroup_list); | |
232 | return 0; | |
233 | } | |
234 | ||
54b353a2 NK |
235 | static int check_and_add_cgroup_name(const char *fpath) |
236 | { | |
237 | struct cgroup_name *cn; | |
238 | ||
239 | list_for_each_entry(cn, &cgroup_list, list) { | |
240 | if (!strcmp(cn->name, fpath)) | |
241 | return 0; | |
242 | } | |
243 | ||
244 | /* pretend if it's added by ftw() */ | |
245 | return add_cgroup_name(fpath, NULL, FTW_D, NULL); | |
246 | } | |
247 | ||
bb1c15b6 NK |
248 | static void release_cgroup_list(void) |
249 | { | |
250 | struct cgroup_name *cn; | |
251 | ||
252 | while (!list_empty(&cgroup_list)) { | |
253 | cn = list_first_entry(&cgroup_list, struct cgroup_name, list); | |
254 | list_del(&cn->list); | |
255 | free(cn); | |
256 | } | |
257 | } | |
258 | ||
259 | /* collect given cgroups only */ | |
260 | static int list_cgroups(const char *str) | |
261 | { | |
262 | const char *p, *e, *eos = str + strlen(str); | |
263 | struct cgroup_name *cn; | |
264 | char *s; | |
265 | ||
54b353a2 | 266 | /* use given name as is when no regex is given */ |
bb1c15b6 NK |
267 | for (;;) { |
268 | p = strchr(str, ','); | |
269 | e = p ? p : eos; | |
270 | ||
271 | if (e - str) { | |
272 | int ret; | |
273 | ||
274 | s = strndup(str, e - str); | |
275 | if (!s) | |
276 | return -1; | |
54b353a2 NK |
277 | |
278 | ret = check_and_add_cgroup_name(s); | |
bb1c15b6 | 279 | free(s); |
54b353a2 | 280 | if (ret < 0) |
bb1c15b6 NK |
281 | return -1; |
282 | } else { | |
54b353a2 | 283 | if (check_and_add_cgroup_name("/") < 0) |
bb1c15b6 NK |
284 | return -1; |
285 | } | |
286 | ||
287 | if (!p) | |
288 | break; | |
289 | str = p+1; | |
290 | } | |
291 | ||
292 | /* these groups will be used */ | |
293 | list_for_each_entry(cn, &cgroup_list, list) | |
294 | cn->used = true; | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
299 | /* collect all cgroups first and then match with the pattern */ | |
300 | static int match_cgroups(const char *str) | |
301 | { | |
302 | char mnt[PATH_MAX]; | |
303 | const char *p, *e, *eos = str + strlen(str); | |
304 | struct cgroup_name *cn; | |
305 | regex_t reg; | |
306 | int prefix_len; | |
307 | char *s; | |
308 | ||
309 | if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event")) | |
310 | return -1; | |
311 | ||
312 | /* cgroup_name will have a full path, skip the root directory */ | |
313 | prefix_len = strlen(mnt); | |
314 | ||
315 | /* collect all cgroups in the cgroup_list */ | |
a81fbb87 | 316 | if (nftw(mnt, add_cgroup_name, 20, 0) < 0) |
bb1c15b6 NK |
317 | return -1; |
318 | ||
319 | for (;;) { | |
320 | p = strchr(str, ','); | |
321 | e = p ? p : eos; | |
322 | ||
323 | /* allow empty cgroups, i.e., skip */ | |
324 | if (e - str) { | |
325 | /* termination added */ | |
326 | s = strndup(str, e - str); | |
327 | if (!s) | |
328 | return -1; | |
329 | if (regcomp(®, s, REG_NOSUB)) { | |
330 | free(s); | |
331 | return -1; | |
332 | } | |
333 | ||
334 | /* check cgroup name with the pattern */ | |
335 | list_for_each_entry(cn, &cgroup_list, list) { | |
336 | char *name = cn->name + prefix_len; | |
337 | ||
338 | if (name[0] == '/' && name[1]) | |
339 | name++; | |
340 | if (!regexec(®, name, 0, NULL, 0)) | |
341 | cn->used = true; | |
342 | } | |
343 | regfree(®); | |
344 | free(s); | |
345 | } else { | |
346 | /* first entry to root cgroup */ | |
347 | cn = list_first_entry(&cgroup_list, struct cgroup_name, | |
348 | list); | |
349 | cn->used = true; | |
350 | } | |
351 | ||
352 | if (!p) | |
353 | break; | |
354 | str = p+1; | |
355 | } | |
356 | return prefix_len; | |
357 | } | |
358 | ||
a6adc9bd | 359 | int parse_cgroups(const struct option *opt, const char *str, |
1d037ca1 | 360 | int unset __maybe_unused) |
023695d9 | 361 | { |
63503dba | 362 | struct evlist *evlist = *(struct evlist **)opt->value; |
32dcd021 | 363 | struct evsel *counter; |
3ca32f69 | 364 | struct cgroup *cgrp = NULL; |
023695d9 SE |
365 | const char *p, *e, *eos = str + strlen(str); |
366 | char *s; | |
25f72f9e | 367 | int ret, i; |
023695d9 | 368 | |
ce9036a6 | 369 | if (list_empty(&evlist->core.entries)) { |
023695d9 SE |
370 | fprintf(stderr, "must define events before cgroups\n"); |
371 | return -1; | |
372 | } | |
373 | ||
374 | for (;;) { | |
375 | p = strchr(str, ','); | |
376 | e = p ? p : eos; | |
377 | ||
378 | /* allow empty cgroups, i.e., skip */ | |
379 | if (e - str) { | |
380 | /* termination added */ | |
381 | s = strndup(str, e - str); | |
382 | if (!s) | |
383 | return -1; | |
384 | ret = add_cgroup(evlist, s); | |
3b569286 ACM |
385 | free(s); |
386 | if (ret) | |
023695d9 | 387 | return -1; |
023695d9 SE |
388 | } |
389 | /* nr_cgroups is increased een for empty cgroups */ | |
390 | nr_cgroups++; | |
391 | if (!p) | |
392 | break; | |
393 | str = p+1; | |
394 | } | |
25f72f9e | 395 | /* for the case one cgroup combine to multiple events */ |
396 | i = 0; | |
397 | if (nr_cgroups == 1) { | |
398 | evlist__for_each_entry(evlist, counter) { | |
399 | if (i == 0) | |
400 | cgrp = counter->cgrp; | |
401 | else { | |
402 | counter->cgrp = cgrp; | |
403 | refcount_inc(&cgrp->refcnt); | |
404 | } | |
405 | i++; | |
406 | } | |
407 | } | |
023695d9 SE |
408 | return 0; |
409 | } | |
d1277aa3 | 410 | |
bb1c15b6 NK |
411 | static bool has_pattern_string(const char *str) |
412 | { | |
413 | return !!strpbrk(str, "{}[]()|*+?^$"); | |
414 | } | |
415 | ||
b214ba8c | 416 | int evlist__expand_cgroup(struct evlist *evlist, const char *str, |
89fb1ca2 | 417 | struct rblist *metric_events, bool open_cgroup) |
d1c5a0e8 NK |
418 | { |
419 | struct evlist *orig_list, *tmp_list; | |
420 | struct evsel *pos, *evsel, *leader; | |
b214ba8c | 421 | struct rblist orig_metric_events; |
d1c5a0e8 | 422 | struct cgroup *cgrp = NULL; |
bb1c15b6 | 423 | struct cgroup_name *cn; |
d1c5a0e8 | 424 | int ret = -1; |
bb1c15b6 | 425 | int prefix_len; |
d1c5a0e8 NK |
426 | |
427 | if (evlist->core.nr_entries == 0) { | |
428 | fprintf(stderr, "must define events before cgroups\n"); | |
429 | return -EINVAL; | |
430 | } | |
431 | ||
432 | orig_list = evlist__new(); | |
433 | tmp_list = evlist__new(); | |
434 | if (orig_list == NULL || tmp_list == NULL) { | |
435 | fprintf(stderr, "memory allocation failed\n"); | |
436 | return -ENOMEM; | |
437 | } | |
438 | ||
439 | /* save original events and init evlist */ | |
e414fd1a | 440 | evlist__splice_list_tail(orig_list, &evlist->core.entries); |
d1c5a0e8 NK |
441 | evlist->core.nr_entries = 0; |
442 | ||
b214ba8c NK |
443 | if (metric_events) { |
444 | orig_metric_events = *metric_events; | |
445 | rblist__init(metric_events); | |
446 | } else { | |
447 | rblist__init(&orig_metric_events); | |
448 | } | |
449 | ||
bb1c15b6 NK |
450 | if (has_pattern_string(str)) |
451 | prefix_len = match_cgroups(str); | |
452 | else | |
453 | prefix_len = list_cgroups(str); | |
d1c5a0e8 | 454 | |
bb1c15b6 NK |
455 | if (prefix_len < 0) |
456 | goto out_err; | |
d1c5a0e8 | 457 | |
bb1c15b6 NK |
458 | list_for_each_entry(cn, &cgroup_list, list) { |
459 | char *name; | |
460 | ||
461 | if (!cn->used) | |
462 | continue; | |
463 | ||
464 | /* cgroup_name might have a full path, skip the prefix */ | |
465 | name = cn->name + prefix_len; | |
466 | if (name[0] == '/' && name[1]) | |
467 | name++; | |
468 | cgrp = cgroup__new(name, open_cgroup); | |
469 | if (cgrp == NULL) | |
470 | goto out_err; | |
d1c5a0e8 NK |
471 | |
472 | leader = NULL; | |
473 | evlist__for_each_entry(orig_list, pos) { | |
474 | evsel = evsel__clone(pos); | |
475 | if (evsel == NULL) | |
476 | goto out_err; | |
477 | ||
478 | cgroup__put(evsel->cgrp); | |
479 | evsel->cgrp = cgroup__get(cgrp); | |
480 | ||
481 | if (evsel__is_group_leader(pos)) | |
482 | leader = evsel; | |
fba7c866 | 483 | evsel__set_leader(evsel, leader); |
d1c5a0e8 NK |
484 | |
485 | evlist__add(tmp_list, evsel); | |
486 | } | |
487 | /* cgroup__new() has a refcount, release it here */ | |
488 | cgroup__put(cgrp); | |
489 | nr_cgroups++; | |
490 | ||
b214ba8c | 491 | if (metric_events) { |
b214ba8c NK |
492 | if (metricgroup__copy_metric_events(tmp_list, cgrp, |
493 | metric_events, | |
494 | &orig_metric_events) < 0) | |
bb1c15b6 | 495 | goto out_err; |
b214ba8c NK |
496 | } |
497 | ||
e414fd1a | 498 | evlist__splice_list_tail(evlist, &tmp_list->core.entries); |
d1c5a0e8 | 499 | tmp_list->core.nr_entries = 0; |
bb1c15b6 | 500 | } |
d1c5a0e8 | 501 | |
bb1c15b6 NK |
502 | if (list_empty(&evlist->core.entries)) { |
503 | fprintf(stderr, "no cgroup matched: %s\n", str); | |
504 | goto out_err; | |
d1c5a0e8 NK |
505 | } |
506 | ||
bb1c15b6 | 507 | ret = 0; |
944138f0 | 508 | cgrp_event_expanded = true; |
bb1c15b6 | 509 | |
d1c5a0e8 NK |
510 | out_err: |
511 | evlist__delete(orig_list); | |
512 | evlist__delete(tmp_list); | |
b214ba8c | 513 | rblist__exit(&orig_metric_events); |
bb1c15b6 | 514 | release_cgroup_list(); |
d1c5a0e8 NK |
515 | |
516 | return ret; | |
517 | } | |
518 | ||
d1277aa3 NK |
519 | static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id, |
520 | bool create, const char *path) | |
521 | { | |
522 | struct rb_node **p = &root->rb_node; | |
523 | struct rb_node *parent = NULL; | |
524 | struct cgroup *cgrp; | |
525 | ||
526 | while (*p != NULL) { | |
527 | parent = *p; | |
528 | cgrp = rb_entry(parent, struct cgroup, node); | |
529 | ||
530 | if (cgrp->id == id) | |
531 | return cgrp; | |
532 | ||
533 | if (cgrp->id < id) | |
534 | p = &(*p)->rb_left; | |
535 | else | |
536 | p = &(*p)->rb_right; | |
537 | } | |
538 | ||
539 | if (!create) | |
540 | return NULL; | |
541 | ||
542 | cgrp = malloc(sizeof(*cgrp)); | |
543 | if (cgrp == NULL) | |
544 | return NULL; | |
545 | ||
546 | cgrp->name = strdup(path); | |
547 | if (cgrp->name == NULL) { | |
548 | free(cgrp); | |
549 | return NULL; | |
550 | } | |
551 | ||
552 | cgrp->fd = -1; | |
553 | cgrp->id = id; | |
554 | refcount_set(&cgrp->refcnt, 1); | |
555 | ||
556 | rb_link_node(&cgrp->node, parent, p); | |
557 | rb_insert_color(&cgrp->node, root); | |
558 | ||
559 | return cgrp; | |
560 | } | |
561 | ||
562 | struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id, | |
563 | const char *path) | |
564 | { | |
565 | struct cgroup *cgrp; | |
566 | ||
567 | down_write(&env->cgroups.lock); | |
568 | cgrp = __cgroup__findnew(&env->cgroups.tree, id, true, path); | |
569 | up_write(&env->cgroups.lock); | |
570 | return cgrp; | |
571 | } | |
572 | ||
2bc12abc NK |
573 | struct cgroup *__cgroup__find(struct rb_root *root, uint64_t id) |
574 | { | |
575 | return __cgroup__findnew(root, id, /*create=*/false, /*path=*/NULL); | |
576 | } | |
577 | ||
d1277aa3 NK |
578 | struct cgroup *cgroup__find(struct perf_env *env, uint64_t id) |
579 | { | |
580 | struct cgroup *cgrp; | |
581 | ||
582 | down_read(&env->cgroups.lock); | |
583 | cgrp = __cgroup__findnew(&env->cgroups.tree, id, false, NULL); | |
584 | up_read(&env->cgroups.lock); | |
585 | return cgrp; | |
586 | } | |
587 | ||
588 | void perf_env__purge_cgroups(struct perf_env *env) | |
589 | { | |
590 | struct rb_node *node; | |
591 | struct cgroup *cgrp; | |
592 | ||
593 | down_write(&env->cgroups.lock); | |
594 | while (!RB_EMPTY_ROOT(&env->cgroups.tree)) { | |
595 | node = rb_first(&env->cgroups.tree); | |
596 | cgrp = rb_entry(node, struct cgroup, node); | |
597 | ||
598 | rb_erase(node, &env->cgroups.tree); | |
599 | cgroup__put(cgrp); | |
600 | } | |
601 | up_write(&env->cgroups.lock); | |
602 | } | |
2bc12abc NK |
603 | |
604 | void read_all_cgroups(struct rb_root *root) | |
605 | { | |
606 | char mnt[PATH_MAX]; | |
607 | struct cgroup_name *cn; | |
608 | int prefix_len; | |
609 | ||
610 | if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event")) | |
611 | return; | |
612 | ||
613 | /* cgroup_name will have a full path, skip the root directory */ | |
614 | prefix_len = strlen(mnt); | |
615 | ||
616 | /* collect all cgroups in the cgroup_list */ | |
617 | if (nftw(mnt, add_cgroup_name, 20, 0) < 0) | |
618 | return; | |
619 | ||
620 | list_for_each_entry(cn, &cgroup_list, list) { | |
621 | const char *name; | |
622 | u64 cgrp_id; | |
623 | ||
624 | /* cgroup_name might have a full path, skip the prefix */ | |
625 | name = cn->name + prefix_len; | |
626 | if (name[0] == '\0') | |
627 | name = "/"; | |
628 | ||
629 | cgrp_id = __read_cgroup_id(cn->name); | |
630 | __cgroup__findnew(root, cgrp_id, /*create=*/true, name); | |
631 | } | |
632 | ||
633 | release_cgroup_list(); | |
634 | } |