cgroup.c 3.4 KB
Newer Older
S
Stephane Eranian 已提交
1 2
#include "util.h"
#include "../perf.h"
3
#include <subcmd/parse-options.h>
S
Stephane Eranian 已提交
4 5 6
#include "evsel.h"
#include "cgroup.h"
#include "evlist.h"
7
#include <linux/stringify.h>
S
Stephane Eranian 已提交
8 9 10 11 12 13 14

int nr_cgroups;

static int
cgroupfs_find_mountpoint(char *buf, size_t maxlen)
{
	FILE *fp;
15
	char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
16
	char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path;
17
	char *token, *saved_ptr = NULL;
S
Stephane Eranian 已提交
18 19 20 21 22 23 24 25 26 27

	fp = fopen("/proc/mounts", "r");
	if (!fp)
		return -1;

	/*
	 * in order to handle split hierarchy, we need to scan /proc/mounts
	 * and inspect every cgroupfs mount point to find one that has
	 * perf_event subsystem
	 */
28 29 30
	path_v1[0] = '\0';
	path_v2[0] = '\0';

31 32
	while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %"
				__stringify(PATH_MAX)"s %*d %*d\n",
S
Stephane Eranian 已提交
33 34
				mountpoint, type, tokens) == 3) {

35
		if (!path_v1[0] && !strcmp(type, "cgroup")) {
S
Stephane Eranian 已提交
36 37 38 39 40

			token = strtok_r(tokens, ",", &saved_ptr);

			while (token != NULL) {
				if (!strcmp(token, "perf_event")) {
41
					strcpy(path_v1, mountpoint);
S
Stephane Eranian 已提交
42 43 44 45 46
					break;
				}
				token = strtok_r(NULL, ",", &saved_ptr);
			}
		}
47 48 49 50 51

		if (!path_v2[0] && !strcmp(type, "cgroup2"))
			strcpy(path_v2, mountpoint);

		if (path_v1[0] && path_v2[0])
S
Stephane Eranian 已提交
52 53 54
			break;
	}
	fclose(fp);
55 56 57 58 59 60

	if (path_v1[0])
		path = path_v1;
	else if (path_v2[0])
		path = path_v2;
	else
S
Stephane Eranian 已提交
61 62
		return -1;

63 64
	if (strlen(path) < maxlen) {
		strcpy(buf, path);
S
Stephane Eranian 已提交
65 66 67 68 69 70 71
		return 0;
	}
	return -1;
}

static int open_cgroup(char *name)
{
72 73
	char path[PATH_MAX + 1];
	char mnt[PATH_MAX + 1];
S
Stephane Eranian 已提交
74 75 76
	int fd;


77
	if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
S
Stephane Eranian 已提交
78 79
		return -1;

80
	snprintf(path, PATH_MAX, "%s/%s", mnt, name);
S
Stephane Eranian 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

	fd = open(path, O_RDONLY);
	if (fd == -1)
		fprintf(stderr, "no access to cgroup %s\n", path);

	return fd;
}

static int add_cgroup(struct perf_evlist *evlist, char *str)
{
	struct perf_evsel *counter;
	struct cgroup_sel *cgrp = NULL;
	int n;
	/*
	 * check if cgrp is already defined, if so we reuse it
	 */
97
	evlist__for_each_entry(evlist, counter) {
S
Stephane Eranian 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		cgrp = counter->cgrp;
		if (!cgrp)
			continue;
		if (!strcmp(cgrp->name, str))
			break;

		cgrp = NULL;
	}

	if (!cgrp) {
		cgrp = zalloc(sizeof(*cgrp));
		if (!cgrp)
			return -1;

		cgrp->name = str;

		cgrp->fd = open_cgroup(str);
		if (cgrp->fd == -1) {
			free(cgrp);
			return -1;
		}
	}

	/*
	 * find corresponding event
	 * if add cgroup N, then need to find event N
	 */
	n = 0;
126
	evlist__for_each_entry(evlist, counter) {
S
Stephane Eranian 已提交
127 128 129 130
		if (n == nr_cgroups)
			goto found;
		n++;
	}
131
	if (refcount_read(&cgrp->refcnt) == 0)
S
Stephane Eranian 已提交
132 133 134 135
		free(cgrp);

	return -1;
found:
136
	refcount_inc(&cgrp->refcnt);
S
Stephane Eranian 已提交
137 138 139 140 141 142
	counter->cgrp = cgrp;
	return 0;
}

void close_cgroup(struct cgroup_sel *cgrp)
{
143
	if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
S
Stephane Eranian 已提交
144
		close(cgrp->fd);
145
		zfree(&cgrp->name);
S
Stephane Eranian 已提交
146 147 148 149
		free(cgrp);
	}
}

150 151
int parse_cgroups(const struct option *opt __maybe_unused, const char *str,
		  int unset __maybe_unused)
S
Stephane Eranian 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
	const char *p, *e, *eos = str + strlen(str);
	char *s;
	int ret;

	if (list_empty(&evlist->entries)) {
		fprintf(stderr, "must define events before cgroups\n");
		return -1;
	}

	for (;;) {
		p = strchr(str, ',');
		e = p ? p : eos;

		/* allow empty cgroups, i.e., skip */
		if (e - str) {
			/* termination added */
			s = strndup(str, e - str);
			if (!s)
				return -1;
			ret = add_cgroup(evlist, s);
			if (ret) {
				free(s);
				return -1;
			}
		}
		/* nr_cgroups is increased een for empty cgroups */
		nr_cgroups++;
		if (!p)
			break;
		str = p+1;
	}
	return 0;
}