xref: /openbmc/linux/tools/perf/util/cgroup.c (revision 39f555fb)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <subcmd/parse-options.h>
3 #include "evsel.h"
4 #include "cgroup.h"
5 #include "evlist.h"
6 #include "rblist.h"
7 #include "metricgroup.h"
8 #include "stat.h"
9 #include <linux/zalloc.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/statfs.h>
13 #include <fcntl.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <api/fs/fs.h>
17 #include <ftw.h>
18 #include <regex.h>
19 
20 int nr_cgroups;
21 bool cgrp_event_expanded;
22 
23 /* used to match cgroup name with patterns */
24 struct cgroup_name {
25 	struct list_head list;
26 	bool used;
27 	char name[];
28 };
29 static LIST_HEAD(cgroup_list);
30 
31 static int open_cgroup(const char *name)
32 {
33 	char path[PATH_MAX + 1];
34 	char mnt[PATH_MAX + 1];
35 	int fd;
36 
37 
38 	if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
39 		return -1;
40 
41 	scnprintf(path, PATH_MAX, "%s/%s", mnt, name);
42 
43 	fd = open(path, O_RDONLY);
44 	if (fd == -1)
45 		fprintf(stderr, "no access to cgroup %s\n", path);
46 
47 	return fd;
48 }
49 
50 #ifdef HAVE_FILE_HANDLE
51 int read_cgroup_id(struct cgroup *cgrp)
52 {
53 	char path[PATH_MAX + 1];
54 	char mnt[PATH_MAX + 1];
55 	struct {
56 		struct file_handle fh;
57 		uint64_t cgroup_id;
58 	} handle;
59 	int mount_id;
60 
61 	if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
62 		return -1;
63 
64 	scnprintf(path, PATH_MAX, "%s/%s", mnt, cgrp->name);
65 
66 	handle.fh.handle_bytes = sizeof(handle.cgroup_id);
67 	if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0)
68 		return -1;
69 
70 	cgrp->id = handle.cgroup_id;
71 	return 0;
72 }
73 #endif  /* HAVE_FILE_HANDLE */
74 
75 #ifndef CGROUP2_SUPER_MAGIC
76 #define CGROUP2_SUPER_MAGIC  0x63677270
77 #endif
78 
79 int cgroup_is_v2(const char *subsys)
80 {
81 	char mnt[PATH_MAX + 1];
82 	struct statfs stbuf;
83 
84 	if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, subsys))
85 		return -1;
86 
87 	if (statfs(mnt, &stbuf) < 0)
88 		return -1;
89 
90 	return (stbuf.f_type == CGROUP2_SUPER_MAGIC);
91 }
92 
93 static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str)
94 {
95 	struct evsel *counter;
96 	/*
97 	 * check if cgrp is already defined, if so we reuse it
98 	 */
99 	evlist__for_each_entry(evlist, counter) {
100 		if (!counter->cgrp)
101 			continue;
102 		if (!strcmp(counter->cgrp->name, str))
103 			return cgroup__get(counter->cgrp);
104 	}
105 
106 	return NULL;
107 }
108 
109 static struct cgroup *cgroup__new(const char *name, bool do_open)
110 {
111 	struct cgroup *cgroup = zalloc(sizeof(*cgroup));
112 
113 	if (cgroup != NULL) {
114 		refcount_set(&cgroup->refcnt, 1);
115 
116 		cgroup->name = strdup(name);
117 		if (!cgroup->name)
118 			goto out_err;
119 
120 		if (do_open) {
121 			cgroup->fd = open_cgroup(name);
122 			if (cgroup->fd == -1)
123 				goto out_free_name;
124 		} else {
125 			cgroup->fd = -1;
126 		}
127 	}
128 
129 	return cgroup;
130 
131 out_free_name:
132 	zfree(&cgroup->name);
133 out_err:
134 	free(cgroup);
135 	return NULL;
136 }
137 
138 struct cgroup *evlist__findnew_cgroup(struct evlist *evlist, const char *name)
139 {
140 	struct cgroup *cgroup = evlist__find_cgroup(evlist, name);
141 
142 	return cgroup ?: cgroup__new(name, true);
143 }
144 
145 static int add_cgroup(struct evlist *evlist, const char *str)
146 {
147 	struct evsel *counter;
148 	struct cgroup *cgrp = evlist__findnew_cgroup(evlist, str);
149 	int n;
150 
151 	if (!cgrp)
152 		return -1;
153 	/*
154 	 * find corresponding event
155 	 * if add cgroup N, then need to find event N
156 	 */
157 	n = 0;
158 	evlist__for_each_entry(evlist, counter) {
159 		if (n == nr_cgroups)
160 			goto found;
161 		n++;
162 	}
163 
164 	cgroup__put(cgrp);
165 	return -1;
166 found:
167 	counter->cgrp = cgrp;
168 	return 0;
169 }
170 
171 static void cgroup__delete(struct cgroup *cgroup)
172 {
173 	if (cgroup->fd >= 0)
174 		close(cgroup->fd);
175 	zfree(&cgroup->name);
176 	free(cgroup);
177 }
178 
179 void cgroup__put(struct cgroup *cgrp)
180 {
181 	if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
182 		cgroup__delete(cgrp);
183 	}
184 }
185 
186 struct cgroup *cgroup__get(struct cgroup *cgroup)
187 {
188        if (cgroup)
189 		refcount_inc(&cgroup->refcnt);
190        return cgroup;
191 }
192 
193 static void evsel__set_default_cgroup(struct evsel *evsel, struct cgroup *cgroup)
194 {
195 	if (evsel->cgrp == NULL)
196 		evsel->cgrp = cgroup__get(cgroup);
197 }
198 
199 void evlist__set_default_cgroup(struct evlist *evlist, struct cgroup *cgroup)
200 {
201 	struct evsel *evsel;
202 
203 	evlist__for_each_entry(evlist, evsel)
204 		evsel__set_default_cgroup(evsel, cgroup);
205 }
206 
207 /* helper function for ftw() in match_cgroups and list_cgroups */
208 static int add_cgroup_name(const char *fpath, const struct stat *sb __maybe_unused,
209 			   int typeflag, struct FTW *ftwbuf __maybe_unused)
210 {
211 	struct cgroup_name *cn;
212 
213 	if (typeflag != FTW_D)
214 		return 0;
215 
216 	cn = malloc(sizeof(*cn) + strlen(fpath) + 1);
217 	if (cn == NULL)
218 		return -1;
219 
220 	cn->used = false;
221 	strcpy(cn->name, fpath);
222 
223 	list_add_tail(&cn->list, &cgroup_list);
224 	return 0;
225 }
226 
227 static int check_and_add_cgroup_name(const char *fpath)
228 {
229 	struct cgroup_name *cn;
230 
231 	list_for_each_entry(cn, &cgroup_list, list) {
232 		if (!strcmp(cn->name, fpath))
233 			return 0;
234 	}
235 
236 	/* pretend if it's added by ftw() */
237 	return add_cgroup_name(fpath, NULL, FTW_D, NULL);
238 }
239 
240 static void release_cgroup_list(void)
241 {
242 	struct cgroup_name *cn;
243 
244 	while (!list_empty(&cgroup_list)) {
245 		cn = list_first_entry(&cgroup_list, struct cgroup_name, list);
246 		list_del(&cn->list);
247 		free(cn);
248 	}
249 }
250 
251 /* collect given cgroups only */
252 static int list_cgroups(const char *str)
253 {
254 	const char *p, *e, *eos = str + strlen(str);
255 	struct cgroup_name *cn;
256 	char *s;
257 
258 	/* use given name as is when no regex is given */
259 	for (;;) {
260 		p = strchr(str, ',');
261 		e = p ? p : eos;
262 
263 		if (e - str) {
264 			int ret;
265 
266 			s = strndup(str, e - str);
267 			if (!s)
268 				return -1;
269 
270 			ret = check_and_add_cgroup_name(s);
271 			free(s);
272 			if (ret < 0)
273 				return -1;
274 		} else {
275 			if (check_and_add_cgroup_name("/") < 0)
276 				return -1;
277 		}
278 
279 		if (!p)
280 			break;
281 		str = p+1;
282 	}
283 
284 	/* these groups will be used */
285 	list_for_each_entry(cn, &cgroup_list, list)
286 		cn->used = true;
287 
288 	return 0;
289 }
290 
291 /* collect all cgroups first and then match with the pattern */
292 static int match_cgroups(const char *str)
293 {
294 	char mnt[PATH_MAX];
295 	const char *p, *e, *eos = str + strlen(str);
296 	struct cgroup_name *cn;
297 	regex_t reg;
298 	int prefix_len;
299 	char *s;
300 
301 	if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event"))
302 		return -1;
303 
304 	/* cgroup_name will have a full path, skip the root directory */
305 	prefix_len = strlen(mnt);
306 
307 	/* collect all cgroups in the cgroup_list */
308 	if (nftw(mnt, add_cgroup_name, 20, 0) < 0)
309 		return -1;
310 
311 	for (;;) {
312 		p = strchr(str, ',');
313 		e = p ? p : eos;
314 
315 		/* allow empty cgroups, i.e., skip */
316 		if (e - str) {
317 			/* termination added */
318 			s = strndup(str, e - str);
319 			if (!s)
320 				return -1;
321 			if (regcomp(&reg, s, REG_NOSUB)) {
322 				free(s);
323 				return -1;
324 			}
325 
326 			/* check cgroup name with the pattern */
327 			list_for_each_entry(cn, &cgroup_list, list) {
328 				char *name = cn->name + prefix_len;
329 
330 				if (name[0] == '/' && name[1])
331 					name++;
332 				if (!regexec(&reg, name, 0, NULL, 0))
333 					cn->used = true;
334 			}
335 			regfree(&reg);
336 			free(s);
337 		} else {
338 			/* first entry to root cgroup */
339 			cn = list_first_entry(&cgroup_list, struct cgroup_name,
340 					      list);
341 			cn->used = true;
342 		}
343 
344 		if (!p)
345 			break;
346 		str = p+1;
347 	}
348 	return prefix_len;
349 }
350 
351 int parse_cgroups(const struct option *opt, const char *str,
352 		  int unset __maybe_unused)
353 {
354 	struct evlist *evlist = *(struct evlist **)opt->value;
355 	struct evsel *counter;
356 	struct cgroup *cgrp = NULL;
357 	const char *p, *e, *eos = str + strlen(str);
358 	char *s;
359 	int ret, i;
360 
361 	if (list_empty(&evlist->core.entries)) {
362 		fprintf(stderr, "must define events before cgroups\n");
363 		return -1;
364 	}
365 
366 	for (;;) {
367 		p = strchr(str, ',');
368 		e = p ? p : eos;
369 
370 		/* allow empty cgroups, i.e., skip */
371 		if (e - str) {
372 			/* termination added */
373 			s = strndup(str, e - str);
374 			if (!s)
375 				return -1;
376 			ret = add_cgroup(evlist, s);
377 			free(s);
378 			if (ret)
379 				return -1;
380 		}
381 		/* nr_cgroups is increased een for empty cgroups */
382 		nr_cgroups++;
383 		if (!p)
384 			break;
385 		str = p+1;
386 	}
387 	/* for the case one cgroup combine to multiple events */
388 	i = 0;
389 	if (nr_cgroups == 1) {
390 		evlist__for_each_entry(evlist, counter) {
391 			if (i == 0)
392 				cgrp = counter->cgrp;
393 			else {
394 				counter->cgrp = cgrp;
395 				refcount_inc(&cgrp->refcnt);
396 			}
397 			i++;
398 		}
399 	}
400 	return 0;
401 }
402 
403 static bool has_pattern_string(const char *str)
404 {
405 	return !!strpbrk(str, "{}[]()|*+?^$");
406 }
407 
408 int evlist__expand_cgroup(struct evlist *evlist, const char *str,
409 			  struct rblist *metric_events, bool open_cgroup)
410 {
411 	struct evlist *orig_list, *tmp_list;
412 	struct evsel *pos, *evsel, *leader;
413 	struct rblist orig_metric_events;
414 	struct cgroup *cgrp = NULL;
415 	struct cgroup_name *cn;
416 	int ret = -1;
417 	int prefix_len;
418 
419 	if (evlist->core.nr_entries == 0) {
420 		fprintf(stderr, "must define events before cgroups\n");
421 		return -EINVAL;
422 	}
423 
424 	orig_list = evlist__new();
425 	tmp_list = evlist__new();
426 	if (orig_list == NULL || tmp_list == NULL) {
427 		fprintf(stderr, "memory allocation failed\n");
428 		return -ENOMEM;
429 	}
430 
431 	/* save original events and init evlist */
432 	evlist__splice_list_tail(orig_list, &evlist->core.entries);
433 	evlist->core.nr_entries = 0;
434 
435 	if (metric_events) {
436 		orig_metric_events = *metric_events;
437 		rblist__init(metric_events);
438 	} else {
439 		rblist__init(&orig_metric_events);
440 	}
441 
442 	if (has_pattern_string(str))
443 		prefix_len = match_cgroups(str);
444 	else
445 		prefix_len = list_cgroups(str);
446 
447 	if (prefix_len < 0)
448 		goto out_err;
449 
450 	list_for_each_entry(cn, &cgroup_list, list) {
451 		char *name;
452 
453 		if (!cn->used)
454 			continue;
455 
456 		/* cgroup_name might have a full path, skip the prefix */
457 		name = cn->name + prefix_len;
458 		if (name[0] == '/' && name[1])
459 			name++;
460 		cgrp = cgroup__new(name, open_cgroup);
461 		if (cgrp == NULL)
462 			goto out_err;
463 
464 		leader = NULL;
465 		evlist__for_each_entry(orig_list, pos) {
466 			evsel = evsel__clone(pos);
467 			if (evsel == NULL)
468 				goto out_err;
469 
470 			cgroup__put(evsel->cgrp);
471 			evsel->cgrp = cgroup__get(cgrp);
472 
473 			if (evsel__is_group_leader(pos))
474 				leader = evsel;
475 			evsel__set_leader(evsel, leader);
476 
477 			evlist__add(tmp_list, evsel);
478 		}
479 		/* cgroup__new() has a refcount, release it here */
480 		cgroup__put(cgrp);
481 		nr_cgroups++;
482 
483 		if (metric_events) {
484 			if (metricgroup__copy_metric_events(tmp_list, cgrp,
485 							    metric_events,
486 							    &orig_metric_events) < 0)
487 				goto out_err;
488 		}
489 
490 		evlist__splice_list_tail(evlist, &tmp_list->core.entries);
491 		tmp_list->core.nr_entries = 0;
492 	}
493 
494 	if (list_empty(&evlist->core.entries)) {
495 		fprintf(stderr, "no cgroup matched: %s\n", str);
496 		goto out_err;
497 	}
498 
499 	ret = 0;
500 	cgrp_event_expanded = true;
501 
502 out_err:
503 	evlist__delete(orig_list);
504 	evlist__delete(tmp_list);
505 	rblist__exit(&orig_metric_events);
506 	release_cgroup_list();
507 
508 	return ret;
509 }
510 
511 static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id,
512 					bool create, const char *path)
513 {
514 	struct rb_node **p = &root->rb_node;
515 	struct rb_node *parent = NULL;
516 	struct cgroup *cgrp;
517 
518 	while (*p != NULL) {
519 		parent = *p;
520 		cgrp = rb_entry(parent, struct cgroup, node);
521 
522 		if (cgrp->id == id)
523 			return cgrp;
524 
525 		if (cgrp->id < id)
526 			p = &(*p)->rb_left;
527 		else
528 			p = &(*p)->rb_right;
529 	}
530 
531 	if (!create)
532 		return NULL;
533 
534 	cgrp = malloc(sizeof(*cgrp));
535 	if (cgrp == NULL)
536 		return NULL;
537 
538 	cgrp->name = strdup(path);
539 	if (cgrp->name == NULL) {
540 		free(cgrp);
541 		return NULL;
542 	}
543 
544 	cgrp->fd = -1;
545 	cgrp->id = id;
546 	refcount_set(&cgrp->refcnt, 1);
547 
548 	rb_link_node(&cgrp->node, parent, p);
549 	rb_insert_color(&cgrp->node, root);
550 
551 	return cgrp;
552 }
553 
554 struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
555 			       const char *path)
556 {
557 	struct cgroup *cgrp;
558 
559 	down_write(&env->cgroups.lock);
560 	cgrp = __cgroup__findnew(&env->cgroups.tree, id, true, path);
561 	up_write(&env->cgroups.lock);
562 	return cgrp;
563 }
564 
565 struct cgroup *cgroup__find(struct perf_env *env, uint64_t id)
566 {
567 	struct cgroup *cgrp;
568 
569 	down_read(&env->cgroups.lock);
570 	cgrp = __cgroup__findnew(&env->cgroups.tree, id, false, NULL);
571 	up_read(&env->cgroups.lock);
572 	return cgrp;
573 }
574 
575 void perf_env__purge_cgroups(struct perf_env *env)
576 {
577 	struct rb_node *node;
578 	struct cgroup *cgrp;
579 
580 	down_write(&env->cgroups.lock);
581 	while (!RB_EMPTY_ROOT(&env->cgroups.tree)) {
582 		node = rb_first(&env->cgroups.tree);
583 		cgrp = rb_entry(node, struct cgroup, node);
584 
585 		rb_erase(node, &env->cgroups.tree);
586 		cgroup__put(cgrp);
587 	}
588 	up_write(&env->cgroups.lock);
589 }
590