3 #include "parse-options.h"
6 #include "debugfs.h" /* MAX_PATH, STR() */
12 cgroupfs_find_mountpoint(char *buf, size_t maxlen)
15 char mountpoint[MAX_PATH+1], tokens[MAX_PATH+1], type[MAX_PATH+1];
16 char *token, *saved_ptr;
19 fp = fopen("/proc/mounts", "r");
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
28 while (fscanf(fp, "%*s %"STR(MAX_PATH)"s %"STR(MAX_PATH)"s %"
29 STR(MAX_PATH)"s %*d %*d\n",
30 mountpoint, type, tokens) == 3) {
32 if (!strcmp(type, "cgroup")) {
34 token = strtok_r(tokens, ",", &saved_ptr);
36 while (token != NULL) {
37 if (!strcmp(token, "perf_event")) {
41 token = strtok_r(NULL, ",", &saved_ptr);
51 if (strlen(mountpoint) < maxlen) {
52 strcpy(buf, mountpoint);
58 static int open_cgroup(char *name)
60 char path[MAX_PATH+1];
65 if (cgroupfs_find_mountpoint(mnt, MAX_PATH+1))
68 snprintf(path, MAX_PATH, "%s/%s", mnt, name);
70 fd = open(path, O_RDONLY);
72 fprintf(stderr, "no access to cgroup %s\n", path);
77 static int add_cgroup(struct perf_evlist *evlist, char *str)
79 struct perf_evsel *counter;
80 struct cgroup_sel *cgrp = NULL;
83 * check if cgrp is already defined, if so we reuse it
85 list_for_each_entry(counter, &evlist->entries, node) {
89 if (!strcmp(cgrp->name, str))
96 cgrp = zalloc(sizeof(*cgrp));
102 cgrp->fd = open_cgroup(str);
103 if (cgrp->fd == -1) {
110 * find corresponding event
111 * if add cgroup N, then need to find event N
114 list_for_each_entry(counter, &evlist->entries, node) {
119 if (cgrp->refcnt == 0)
125 counter->cgrp = cgrp;
129 void close_cgroup(struct cgroup_sel *cgrp)
134 /* XXX: not reentrant */
135 if (--cgrp->refcnt == 0) {
142 int parse_cgroups(const struct option *opt __used, const char *str,
145 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
146 const char *p, *e, *eos = str + strlen(str);
150 if (list_empty(&evlist->entries)) {
151 fprintf(stderr, "must define events before cgroups\n");
156 p = strchr(str, ',');
159 /* allow empty cgroups, i.e., skip */
161 /* termination added */
162 s = strndup(str, e - str);
165 ret = add_cgroup(evlist, s);
171 /* nr_cgroups is increased een for empty cgroups */