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>
22 /* used to match cgroup name with patterns */
24 struct list_head list;
28 static LIST_HEAD(cgroup_list);
30 static int open_cgroup(const char *name)
32 char path[PATH_MAX + 1];
33 char mnt[PATH_MAX + 1];
37 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
40 scnprintf(path, PATH_MAX, "%s/%s", mnt, name);
42 fd = open(path, O_RDONLY);
44 fprintf(stderr, "no access to cgroup %s\n", path);
49 #ifdef HAVE_FILE_HANDLE
50 int read_cgroup_id(struct cgroup *cgrp)
52 char path[PATH_MAX + 1];
53 char mnt[PATH_MAX + 1];
55 struct file_handle fh;
60 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
63 scnprintf(path, PATH_MAX, "%s/%s", mnt, cgrp->name);
65 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
66 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0)
69 cgrp->id = handle.cgroup_id;
72 #endif /* HAVE_FILE_HANDLE */
74 #ifndef CGROUP2_SUPER_MAGIC
75 #define CGROUP2_SUPER_MAGIC 0x63677270
78 int cgroup_is_v2(const char *subsys)
80 char mnt[PATH_MAX + 1];
83 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, subsys))
86 if (statfs(mnt, &stbuf) < 0)
89 return (stbuf.f_type == CGROUP2_SUPER_MAGIC);
92 static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str)
94 struct evsel *counter;
96 * check if cgrp is already defined, if so we reuse it
98 evlist__for_each_entry(evlist, counter) {
101 if (!strcmp(counter->cgrp->name, str))
102 return cgroup__get(counter->cgrp);
108 static struct cgroup *cgroup__new(const char *name, bool do_open)
110 struct cgroup *cgroup = zalloc(sizeof(*cgroup));
112 if (cgroup != NULL) {
113 refcount_set(&cgroup->refcnt, 1);
115 cgroup->name = strdup(name);
120 cgroup->fd = open_cgroup(name);
121 if (cgroup->fd == -1)
131 zfree(&cgroup->name);
137 struct cgroup *evlist__findnew_cgroup(struct evlist *evlist, const char *name)
139 struct cgroup *cgroup = evlist__find_cgroup(evlist, name);
141 return cgroup ?: cgroup__new(name, true);
144 static int add_cgroup(struct evlist *evlist, const char *str)
146 struct evsel *counter;
147 struct cgroup *cgrp = evlist__findnew_cgroup(evlist, str);
153 * find corresponding event
154 * if add cgroup N, then need to find event N
157 evlist__for_each_entry(evlist, counter) {
166 counter->cgrp = cgrp;
170 static void cgroup__delete(struct cgroup *cgroup)
174 zfree(&cgroup->name);
178 void cgroup__put(struct cgroup *cgrp)
180 if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
181 cgroup__delete(cgrp);
185 struct cgroup *cgroup__get(struct cgroup *cgroup)
188 refcount_inc(&cgroup->refcnt);
192 static void evsel__set_default_cgroup(struct evsel *evsel, struct cgroup *cgroup)
194 if (evsel->cgrp == NULL)
195 evsel->cgrp = cgroup__get(cgroup);
198 void evlist__set_default_cgroup(struct evlist *evlist, struct cgroup *cgroup)
202 evlist__for_each_entry(evlist, evsel)
203 evsel__set_default_cgroup(evsel, cgroup);
206 /* helper function for ftw() in match_cgroups and list_cgroups */
207 static int add_cgroup_name(const char *fpath, const struct stat *sb __maybe_unused,
208 int typeflag, struct FTW *ftwbuf __maybe_unused)
210 struct cgroup_name *cn;
212 if (typeflag != FTW_D)
215 cn = malloc(sizeof(*cn) + strlen(fpath) + 1);
220 strcpy(cn->name, fpath);
222 list_add_tail(&cn->list, &cgroup_list);
226 static void release_cgroup_list(void)
228 struct cgroup_name *cn;
230 while (!list_empty(&cgroup_list)) {
231 cn = list_first_entry(&cgroup_list, struct cgroup_name, list);
237 /* collect given cgroups only */
238 static int list_cgroups(const char *str)
240 const char *p, *e, *eos = str + strlen(str);
241 struct cgroup_name *cn;
244 /* use given name as is - for testing purpose */
246 p = strchr(str, ',');
252 s = strndup(str, e - str);
255 /* pretend if it's added by ftw() */
256 ret = add_cgroup_name(s, NULL, FTW_D, NULL);
261 if (add_cgroup_name("", NULL, FTW_D, NULL) < 0)
270 /* these groups will be used */
271 list_for_each_entry(cn, &cgroup_list, list)
277 /* collect all cgroups first and then match with the pattern */
278 static int match_cgroups(const char *str)
281 const char *p, *e, *eos = str + strlen(str);
282 struct cgroup_name *cn;
287 if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event"))
290 /* cgroup_name will have a full path, skip the root directory */
291 prefix_len = strlen(mnt);
293 /* collect all cgroups in the cgroup_list */
294 if (nftw(mnt, add_cgroup_name, 20, 0) < 0)
298 p = strchr(str, ',');
301 /* allow empty cgroups, i.e., skip */
303 /* termination added */
304 s = strndup(str, e - str);
307 if (regcomp(®, s, REG_NOSUB)) {
312 /* check cgroup name with the pattern */
313 list_for_each_entry(cn, &cgroup_list, list) {
314 char *name = cn->name + prefix_len;
316 if (name[0] == '/' && name[1])
318 if (!regexec(®, name, 0, NULL, 0))
324 /* first entry to root cgroup */
325 cn = list_first_entry(&cgroup_list, struct cgroup_name,
337 int parse_cgroups(const struct option *opt, const char *str,
338 int unset __maybe_unused)
340 struct evlist *evlist = *(struct evlist **)opt->value;
341 struct evsel *counter;
342 struct cgroup *cgrp = NULL;
343 const char *p, *e, *eos = str + strlen(str);
347 if (list_empty(&evlist->core.entries)) {
348 fprintf(stderr, "must define events before cgroups\n");
353 p = strchr(str, ',');
356 /* allow empty cgroups, i.e., skip */
358 /* termination added */
359 s = strndup(str, e - str);
362 ret = add_cgroup(evlist, s);
367 /* nr_cgroups is increased een for empty cgroups */
373 /* for the case one cgroup combine to multiple events */
375 if (nr_cgroups == 1) {
376 evlist__for_each_entry(evlist, counter) {
378 cgrp = counter->cgrp;
380 counter->cgrp = cgrp;
381 refcount_inc(&cgrp->refcnt);
389 static bool has_pattern_string(const char *str)
391 return !!strpbrk(str, "{}[]()|*+?^$");
394 int evlist__expand_cgroup(struct evlist *evlist, const char *str,
395 struct rblist *metric_events, bool open_cgroup)
397 struct evlist *orig_list, *tmp_list;
398 struct evsel *pos, *evsel, *leader;
399 struct rblist orig_metric_events;
400 struct cgroup *cgrp = NULL;
401 struct cgroup_name *cn;
405 if (evlist->core.nr_entries == 0) {
406 fprintf(stderr, "must define events before cgroups\n");
410 orig_list = evlist__new();
411 tmp_list = evlist__new();
412 if (orig_list == NULL || tmp_list == NULL) {
413 fprintf(stderr, "memory allocation failed\n");
417 /* save original events and init evlist */
418 evlist__splice_list_tail(orig_list, &evlist->core.entries);
419 evlist->core.nr_entries = 0;
422 orig_metric_events = *metric_events;
423 rblist__init(metric_events);
425 rblist__init(&orig_metric_events);
428 if (has_pattern_string(str))
429 prefix_len = match_cgroups(str);
431 prefix_len = list_cgroups(str);
436 list_for_each_entry(cn, &cgroup_list, list) {
442 /* cgroup_name might have a full path, skip the prefix */
443 name = cn->name + prefix_len;
444 if (name[0] == '/' && name[1])
446 cgrp = cgroup__new(name, open_cgroup);
451 evlist__for_each_entry(orig_list, pos) {
452 evsel = evsel__clone(pos);
456 cgroup__put(evsel->cgrp);
457 evsel->cgrp = cgroup__get(cgrp);
459 if (evsel__is_group_leader(pos))
461 evsel->leader = leader;
463 evlist__add(tmp_list, evsel);
465 /* cgroup__new() has a refcount, release it here */
470 perf_stat__collect_metric_expr(tmp_list);
471 if (metricgroup__copy_metric_events(tmp_list, cgrp,
473 &orig_metric_events) < 0)
477 evlist__splice_list_tail(evlist, &tmp_list->core.entries);
478 tmp_list->core.nr_entries = 0;
481 if (list_empty(&evlist->core.entries)) {
482 fprintf(stderr, "no cgroup matched: %s\n", str);
489 evlist__delete(orig_list);
490 evlist__delete(tmp_list);
491 rblist__exit(&orig_metric_events);
492 release_cgroup_list();
497 static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id,
498 bool create, const char *path)
500 struct rb_node **p = &root->rb_node;
501 struct rb_node *parent = NULL;
506 cgrp = rb_entry(parent, struct cgroup, node);
520 cgrp = malloc(sizeof(*cgrp));
524 cgrp->name = strdup(path);
525 if (cgrp->name == NULL) {
532 refcount_set(&cgrp->refcnt, 1);
534 rb_link_node(&cgrp->node, parent, p);
535 rb_insert_color(&cgrp->node, root);
540 struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
545 down_write(&env->cgroups.lock);
546 cgrp = __cgroup__findnew(&env->cgroups.tree, id, true, path);
547 up_write(&env->cgroups.lock);
551 struct cgroup *cgroup__find(struct perf_env *env, uint64_t id)
555 down_read(&env->cgroups.lock);
556 cgrp = __cgroup__findnew(&env->cgroups.tree, id, false, NULL);
557 up_read(&env->cgroups.lock);
561 void perf_env__purge_cgroups(struct perf_env *env)
563 struct rb_node *node;
566 down_write(&env->cgroups.lock);
567 while (!RB_EMPTY_ROOT(&env->cgroups.tree)) {
568 node = rb_first(&env->cgroups.tree);
569 cgrp = rb_entry(node, struct cgroup, node);
571 rb_erase(node, &env->cgroups.tree);
574 up_write(&env->cgroups.lock);