cgroup.c 4.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
S
Stephane Eranian 已提交
2 3
#include "util.h"
#include "../perf.h"
4
#include <subcmd/parse-options.h>
S
Stephane Eranian 已提交
5 6 7
#include "evsel.h"
#include "cgroup.h"
#include "evlist.h"
8
#include <linux/stringify.h>
9 10 11
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
S
Stephane Eranian 已提交
12 13 14 15 16 17 18

int nr_cgroups;

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

	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
	 */
32 33 34
	path_v1[0] = '\0';
	path_v2[0] = '\0';

35 36
	while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %"
				__stringify(PATH_MAX)"s %*d %*d\n",
S
Stephane Eranian 已提交
37 38
				mountpoint, type, tokens) == 3) {

39
		if (!path_v1[0] && !strcmp(type, "cgroup")) {
S
Stephane Eranian 已提交
40 41 42 43 44

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

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

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

		if (path_v1[0] && path_v2[0])
S
Stephane Eranian 已提交
56 57 58
			break;
	}
	fclose(fp);
59 60 61 62 63 64

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

67 68
	if (strlen(path) < maxlen) {
		strcpy(buf, path);
S
Stephane Eranian 已提交
69 70 71 72 73 74 75
		return 0;
	}
	return -1;
}

static int open_cgroup(char *name)
{
76 77
	char path[PATH_MAX + 1];
	char mnt[PATH_MAX + 1];
S
Stephane Eranian 已提交
78 79 80
	int fd;


81
	if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
S
Stephane Eranian 已提交
82 83
		return -1;

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

	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;
96
	struct cgroup *cgrp = NULL;
S
Stephane Eranian 已提交
97 98 99 100
	int n;
	/*
	 * check if cgrp is already defined, if so we reuse it
	 */
101
	evlist__for_each_entry(evlist, counter) {
102
		if (!counter->cgrp)
S
Stephane Eranian 已提交
103
			continue;
104 105
		if (!strcmp(counter->cgrp->name, str)) {
			cgrp = cgroup__get(counter->cgrp);
S
Stephane Eranian 已提交
106
			break;
107
		}
S
Stephane Eranian 已提交
108 109 110 111 112 113 114 115
	}

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

		cgrp->name = str;
116
		refcount_set(&cgrp->refcnt, 1);
S
Stephane Eranian 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129

		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;
130
	evlist__for_each_entry(evlist, counter) {
S
Stephane Eranian 已提交
131 132 133 134 135
		if (n == nr_cgroups)
			goto found;
		n++;
	}

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

143 144 145 146 147 148 149
static void cgroup__delete(struct cgroup *cgroup)
{
	close(cgroup->fd);
	zfree(&cgroup->name);
	free(cgroup);
}

150
void cgroup__put(struct cgroup *cgrp)
S
Stephane Eranian 已提交
151
{
152
	if (cgrp && refcount_dec_and_test(&cgrp->refcnt)) {
153
		cgroup__delete(cgrp);
S
Stephane Eranian 已提交
154 155 156
	}
}

157 158 159 160 161 162 163
struct cgroup *cgroup__get(struct cgroup *cgroup)
{
       if (cgroup)
		refcount_inc(&cgroup->refcnt);
       return cgroup;
}

164
int parse_cgroups(const struct option *opt, const char *str,
165
		  int unset __maybe_unused)
S
Stephane Eranian 已提交
166 167
{
	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
168
	struct perf_evsel *counter;
169
	struct cgroup *cgrp = NULL;
S
Stephane Eranian 已提交
170 171
	const char *p, *e, *eos = str + strlen(str);
	char *s;
172
	int ret, i;
S
Stephane Eranian 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

	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;
	}
201 202 203 204 205 206 207 208 209 210 211 212 213
	/* for the case one cgroup combine to multiple events */
	i = 0;
	if (nr_cgroups == 1) {
		evlist__for_each_entry(evlist, counter) {
			if (i == 0)
				cgrp = counter->cgrp;
			else {
				counter->cgrp = cgrp;
				refcount_inc(&cgrp->refcnt);
			}
			i++;
		}
	}
S
Stephane Eranian 已提交
214 215
	return 0;
}