blob: decb91f9da82b54db7460beb182d9aa2598e18f4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Stephane Eranian023695d2011-02-14 11:20:01 +02002#include "util.h"
3#include "../perf.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06004#include <subcmd/parse-options.h>
Stephane Eranian023695d2011-02-14 11:20:01 +02005#include "evsel.h"
6#include "cgroup.h"
Stephane Eranian023695d2011-02-14 11:20:01 +02007#include "evlist.h"
Arnaldo Carvalho de Meloaa8cc2f2017-04-17 15:36:40 -03008#include <linux/stringify.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03009#include <sys/types.h>
10#include <sys/stat.h>
11#include <fcntl.h>
Stephane Eranian023695d2011-02-14 11:20:01 +020012
13int nr_cgroups;
14
15static int
16cgroupfs_find_mountpoint(char *buf, size_t maxlen)
17{
18 FILE *fp;
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020019 char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
Tejun Heo968ebff2017-01-29 14:35:20 -050020 char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path;
Eric Dumazet621d2652011-04-08 12:11:06 +020021 char *token, *saved_ptr = NULL;
Stephane Eranian023695d2011-02-14 11:20:01 +020022
23 fp = fopen("/proc/mounts", "r");
24 if (!fp)
25 return -1;
26
27 /*
28 * in order to handle split hierarchy, we need to scan /proc/mounts
29 * and inspect every cgroupfs mount point to find one that has
30 * perf_event subsystem
31 */
Tejun Heo968ebff2017-01-29 14:35:20 -050032 path_v1[0] = '\0';
33 path_v2[0] = '\0';
34
Arnaldo Carvalho de Meloaa8cc2f2017-04-17 15:36:40 -030035 while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %"
36 __stringify(PATH_MAX)"s %*d %*d\n",
Stephane Eranian023695d2011-02-14 11:20:01 +020037 mountpoint, type, tokens) == 3) {
38
Tejun Heo968ebff2017-01-29 14:35:20 -050039 if (!path_v1[0] && !strcmp(type, "cgroup")) {
Stephane Eranian023695d2011-02-14 11:20:01 +020040
41 token = strtok_r(tokens, ",", &saved_ptr);
42
43 while (token != NULL) {
44 if (!strcmp(token, "perf_event")) {
Tejun Heo968ebff2017-01-29 14:35:20 -050045 strcpy(path_v1, mountpoint);
Stephane Eranian023695d2011-02-14 11:20:01 +020046 break;
47 }
48 token = strtok_r(NULL, ",", &saved_ptr);
49 }
50 }
Tejun Heo968ebff2017-01-29 14:35:20 -050051
52 if (!path_v2[0] && !strcmp(type, "cgroup2"))
53 strcpy(path_v2, mountpoint);
54
55 if (path_v1[0] && path_v2[0])
Stephane Eranian023695d2011-02-14 11:20:01 +020056 break;
57 }
58 fclose(fp);
Tejun Heo968ebff2017-01-29 14:35:20 -050059
60 if (path_v1[0])
61 path = path_v1;
62 else if (path_v2[0])
63 path = path_v2;
64 else
Stephane Eranian023695d2011-02-14 11:20:01 +020065 return -1;
66
Tejun Heo968ebff2017-01-29 14:35:20 -050067 if (strlen(path) < maxlen) {
68 strcpy(buf, path);
Stephane Eranian023695d2011-02-14 11:20:01 +020069 return 0;
70 }
71 return -1;
72}
73
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -030074static int open_cgroup(const char *name)
Stephane Eranian023695d2011-02-14 11:20:01 +020075{
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020076 char path[PATH_MAX + 1];
77 char mnt[PATH_MAX + 1];
Stephane Eranian023695d2011-02-14 11:20:01 +020078 int fd;
79
80
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020081 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
Stephane Eranian023695d2011-02-14 11:20:01 +020082 return -1;
83
Jiri Olsa77f18152018-03-19 09:29:01 +010084 scnprintf(path, PATH_MAX, "%s/%s", mnt, name);
Stephane Eranian023695d2011-02-14 11:20:01 +020085
86 fd = open(path, O_RDONLY);
87 if (fd == -1)
88 fprintf(stderr, "no access to cgroup %s\n", path);
89
90 return fd;
91}
92
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -030093static struct cgroup *evlist__find_cgroup(struct perf_evlist *evlist, const char *str)
Stephane Eranian023695d2011-02-14 11:20:01 +020094{
95 struct perf_evsel *counter;
Arnaldo Carvalho de Melo3ca32f62018-03-06 09:51:48 -030096 struct cgroup *cgrp = NULL;
Stephane Eranian023695d2011-02-14 11:20:01 +020097 /*
98 * check if cgrp is already defined, if so we reuse it
99 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300100 evlist__for_each_entry(evlist, counter) {
Arnaldo Carvalho de Melofc9ffb92018-03-06 10:18:40 -0300101 if (!counter->cgrp)
Stephane Eranian023695d2011-02-14 11:20:01 +0200102 continue;
Arnaldo Carvalho de Melofc9ffb92018-03-06 10:18:40 -0300103 if (!strcmp(counter->cgrp->name, str)) {
104 cgrp = cgroup__get(counter->cgrp);
Stephane Eranian023695d2011-02-14 11:20:01 +0200105 break;
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300106 }
Stephane Eranian023695d2011-02-14 11:20:01 +0200107 }
108
Arnaldo Carvalho de Melob80271f2018-03-06 10:27:00 -0300109 return cgrp;
110}
111
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -0300112static struct cgroup *cgroup__new(const char *name)
Arnaldo Carvalho de Melo923a0fb32018-03-06 10:33:04 -0300113{
114 struct cgroup *cgroup = zalloc(sizeof(*cgroup));
115
116 if (cgroup != NULL) {
Arnaldo Carvalho de Melo923a0fb32018-03-06 10:33:04 -0300117 refcount_set(&cgroup->refcnt, 1);
118
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -0300119 cgroup->name = strdup(name);
120 if (!cgroup->name)
121 goto out_err;
Arnaldo Carvalho de Melo923a0fb32018-03-06 10:33:04 -0300122 cgroup->fd = open_cgroup(name);
123 if (cgroup->fd == -1)
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -0300124 goto out_free_name;
Arnaldo Carvalho de Melo923a0fb32018-03-06 10:33:04 -0300125 }
126
127 return cgroup;
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -0300128
129out_free_name:
130 free(cgroup->name);
Arnaldo Carvalho de Melo923a0fb32018-03-06 10:33:04 -0300131out_err:
132 free(cgroup);
133 return NULL;
134}
135
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -0300136struct cgroup *evlist__findnew_cgroup(struct perf_evlist *evlist, const char *name)
Arnaldo Carvalho de Melo69239ec2018-03-06 15:27:08 -0300137{
138 struct cgroup *cgroup = evlist__find_cgroup(evlist, name);
139
140 return cgroup ?: cgroup__new(name);
141}
142
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -0300143static int add_cgroup(struct perf_evlist *evlist, const char *str)
Arnaldo Carvalho de Melob80271f2018-03-06 10:27:00 -0300144{
145 struct perf_evsel *counter;
Arnaldo Carvalho de Melo69239ec2018-03-06 15:27:08 -0300146 struct cgroup *cgrp = evlist__findnew_cgroup(evlist, str);
Arnaldo Carvalho de Melob80271f2018-03-06 10:27:00 -0300147 int n;
148
Arnaldo Carvalho de Melo69239ec2018-03-06 15:27:08 -0300149 if (!cgrp)
150 return -1;
Stephane Eranian023695d2011-02-14 11:20:01 +0200151 /*
152 * find corresponding event
153 * if add cgroup N, then need to find event N
154 */
155 n = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300156 evlist__for_each_entry(evlist, counter) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200157 if (n == nr_cgroups)
158 goto found;
159 n++;
160 }
Stephane Eranian023695d2011-02-14 11:20:01 +0200161
Arnaldo Carvalho de Meloa53b6462018-03-06 10:10:45 -0300162 cgroup__put(cgrp);
Stephane Eranian023695d2011-02-14 11:20:01 +0200163 return -1;
164found:
Stephane Eranian023695d2011-02-14 11:20:01 +0200165 counter->cgrp = cgrp;
166 return 0;
167}
168
Arnaldo Carvalho de Melo9450d0d2018-03-06 10:08:10 -0300169static void cgroup__delete(struct cgroup *cgroup)
170{
171 close(cgroup->fd);
172 zfree(&cgroup->name);
173 free(cgroup);
174}
175
Arnaldo Carvalho de Meloa53b6462018-03-06 10:10:45 -0300176void cgroup__put(struct cgroup *cgrp)
Stephane Eranian023695d2011-02-14 11:20:01 +0200177{
Elena Reshetova79c5fe62017-02-21 17:34:55 +0200178 if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
Arnaldo Carvalho de Melo9450d0d2018-03-06 10:08:10 -0300179 cgroup__delete(cgrp);
Stephane Eranian023695d2011-02-14 11:20:01 +0200180 }
181}
182
Arnaldo Carvalho de Melofc9ffb92018-03-06 10:18:40 -0300183struct cgroup *cgroup__get(struct cgroup *cgroup)
184{
185 if (cgroup)
186 refcount_inc(&cgroup->refcnt);
187 return cgroup;
188}
189
Arnaldo Carvalho de Melo483322d2018-03-06 15:48:10 -0300190static void evsel__set_default_cgroup(struct perf_evsel *evsel, struct cgroup *cgroup)
191{
192 if (evsel->cgrp == NULL)
193 evsel->cgrp = cgroup__get(cgroup);
194}
195
196void evlist__set_default_cgroup(struct perf_evlist *evlist, struct cgroup *cgroup)
197{
198 struct perf_evsel *evsel;
199
200 evlist__for_each_entry(evlist, evsel)
201 evsel__set_default_cgroup(evsel, cgroup);
202}
203
Arnaldo Carvalho de Meloa6adc9b2018-03-06 09:31:53 -0300204int parse_cgroups(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300205 int unset __maybe_unused)
Stephane Eranian023695d2011-02-14 11:20:01 +0200206{
207 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
weiping zhang25f72f92018-01-29 23:48:09 +0800208 struct perf_evsel *counter;
Arnaldo Carvalho de Melo3ca32f62018-03-06 09:51:48 -0300209 struct cgroup *cgrp = NULL;
Stephane Eranian023695d2011-02-14 11:20:01 +0200210 const char *p, *e, *eos = str + strlen(str);
211 char *s;
weiping zhang25f72f92018-01-29 23:48:09 +0800212 int ret, i;
Stephane Eranian023695d2011-02-14 11:20:01 +0200213
214 if (list_empty(&evlist->entries)) {
215 fprintf(stderr, "must define events before cgroups\n");
216 return -1;
217 }
218
219 for (;;) {
220 p = strchr(str, ',');
221 e = p ? p : eos;
222
223 /* allow empty cgroups, i.e., skip */
224 if (e - str) {
225 /* termination added */
226 s = strndup(str, e - str);
227 if (!s)
228 return -1;
229 ret = add_cgroup(evlist, s);
Arnaldo Carvalho de Melo3b569282018-03-06 15:53:22 -0300230 free(s);
231 if (ret)
Stephane Eranian023695d2011-02-14 11:20:01 +0200232 return -1;
Stephane Eranian023695d2011-02-14 11:20:01 +0200233 }
234 /* nr_cgroups is increased een for empty cgroups */
235 nr_cgroups++;
236 if (!p)
237 break;
238 str = p+1;
239 }
weiping zhang25f72f92018-01-29 23:48:09 +0800240 /* for the case one cgroup combine to multiple events */
241 i = 0;
242 if (nr_cgroups == 1) {
243 evlist__for_each_entry(evlist, counter) {
244 if (i == 0)
245 cgrp = counter->cgrp;
246 else {
247 counter->cgrp = cgrp;
248 refcount_inc(&cgrp->refcnt);
249 }
250 i++;
251 }
252 }
Stephane Eranian023695d2011-02-14 11:20:01 +0200253 return 0;
254}