blob: 0e77bc9e5f3c3b354b023e5d26efd08ead615104 [file] [log] [blame]
Stephane Eranian023695d2011-02-14 11:20:01 +02001#include "util.h"
2#include "../perf.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06003#include <subcmd/parse-options.h>
Stephane Eranian023695d2011-02-14 11:20:01 +02004#include "evsel.h"
5#include "cgroup.h"
Stephane Eranian023695d2011-02-14 11:20:01 +02006#include "evlist.h"
Arnaldo Carvalho de Meloaa8cc2f2017-04-17 15:36:40 -03007#include <linux/stringify.h>
Stephane Eranian023695d2011-02-14 11:20:01 +02008
9int nr_cgroups;
10
11static int
12cgroupfs_find_mountpoint(char *buf, size_t maxlen)
13{
14 FILE *fp;
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020015 char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
Tejun Heo968ebff2017-01-29 14:35:20 -050016 char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path;
Eric Dumazet621d2652011-04-08 12:11:06 +020017 char *token, *saved_ptr = NULL;
Stephane Eranian023695d2011-02-14 11:20:01 +020018
19 fp = fopen("/proc/mounts", "r");
20 if (!fp)
21 return -1;
22
23 /*
24 * in order to handle split hierarchy, we need to scan /proc/mounts
25 * and inspect every cgroupfs mount point to find one that has
26 * perf_event subsystem
27 */
Tejun Heo968ebff2017-01-29 14:35:20 -050028 path_v1[0] = '\0';
29 path_v2[0] = '\0';
30
Arnaldo Carvalho de Meloaa8cc2f2017-04-17 15:36:40 -030031 while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %"
32 __stringify(PATH_MAX)"s %*d %*d\n",
Stephane Eranian023695d2011-02-14 11:20:01 +020033 mountpoint, type, tokens) == 3) {
34
Tejun Heo968ebff2017-01-29 14:35:20 -050035 if (!path_v1[0] && !strcmp(type, "cgroup")) {
Stephane Eranian023695d2011-02-14 11:20:01 +020036
37 token = strtok_r(tokens, ",", &saved_ptr);
38
39 while (token != NULL) {
40 if (!strcmp(token, "perf_event")) {
Tejun Heo968ebff2017-01-29 14:35:20 -050041 strcpy(path_v1, mountpoint);
Stephane Eranian023695d2011-02-14 11:20:01 +020042 break;
43 }
44 token = strtok_r(NULL, ",", &saved_ptr);
45 }
46 }
Tejun Heo968ebff2017-01-29 14:35:20 -050047
48 if (!path_v2[0] && !strcmp(type, "cgroup2"))
49 strcpy(path_v2, mountpoint);
50
51 if (path_v1[0] && path_v2[0])
Stephane Eranian023695d2011-02-14 11:20:01 +020052 break;
53 }
54 fclose(fp);
Tejun Heo968ebff2017-01-29 14:35:20 -050055
56 if (path_v1[0])
57 path = path_v1;
58 else if (path_v2[0])
59 path = path_v2;
60 else
Stephane Eranian023695d2011-02-14 11:20:01 +020061 return -1;
62
Tejun Heo968ebff2017-01-29 14:35:20 -050063 if (strlen(path) < maxlen) {
64 strcpy(buf, path);
Stephane Eranian023695d2011-02-14 11:20:01 +020065 return 0;
66 }
67 return -1;
68}
69
70static int open_cgroup(char *name)
71{
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020072 char path[PATH_MAX + 1];
73 char mnt[PATH_MAX + 1];
Stephane Eranian023695d2011-02-14 11:20:01 +020074 int fd;
75
76
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020077 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
Stephane Eranian023695d2011-02-14 11:20:01 +020078 return -1;
79
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020080 snprintf(path, PATH_MAX, "%s/%s", mnt, name);
Stephane Eranian023695d2011-02-14 11:20:01 +020081
82 fd = open(path, O_RDONLY);
83 if (fd == -1)
84 fprintf(stderr, "no access to cgroup %s\n", path);
85
86 return fd;
87}
88
89static int add_cgroup(struct perf_evlist *evlist, char *str)
90{
91 struct perf_evsel *counter;
92 struct cgroup_sel *cgrp = NULL;
93 int n;
94 /*
95 * check if cgrp is already defined, if so we reuse it
96 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -030097 evlist__for_each_entry(evlist, counter) {
Stephane Eranian023695d2011-02-14 11:20:01 +020098 cgrp = counter->cgrp;
99 if (!cgrp)
100 continue;
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300101 if (!strcmp(cgrp->name, str)) {
102 refcount_inc(&cgrp->refcnt);
Stephane Eranian023695d2011-02-14 11:20:01 +0200103 break;
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300104 }
Stephane Eranian023695d2011-02-14 11:20:01 +0200105
106 cgrp = NULL;
107 }
108
109 if (!cgrp) {
110 cgrp = zalloc(sizeof(*cgrp));
111 if (!cgrp)
112 return -1;
113
114 cgrp->name = str;
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300115 refcount_set(&cgrp->refcnt, 1);
Stephane Eranian023695d2011-02-14 11:20:01 +0200116
117 cgrp->fd = open_cgroup(str);
118 if (cgrp->fd == -1) {
119 free(cgrp);
120 return -1;
121 }
122 }
123
124 /*
125 * find corresponding event
126 * if add cgroup N, then need to find event N
127 */
128 n = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300129 evlist__for_each_entry(evlist, counter) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200130 if (n == nr_cgroups)
131 goto found;
132 n++;
133 }
Arnaldo Carvalho de Melocd8dd032017-07-18 20:20:19 -0300134 if (refcount_dec_and_test(&cgrp->refcnt))
Stephane Eranian023695d2011-02-14 11:20:01 +0200135 free(cgrp);
136
137 return -1;
138found:
Stephane Eranian023695d2011-02-14 11:20:01 +0200139 counter->cgrp = cgrp;
140 return 0;
141}
142
143void close_cgroup(struct cgroup_sel *cgrp)
144{
Elena Reshetova79c5fe62017-02-21 17:34:55 +0200145 if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200146 close(cgrp->fd);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300147 zfree(&cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200148 free(cgrp);
149 }
150}
151
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300152int parse_cgroups(const struct option *opt __maybe_unused, const char *str,
153 int unset __maybe_unused)
Stephane Eranian023695d2011-02-14 11:20:01 +0200154{
155 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
156 const char *p, *e, *eos = str + strlen(str);
157 char *s;
158 int ret;
159
160 if (list_empty(&evlist->entries)) {
161 fprintf(stderr, "must define events before cgroups\n");
162 return -1;
163 }
164
165 for (;;) {
166 p = strchr(str, ',');
167 e = p ? p : eos;
168
169 /* allow empty cgroups, i.e., skip */
170 if (e - str) {
171 /* termination added */
172 s = strndup(str, e - str);
173 if (!s)
174 return -1;
175 ret = add_cgroup(evlist, s);
176 if (ret) {
177 free(s);
178 return -1;
179 }
180 }
181 /* nr_cgroups is increased een for empty cgroups */
182 nr_cgroups++;
183 if (!p)
184 break;
185 str = p+1;
186 }
187 return 0;
188}