1 // SPDX-License-Identifier: GPL-2.0
2 #include <subcmd/parse-options.h>
7 #include "metricgroup.h"
9 #include <linux/zalloc.h>
10 #include <sys/types.h>
12 #include <sys/statfs.h>
16 #include <api/fs/fs.h>
21 bool cgrp_event_expanded;
23 /* used to match cgroup name with patterns */
25 struct list_head list;
29 static LIST_HEAD(cgroup_list);
31 static int open_cgroup(const char *name)
33 char path[PATH_MAX + 1];
34 char mnt[PATH_MAX + 1];
38 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
41 scnprintf(path, PATH_MAX, "%s/%s", mnt, name);
43 fd = open(path, O_RDONLY);
45 fprintf(stderr, "no access to cgroup %s\n", path);
50 #ifdef HAVE_FILE_HANDLE
51 int read_cgroup_id(struct cgroup *cgrp)
53 char path[PATH_MAX + 1];
54 char mnt[PATH_MAX + 1];
56 struct file_handle fh;
61 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
64 scnprintf(path, PATH_MAX, "%s/%s", mnt, cgrp->name);
66 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
67 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0)
70 cgrp->id = handle.cgroup_id;
73 #endif /* HAVE_FILE_HANDLE */
75 #ifndef CGROUP2_SUPER_MAGIC
76 #define CGROUP2_SUPER_MAGIC 0x63677270
79 int cgroup_is_v2(const char *subsys)
81 char mnt[PATH_MAX + 1];
84 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, subsys))
87 if (statfs(mnt, &stbuf) < 0)
90 return (stbuf.f_type == CGROUP2_SUPER_MAGIC);
93 static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str)
95 struct evsel *counter;
97 * check if cgrp is already defined, if so we reuse it
99 evlist__for_each_entry(evlist, counter) {
102 if (!strcmp(counter->cgrp->name, str))
103 return cgroup__get(counter->cgrp);
109 static struct cgroup *cgroup__new(const char *name, bool do_open)
111 struct cgroup *cgroup = zalloc(sizeof(*cgroup));
113 if (cgroup != NULL) {
114 refcount_set(&cgroup->refcnt, 1);
116 cgroup->name = strdup(name);
121 cgroup->fd = open_cgroup(name);
122 if (cgroup->fd == -1)
132 zfree(&cgroup->name);
138 struct cgroup *evlist__findnew_cgroup(struct evlist *evlist, const char *name)
140 struct cgroup *cgroup = evlist__find_cgroup(evlist, name);
142 return cgroup ?: cgroup__new(name, true);
145 static int add_cgroup(struct evlist *evlist, const char *str)
147 struct evsel *counter;
148 struct cgroup *cgrp = evlist__findnew_cgroup(evlist, str);
154 * find corresponding event
155 * if add cgroup N, then need to find event N
158 evlist__for_each_entry(evlist, counter) {
167 counter->cgrp = cgrp;
171 static void cgroup__delete(struct cgroup *cgroup)
175 zfree(&cgroup->name);
179 void cgroup__put(struct cgroup *cgrp)
181 if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
182 cgroup__delete(cgrp);
186 struct cgroup *cgroup__get(struct cgroup *cgroup)
189 refcount_inc(&cgroup->refcnt);
193 static void evsel__set_default_cgroup(struct evsel *evsel, struct cgroup *cgroup)
195 if (evsel->cgrp == NULL)
196 evsel->cgrp = cgroup__get(cgroup);
199 void evlist__set_default_cgroup(struct evlist *evlist, struct cgroup *cgroup)
203 evlist__for_each_entry(evlist, evsel)
204 evsel__set_default_cgroup(evsel, cgroup);
207 /* helper function for ftw() in match_cgroups and list_cgroups */
208 static int add_cgroup_name(const char *fpath, const struct stat *sb __maybe_unused,
209 int typeflag, struct FTW *ftwbuf __maybe_unused)
211 struct cgroup_name *cn;
213 if (typeflag != FTW_D)
216 cn = malloc(sizeof(*cn) + strlen(fpath) + 1);
221 strcpy(cn->name, fpath);
223 list_add_tail(&cn->list, &cgroup_list);
227 static int check_and_add_cgroup_name(const char *fpath)
229 struct cgroup_name *cn;
231 list_for_each_entry(cn, &cgroup_list, list) {
232 if (!strcmp(cn->name, fpath))
236 /* pretend if it's added by ftw() */
237 return add_cgroup_name(fpath, NULL, FTW_D, NULL);
240 static void release_cgroup_list(void)
242 struct cgroup_name *cn;
244 while (!list_empty(&cgroup_list)) {
245 cn = list_first_entry(&cgroup_list, struct cgroup_name, list);
251 /* collect given cgroups only */
252 static int list_cgroups(const char *str)
254 const char *p, *e, *eos = str + strlen(str);
255 struct cgroup_name *cn;
258 /* use given name as is when no regex is given */
260 p = strchr(str, ',');
266 s = strndup(str, e - str);
270 ret = check_and_add_cgroup_name(s);
275 if (check_and_add_cgroup_name("/") < 0)
284 /* these groups will be used */
285 list_for_each_entry(cn, &cgroup_list, list)
291 /* collect all cgroups first and then match with the pattern */
292 static int match_cgroups(const char *str)
295 const char *p, *e, *eos = str + strlen(str);
296 struct cgroup_name *cn;
301 if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event"))
304 /* cgroup_name will have a full path, skip the root directory */
305 prefix_len = strlen(mnt);
307 /* collect all cgroups in the cgroup_list */
308 if (nftw(mnt, add_cgroup_name, 20, 0) < 0)
312 p = strchr(str, ',');
315 /* allow empty cgroups, i.e., skip */
317 /* termination added */
318 s = strndup(str, e - str);
321 if (regcomp(®, s, REG_NOSUB)) {
326 /* check cgroup name with the pattern */
327 list_for_each_entry(cn, &cgroup_list, list) {
328 char *name = cn->name + prefix_len;
330 if (name[0] == '/' && name[1])
332 if (!regexec(®, name, 0, NULL, 0))
338 /* first entry to root cgroup */
339 cn = list_first_entry(&cgroup_list, struct cgroup_name,
351 int parse_cgroups(const struct option *opt, const char *str,
352 int unset __maybe_unused)
354 struct evlist *evlist = *(struct evlist **)opt->value;
355 struct evsel *counter;
356 struct cgroup *cgrp = NULL;
357 const char *p, *e, *eos = str + strlen(str);
361 if (list_empty(&evlist->core.entries)) {
362 fprintf(stderr, "must define events before cgroups\n");
367 p = strchr(str, ',');
370 /* allow empty cgroups, i.e., skip */
372 /* termination added */
373 s = strndup(str, e - str);
376 ret = add_cgroup(evlist, s);
381 /* nr_cgroups is increased een for empty cgroups */
387 /* for the case one cgroup combine to multiple events */
389 if (nr_cgroups == 1) {
390 evlist__for_each_entry(evlist, counter) {
392 cgrp = counter->cgrp;
394 counter->cgrp = cgrp;
395 refcount_inc(&cgrp->refcnt);
403 static bool has_pattern_string(const char *str)
405 return !!strpbrk(str, "{}[]()|*+?^$");
408 int evlist__expand_cgroup(struct evlist *evlist, const char *str,
409 struct rblist *metric_events, bool open_cgroup)
411 struct evlist *orig_list, *tmp_list;
412 struct evsel *pos, *evsel, *leader;
413 struct rblist orig_metric_events;
414 struct cgroup *cgrp = NULL;
415 struct cgroup_name *cn;
419 if (evlist->core.nr_entries == 0) {
420 fprintf(stderr, "must define events before cgroups\n");
424 orig_list = evlist__new();
425 tmp_list = evlist__new();
426 if (orig_list == NULL || tmp_list == NULL) {
427 fprintf(stderr, "memory allocation failed\n");
431 /* save original events and init evlist */
432 evlist__splice_list_tail(orig_list, &evlist->core.entries);
433 evlist->core.nr_entries = 0;
436 orig_metric_events = *metric_events;
437 rblist__init(metric_events);
439 rblist__init(&orig_metric_events);
442 if (has_pattern_string(str))
443 prefix_len = match_cgroups(str);
445 prefix_len = list_cgroups(str);
450 list_for_each_entry(cn, &cgroup_list, list) {
456 /* cgroup_name might have a full path, skip the prefix */
457 name = cn->name + prefix_len;
458 if (name[0] == '/' && name[1])
460 cgrp = cgroup__new(name, open_cgroup);
465 evlist__for_each_entry(orig_list, pos) {
466 evsel = evsel__clone(pos);
470 cgroup__put(evsel->cgrp);
471 evsel->cgrp = cgroup__get(cgrp);
473 if (evsel__is_group_leader(pos))
475 evsel__set_leader(evsel, leader);
477 evlist__add(tmp_list, evsel);
479 /* cgroup__new() has a refcount, release it here */
484 if (metricgroup__copy_metric_events(tmp_list, cgrp,
486 &orig_metric_events) < 0)
490 evlist__splice_list_tail(evlist, &tmp_list->core.entries);
491 tmp_list->core.nr_entries = 0;
494 if (list_empty(&evlist->core.entries)) {
495 fprintf(stderr, "no cgroup matched: %s\n", str);
500 cgrp_event_expanded = true;
503 evlist__delete(orig_list);
504 evlist__delete(tmp_list);
505 rblist__exit(&orig_metric_events);
506 release_cgroup_list();
511 static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id,
512 bool create, const char *path)
514 struct rb_node **p = &root->rb_node;
515 struct rb_node *parent = NULL;
520 cgrp = rb_entry(parent, struct cgroup, node);
534 cgrp = malloc(sizeof(*cgrp));
538 cgrp->name = strdup(path);
539 if (cgrp->name == NULL) {
546 refcount_set(&cgrp->refcnt, 1);
548 rb_link_node(&cgrp->node, parent, p);
549 rb_insert_color(&cgrp->node, root);
554 struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
559 down_write(&env->cgroups.lock);
560 cgrp = __cgroup__findnew(&env->cgroups.tree, id, true, path);
561 up_write(&env->cgroups.lock);
565 struct cgroup *cgroup__find(struct perf_env *env, uint64_t id)
569 down_read(&env->cgroups.lock);
570 cgrp = __cgroup__findnew(&env->cgroups.tree, id, false, NULL);
571 up_read(&env->cgroups.lock);
575 void perf_env__purge_cgroups(struct perf_env *env)
577 struct rb_node *node;
580 down_write(&env->cgroups.lock);
581 while (!RB_EMPTY_ROOT(&env->cgroups.tree)) {
582 node = rb_first(&env->cgroups.tree);
583 cgrp = rb_entry(node, struct cgroup, node);
585 rb_erase(node, &env->cgroups.tree);
588 up_write(&env->cgroups.lock);