blob: eafbf11442b224f90ad9c5d704df75d86985f917 [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"
7
8int nr_cgroups;
9
10static int
11cgroupfs_find_mountpoint(char *buf, size_t maxlen)
12{
13 FILE *fp;
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020014 char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
Tejun Heo968ebff2017-01-29 14:35:20 -050015 char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path;
Eric Dumazet621d2652011-04-08 12:11:06 +020016 char *token, *saved_ptr = NULL;
Stephane Eranian023695d2011-02-14 11:20:01 +020017
18 fp = fopen("/proc/mounts", "r");
19 if (!fp)
20 return -1;
21
22 /*
23 * in order to handle split hierarchy, we need to scan /proc/mounts
24 * and inspect every cgroupfs mount point to find one that has
25 * perf_event subsystem
26 */
Tejun Heo968ebff2017-01-29 14:35:20 -050027 path_v1[0] = '\0';
28 path_v2[0] = '\0';
29
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020030 while (fscanf(fp, "%*s %"STR(PATH_MAX)"s %"STR(PATH_MAX)"s %"
31 STR(PATH_MAX)"s %*d %*d\n",
Stephane Eranian023695d2011-02-14 11:20:01 +020032 mountpoint, type, tokens) == 3) {
33
Tejun Heo968ebff2017-01-29 14:35:20 -050034 if (!path_v1[0] && !strcmp(type, "cgroup")) {
Stephane Eranian023695d2011-02-14 11:20:01 +020035
36 token = strtok_r(tokens, ",", &saved_ptr);
37
38 while (token != NULL) {
39 if (!strcmp(token, "perf_event")) {
Tejun Heo968ebff2017-01-29 14:35:20 -050040 strcpy(path_v1, mountpoint);
Stephane Eranian023695d2011-02-14 11:20:01 +020041 break;
42 }
43 token = strtok_r(NULL, ",", &saved_ptr);
44 }
45 }
Tejun Heo968ebff2017-01-29 14:35:20 -050046
47 if (!path_v2[0] && !strcmp(type, "cgroup2"))
48 strcpy(path_v2, mountpoint);
49
50 if (path_v1[0] && path_v2[0])
Stephane Eranian023695d2011-02-14 11:20:01 +020051 break;
52 }
53 fclose(fp);
Tejun Heo968ebff2017-01-29 14:35:20 -050054
55 if (path_v1[0])
56 path = path_v1;
57 else if (path_v2[0])
58 path = path_v2;
59 else
Stephane Eranian023695d2011-02-14 11:20:01 +020060 return -1;
61
Tejun Heo968ebff2017-01-29 14:35:20 -050062 if (strlen(path) < maxlen) {
63 strcpy(buf, path);
Stephane Eranian023695d2011-02-14 11:20:01 +020064 return 0;
65 }
66 return -1;
67}
68
69static int open_cgroup(char *name)
70{
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020071 char path[PATH_MAX + 1];
72 char mnt[PATH_MAX + 1];
Stephane Eranian023695d2011-02-14 11:20:01 +020073 int fd;
74
75
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020076 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
Stephane Eranian023695d2011-02-14 11:20:01 +020077 return -1;
78
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020079 snprintf(path, PATH_MAX, "%s/%s", mnt, name);
Stephane Eranian023695d2011-02-14 11:20:01 +020080
81 fd = open(path, O_RDONLY);
82 if (fd == -1)
83 fprintf(stderr, "no access to cgroup %s\n", path);
84
85 return fd;
86}
87
88static int add_cgroup(struct perf_evlist *evlist, char *str)
89{
90 struct perf_evsel *counter;
91 struct cgroup_sel *cgrp = NULL;
92 int n;
93 /*
94 * check if cgrp is already defined, if so we reuse it
95 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -030096 evlist__for_each_entry(evlist, counter) {
Stephane Eranian023695d2011-02-14 11:20:01 +020097 cgrp = counter->cgrp;
98 if (!cgrp)
99 continue;
100 if (!strcmp(cgrp->name, str))
101 break;
102
103 cgrp = NULL;
104 }
105
106 if (!cgrp) {
107 cgrp = zalloc(sizeof(*cgrp));
108 if (!cgrp)
109 return -1;
110
111 cgrp->name = str;
112
113 cgrp->fd = open_cgroup(str);
114 if (cgrp->fd == -1) {
115 free(cgrp);
116 return -1;
117 }
118 }
119
120 /*
121 * find corresponding event
122 * if add cgroup N, then need to find event N
123 */
124 n = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300125 evlist__for_each_entry(evlist, counter) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200126 if (n == nr_cgroups)
127 goto found;
128 n++;
129 }
Arnaldo Carvalho de Melof812d302015-05-15 16:12:20 -0300130 if (atomic_read(&cgrp->refcnt) == 0)
Stephane Eranian023695d2011-02-14 11:20:01 +0200131 free(cgrp);
132
133 return -1;
134found:
Arnaldo Carvalho de Melof812d302015-05-15 16:12:20 -0300135 atomic_inc(&cgrp->refcnt);
Stephane Eranian023695d2011-02-14 11:20:01 +0200136 counter->cgrp = cgrp;
137 return 0;
138}
139
140void close_cgroup(struct cgroup_sel *cgrp)
141{
Arnaldo Carvalho de Melof812d302015-05-15 16:12:20 -0300142 if (cgrp && atomic_dec_and_test(&cgrp->refcnt)) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200143 close(cgrp->fd);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300144 zfree(&cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200145 free(cgrp);
146 }
147}
148
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300149int parse_cgroups(const struct option *opt __maybe_unused, const char *str,
150 int unset __maybe_unused)
Stephane Eranian023695d2011-02-14 11:20:01 +0200151{
152 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
153 const char *p, *e, *eos = str + strlen(str);
154 char *s;
155 int ret;
156
157 if (list_empty(&evlist->entries)) {
158 fprintf(stderr, "must define events before cgroups\n");
159 return -1;
160 }
161
162 for (;;) {
163 p = strchr(str, ',');
164 e = p ? p : eos;
165
166 /* allow empty cgroups, i.e., skip */
167 if (e - str) {
168 /* termination added */
169 s = strndup(str, e - str);
170 if (!s)
171 return -1;
172 ret = add_cgroup(evlist, s);
173 if (ret) {
174 free(s);
175 return -1;
176 }
177 }
178 /* nr_cgroups is increased een for empty cgroups */
179 nr_cgroups++;
180 if (!p)
181 break;
182 str = p+1;
183 }
184 return 0;
185}