Release 4.10 tools/perf/util/cgroup.c
#include "util.h"
#include "../perf.h"
#include <subcmd/parse-options.h>
#include "evsel.h"
#include "cgroup.h"
#include "evlist.h"
int nr_cgroups;
static int
cgroupfs_find_mountpoint(char *buf, size_t maxlen)
{
FILE *fp;
char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
char *token, *saved_ptr = NULL;
int found = 0;
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
*/
while (fscanf(fp, "%*s %"STR(PATH_MAX)"s %"STR(PATH_MAX)"s %"
STR(PATH_MAX)"s %*d %*d\n",
mountpoint, type, tokens) == 3) {
if (!strcmp(type, "cgroup")) {
token = strtok_r(tokens, ",", &saved_ptr);
while (token != NULL) {
if (!strcmp(token, "perf_event")) {
found = 1;
break;
}
token = strtok_r(NULL, ",", &saved_ptr);
}
}
if (found)
break;
}
fclose(fp);
if (!found)
return -1;
if (strlen(mountpoint) < maxlen) {
strcpy(buf, mountpoint);
return 0;
}
return -1;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
stephane eranian | stephane eranian | 203 | 96.21% | 1 | 33.33% |
arnaldo carvalho de melo | arnaldo carvalho de melo | 6 | 2.84% | 1 | 33.33% |
eric dumazet | eric dumazet | 2 | 0.95% | 1 | 33.33% |
| Total | 211 | 100.00% | 3 | 100.00% |
static int open_cgroup(char *name)
{
char path[PATH_MAX + 1];
char mnt[PATH_MAX + 1];
int fd;
if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
return -1;
snprintf(path, PATH_MAX, "%s/%s", mnt, name);
fd = open(path, O_RDONLY);
if (fd == -1)
fprintf(stderr, "no access to cgroup %s\n", path);
return fd;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
stephane eranian | stephane eranian | 81 | 95.29% | 1 | 50.00% |
arnaldo carvalho de melo | arnaldo carvalho de melo | 4 | 4.71% | 1 | 50.00% |
| Total | 85 | 100.00% | 2 | 100.00% |
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
*/
evlist__for_each_entry(evlist, counter) {
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;
evlist__for_each_entry(evlist, counter) {
if (n == nr_cgroups)
goto found;
n++;
}
if (atomic_read(&cgrp->refcnt) == 0)
free(cgrp);
return -1;
found:
atomic_inc(&cgrp->refcnt);
counter->cgrp = cgrp;
return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
stephane eranian | stephane eranian | 181 | 92.82% | 1 | 25.00% |
arnaldo carvalho de melo | arnaldo carvalho de melo | 14 | 7.18% | 3 | 75.00% |
| Total | 195 | 100.00% | 4 | 100.00% |
void close_cgroup(struct cgroup_sel *cgrp)
{
if (cgrp && atomic_dec_and_test(&cgrp->refcnt)) {
close(cgrp->fd);
zfree(&cgrp->name);
free(cgrp);
}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
stephane eranian | stephane eranian | 37 | 84.09% | 1 | 33.33% |
arnaldo carvalho de melo | arnaldo carvalho de melo | 7 | 15.91% | 2 | 66.67% |
| Total | 44 | 100.00% | 3 | 100.00% |
int parse_cgroups(const struct option *opt __maybe_unused, const char *str,
int unset __maybe_unused)
{
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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
stephane eranian | stephane eranian | 180 | 98.90% | 1 | 50.00% |
irina tirdea | irina tirdea | 2 | 1.10% | 1 | 50.00% |
| Total | 182 | 100.00% | 2 | 100.00% |
Overall Contributors
| Person | Tokens | Prop | Commits | CommitProp |
stephane eranian | stephane eranian | 702 | 95.12% | 1 | 11.11% |
arnaldo carvalho de melo | arnaldo carvalho de melo | 31 | 4.20% | 5 | 55.56% |
irina tirdea | irina tirdea | 2 | 0.27% | 1 | 11.11% |
eric dumazet | eric dumazet | 2 | 0.27% | 1 | 11.11% |
josh poimboeuf | josh poimboeuf | 1 | 0.14% | 1 | 11.11% |
| Total | 738 | 100.00% | 9 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.