diff mbox

[10/54] perf stat: Forbid user passing improper config terms

Message ID 1454680939-24963-11-git-send-email-wangnan0@huawei.com
State New
Headers show

Commit Message

Wang Nan Feb. 5, 2016, 2:01 p.m. UTC
'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 forbit most of those config terms for 'perf stat'.

Result:

 # ./perf stat -e 'instructions/no-inherit/' -e 'instructions/inherit/' bash
 event syntax error: 'instructions/no-inherit/'
                      \___ Don't use record mode only config terms
 ...

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

Signed-off-by: Wang Nan <wangnan0@huawei.com>

Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
---
 tools/perf/builtin-stat.c      |  1 +
 tools/perf/util/parse-events.c | 42 +++++++++++++++++++++++++++++++++++++++---
 tools/perf/util/parse-events.h |  1 +
 3 files changed, 41 insertions(+), 3 deletions(-)

-- 
1.8.3.4
diff mbox

Patch

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 038e877..bee8fcc 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -1825,6 +1825,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);
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 8e08990..962a364 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -40,6 +40,26 @@  static struct perf_pmu_event_symbol *perf_pmu_events_list;
  */
 static int perf_pmu_events_list_num;
 
+typedef int config_term_func_t(struct perf_event_attr *attr,
+			       struct parse_events_term *term,
+			       struct parse_events_error *err);
+static int config_term_base(struct perf_event_attr *attr,
+			    struct parse_events_term *term,
+			    struct parse_events_error *err);
+static int config_term_limited(struct perf_event_attr *attr,
+			       struct parse_events_term *term,
+			       struct parse_events_error *err);
+static config_term_func_t *config_term_common = &config_term_base;
+static int config_attr(struct perf_event_attr *attr,
+		       struct list_head *head,
+		       struct parse_events_error *err,
+		       config_term_func_t config_term);
+
+void parse_events__shrink_config_terms(void)
+{
+	config_term_common = &config_term_limited;
+}
+
 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
 	[PERF_COUNT_HW_CPU_CYCLES] = {
 		.symbol = "cpu-cycles",
@@ -801,9 +821,9 @@  typedef int config_term_func_t(struct perf_event_attr *attr,
 			       struct parse_events_term *term,
 			       struct parse_events_error *err);
 
-static int config_term_common(struct perf_event_attr *attr,
-			      struct parse_events_term *term,
-			      struct parse_events_error *err)
+static int config_term_base(struct perf_event_attr *attr,
+			    struct parse_events_term *term,
+			    struct parse_events_error *err)
 {
 #define CHECK_TYPE_VAL(type)						   \
 do {									   \
@@ -870,6 +890,22 @@  do {									   \
 #undef CHECK_TYPE_VAL
 }
 
+static int config_term_limited(struct perf_event_attr *attr,
+			       struct parse_events_term *term,
+			       struct parse_events_error *err)
+{
+	switch (term->type_term) {
+	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 config_term_base(attr, term, err);
+	default:
+		err->str = strdup("Don't use record mode only config terms");
+		return -EINVAL;
+	}
+}
+
 static int config_term_pmu(struct perf_event_attr *attr,
 			   struct parse_events_term *term,
 			   struct parse_events_error *err)
diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
index 2a2b172..fc61a63 100644
--- a/tools/perf/util/parse-events.h
+++ b/tools/perf/util/parse-events.h
@@ -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,