Skip to content

Commit 023695d

Browse files
Stephane EranianIngo Molnar
authored andcommitted
perf tool: Add cgroup support
This patch adds the ability to filter monitoring based on container groups (cgroups) for both perf stat and perf record. It is possible to monitor multiple cgroup in parallel. There is one cgroup per event. The cgroups to monitor are passed via a new -G option followed by a comma separated list of cgroup names. The cgroup filesystem has to be mounted. Given a cgroup name, the perf tool finds the corresponding directory in the cgroup filesystem and opens it. It then passes that file descriptor to the kernel. Example: $ perf stat -B -a -e cycles:u,cycles:u,cycles:u -G test1,,test2 -- sleep 1 Performance counter stats for 'sleep 1': 2,368,667,414 cycles test1 2,369,661,459 cycles <not counted> cycles test2 1.001856890 seconds time elapsed Signed-off-by: Stephane Eranian <eranian@google.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <4d590290.825bdf0a.7d0a.4890@mx.google.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent e5d1367 commit 023695d

9 files changed

Lines changed: 276 additions & 10 deletions

File tree

‎tools/perf/Documentation/perf-record.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ Do not update the builid cache. This saves some overhead in situations
137137
where the information in the perf.data file (which includes buildids)
138138
is sufficient.
139139

140+
-G name,...::
141+
--cgroup name,...::
142+
monitor only in the container (cgroup) called "name". This option is available only
143+
in per-cpu mode. The cgroup filesystem must be mounted. All threads belonging to
144+
container "name" are monitored when they run on the monitored CPUs. Multiple cgroups
145+
can be provided. Each cgroup is applied to the corresponding event, i.e., first cgroup
146+
to first event, second cgroup to second event and so on. It is possible to provide
147+
an empty cgroup (monitor all the time) using, e.g., -G foo,,bar. Cgroups must have
148+
corresponding events, i.e., they always refer to events defined earlier on the command
149+
line.
150+
140151
SEE ALSO
141152
--------
142153
linkperf:perf-stat[1], linkperf:perf-list[1]

‎tools/perf/Documentation/perf-stat.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ This option is only valid in system-wide mode.
8383
print counts using a CSV-style output to make it easy to import directly into
8484
spreadsheets. Columns are separated by the string specified in SEP.
8585

86+
-G name::
87+
--cgroup name::
88+
monitor only in the container (cgroup) called "name". This option is available only
89+
in per-cpu mode. The cgroup filesystem must be mounted. All threads belonging to
90+
container "name" are monitored when they run on the monitored CPUs. Multiple cgroups
91+
can be provided. Each cgroup is applied to the corresponding event, i.e., first cgroup
92+
to first event, second cgroup to second event and so on. It is possible to provide
93+
an empty cgroup (monitor all the time) using, e.g., -G foo,,bar. Cgroups must have
94+
corresponding events, i.e., they always refer to events defined earlier on the command
95+
line.
96+
8697
EXAMPLES
8798
--------
8899

‎tools/perf/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ LIB_H += util/pstack.h
442442
LIB_H += util/cpumap.h
443443
LIB_H += util/top.h
444444
LIB_H += $(ARCH_INCLUDE)
445+
LIB_H += util/cgroup.h
445446

446447
LIB_OBJS += $(OUTPUT)util/abspath.o
447448
LIB_OBJS += $(OUTPUT)util/alias.o
@@ -496,6 +497,7 @@ LIB_OBJS += $(OUTPUT)util/probe-event.o
496497
LIB_OBJS += $(OUTPUT)util/util.o
497498
LIB_OBJS += $(OUTPUT)util/xyarray.o
498499
LIB_OBJS += $(OUTPUT)util/cpumap.o
500+
LIB_OBJS += $(OUTPUT)util/cgroup.o
499501

500502
BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
501503

‎tools/perf/builtin-record.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,9 @@ const struct option record_options[] = {
807807
"do not update the buildid cache"),
808808
OPT_BOOLEAN('B', "no-buildid", &no_buildid,
809809
"do not collect buildids in perf.data"),
810+
OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
811+
"monitor event in cgroup name only",
812+
parse_cgroups),
810813
OPT_END()
811814
};
812815

@@ -835,6 +838,12 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
835838
write_mode = WRITE_FORCE;
836839
}
837840

841+
if (nr_cgroups && !system_wide) {
842+
fprintf(stderr, "cgroup monitoring only available in"
843+
" system-wide mode\n");
844+
usage_with_options(record_usage, record_options);
845+
}
846+
838847
symbol__init();
839848

840849
if (no_buildid_cache || no_buildid)

‎tools/perf/builtin-stat.c‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
390390

391391
fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel));
392392

