perf stat: Bail out on unsupported event config modifiers
authorWang Nan <wangnan0@huawei.com>
Fri, 19 Feb 2016 11:43:58 +0000 (11:43 +0000)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 19 Feb 2016 22:12:49 +0000 (19:12 -0300)
'perf stat' accepts some config terms but doesn't apply them. For
example:

  # perf stat -e 'instructions/no-inherit/' -e 'instructions/inherit/' bash
  # ls
  # exit

  Performance counter stats for 'bash':

         266258061      instructions/no-inherit/
         266258061      instructions/inherit/

       1.402183915 seconds time elapsed

The result is confusing, because user may expect the first
'instructions' event exclude the 'ls' command.

This patch forbid most of these config terms for 'perf stat'.

Result:

  # ./perf stat -e 'instructions/no-inherit/' -e 'instructions/inherit/' bash
  event syntax error: 'instructions/no-inherit/'
                       \___ 'no-inherit' is not usable in 'perf stat'
  ...

We can add blocked config terms back when 'perf stat' really supports them.

This patch also removes unavailable config term from error message:

  # ./perf stat -e 'instructions/badterm/' ls
  event syntax error: 'instructions/badterm/'
                                    \___ unknown term

  valid terms: config,config1,config2,name

  # ./perf stat -e 'cpu/badterm/' ls
  event syntax error: 'cpu/badterm/'
                           \___ unknown term

  valid terms: pc,any,inv,edge,cmask,event,in_tx,ldlat,umask,in_tx_cp,offcore_rsp,config,config1,config2,name

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Cody P Schafer <dev@codyps.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jeremie Galarneau <jeremie.galarneau@efficios.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kirill Smelkov <kirr@nexedi.com>
Cc: Li Zefan <lizefan@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1455882283-79592-11-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-stat.c
tools/perf/util/parse-events.c
tools/perf/util/parse-events.h

index 86289dfcb4524457599910777825fb2ecba92973..8c0bc0fe51790706f9041a46d1c01a0cfee63eb7 100644 (file)
@@ -1831,6 +1831,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
        if (evsel_list == NULL)
                return -ENOMEM;
 
+       parse_events__shrink_config_terms();
        argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
                                        (const char **) stat_usage,
                                        PARSE_OPT_STOP_AT_NON_OPTION);
index fd085d5f5c7921729486db660c2f2ac18d84d08b..eb5df43ec68fa01c9dd7e5fd7e6aecfd90ed1859 100644 (file)
@@ -765,6 +765,41 @@ static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
        [PARSE_EVENTS__TERM_TYPE_INHERIT]               = "inherit",
 };
 
+static bool config_term_shrinked;
+
+static bool
+config_term_avail(int term_type, struct parse_events_error *err)
+{
+       if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
+               err->str = strdup("Invalid term_type");
+               return false;
+       }
+       if (!config_term_shrinked)
+               return true;
+
+       switch (term_type) {
+       case PARSE_EVENTS__TERM_TYPE_CONFIG:
+       case PARSE_EVENTS__TERM_TYPE_CONFIG1:
+       case PARSE_EVENTS__TERM_TYPE_CONFIG2:
+       case PARSE_EVENTS__TERM_TYPE_NAME:
+               return true;
+       default:
+               if (!err)
+                       return false;
+
+               /* term_type is validated so indexing is safe */
+               if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
+                            config_term_names[term_type]) < 0)
+                       err->str = NULL;
+               return false;
+       }
+}
+
+void parse_events__shrink_config_terms(void)
+{
+       config_term_shrinked = true;
+}
+
 typedef int config_term_func_t(struct perf_event_attr *attr,
                               struct parse_events_term *term,
                               struct parse_events_error *err);
@@ -834,6 +869,17 @@ do {                                                                          \
                return -EINVAL;
        }
 
+       /*
+        * Check term availbility after basic checking so
+        * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
+        *
+        * If check availbility at the entry of this function,
+        * user will see "'<sysfs term>' is not usable in 'perf stat'"
+        * if an invalid config term is provided for legacy events
+        * (for example, instructions/badterm/...), which is confusing.
+        */
+       if (!config_term_avail(term->type_term, err))
+               return -EINVAL;
        return 0;
 #undef CHECK_TYPE_VAL
 }
@@ -2125,6 +2171,8 @@ static void config_terms_list(char *buf, size_t buf_sz)
        for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
                const char *name = config_term_names[i];
 
+               if (!config_term_avail(i, NULL))
+                       continue;
                if (!name)
                        continue;
                if (name[0] == '<')
index b50d50b96f95494b5f1a1dddd6cea9be6ae53299..76151f9f00d2176920dfe014b79624f40c0607eb 100644 (file)
@@ -105,6 +105,7 @@ struct parse_events_terms {
        struct list_head *terms;
 };
 
+void parse_events__shrink_config_terms(void);
 int parse_events__is_hardcoded_term(struct parse_events_term *term);
 int parse_events_term__num(struct parse_events_term **term,
                           int type_term, char *config, u64 num,