Merge tag 'amd-drm-fixes-5.12-2021-03-03' of https://gitlab.freedesktop.org/agd5f...
[linux-2.6-microblaze.git] / tools / perf / util / cgroup.c
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>
18
19 int nr_cgroups;
20
21 /* used to match cgroup name with patterns */
22 struct cgroup_name {
23         struct list_head list;
24         bool used;
25         char name[];
26 };
27 static LIST_HEAD(cgroup_list);
28
29 static int open_cgroup(const char *name)
30 {
31         char path[PATH_MAX + 1];
32         char mnt[PATH_MAX + 1];
33         int fd;
34
35
36         if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
37                 return -1;
38
39         scnprintf(path, PATH_MAX, "%s/%s", mnt, name);
40
41         fd = open(path, O_RDONLY);
42         if (fd == -1)
43                 fprintf(stderr, "no access to cgroup %s\n", path);
44
45         return fd;
46 }
47
48 static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str)
49 {
50         struct evsel *counter;
51         /*
52          * check if cgrp is already defined, if so we reuse it
53          */
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);
59         }
60
61         return NULL;
62 }
63
64 static struct cgroup *cgroup__new(const char *name, bool do_open)
65 {
66         struct cgroup *cgroup = zalloc(sizeof(*cgroup));
67
68         if (cgroup != NULL) {
69                 refcount_set(&cgroup->refcnt, 1);
70
71                 cgroup->name = strdup(name);
72                 if (!cgroup->name)
73                         goto out_err;
74
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;
81                 }
82         }
83
84         return cgroup;
85
86 out_free_name:
87         zfree(&cgroup->name);
88 out_err:
89         free(cgroup);
90         return NULL;
91 }
92
93 struct cgroup *evlist__findnew_cgroup(struct evlist *evlist, const char *name)
94 {
95         struct cgroup *cgroup = evlist__find_cgroup(evlist, name);
96
97         return cgroup ?: cgroup__new(name, true);
98 }
99
100 static int add_cgroup(struct evlist *evlist, const char *str)
101 {
102         struct evsel *counter;
103         struct cgroup *cgrp = evlist__findnew_cgroup(evlist, str);
104         int n;
105
106         if (!cgrp)
107                 return -1;
108         /*
109          * find corresponding event
110          * if add cgroup N, then need to find event N
111          */
112         n = 0;
113         evlist__for_each_entry(evlist, counter) {
114                 if (n == nr_cgroups)
115                         goto found;
116                 n++;
117         }
118
119         cgroup__put(cgrp);
120         return -1;
121 found:
122         counter->cgrp = cgrp;
123         return 0;
124 }
125
126 static void cgroup__delete(struct cgroup *cgroup)
127 {
128         if (cgroup->fd >= 0)
129                 close(cgroup->fd);
130         zfree(&cgroup->name);
131         free(cgroup);
132 }
133
134 void cgroup__put(struct cgroup *cgrp)
135 {
136         if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
137                 cgroup__delete(cgrp);
138         }
139 }
140
141 struct cgroup *cgroup__get(struct cgroup *cgroup)
142 {
143        if (cgroup)
144                 refcount_inc(&cgroup->refcnt);
145        return cgroup;
146 }
147
148 static void evsel__set_default_cgroup(struct evsel *evsel, struct cgroup *cgroup)
149 {
150         if (evsel->cgrp == NULL)
151                 evsel->cgrp = cgroup__get(cgroup);
152 }
153
154 void evlist__set_default_cgroup(struct evlist *evlist, struct cgroup *cgroup)
155 {
156         struct evsel *evsel;
157
158         evlist__for_each_entry(evlist, evsel)
159                 evsel__set_default_cgroup(evsel, cgroup);
160 }
161
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, struct FTW *ftwbuf __maybe_unused)
165 {
166         struct cgroup_name *cn;
167
168         if (typeflag != FTW_D)
169                 return 0;
170
171         cn = malloc(sizeof(*cn) + strlen(fpath) + 1);
172         if (cn == NULL)
173                 return -1;
174
175         cn->used = false;
176         strcpy(cn->name, fpath);
177
178         list_add_tail(&cn->list, &cgroup_list);
179         return 0;
180 }
181
182 static void release_cgroup_list(void)
183 {
184         struct cgroup_name *cn;
185
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);
190         }
191 }
192
193 /* collect given cgroups only */
194 static int list_cgroups(const char *str)
195 {
196         const char *p, *e, *eos = str + strlen(str);
197         struct cgroup_name *cn;
198         char *s;
199
200         /* use given name as is - for testing purpose */
201         for (;;) {
202                 p = strchr(str, ',');
203                 e = p ? p : eos;
204
205                 if (e - str) {
206                         int ret;
207
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, NULL);
213                         free(s);
214                         if (ret)
215                                 return -1;
216                 } else {
217                         if (add_cgroup_name("", NULL, FTW_D, NULL) < 0)
218                                 return -1;
219                 }
220
221                 if (!p)
222                         break;
223                 str = p+1;
224         }
225
226         /* these groups will be used */
227         list_for_each_entry(cn, &cgroup_list, list)
228                 cn->used = true;
229
230         return 0;
231 }
232
233 /* collect all cgroups first and then match with the pattern */
234 static int match_cgroups(const char *str)
235 {
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;
242
243         if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event"))
244                 return -1;
245
246         /* cgroup_name will have a full path, skip the root directory */
247         prefix_len = strlen(mnt);
248
249         /* collect all cgroups in the cgroup_list */
250         if (nftw(mnt, add_cgroup_name, 20, 0) < 0)
251                 return -1;
252
253         for (;;) {
254                 p = strchr(str, ',');
255                 e = p ? p : eos;
256
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;
266                         }
267
268                         /* check cgroup name with the pattern */
269                         list_for_each_entry(cn, &cgroup_list, list) {
270                                 char *name = cn->name + prefix_len;
271
272                                 if (name[0] == '/' && name[1])
273                                         name++;
274                                 if (!regexec(&reg, name, 0, NULL, 0))
275                                         cn->used = true;
276                         }
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;
284                 }
285
286                 if (!p)
287                         break;
288                 str = p+1;
289         }
290         return prefix_len;
291 }
292
293 int parse_cgroups(const struct option *opt, const char *str,
294                   int unset __maybe_unused)
295 {
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;
302
303         if (list_empty(&evlist->core.entries)) {
304                 fprintf(stderr, "must define events before cgroups\n");
305                 return -1;
306         }
307
308         for (;;) {
309                 p = strchr(str, ',');
310                 e = p ? p : eos;
311
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;
322                 }
323                 /* nr_cgroups is increased een for empty cgroups */
324                 nr_cgroups++;
325                 if (!p)
326                         break;
327                 str = p+1;
328         }
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);
338                         }
339                         i++;
340                 }
341         }
342         return 0;
343 }
344
345 static bool has_pattern_string(const char *str)
346 {
347         return !!strpbrk(str, "{}[]()|*+?^$");
348 }
349
350 int evlist__expand_cgroup(struct evlist *evlist, const char *str,
351                           struct rblist *metric_events, bool open_cgroup)
352 {
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;
360
361         if (evlist->core.nr_entries == 0) {
362                 fprintf(stderr, "must define events before cgroups\n");
363                 return -EINVAL;
364         }
365
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;
371         }
372
373         /* save original events and init evlist */
374         evlist__splice_list_tail(orig_list, &evlist->core.entries);
375         evlist->core.nr_entries = 0;
376
377         if (metric_events) {
378                 orig_metric_events = *metric_events;
379                 rblist__init(metric_events);
380         } else {
381                 rblist__init(&orig_metric_events);
382         }
383
384         if (has_pattern_string(str))
385                 prefix_len = match_cgroups(str);
386         else
387                 prefix_len = list_cgroups(str);
388
389         if (prefix_len < 0)
390                 goto out_err;
391
392         list_for_each_entry(cn, &cgroup_list, list) {
393                 char *name;
394
395                 if (!cn->used)
396                         continue;
397
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;
405
406                 leader = NULL;
407                 evlist__for_each_entry(orig_list, pos) {
408                         evsel = evsel__clone(pos);
409                         if (evsel == NULL)
410                                 goto out_err;
411
412                         cgroup__put(evsel->cgrp);
413                         evsel->cgrp = cgroup__get(cgrp);
414
415                         if (evsel__is_group_leader(pos))
416                                 leader = evsel;
417                         evsel->leader = leader;
418
419                         evlist__add(tmp_list, evsel);
420                 }
421                 /* cgroup__new() has a refcount, release it here */
422                 cgroup__put(cgrp);
423                 nr_cgroups++;
424
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;
431                 }
432
433                 evlist__splice_list_tail(evlist, &tmp_list->core.entries);
434                 tmp_list->core.nr_entries = 0;
435         }
436
437         if (list_empty(&evlist->core.entries)) {
438                 fprintf(stderr, "no cgroup matched: %s\n", str);
439                 goto out_err;
440         }
441
442         ret = 0;
443
444 out_err:
445         evlist__delete(orig_list);
446         evlist__delete(tmp_list);
447         rblist__exit(&orig_metric_events);
448         release_cgroup_list();
449
450         return ret;
451 }
452
453 static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id,
454                                         bool create, const char *path)
455 {
456         struct rb_node **p = &root->rb_node;
457         struct rb_node *parent = NULL;
458         struct cgroup *cgrp;
459
460         while (*p != NULL) {
461                 parent = *p;
462                 cgrp = rb_entry(parent, struct cgroup, node);
463
464                 if (cgrp->id == id)
465                         return cgrp;
466
467                 if (cgrp->id < id)
468                         p = &(*p)->rb_left;
469                 else
470                         p = &(*p)->rb_right;
471         }
472
473         if (!create)
474                 return NULL;
475
476         cgrp = malloc(sizeof(*cgrp));
477         if (cgrp == NULL)
478                 return NULL;
479
480         cgrp->name = strdup(path);
481         if (cgrp->name == NULL) {
482                 free(cgrp);
483                 return NULL;
484         }
485
486         cgrp->fd = -1;
487         cgrp->id = id;
488         refcount_set(&cgrp->refcnt, 1);
489
490         rb_link_node(&cgrp->node, parent, p);
491         rb_insert_color(&cgrp->node, root);
492
493         return cgrp;
494 }
495
496 struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
497                                const char *path)
498 {
499         struct cgroup *cgrp;
500
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;
505 }
506
507 struct cgroup *cgroup__find(struct perf_env *env, uint64_t id)
508 {
509         struct cgroup *cgrp;
510
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;
515 }
516
517 void perf_env__purge_cgroups(struct perf_env *env)
518 {
519         struct rb_node *node;
520         struct cgroup *cgrp;
521
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);
526
527                 rb_erase(node, &env->cgroups.tree);
528                 cgroup__put(cgrp);
529         }
530         up_write(&env->cgroups.lock);
531 }