xref: /linux/tools/perf/util/cgroup.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 #include "util.h"
2 #include "../perf.h"
3 #include <subcmd/parse-options.h>
4 #include "evsel.h"
5 #include "cgroup.h"
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;
14 	char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
15 	char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path;
16 	char *token, *saved_ptr = NULL;
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 	 */
27 	path_v1[0] = '\0';
28 	path_v2[0] = '\0';
29 
30 	while (fscanf(fp, "%*s %"STR(PATH_MAX)"s %"STR(PATH_MAX)"s %"
31 				STR(PATH_MAX)"s %*d %*d\n",
32 				mountpoint, type, tokens) == 3) {
33 
34 		if (!path_v1[0] && !strcmp(type, "cgroup")) {
35 
36 			token = strtok_r(tokens, ",", &saved_ptr);
37 
38 			while (token != NULL) {
39 				if (!strcmp(token, "perf_event")) {
40 					strcpy(path_v1, mountpoint);
41 					break;
42 				}
43 				token = strtok_r(NULL, ",", &saved_ptr);
44 			}
45 		}
46 
47 		if (!path_v2[0] && !strcmp(type, "cgroup2"))
48 			strcpy(path_v2, mountpoint);
49 
50 		if (path_v1[0] && path_v2[0])
51 			break;
52 	}
53 	fclose(fp);
54 
55 	if (path_v1[0])
56 		path = path_v1;
57 	else if (path_v2[0])
58 		path = path_v2;
59 	else
60 		return -1;
61 
62 	if (strlen(path) < maxlen) {
63 		strcpy(buf, path);
64 		return 0;
65 	}
66 	return -1;
67 }
68 
69 static int open_cgroup(char *name)
70 {
71 	char path[PATH_MAX + 1];
72 	char mnt[PATH_MAX + 1];
73 	int fd;
74 
75 
76 	if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
77 		return -1;
78 
79 	snprintf(path, PATH_MAX, "%s/%s", mnt, name);
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 	 */
96 	evlist__for_each_entry(evlist, counter) {
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;
125 	evlist__for_each_entry(evlist, counter) {
126 		if (n == nr_cgroups)
127 			goto found;
128 		n++;
129 	}
130 	if (atomic_read(&cgrp->refcnt) == 0)
131 		free(cgrp);
132 
133 	return -1;
134 found:
135 	atomic_inc(&cgrp->refcnt);
136 	counter->cgrp = cgrp;
137 	return 0;
138 }
139 
140 void close_cgroup(struct cgroup_sel *cgrp)
141 {
142 	if (cgrp && atomic_dec_and_test(&cgrp->refcnt)) {
143 		close(cgrp->fd);
144 		zfree(&cgrp->name);
145 		free(cgrp);
146 	}
147 }
148 
149 int parse_cgroups(const struct option *opt __maybe_unused, const char *str,
150 		  int unset __maybe_unused)
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 }
186