[PATCH v2] perf stat: Append to default list if use -e +event

Jin Yao
2021-01-04T02:22:38Z

The default event list includes the most common events which are widely
used by users. But with -e option, the current perf only counts the events
assigned by -e option. Users may want to collect some extra events with
the default list. For this case, users have to manually add all the events
from the default list. It's inconvenient. Also, users may don't know how to
get the default list.

Now it supports a simple syntax: -e +event

The prefix '+' tells perf to append this event (or event list) to default
event list.

Before:

root@kbl-ppc:~# ./perf stat -e power/energy-pkg/ -a -- sleep 1

Performance counter stats for 'system wide':

2.04 Joules power/energy-pkg/

1.000863884 seconds time elapsed

After:

root@kbl-ppc:~# ./perf stat -e +power/energy-pkg/ -a -- sleep 1

Performance counter stats for 'system wide':

2.11 Joules +power/energy-pkg/ # 0.000 K/sec
8,007.17 msec cpu-clock # 7.993 CPUs utilized
125 context-switches # 0.016 K/sec
8 cpu-migrations # 0.001 K/sec
2 page-faults # 0.000 K/sec
8,520,084 cycles # 0.001 GHz
2,808,302 instructions # 0.33 insn per cycle
555,427 branches # 0.069 M/sec
59,005 branch-misses # 10.62% of all branches

1.001832003 seconds time elapsed

Signed-off-by: Jin Yao <yao.jin@linux.intel.com>
---
v2:
We just use a simple syntax: -e +event to append the -e event
to default list.

v1:
Create a new option '--add-default' to append -e event to
default list.

tools/perf/Documentation/perf-stat.txt | 1 +
tools/perf/builtin-stat.c | 3 ++-
tools/perf/util/evlist.c | 10 ++++++++++
tools/perf/util/evlist.h | 1 +
4 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
index 5d4a673d7621..f60af902b8e1 100644
--- a/tools/perf/Documentation/perf-stat.txt
+++ b/tools/perf/Documentation/perf-stat.txt
@@ -63,6 +63,7 @@ report::
Multiple PMU instances are typical for uncore PMUs, so the prefix
'uncore_' is also ignored when performing this match.

+ If the prefix '+' is used, the event is appended to default event list.

-i::
--no-inherit::
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 8cc24967bc27..8400953473ef 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -1763,7 +1763,8 @@ static int add_default_attributes(void)
free(str);
}

- if (!evsel_list->core.nr_entries) {
+ if (!evsel_list->core.nr_entries ||
+ evlist__append_default_list(evsel_list)) {
if (target__has_cpu(&target))
default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 05363a7247c4..fceef4e57964 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -2010,3 +2010,13 @@ struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
}
return NULL;
}
+
+bool evlist__append_default_list(struct evlist *evlist)
+{
+ struct evsel *first = evlist__first(evlist);
+
+ if (first->name && first->name[0] == '+')
+ return true;
+
+ return false;
+}
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 1aae75895dea..54592bd22bc2 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -353,4 +353,5 @@ int evlist__ctlfd_ack(struct evlist *evlist);
#define EVLIST_DISABLED_MSG "Events disabled\n"

struct evsel *evlist__find_evsel(struct evlist *evlist, int idx);
+bool evlist__append_default_list(struct evlist *evlist);
#endif /* __PERF_EVLIST_H */
--
2.17.1