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 void release_cgroup_list(void)
229 struct cgroup_name *cn;
231 while (!list_empty(&cgroup_list)) {
232 cn = list_first_entry(&cgroup_list, struct cgroup_name, list);
238 /* collect given cgroups only */
239 static int list_cgroups(const char *str)
241 const char *p, *e, *eos = str + strlen(str);
242 struct cgroup_name *cn;
245 /* use given name as is - for testing purpose */
247 p = strchr(str, ',');
253 s = strndup(str, e - str);
256 /* pretend if it's added by ftw() */
257 ret = add_cgroup_name(s, NULL, FTW_D, NULL);
262 if (add_cgroup_name("", NULL, FTW_D, NULL) < 0)
271 /* these groups will be used */
272 list_for_each_entry(cn, &cgroup_list, list)
278 /* collect all cgroups first and then match with the pattern */
279 static int match_cgroups(const char *str)
282 const char *p, *e, *eos = str + strlen(str);
283 struct cgroup_name *cn;
288 if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event"))
291 /* cgroup_name will have a full path, skip the root directory */
292 prefix_len = strlen(mnt);
294 /* collect all cgroups in the cgroup_list */
295 if (nftw(mnt, add_cgroup_name, 20, 0) < 0)
299 p = strchr(str, ',');
302 /* allow empty cgroups, i.e., skip */
304 /* termination added */
305 s = strndup(str, e - str);
308 if (regcomp(®, s, REG_NOSUB)) {
313 /* check cgroup name with the pattern */
314 list_for_each_entry(cn, &cgroup_list, list) {
315 char *name = cn->name + prefix_len;
317 if (name[0] == '/' && name[1])
319 if (!regexec(®, name, 0, NULL, 0))
325 /* first entry to root cgroup */
326 cn = list_first_entry(&cgroup_list, struct cgroup_name,
338 int parse_cgroups(const struct option *opt, const char *str,
339 int unset __maybe_unused)
341 struct evlist *evlist = *(struct evlist **)opt->value;
342 struct evsel *counter;
343 struct cgroup *cgrp = NULL;
344 const char *p, *e, *eos = str + strlen(str);
348 if (list_empty(&evlist->core.entries)) {
349 fprintf(stderr, "must define events before cgroups\n");
354 p = strchr(str, ',');
357 /* allow empty cgroups, i.e., skip */
359 /* termination added */
360 s = strndup(str, e - str);
363 ret = add_cgroup(evlist, s);
368 /* nr_cgroups is increased een for empty cgroups */
374 /* for the case one cgroup combine to multiple events */
376 if (nr_cgroups == 1) {
377 evlist__for_each_entry(evlist, counter) {
379 cgrp = counter->cgrp;
381 counter->cgrp = cgrp;
382 refcount_inc(&cgrp->refcnt);
390 static bool has_pattern_string(const char *str)
392 return !!strpbrk(str, "{}[]()|*+?^$");
395 int evlist__expand_cgroup(struct evlist *evlist, const char *str,
396 struct rblist *metric_events, bool open_cgroup)
398 struct evlist *orig_list, *tmp_list;
399 struct evsel *pos, *evsel, *leader;
400 struct rblist orig_metric_events;
401 struct cgroup *cgrp = NULL;
402 struct cgroup_name *cn;
406 if (evlist->core.nr_entries == 0) {
407 fprintf(stderr, "must define events before cgroups\n");
411 orig_list = evlist__new();
412 tmp_list = evlist__new();
413 if (orig_list == NULL || tmp_list == NULL) {
414 fprintf(stderr, "memory allocation failed\n");
418 /* save original events and init evlist */
419 evlist__splice_list_tail(orig_list, &evlist->core.entries);
420 evlist->core.nr_entries = 0;
423 orig_metric_events = *metric_events;
424 rblist__init(metric_events);
426 rblist__init(&orig_metric_events);
429 if (has_pattern_string(str))
430 prefix_len = match_cgroups(str);
432 prefix_len = list_cgroups(str);
437 list_for_each_entry(cn, &cgroup_list, list) {
443 /* cgroup_name might have a full path, skip the prefix */
444 name = cn->name + prefix_len;
445 if (name[0] == '/' && name[1])
447 cgrp = cgroup__new(name, open_cgroup);
452 evlist__for_each_entry(orig_list, pos) {
453 evsel = evsel__clone(pos);
457 cgroup__put(evsel->cgrp);
458 evsel->cgrp = cgroup__get(cgrp);
460 if (evsel__is_group_leader(pos))
462 evsel__set_leader(evsel, leader);
464 evlist__add(tmp_list, evsel);
466 /* cgroup__new() has a refcount, release it here */
471 perf_stat__collect_metric_expr(tmp_list);
472 if (metricgroup__copy_metric_events(tmp_list, cgrp,
474 &orig_metric_events) < 0)
478 evlist__splice_list_tail(evlist, &tmp_list->core.entries);
479 tmp_list->core.nr_entries = 0;
482 if (list_empty(&evlist->core.entries)) {
483 fprintf(stderr, "no cgroup matched: %s\n", str);
488 cgrp_event_expanded = true;
491 evlist__delete(orig_list);
492 evlist__delete(tmp_list);
493 rblist__exit(&orig_metric_events);
494 release_cgroup_list();
499 static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id,
500 bool create, const char *path)
502 struct rb_node **p = &root->rb_node;
503 struct rb_node *parent = NULL;
508 cgrp = rb_entry(parent, struct cgroup, node);
522 cgrp = malloc(sizeof(*cgrp));
526 cgrp->name = strdup(path);
527 if (cgrp->name == NULL) {
534 refcount_set(&cgrp->refcnt, 1);
536 rb_link_node(&cgrp->node, parent, p);
537 rb_insert_color(&cgrp->node, root);
542 struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
547 down_write(&env->cgroups.lock);
548 cgrp = __cgroup__findnew(&env->cgroups.tree, id, true, path);
549 up_write(&env->cgroups.lock);
553 struct cgroup *cgroup__find(struct perf_env *env, uint64_t id)
557 down_read(&env->cgroups.lock);
558 cgrp = __cgroup__findnew(&env->cgroups.tree, id, false, NULL);
559 up_read(&env->cgroups.lock);
563 void perf_env__purge_cgroups(struct perf_env *env)
565 struct rb_node *node;
568 down_write(&env->cgroups.lock);
569 while (!RB_EMPTY_ROOT(&env->cgroups.tree)) {
570 node = rb_first(&env->cgroups.tree);
571 cgrp = rb_entry(node, struct cgroup, node);
573 rb_erase(node, &env->cgroups.tree);
576 up_write(&env->cgroups.lock);