Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1 | #include "util.h" |
| 2 | #include "../perf.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 3 | #include <subcmd/parse-options.h> |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4 | #include "evsel.h" |
| 5 | #include "cgroup.h" |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6 | #include "evlist.h" |
| 7 | |
| 8 | int nr_cgroups; |
| 9 | |
| 10 | static int |
| 11 | cgroupfs_find_mountpoint(char *buf, size_t maxlen) |
| 12 | { |
| 13 | FILE *fp; |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 14 | 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] | 15 | char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path; |
Eric Dumazet | 621d265 | 2011-04-08 12:11:06 +0200 | [diff] [blame] | 16 | char *token, *saved_ptr = NULL; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 17 | |
| 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 Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 27 | path_v1[0] = '\0'; |
| 28 | path_v2[0] = '\0'; |
| 29 | |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 30 | while (fscanf(fp, "%*s %"STR(PATH_MAX)"s %"STR(PATH_MAX)"s %" |
| 31 | STR(PATH_MAX)"s %*d %*d\n", |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 32 | mountpoint, type, tokens) == 3) { |
| 33 | |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 34 | if (!path_v1[0] && !strcmp(type, "cgroup")) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 35 | |
| 36 | token = strtok_r(tokens, ",", &saved_ptr); |
| 37 | |
| 38 | while (token != NULL) { |
| 39 | if (!strcmp(token, "perf_event")) { |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 40 | strcpy(path_v1, mountpoint); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 41 | break; |
| 42 | } |
| 43 | token = strtok_r(NULL, ",", &saved_ptr); |
| 44 | } |
| 45 | } |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 46 | |
| 47 | if (!path_v2[0] && !strcmp(type, "cgroup2")) |
| 48 | strcpy(path_v2, mountpoint); |
| 49 | |
| 50 | if (path_v1[0] && path_v2[0]) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 51 | break; |
| 52 | } |
| 53 | fclose(fp); |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 54 | |
| 55 | if (path_v1[0]) |
| 56 | path = path_v1; |
| 57 | else if (path_v2[0]) |
| 58 | path = path_v2; |
| 59 | else |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 60 | return -1; |
| 61 | |
Tejun Heo | 968ebff | 2017-01-29 14:35:20 -0500 | [diff] [blame] | 62 | if (strlen(path) < maxlen) { |
| 63 | strcpy(buf, path); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | static int open_cgroup(char *name) |
| 70 | { |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 71 | char path[PATH_MAX + 1]; |
| 72 | char mnt[PATH_MAX + 1]; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 73 | int fd; |
| 74 | |
| 75 | |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 76 | if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1)) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 77 | return -1; |
| 78 | |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 79 | snprintf(path, PATH_MAX, "%s/%s", mnt, name); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 80 | |
| 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 | |
| 88 | static 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 Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 96 | evlist__for_each_entry(evlist, counter) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 97 | 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 Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 125 | evlist__for_each_entry(evlist, counter) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 126 | if (n == nr_cgroups) |
| 127 | goto found; |
| 128 | n++; |
| 129 | } |
Arnaldo Carvalho de Melo | f812d30 | 2015-05-15 16:12:20 -0300 | [diff] [blame] | 130 | if (atomic_read(&cgrp->refcnt) == 0) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 131 | free(cgrp); |
| 132 | |
| 133 | return -1; |
| 134 | found: |
Arnaldo Carvalho de Melo | f812d30 | 2015-05-15 16:12:20 -0300 | [diff] [blame] | 135 | atomic_inc(&cgrp->refcnt); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 136 | counter->cgrp = cgrp; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | void close_cgroup(struct cgroup_sel *cgrp) |
| 141 | { |
Arnaldo Carvalho de Melo | f812d30 | 2015-05-15 16:12:20 -0300 | [diff] [blame] | 142 | if (cgrp && atomic_dec_and_test(&cgrp->refcnt)) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 143 | close(cgrp->fd); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 144 | zfree(&cgrp->name); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 145 | free(cgrp); |
| 146 | } |
| 147 | } |
| 148 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 149 | int parse_cgroups(const struct option *opt __maybe_unused, const char *str, |
| 150 | int unset __maybe_unused) |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 151 | { |
| 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 | } |