393+
if (evsel->cgrp)
394+
fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);
395+
393396
if (csv_output)
394397
return;
395398

@@ -420,6 +423,9 @@ static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
420423

421424
fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(evsel));
422425

426+
if (evsel->cgrp)
427+
fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);
428+
423429
if (csv_output)
424430
return;
425431

@@ -460,9 +466,17 @@ static void print_counter_aggr(struct perf_evsel *counter)
460466
int scaled = counter->counts->scaled;
461467

462468
if (scaled == -1) {
463-
fprintf(stderr, "%*s%s%-24s\n",
469+
fprintf(stderr, "%*s%s%*s",
464470
csv_output ? 0 : 18,
465-
"<not counted>", csv_sep, event_name(counter));
471+
"<not counted>",
472+
csv_sep,
473+
csv_output ? 0 : -24,
474+
event_name(counter));
475+
476+
if (counter->cgrp)
477+
fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);
478+
479+
fputc('\n', stderr);
466480
return;
467481
}
468482

@@ -487,7 +501,6 @@ static void print_counter_aggr(struct perf_evsel *counter)
487501
fprintf(stderr, " (scaled from %.2f%%)",
488502
100 * avg_running / avg_enabled);
489503
}
490-
491504
fprintf(stderr, "\n");
492505
}
493506

@@ -505,14 +518,18 @@ static void print_counter(struct perf_evsel *counter)
505518
ena = counter->counts->cpu[cpu].ena;
506519
run = counter->counts->cpu[cpu].run;
507520
if (run == 0 || ena == 0) {
508-
fprintf(stderr, "CPU%*d%s%*s%s%-24s",
521+
fprintf(stderr, "CPU%*d%s%*s%s%*s",
509522
csv_output ? 0 : -4,
510523
evsel_list->cpus->map[cpu], csv_sep,
511524
csv_output ? 0 : 18,
512525
"<not counted>", csv_sep,
526+
csv_output ? 0 : -24,
513527
event_name(counter));
514528

515-
fprintf(stderr, "\n");
529+
if (counter->cgrp)
530+
fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);
531+
532+
fputc('\n', stderr);
516533
continue;
517534
}
518535

@@ -529,7 +546,7 @@ static void print_counter(struct perf_evsel *counter)
529546
100.0 * run / ena);
530547
}
531548
}
532-
fprintf(stderr, "\n");
549+
fputc('\n', stderr);
533550
}
534551
}
535552

@@ -642,6 +659,9 @@ static const struct option options[] = {
642659
"disable CPU count aggregation"),
643660
OPT_STRING('x', "field-separator", &csv_sep, "separator",
644661
"print counts with custom separator"),
662+
OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
663+
"monitor event in cgroup name only",
664+
parse_cgroups),
645665
OPT_END()
646666
};
647667

@@ -682,9 +702,13 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
682702
if (run_count <= 0)
683703
usage_with_options(stat_usage, options);
684704

685-
/* no_aggr is for system-wide only */
686-
if (no_aggr && !system_wide)
705+
/* no_aggr, cgroup are for system-wide only */
706+
if ((no_aggr || nr_cgroups) && !system_wide) {
707+
fprintf(stderr, "both cgroup and no-aggregation "
708+
"modes only available in system-wide mode\n");
709+
687710
usage_with_options(stat_usage, options);
711+
}
688712

