Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / cgroup.c
blob5dff7e489921bd3fdce8224ca33db2b45cd82468
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 <fcntl.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <api/fs/fs.h>
16 #include <ftw.h>
17 #include <regex.h>
19 int nr_cgroups;
21 /* used to match cgroup name with patterns */
22 struct cgroup_name {
23 struct list_head list;
24 bool used;
25 char name[];
27 static LIST_HEAD(cgroup_list);
29 static int open_cgroup(const char *name)
31 char path[PATH_MAX + 1];
32 char mnt[PATH_MAX + 1];
33 int fd;
36 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
37 return -1;
39 scnprintf(path, PATH_MAX, "%s/%s", mnt, name);
41 fd = open(path, O_RDONLY);
42 if (fd == -1)
43 fprintf(stderr, "no access to cgroup %s\n", path);
45 return fd;
48 static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str)
50 struct evsel *counter;
52 * check if cgrp is already defined, if so we reuse it
54 evlist__for_each_entry(evlist, counter) {
55 if (!counter->cgrp)
56 continue;
57 if (!strcmp(counter->cgrp->name, str))
58 return cgroup__get(counter->cgrp);
61 return NULL;
64 static struct cgroup *cgroup__new(const char *name, bool do_open)
66 struct cgroup *cgroup = zalloc(sizeof(*cgroup));
68 if (cgroup != NULL) {
69 refcount_set(&cgroup->refcnt, 1);
71 cgroup->name = strdup(name);
72 if (!cgroup->name)
73 goto out_err;
75 if (do_open) {
76 cgroup->fd = open_cgroup(name);
77 if (cgroup->fd == -1)
78 goto out_free_name;
79 } else {
80 cgroup->fd = -1;
84 return cgroup;
86 out_free_name:
87 zfree(&cgroup->name);
88 out_err:
89 free(cgroup);
90 return NULL;
93 struct cgroup *evlist__findnew_cgroup(struct evlist *evlist, const char *name)
95 struct cgroup *cgroup = evlist__find_cgroup(evlist, name);
97 return cgroup ?: cgroup__new(name, true);
100 static int add_cgroup(struct evlist *evlist, const char *str)
102 struct evsel *counter;
103 struct cgroup *cgrp = evlist__findnew_cgroup(evlist, str);
104 int n;
106 if (!cgrp)
107 return -1;
109 * find corresponding event
110 * if add cgroup N, then need to find event N
112 n = 0;
113 evlist__for_each_entry(evlist, counter) {
114 if (n == nr_cgroups)
115 goto found;
116 n++;
119 cgroup__put(cgrp);
120 return -1;
121 found:
122 counter->cgrp = cgrp;
123 return 0;
126 static void cgroup__delete(struct cgroup *cgroup)
128 if (cgroup->fd >= 0)
129 close(cgroup->fd);
130 zfree(&cgroup->name);
131 free(cgroup);
134 void cgroup__put(struct cgroup *cgrp)
136 if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
137 cgroup__delete(cgrp);
141 struct cgroup *cgroup__get(struct cgroup *cgroup)
143 if (cgroup)
144 refcount_inc(&cgroup->refcnt);
145 return cgroup;
148 static void evsel__set_default_cgroup(struct evsel *evsel, struct cgroup *cgroup)
150 if (evsel->cgrp == NULL)
151 evsel->cgrp = cgroup__get(cgroup);
154 void evlist__set_default_cgroup(struct evlist *evlist, struct cgroup *cgroup)
156 struct evsel *evsel;
158 evlist__for_each_entry(evlist, evsel)
159 evsel__set_default_cgroup(evsel, cgroup);
162 /* helper function for ftw() in match_cgroups and list_cgroups */
163 static int add_cgroup_name(const char *fpath, const struct stat *sb __maybe_unused,
164 int typeflag)
166 struct cgroup_name *cn;
168 if (typeflag != FTW_D)
169 return 0;
171 cn = malloc(sizeof(*cn) + strlen(fpath) + 1);
172 if (cn == NULL)
173 return -1;
175 cn->used = false;
176 strcpy(cn->name, fpath);
178 list_add_tail(&cn->list, &cgroup_list);
179 return 0;
182 static void release_cgroup_list(void)
184 struct cgroup_name *cn;
186 while (!list_empty(&cgroup_list)) {
187 cn = list_first_entry(&cgroup_list, struct cgroup_name, list);
188 list_del(&cn->list);
189 free(cn);
193 /* collect given cgroups only */
194 static int list_cgroups(const char *str)
196 const char *p, *e, *eos = str + strlen(str);
197 struct cgroup_name *cn;
198 char *s;
200 /* use given name as is - for testing purpose */
201 for (;;) {
202 p = strchr(str, ',');
203 e = p ? p : eos;
205 if (e - str) {
206 int ret;
208 s = strndup(str, e - str);
209 if (!s)
210 return -1;
211 /* pretend if it's added by ftw() */
212 ret = add_cgroup_name(s, NULL, FTW_D);
213 free(s);
214 if (ret)
215 return -1;
216 } else {
217 if (add_cgroup_name("", NULL, FTW_D) < 0)
218 return -1;
221 if (!p)
222 break;
223 str = p+1;
226 /* these groups will be used */
227 list_for_each_entry(cn, &cgroup_list, list)
228 cn->used = true;
230 return 0;
233 /* collect all cgroups first and then match with the pattern */
234 static int match_cgroups(const char *str)
236 char mnt[PATH_MAX];
237 const char *p, *e, *eos = str + strlen(str);
238 struct cgroup_name *cn;
239 regex_t reg;
240 int prefix_len;
241 char *s;
243 if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event"))
244 return -1;
246 /* cgroup_name will have a full path, skip the root directory */
247 prefix_len = strlen(mnt);
249 /* collect all cgroups in the cgroup_list */
250 if (ftw(mnt, add_cgroup_name, 20) < 0)
251 return -1;
253 for (;;) {
254 p = strchr(str, ',');
255 e = p ? p : eos;
257 /* allow empty cgroups, i.e., skip */
258 if (e - str) {
259 /* termination added */
260 s = strndup(str, e - str);
261 if (!s)
262 return -1;
263 if (regcomp(&reg, s, REG_NOSUB)) {
264 free(s);
265 return -1;
268 /* check cgroup name with the pattern */
269 list_for_each_entry(cn, &cgroup_list, list) {
270 char *name = cn->name + prefix_len;
272 if (name[0] == '/' && name[1])
273 name++;
274 if (!regexec(&reg, name, 0, NULL, 0))
275 cn->used = true;
277 regfree(&reg);
278 free(s);
279 } else {
280 /* first entry to root cgroup */
281 cn = list_first_entry(&cgroup_list, struct cgroup_name,
282 list);
283 cn->used = true;
286 if (!p)
287 break;
288 str = p+1;
290 return prefix_len;
293 int parse_cgroups(const struct option *opt, const char *str,
294 int unset __maybe_unused)
296 struct evlist *evlist = *(struct evlist **)opt->value;
297 struct evsel *counter;
298 struct cgroup *cgrp = NULL;
299 const char *p, *e, *eos = str + strlen(str);
300 char *s;
301 int ret, i;
303 if (list_empty(&evlist->core.entries)) {
304 fprintf(stderr, "must define events before cgroups\n");
305 return -1;
308 for (;;) {
309 p = strchr(str, ',');
310 e = p ? p : eos;
312 /* allow empty cgroups, i.e., skip */
313 if (e - str) {
314 /* termination added */
315 s = strndup(str, e - str);
316 if (!s)
317 return -1;
318 ret = add_cgroup(evlist, s);
319 free(s);
320 if (ret)
321 return -1;
323 /* nr_cgroups is increased een for empty cgroups */
324 nr_cgroups++;
325 if (!p)
326 break;
327 str = p+1;
329 /* for the case one cgroup combine to multiple events */
330 i = 0;
331 if (nr_cgroups == 1) {
332 evlist__for_each_entry(evlist, counter) {
333 if (i == 0)
334 cgrp = counter->cgrp;
335 else {
336 counter->cgrp = cgrp;
337 refcount_inc(&cgrp->refcnt);
339 i++;
342 return 0;
345 static bool has_pattern_string(const char *str)
347 return !!strpbrk(str, "{}[]()|*+?^$");
350 int evlist__expand_cgroup(struct evlist *evlist, const char *str,
351 struct rblist *metric_events, bool open_cgroup)
353 struct evlist *orig_list, *tmp_list;
354 struct evsel *pos, *evsel, *leader;
355 struct rblist orig_metric_events;
356 struct cgroup *cgrp = NULL;
357 struct cgroup_name *cn;
358 int ret = -1;
359 int prefix_len;
361 if (evlist->core.nr_entries == 0) {
362 fprintf(stderr, "must define events before cgroups\n");
363 return -EINVAL;
366 orig_list = evlist__new();
367 tmp_list = evlist__new();
368 if (orig_list == NULL || tmp_list == NULL) {
369 fprintf(stderr, "memory allocation failed\n");
370 return -ENOMEM;
373 /* save original events and init evlist */
374 evlist__splice_list_tail(orig_list, &evlist->core.entries);
375 evlist->core.nr_entries = 0;
377 if (metric_events) {
378 orig_metric_events = *metric_events;
379 rblist__init(metric_events);
380 } else {
381 rblist__init(&orig_metric_events);
384 if (has_pattern_string(str))
385 prefix_len = match_cgroups(str);
386 else
387 prefix_len = list_cgroups(str);
389 if (prefix_len < 0)
390 goto out_err;
392 list_for_each_entry(cn, &cgroup_list, list) {
393 char *name;
395 if (!cn->used)
396 continue;
398 /* cgroup_name might have a full path, skip the prefix */
399 name = cn->name + prefix_len;
400 if (name[0] == '/' && name[1])
401 name++;
402 cgrp = cgroup__new(name, open_cgroup);
403 if (cgrp == NULL)
404 goto out_err;
406 leader = NULL;
407 evlist__for_each_entry(orig_list, pos) {
408 evsel = evsel__clone(pos);
409 if (evsel == NULL)
410 goto out_err;
412 cgroup__put(evsel->cgrp);
413 evsel->cgrp = cgroup__get(cgrp);
415 if (evsel__is_group_leader(pos))
416 leader = evsel;
417 evsel->leader = leader;
419 evlist__add(tmp_list, evsel);
421 /* cgroup__new() has a refcount, release it here */
422 cgroup__put(cgrp);
423 nr_cgroups++;
425 if (metric_events) {
426 perf_stat__collect_metric_expr(tmp_list);
427 if (metricgroup__copy_metric_events(tmp_list, cgrp,
428 metric_events,
429 &orig_metric_events) < 0)
430 goto out_err;
433 evlist__splice_list_tail(evlist, &tmp_list->core.entries);
434 tmp_list->core.nr_entries = 0;
437 if (list_empty(&evlist->core.entries)) {
438 fprintf(stderr, "no cgroup matched: %s\n", str);
439 goto out_err;
442 ret = 0;
444 out_err:
445 evlist__delete(orig_list);
446 evlist__delete(tmp_list);
447 rblist__exit(&orig_metric_events);
448 release_cgroup_list();
450 return ret;
453 static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id,
454 bool create, const char *path)
456 struct rb_node **p = &root->rb_node;
457 struct rb_node *parent = NULL;
458 struct cgroup *cgrp;
460 while (*p != NULL) {
461 parent = *p;
462 cgrp = rb_entry(parent, struct cgroup, node);
464 if (cgrp->id == id)
465 return cgrp;
467 if (cgrp->id < id)
468 p = &(*p)->rb_left;
469 else
470 p = &(*p)->rb_right;
473 if (!create)
474 return NULL;
476 cgrp = malloc(sizeof(*cgrp));
477 if (cgrp == NULL)
478 return NULL;
480 cgrp->name = strdup(path);
481 if (cgrp->name == NULL) {
482 free(cgrp);
483 return NULL;
486 cgrp->fd = -1;
487 cgrp->id = id;
488 refcount_set(&cgrp->refcnt, 1);
490 rb_link_node(&cgrp->node, parent, p);
491 rb_insert_color(&cgrp->node, root);
493 return cgrp;
496 struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
497 const char *path)
499 struct cgroup *cgrp;
501 down_write(&env->cgroups.lock);
502 cgrp = __cgroup__findnew(&env->cgroups.tree, id, true, path);
503 up_write(&env->cgroups.lock);
504 return cgrp;
507 struct cgroup *cgroup__find(struct perf_env *env, uint64_t id)
509 struct cgroup *cgrp;
511 down_read(&env->cgroups.lock);
512 cgrp = __cgroup__findnew(&env->cgroups.tree, id, false, NULL);
513 up_read(&env->cgroups.lock);
514 return cgrp;
517 void perf_env__purge_cgroups(struct perf_env *env)
519 struct rb_node *node;
520 struct cgroup *cgrp;
522 down_write(&env->cgroups.lock);
523 while (!RB_EMPTY_ROOT(&env->cgroups.tree)) {
524 node = rb_first(&env->cgroups.tree);
525 cgrp = rb_entry(node, struct cgroup, node);
527 rb_erase(node, &env->cgroups.tree);
528 cgroup__put(cgrp);
530 up_write(&env->cgroups.lock);