Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2 | #include "util.h" |
| 3 | #include "../perf.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 4 | #include <subcmd/parse-options.h> |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5 | #include "evsel.h" |
| 6 | #include "cgroup.h" |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7 | #include "evlist.h" |
Arnaldo Carvalho de Melo | aa8cc2f | 2017-04-17 15:36:40 -0300 | [diff] [blame] | 8 | #include <linux/stringify.h> |
Arnaldo Carvalho de Melo | bafae98 | 2018-01-22 16:42:16 -0300 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <fcntl.h> |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 12 | |
| 13 | int nr_cgroups; |
| 14 | |
| 15 | static int |
| 16 | cgroupfs_find_mountpoint(char *buf, size_t maxlen) |
| 17 | { |
| 18 | FILE *fp; |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 19 | char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1]; |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 20 | char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path; |
Eric Dumazet | 621d265 | 2011-04-08 12:11:06 +0200 | [diff] [blame] | 21 | char *token, *saved_ptr = NULL; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 22 | |
| 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 Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 32 | path_v1[0] = '\0'; |
| 33 | path_v2[0] = '\0'; |
| 34 | |
Arnaldo Carvalho de Melo | aa8cc2f | 2017-04-17 15:36:40 -0300 | [diff] [blame] | 35 | while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %" |
| 36 | __stringify(PATH_MAX)"s %*d %*d\n", |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 37 | mountpoint, type, tokens) == 3) { |
| 38 | |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 39 | if (!path_v1[0] && !strcmp(type, "cgroup")) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 40 | |
| 41 | token = strtok_r(tokens, ",", &saved_ptr); |
| 42 | |
| 43 | while (token != NULL) { |
| 44 | if (!strcmp(token, "perf_event")) { |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 45 | strcpy(path_v1, mountpoint); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | token = strtok_r(NULL, ",", &saved_ptr); |
| 49 | } |
| 50 | } |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 51 | |
| 52 | if (!path_v2[0] && !strcmp(type, "cgroup2")) |
| 53 | strcpy(path_v2, mountpoint); |
| 54 | |
| 55 | if (path_v1[0] && path_v2[0]) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 56 | break; |
| 57 | } |
| 58 | fclose(fp); |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 59 | |
| 60 | if (path_v1[0]) |
| 61 | path = path_v1; |
| 62 | else if (path_v2[0]) |
| 63 | path = path_v2; |
| 64 | else |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 65 | return -1; |
| 66 | |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 67 | if (strlen(path) < maxlen) { |
| 68 | strcpy(buf, path); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | return -1; |
| 72 | } |
| 73 | |
Arnaldo Carvalho de Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 74 | static int open_cgroup(const char *name) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 75 | { |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 76 | char path[PATH_MAX + 1]; |
| 77 | char mnt[PATH_MAX + 1]; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 78 | int fd; |
| 79 | |
| 80 | |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 81 | if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1)) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 82 | return -1; |
| 83 | |
Jiri Olsa | 77f1815 | 2018-03-19 09:29:01 +0100 | [diff] [blame] | 84 | scnprintf(path, PATH_MAX, "%s/%s", mnt, name); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 85 | |
| 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 Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 93 | static struct cgroup *evlist__find_cgroup(struct perf_evlist *evlist, const char *str) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 94 | { |
| 95 | struct perf_evsel *counter; |
Arnaldo Carvalho de Melo | 3ca32f6 | 2018-03-06 09:51:48 -0300 | [diff] [blame] | 96 | struct cgroup *cgrp = NULL; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 97 | /* |
| 98 | * check if cgrp is already defined, if so we reuse it |
| 99 | */ |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 100 | evlist__for_each_entry(evlist, counter) { |
Arnaldo Carvalho de Melo | fc9ffb9 | 2018-03-06 10:18:40 -0300 | [diff] [blame] | 101 | if (!counter->cgrp) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 102 | continue; |
Arnaldo Carvalho de Melo | fc9ffb9 | 2018-03-06 10:18:40 -0300 | [diff] [blame] | 103 | if (!strcmp(counter->cgrp->name, str)) { |
| 104 | cgrp = cgroup__get(counter->cgrp); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 105 | break; |
Arnaldo Carvalho de Melo | cd8dd03 | 2017-07-18 20:20:19 -0300 | [diff] [blame] | 106 | } |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Arnaldo Carvalho de Melo | b80271f | 2018-03-06 10:27:00 -0300 | [diff] [blame] | 109 | return cgrp; |
| 110 | } |
| 111 | |
Arnaldo Carvalho de Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 112 | static struct cgroup *cgroup__new(const char *name) |
Arnaldo Carvalho de Melo | 923a0fb3 | 2018-03-06 10:33:04 -0300 | [diff] [blame] | 113 | { |
| 114 | struct cgroup *cgroup = zalloc(sizeof(*cgroup)); |
| 115 | |
| 116 | if (cgroup != NULL) { |
Arnaldo Carvalho de Melo | 923a0fb3 | 2018-03-06 10:33:04 -0300 | [diff] [blame] | 117 | refcount_set(&cgroup->refcnt, 1); |
| 118 | |
Arnaldo Carvalho de Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 119 | cgroup->name = strdup(name); |
| 120 | if (!cgroup->name) |
| 121 | goto out_err; |
Arnaldo Carvalho de Melo | 923a0fb3 | 2018-03-06 10:33:04 -0300 | [diff] [blame] | 122 | cgroup->fd = open_cgroup(name); |
| 123 | if (cgroup->fd == -1) |
Arnaldo Carvalho de Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 124 | goto out_free_name; |
Arnaldo Carvalho de Melo | 923a0fb3 | 2018-03-06 10:33:04 -0300 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return cgroup; |
Arnaldo Carvalho de Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 128 | |
| 129 | out_free_name: |
| 130 | free(cgroup->name); |
Arnaldo Carvalho de Melo | 923a0fb3 | 2018-03-06 10:33:04 -0300 | [diff] [blame] | 131 | out_err: |
| 132 | free(cgroup); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Arnaldo Carvalho de Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 136 | struct cgroup *evlist__findnew_cgroup(struct perf_evlist *evlist, const char *name) |
Arnaldo Carvalho de Melo | 69239ec | 2018-03-06 15:27:08 -0300 | [diff] [blame] | 137 | { |
| 138 | struct cgroup *cgroup = evlist__find_cgroup(evlist, name); |
| 139 | |
| 140 | return cgroup ?: cgroup__new(name); |
| 141 | } |
| 142 | |
Arnaldo Carvalho de Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 143 | static int add_cgroup(struct perf_evlist *evlist, const char *str) |
Arnaldo Carvalho de Melo | b80271f | 2018-03-06 10:27:00 -0300 | [diff] [blame] | 144 | { |
| 145 | struct perf_evsel *counter; |
Arnaldo Carvalho de Melo | 69239ec | 2018-03-06 15:27:08 -0300 | [diff] [blame] | 146 | struct cgroup *cgrp = evlist__findnew_cgroup(evlist, str); |
Arnaldo Carvalho de Melo | b80271f | 2018-03-06 10:27:00 -0300 | [diff] [blame] | 147 | int n; |
| 148 | |
Arnaldo Carvalho de Melo | 69239ec | 2018-03-06 15:27:08 -0300 | [diff] [blame] | 149 | if (!cgrp) |
| 150 | return -1; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 151 | /* |
| 152 | * find corresponding event |
| 153 | * if add cgroup N, then need to find event N |
| 154 | */ |
| 155 | n = 0; |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 156 | evlist__for_each_entry(evlist, counter) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 157 | if (n == nr_cgroups) |
| 158 | goto found; |
| 159 | n++; |
| 160 | } |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 161 | |
Arnaldo Carvalho de Melo | a53b646 | 2018-03-06 10:10:45 -0300 | [diff] [blame] | 162 | cgroup__put(cgrp); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 163 | return -1; |
| 164 | found: |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 165 | counter->cgrp = cgrp; |
| 166 | return 0; |
| 167 | } |
| 168 | |
Arnaldo Carvalho de Melo | 9450d0d | 2018-03-06 10:08:10 -0300 | [diff] [blame] | 169 | static void cgroup__delete(struct cgroup *cgroup) |
| 170 | { |
| 171 | close(cgroup->fd); |
| 172 | zfree(&cgroup->name); |
| 173 | free(cgroup); |
| 174 | } |
| 175 | |
Arnaldo Carvalho de Melo | a53b646 | 2018-03-06 10:10:45 -0300 | [diff] [blame] | 176 | void cgroup__put(struct cgroup *cgrp) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 177 | { |
Elena Reshetova | 79c5fe6 | 2017-02-21 17:34:55 +0200 | [diff] [blame] | 178 | if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) { |
Arnaldo Carvalho de Melo | 9450d0d | 2018-03-06 10:08:10 -0300 | [diff] [blame] | 179 | cgroup__delete(cgrp); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Arnaldo Carvalho de Melo | fc9ffb9 | 2018-03-06 10:18:40 -0300 | [diff] [blame] | 183 | struct cgroup *cgroup__get(struct cgroup *cgroup) |
| 184 | { |
| 185 | if (cgroup) |
| 186 | refcount_inc(&cgroup->refcnt); |
| 187 | return cgroup; |
| 188 | } |
| 189 | |
Arnaldo Carvalho de Melo | 483322d | 2018-03-06 15:48:10 -0300 | [diff] [blame] | 190 | static 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 | |
| 196 | void 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 Melo | a6adc9b | 2018-03-06 09:31:53 -0300 | [diff] [blame] | 204 | int parse_cgroups(const struct option *opt, const char *str, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 205 | int unset __maybe_unused) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 206 | { |
| 207 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; |
weiping zhang | 25f72f9 | 2018-01-29 23:48:09 +0800 | [diff] [blame] | 208 | struct perf_evsel *counter; |
Arnaldo Carvalho de Melo | 3ca32f6 | 2018-03-06 09:51:48 -0300 | [diff] [blame] | 209 | struct cgroup *cgrp = NULL; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 210 | const char *p, *e, *eos = str + strlen(str); |
| 211 | char *s; |
weiping zhang | 25f72f9 | 2018-01-29 23:48:09 +0800 | [diff] [blame] | 212 | int ret, i; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 213 | |
| 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 Melo | 3b56928 | 2018-03-06 15:53:22 -0300 | [diff] [blame] | 230 | free(s); |
| 231 | if (ret) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 232 | return -1; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 233 | } |
| 234 | /* nr_cgroups is increased een for empty cgroups */ |
| 235 | nr_cgroups++; |
| 236 | if (!p) |
| 237 | break; |
| 238 | str = p+1; |
| 239 | } |
weiping zhang | 25f72f9 | 2018-01-29 23:48:09 +0800 | [diff] [blame] | 240 | /* 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 Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 253 | return 0; |
| 254 | } |