Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 023695d9 authored by Stephane Eranian's avatar Stephane Eranian Committed by Ingo Molnar
Browse files

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: default avatarStephane Eranian <eranian@google.com>
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
LKML-Reference: <4d590290.825bdf0a.7d0a.4890@mx.google.com>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent e5d1367f
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -137,6 +137,17 @@ Do not update the builid cache. This saves some overhead in situations
where the information in the perf.data file (which includes buildids)
is sufficient.

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

SEE ALSO
--------
linkperf:perf-stat[1], linkperf:perf-list[1]
+11 −0
Original line number Diff line number Diff line
@@ -83,6 +83,17 @@ This option is only valid in system-wide mode.
print counts using a CSV-style output to make it easy to import directly into
spreadsheets. Columns are separated by the string specified in SEP.

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

EXAMPLES
--------

+2 −0
Original line number Diff line number Diff line
@@ -442,6 +442,7 @@ LIB_H += util/pstack.h
LIB_H += util/cpumap.h
LIB_H += util/top.h
LIB_H += $(ARCH_INCLUDE)
LIB_H += util/cgroup.h

LIB_OBJS += $(OUTPUT)util/abspath.o
LIB_OBJS += $(OUTPUT)util/alias.o
@@ -496,6 +497,7 @@ LIB_OBJS += $(OUTPUT)util/probe-event.o
LIB_OBJS += $(OUTPUT)util/util.o
LIB_OBJS += $(OUTPUT)util/xyarray.o
LIB_OBJS += $(OUTPUT)util/cpumap.o
LIB_OBJS += $(OUTPUT)util/cgroup.o

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

+9 −0
Original line number Diff line number Diff line
@@ -807,6 +807,9 @@ const struct option record_options[] = {
		    "do not update the buildid cache"),
	OPT_BOOLEAN('B', "no-buildid", &no_buildid,
		    "do not collect buildids in perf.data"),
	OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
		     "monitor event in cgroup name only",
		     parse_cgroups),
	OPT_END()
};

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

	if (nr_cgroups && !system_wide) {
		fprintf(stderr, "cgroup monitoring only available in"
			" system-wide mode\n");
		usage_with_options(record_usage, record_options);
	}

	symbol__init();

	if (no_buildid_cache || no_buildid)
+32 −8
Original line number Diff line number Diff line
@@ -390,6 +390,9 @@ static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)

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

	if (evsel->cgrp)
		fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);

	if (csv_output)
		return;

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

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

	if (evsel->cgrp)
		fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);

	if (csv_output)
		return;

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

	if (scaled == -1) {
		fprintf(stderr, "%*s%s%-24s\n",
		fprintf(stderr, "%*s%s%*s",
			csv_output ? 0 : 18,
			"<not counted>", csv_sep, event_name(counter));
			"<not counted>",
			csv_sep,
			csv_output ? 0 : -24,
			event_name(counter));

		if (counter->cgrp)
			fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);

		fputc('\n', stderr);
		return;
	}

@@ -487,7 +501,6 @@ static void print_counter_aggr(struct perf_evsel *counter)
		fprintf(stderr, "  (scaled from %.2f%%)",
				100 * avg_running / avg_enabled);
	}

	fprintf(stderr, "\n");
}

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

			fprintf(stderr, "\n");
			if (counter->cgrp)
				fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);

			fputc('\n', stderr);
			continue;
		}

@@ -529,7 +546,7 @@ static void print_counter(struct perf_evsel *counter)
					100.0 * run / ena);
			}
		}
		fprintf(stderr, "\n");
		fputc('\n', stderr);
	}
}

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

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

	/* no_aggr is for system-wide only */
	if (no_aggr && !system_wide)
	/* no_aggr, cgroup are for system-wide only */
	if ((no_aggr || nr_cgroups) && !system_wide) {
		fprintf(stderr, "both cgroup and no-aggregation "
			"modes only available in system-wide mode\n");

		usage_with_options(stat_usage, options);
	}

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