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