blob: 984f69144f87e2dfef2bf5123537d36d4eeeaa28 [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
74static int open_cgroup(char *name)
75{
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
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020084 snprintf(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
93static int add_cgroup(struct perf_evlist *evlist, char *str)
94{
95 struct perf_evsel *counter;
96 struct cgroup_sel *cgrp = NULL;
97 int n;
98 /*
99 * check if cgrp is already defined, if so we reuse it
100 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300101 evlist__for_each_entry(evlist, counter) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200102 cgrp = counter->cgrp;
103 if (!cgrp)
104 continue;
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300105 if (!strcmp(cgrp->name, str)) {
106 refcount_inc(&cgrp->refcnt);
Stephane Eranian023695d2011-02-14 11:20:01 +0200107 break;
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300108 }
Stephane Eranian023695d2011-02-14 11:20:01 +0200109
110 cgrp = NULL;
111 }
112
113 if (!cgrp) {
114 cgrp = zalloc(sizeof(*cgrp));
115 if (!cgrp)
116 return -1;
117
118 cgrp->name = str;
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300119 refcount_set(&cgrp->refcnt, 1);
Stephane Eranian023695d2011-02-14 11:20:01 +0200120
121 cgrp->fd = open_cgroup(str);
122 if (cgrp->fd == -1) {
123 free(cgrp);
124 return -1;
125 }
126 }
127
128 /*
129 * find corresponding event
130 * if add cgroup N, then need to find event N
131 */
132 n = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300133 evlist__for_each_entry(evlist, counter) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200134 if (n == nr_cgroups)
135 goto found;
136 n++;
137 }
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300138 if (refcount_dec_and_test(&cgrp->refcnt))
Stephane Eranian023695d2011-02-14 11:20:01 +0200139 free(cgrp);
140
141 return -1;
142found:
Stephane Eranian023695d2011-02-14 11:20:01 +0200143 counter->cgrp = cgrp;
144 return 0;
145}
146
147void close_cgroup(struct cgroup_sel *cgrp)
148{
Elena Reshetova79c5fe62017-02-21 17:34:55 +0200149 if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200150 close(cgrp->fd);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300151 zfree(&cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200152 free(cgrp);
153 }
154}
155
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300156int parse_cgroups(const struct option *opt __maybe_unused, const char *str,
157 int unset __maybe_unused)
Stephane Eranian023695d2011-02-14 11:20:01 +0200158{
159 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
160 const char *p, *e, *eos = str + strlen(str);
161 char *s;
162 int ret;
163
164 if (list_empty(&evlist->entries)) {
165 fprintf(stderr, "must define events before cgroups\n");
166 return -1;
167 }
168
169 for (;;) {
170 p = strchr(str, ',');
171 e = p ? p : eos;
172
173 /* allow empty cgroups, i.e., skip */
174 if (e - str) {
175 /* termination added */
176 s = strndup(str, e - str);
177 if (!s)
178 return -1;
179 ret = add_cgroup(evlist, s);
180 if (ret) {
181 free(s);
182 return -1;
183 }
184 }
185 /* nr_cgroups is increased een for empty cgroups */
186 nr_cgroups++;
187 if (!p)
188 break;
189 str = p+1;
190 }
191 return 0;
192}