689713
/* Set attrs and nr_counters if no event is selected and !null_run */
690714
if (!null_run && !evsel_list->nr_entries) {

‎tools/perf/util/cgroup.c‎

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include "util.h"
2+
#include "../perf.h"
3+
#include "parse-options.h"
4+
#include "evsel.h"
5+
#include "cgroup.h"
6+
#include "debugfs.h" /* MAX_PATH, STR() */
7+
#include "evlist.h"
8+
9+
int nr_cgroups;
10+
11+
static int
12+
cgroupfs_find_mountpoint(char *buf, size_t maxlen)
13+
{
14+
FILE *fp;
15+
char mountpoint[MAX_PATH+1], tokens[MAX_PATH+1], type[MAX_PATH+1];
16+
char *token, *saved_ptr;
17+
int found = 0;
18+
19+
fp = fopen("/proc/mounts", "r");
20+
if (!fp)
21+
return -1;
22+
23+
/*
24+
* in order to handle split hierarchy, we need to scan /proc/mounts
25+
* and inspect every cgroupfs mount point to find one that has
26+
* perf_event subsystem
27+
*/
28+
while (fscanf(fp, "%*s %"STR(MAX_PATH)"s %"STR(MAX_PATH)"s %"
29+
STR(MAX_PATH)"s %*d %*d\n",
30+
mountpoint, type, tokens) == 3) {
31+
32+
if (!strcmp(type, "cgroup")) {
33+
34+
token = strtok_r(tokens, ",", &saved_ptr);
35+
36+
while (token != NULL) {
37+
if (!strcmp(token, "perf_event")) {
38+
found = 1;
39+
break;
40+
}
41+
token = strtok_r(NULL, ",", &saved_ptr);
42+
}
43+
}
44+
if (found)
45+
break;
46+
}
47+
fclose(fp);
48+
if (!found)
49+
return -1;
50+
51+
if (strlen(mountpoint) < maxlen) {
52+
strcpy(buf, mountpoint);
53+
return 0;
54+
}
55+
return -1;
56+
}
57+
58+
static int open_cgroup(char *name)
59+
{
60+
char path[MAX_PATH+1];
61+
char mnt[MAX_PATH+1];
62+
int fd;
63+
64+
65+
if (cgroupfs_find_mountpoint(mnt, MAX_PATH+1))
66+
return -1;
67+
68+
snprintf(path, MAX_PATH, "%s/%s", mnt, name);
69+
70+
fd = open(path, O_RDONLY);
71+
if (fd == -1)
72+
fprintf(stderr, "no access to cgroup %s\n", path);
73+
74+
return fd;
75+
}
76+
77+
static int add_cgroup(struct perf_evlist *evlist, char *str)
78+
{
79+
struct perf_evsel *counter;
80+
struct cgroup_sel *cgrp = NULL;
81+
int n;
82+
/*
83+
* check if cgrp is already defined, if so we reuse it
84+
*/
85+
list_for_each_entry(counter, &evlist->entries, node) {
86+
cgrp = counter->cgrp;
87+
if (!cgrp)
88+
continue;
89+
if (!strcmp(cgrp->name, str))
90+
break;
91+
92+
cgrp = NULL;
93+
}
94+
95+
if (!cgrp) {
96+
cgrp = zalloc(sizeof(*cgrp));
97+
if (!cgrp)
98+
return -1;
99+
100+
cgrp->name = str;
101+
102+
cgrp->fd = open_cgroup(str);
103+
if (cgrp->fd == -1) {
104+
free(cgrp);
105+
return -1;
106+
}
107+
}
108+
109+
/*
110+
* find corresponding event
111+
* if add cgroup N, then need to find event N
112+
*/
113+
n = 0;
114+
list_for_each_entry(counter, &evlist->entries, node) {
115+
if (n == nr_cgroups)
116+
goto found;
117+
n++;
118+
}
119+
if (cgrp->refcnt == 0)
120+
free(cgrp);
121+
122+
return -1;
123+
found:
124+
cgrp->refcnt++;
125+
counter->cgrp = cgrp;
126+
return 0;
127+
}
128+
129+
void close_cgroup(struct cgroup_sel *cgrp)
130+
{
131+
if (!cgrp)
132+
return;
133+
134+
/* XXX: not reentrant */
135+
if (--cgrp->refcnt == 0) {
136+
close(cgrp->fd);
137+
free(cgrp->name);
138+
free(cgrp);
139+
}
140+
}
141+
142+
int parse_cgroups(const struct option *opt __used, const char *str,
143+
int unset __used)
144+
{
145+
struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
146+
const char *p, *e, *eos = str + strlen(str);
147+
char *s;
148+
int ret;
149+
150+
if (list_empty(&evlist->entries)) {
151+
fprintf(stderr, "must define events before cgroups\n");
152+
return -1;
153+
}
154+
155+
for (;;) {
156+
p = strchr(str, ',');
157+
e = p ? p : eos;
158+
159+
/* allow empty cgroups, i.e., skip */
160+
if (e - str) {
161+
/* termination added */
162+
s = strndup(str, e - str);
163+
if (!s)
164+
return -1;
165+
ret = add_cgroup(evlist, s);
166+
if (ret) {
167+
free(s);
168+
return -1;
169+
}
170+
}
171+
/* nr_cgroups is increased een for empty cgroups */
172+
nr_cgroups++;
173+
if (!p)
174+
break;
175+
str = p+1;
176+
}
177+
return 0;
178+
}

‎tools/perf/util/cgroup.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef __CGROUP_H__
2+
#define __CGROUP_H__
3+
4+
struct option;
5+
6+
struct cgroup_sel {
7+
char *name;
8+
int fd;
9+
int refcnt;
10+
};
11+
12+
13+
extern int nr_cgroups; /* number of explicit cgroups defined */
14+
extern void close_cgroup(struct cgroup_sel *cgrp);
15+
extern int parse_cgroups(const struct option *opt, const char *str, int unset);
16+
17+
#endif /* __CGROUP_H__ */

0 commit comments

Comments
 (0)