[PATCHSET 0/4] perf stat: Add --multiply-cgroup option

Namhyung Kim
2020-09-08T04:42:46Z

Hello,

When we profile cgroup events with perf stat, it's very annoying to
specify events and cgroups on the command line as it requires the
mapping between events and cgroups. (Note that perf record can use
cgroup sampling but it's not usable for perf stat).

I guess most cases we just want to use a same set of events (N) for
all cgroups (M), but we need to specify NxM events and NxM cgroups.
This is not good especially when profiling large number of cgroups:
say M=200.

So I added --multiply-cgroup option to make it easy for that case. It
will create NxM events from N events and M cgroups. One more upside
is that it can handle metrics too.

For example, the following example measures IPC metric for 3 cgroups

$ cat perf-multi-cgrp.sh
#!/bin/sh

METRIC=${1:-IPC}
CGROUP_DIR=/sys/fs/cgroup/perf_event

sudo mkdir $CGROUP_DIR/A $CGROUP_DIR/B $CGROUP_DIR/C

# add backgroupd workload for each cgroup
echo $$ | sudo tee $CGROUP_DIR/A/cgroup.procs > /dev/null
yes > /dev/null &
echo $$ | sudo tee $CGROUP_DIR/B/cgroup.procs > /dev/null
yes > /dev/null &
echo $$ | sudo tee $CGROUP_DIR/C/cgroup.procs > /dev/null
yes > /dev/null &

# run 'perf stat' in the root cgroup
echo $$ | sudo tee $CGROUP_DIR/cgroup.procs > /dev/null
perf stat -a -M $METRIC --multiply-cgroup -G A,B,C sleep 1

kill %1 %2 %3
sudo rmdir $CGROUP_DIR/A $CGROUP_DIR/B $CGROUP_DIR/C

$ ./perf-multi-cgrp.sh IPC

Performance counter stats for 'system wide':

11,284,850,010 inst_retired.any A # 2.71 IPC
4,157,915,982 cpu_clk_unhalted.thread A
11,342,188,640 inst_retired.any B # 2.72 IPC
4,173,014,732 cpu_clk_unhalted.thread B
11,135,863,604 inst_retired.any C # 2.67 IPC
4,171,375,184 cpu_clk_unhalted.thread C

1.011948803 seconds time elapsed

The code is available at 'perf/cgroup-multiply-v1' branch on

git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks
Namhyung

Namhyung Kim (4):
perf evsel: Add evsel__clone() function
perf stat: Add --multiply-cgroup option
perf tools: Copy metric events properly when multiply cgroups
perf test: Add multiply cgroup event test

tools/perf/builtin-stat.c | 21 ++-
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 4 +
tools/perf/tests/multiply-cgroup.c | 203 +++++++++++++++++++++++++++++
tools/perf/tests/tests.h | 1 +
tools/perf/util/cgroup.c | 106 ++++++++++++++-
tools/perf/util/cgroup.h | 4 +
tools/perf/util/evlist.c | 11 ++
tools/perf/util/evlist.h | 1 +
tools/perf/util/evsel.c | 57 ++++++++
tools/perf/util/evsel.h | 1 +
tools/perf/util/metricgroup.c | 77 +++++++++++
tools/perf/util/metricgroup.h | 6 +
tools/perf/util/stat.h | 1 +
14 files changed, 488 insertions(+), 6 deletions(-)
create mode 100644 tools/perf/tests/multiply-cgroup.c

--
2.28.0.526.ge36021eeef-goog