1 // SPDX-License-Identifier: GPL-2.0
2 #include <subcmd/parse-options.h>
6 #include <linux/stringify.h>
7 #include <linux/zalloc.h>
17 cgroupfs_find_mountpoint(char *buf
, size_t maxlen
)
20 char mountpoint
[PATH_MAX
+ 1], tokens
[PATH_MAX
+ 1], type
[PATH_MAX
+ 1];
21 char path_v1
[PATH_MAX
+ 1], path_v2
[PATH_MAX
+ 2], *path
;
22 char *token
, *saved_ptr
= NULL
;
24 fp
= fopen("/proc/mounts", "r");
29 * in order to handle split hierarchy, we need to scan /proc/mounts
30 * and inspect every cgroupfs mount point to find one that has
31 * perf_event subsystem
36 while (fscanf(fp
, "%*s %"__stringify(PATH_MAX
)"s %"__stringify(PATH_MAX
)"s %"
37 __stringify(PATH_MAX
)"s %*d %*d\n",
38 mountpoint
, type
, tokens
) == 3) {
40 if (!path_v1
[0] && !strcmp(type
, "cgroup")) {
42 token
= strtok_r(tokens
, ",", &saved_ptr
);
44 while (token
!= NULL
) {
45 if (!strcmp(token
, "perf_event")) {
46 strcpy(path_v1
, mountpoint
);
49 token
= strtok_r(NULL
, ",", &saved_ptr
);
53 if (!path_v2
[0] && !strcmp(type
, "cgroup2"))
54 strcpy(path_v2
, mountpoint
);
56 if (path_v1
[0] && path_v2
[0])
68 if (strlen(path
) < maxlen
) {
75 static int open_cgroup(const char *name
)
77 char path
[PATH_MAX
+ 1];
78 char mnt
[PATH_MAX
+ 1];
82 if (cgroupfs_find_mountpoint(mnt
, PATH_MAX
+ 1))
85 scnprintf(path
, PATH_MAX
, "%s/%s", mnt
, name
);
87 fd
= open(path
, O_RDONLY
);
89 fprintf(stderr
, "no access to cgroup %s\n", path
);
94 static struct cgroup
*evlist__find_cgroup(struct evlist
*evlist
, const char *str
)
96 struct evsel
*counter
;
98 * check if cgrp is already defined, if so we reuse it
100 evlist__for_each_entry(evlist
, counter
) {
103 if (!strcmp(counter
->cgrp
->name
, str
))
104 return cgroup__get(counter
->cgrp
);
110 static struct cgroup
*cgroup__new(const char *name
)
112 struct cgroup
*cgroup
= zalloc(sizeof(*cgroup
));
114 if (cgroup
!= NULL
) {
115 refcount_set(&cgroup
->refcnt
, 1);
117 cgroup
->name
= strdup(name
);
120 cgroup
->fd
= open_cgroup(name
);
121 if (cgroup
->fd
== -1)
128 zfree(&cgroup
->name
);
134 struct cgroup
*evlist__findnew_cgroup(struct evlist
*evlist
, const char *name
)
136 struct cgroup
*cgroup
= evlist__find_cgroup(evlist
, name
);
138 return cgroup
?: cgroup__new(name
);
141 static int add_cgroup(struct evlist
*evlist
, const char *str
)
143 struct evsel
*counter
;
144 struct cgroup
*cgrp
= evlist__findnew_cgroup(evlist
, str
);
150 * find corresponding event
151 * if add cgroup N, then need to find event N
154 evlist__for_each_entry(evlist
, counter
) {
163 counter
->cgrp
= cgrp
;
167 static void cgroup__delete(struct cgroup
*cgroup
)
170 zfree(&cgroup
->name
);
174 void cgroup__put(struct cgroup
*cgrp
)
176 if (cgrp
&& refcount_dec_and_test(&cgrp
->refcnt
)) {
177 cgroup__delete(cgrp
);
181 struct cgroup
*cgroup__get(struct cgroup
*cgroup
)
184 refcount_inc(&cgroup
->refcnt
);
188 static void evsel__set_default_cgroup(struct evsel
*evsel
, struct cgroup
*cgroup
)
190 if (evsel
->cgrp
== NULL
)
191 evsel
->cgrp
= cgroup__get(cgroup
);
194 void evlist__set_default_cgroup(struct evlist
*evlist
, struct cgroup
*cgroup
)
198 evlist__for_each_entry(evlist
, evsel
)
199 evsel__set_default_cgroup(evsel
, cgroup
);
202 int parse_cgroups(const struct option
*opt
, const char *str
,
203 int unset __maybe_unused
)
205 struct evlist
*evlist
= *(struct evlist
**)opt
->value
;
206 struct evsel
*counter
;
207 struct cgroup
*cgrp
= NULL
;
208 const char *p
, *e
, *eos
= str
+ strlen(str
);
212 if (list_empty(&evlist
->core
.entries
)) {
213 fprintf(stderr
, "must define events before cgroups\n");
218 p
= strchr(str
, ',');
221 /* allow empty cgroups, i.e., skip */
223 /* termination added */
224 s
= strndup(str
, e
- str
);
227 ret
= add_cgroup(evlist
, s
);
232 /* nr_cgroups is increased een for empty cgroups */
238 /* for the case one cgroup combine to multiple events */
240 if (nr_cgroups
== 1) {
241 evlist__for_each_entry(evlist
, counter
) {
243 cgrp
= counter
->cgrp
;
245 counter
->cgrp
= cgrp
;
246 refcount_inc(&cgrp
->refcnt
);