Skip to contentSkip to conversation

perf tool: event parsing enhancements/fixes

PATCH In review Jiri Olsa 2 participants 13 messages Apr 4, 2012 – Apr 13, 2012 perf events

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

cover perf tool: event parsing enhancements/fixes

Hi,
adding some enhancements/fixies for perf event parsing.

- patch 1 is simple fix

- patches 2, 3, 4 and 5 are event config terms enhancements

- patches 6, 7 and 8 are maintanance patches either helping with developement (6, 7) or making the code more clear (8).

Attached patches:
1/8 perf, tool: Fix NULL deref in hists browsing code
2/8 perf, tool: Split term type into value type and term type 3/8 perf, tool: Add list type for event term parsing
4/8 perf, tool: Add hardcoded name term for pmu events
5/8 perf, tool: Add pmu event parse support for branch_sample_type values 6/8 perf, tool: Move parse event automated tests to separated object 7/8 perf, tool: Add support for displaying event parser debug info 8/8 perf, tool: Use allocated list for each parsed event

thanks,
jirka

---
 tools/perf/Makefile                 |   13 +-
 tools/perf/builtin-record.c         |   31 +--
 tools/perf/builtin-test.c           |  522 +-------------------------------
 tools/perf/util/parse-events-test.c |  588 +++++++++++++++++++++++++++++++++++
 tools/perf/util/parse-events.c      |  240 +++++++++++++--
 tools/perf/util/parse-events.h      |   59 +++-
 tools/perf/util/parse-events.l      |    2 +
 tools/perf/util/parse-events.y      |  147 ++++++++--
 tools/perf/util/pmu.c               |   85 ++++--
 tools/perf/util/pmu.h               |    2 +
 tools/perf/util/ui/browsers/hists.c |    3 +
 11 files changed, 1037 insertions(+), 655 deletions(-)
author
#1 lore raw

PATCH 1/8 perf, tool: Fix NULL deref in hists browsing code

If there's an event with no samples in data file, the perf report command can segfault after entering the event details menu.

Following steps reproduce the issue:

 # ./perf record -e syscalls:sys_enter_kexec_load,syscalls:sys_enter_mmap ls
 # ./perf report
 # enter '0 syscalls:sys_enter_kexec_load' menu
 # pres ENTER twice

Above steps are valid assuming ls wont run kexec.. ;)

The check for sellection to be NULL is missing. The fix makes sure it's being check. Above steps now endup with menu being displayed allowing 'Exit' as the only option.

---
 tools/perf/util/ui/browsers/hists.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
tools/perf/util/ui/browsers/hists.c +3 -0
index d7a1c4a..2f83e5d 100644
@@ -125,6 +125,9 @@ static int callchain__count_rows(struct rb_root *chain)
 
 static bool map_symbol__toggle_fold(struct map_symbol *self)
 {
+	if (!self)
+		return false;
+
 	if (!self->has_children)
 		return false;
 
Signature · 1 line
1.7.7.6
#2 lore raw

[tip:perf/core] perf hists browser: Fix NULL deref in hists browsing code

Commit-ID:  8493fe1daf15324eb13a4cc2f94e258716daa568
Gitweb:     http://git.kernel.org/tip/8493fe1daf15324eb13a4cc2f94e258716daa568
Author:     Jiri Olsa <[email protected]>
AuthorDate: Wed, 4 Apr 2012 22:21:31 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 5 Apr 2012 18:58:45 -0300

perf hists browser: Fix NULL deref in hists browsing code

If there's an event with no samples in data file, the perf report command can segfault after entering the event details menu.

Following steps reproduce the issue:

 # ./perf record -e syscalls:sys_enter_kexec_load,syscalls:sys_enter_mmap ls
 # ./perf report
 # enter '0 syscalls:sys_enter_kexec_load' menu
 # pres ENTER twice

Above steps are valid assuming ls wont run kexec.. ;)

The check for sellection to be NULL is missing. The fix makes sure it's being check. Above steps now endup with menu being displayed allowing 'Exit' as the only option.

---
 tools/perf/util/ui/browsers/hists.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
tools/perf/util/ui/browsers/hists.c +3 -0
index d7a1c4a..2f83e5d 100644
@@ -125,6 +125,9 @@ static int callchain__count_rows(struct rb_root *chain)
 
 static bool map_symbol__toggle_fold(struct map_symbol *self)
 {
+	if (!self)
+		return false;
+
 	if (!self->has_children)
 		return false;
 
author
#3 lore raw

PATCH 2/8 perf, tool: Split term type into value type and term type

In reply to Jiri Olsa

Introducing type_val and type_term for term instead of a single type value. Currently the term type marked out the value type as well.

With this change we can have future string term values being specified by user and translated into proper number along the processing. This is going to be the case for the branch_sample_type comming in shortly.

---
 tools/perf/util/parse-events.c |   31 +++++++++++------
 tools/perf/util/parse-events.h |   19 ++++++-----
 tools/perf/util/parse-events.y |   26 ++++++++++----
 tools/perf/util/pmu.c          |   70 ++++++++++++++++++++++++----------------
 4 files changed, 90 insertions(+), 56 deletions(-)
tools/perf/util/parse-events.c +20 -11
index 5b3a0ef..37b8fc9 100644
@@ -593,17 +593,27 @@ int parse_events_add_breakpoint(struct list_head *list, int *idx,
 static int config_term(struct perf_event_attr *attr,
 		       struct parse_events__term *term)
 {
-	switch (term->type) {
+#define CHECK_TYPE_VAL(type)					\
+do {								\
+	if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val)	\
+		return -EINVAL;					\
+} while (0)
+
+	switch (term->type_term) {
 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
+		CHECK_TYPE_VAL(NUM);
 		attr->config = term->val.num;
 		break;
 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
+		CHECK_TYPE_VAL(NUM);
 		attr->config1 = term->val.num;
 		break;
 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
+		CHECK_TYPE_VAL(NUM);
 		attr->config2 = term->val.num;
 		break;
 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
+		CHECK_TYPE_VAL(NUM);
 		attr->sample_period = term->val.num;
 		break;
 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
@@ -615,7 +625,10 @@ static int config_term(struct perf_event_attr *attr,
 	default:
 		return -EINVAL;
 	}
+
 	return 0;
+
+#undef CHECK_TYPE_VAL
 }
 
 static int config_attr(struct perf_event_attr *attr,
@@ -1015,11 +1028,11 @@ void print_events(const char *event_glob)
 
 int parse_events__is_hardcoded_term(struct parse_events__term *term)
 {
-	return term->type <= PARSE_EVENTS__TERM_TYPE_HARDCODED_MAX;
+	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
 }
 
-int parse_events__new_term(struct parse_events__term **_term, int type,
-			   char *config, char *str, long num)
+int parse_events__new_term(struct parse_events__term **_term, int type_val,
+			   int type_term, char *config, char *str, long num)
 {
 	struct parse_events__term *term;
 
@@ -1028,15 +1041,11 @@ int parse_events__new_term(struct parse_events__term **_term, int type,
 		return -ENOMEM;
 
 	INIT_LIST_HEAD(&term->list);
-	term->type = type;
+	term->type_val  = type_val;
+	term->type_term = type_term;
 	term->config = config;
 
-	switch (type) {
-	case PARSE_EVENTS__TERM_TYPE_CONFIG:
-	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
-	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
-	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
-	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
+	switch (type_val) {
 	case PARSE_EVENTS__TERM_TYPE_NUM:
 		term->val.num = num;
 		break;
tools/perf/util/parse-events.h +10 -9
index ca069f8..7053aa3 100644
@@ -34,16 +34,17 @@ extern int parse_filter(const struct option *opt, const char *str, int unset);
 #define EVENTS_HELP_MAX (128*1024)
 
 enum {
+	PARSE_EVENTS__TERM_TYPE_NUM,
+	PARSE_EVENTS__TERM_TYPE_STR,
+};
+
+enum {
+	PARSE_EVENTS__TERM_TYPE_USER,
 	PARSE_EVENTS__TERM_TYPE_CONFIG,
 	PARSE_EVENTS__TERM_TYPE_CONFIG1,
 	PARSE_EVENTS__TERM_TYPE_CONFIG2,
 	PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD,
 	PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE,
-	PARSE_EVENTS__TERM_TYPE_NUM,
-	PARSE_EVENTS__TERM_TYPE_STR,
-
-	PARSE_EVENTS__TERM_TYPE_HARDCODED_MAX =
-		PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE,
 };
 
 struct parse_events__term {
@@ -52,14 +53,14 @@ struct parse_events__term {
 		char *str;
 		long  num;
 	} val;
-	int type;
-
+	int type_val;
+	int type_term;
 	struct list_head list;
 };
 
 int parse_events__is_hardcoded_term(struct parse_events__term *term);
-int parse_events__new_term(struct parse_events__term **term, int type,
-			   char *config, char *str, long num);
+int parse_events__new_term(struct parse_events__term **_term, int type_val,
+			   int type_term, char *config, char *str, long num);
 void parse_events__free_terms(struct list_head *terms);
 int parse_events_modifier(struct list_head *list __used, char *str __used);
 int parse_events_add_tracepoint(struct list_head *list, int *idx,
tools/perf/util/parse-events.y +18 -8
index d9637da..b3b45e4 100644
@@ -176,8 +176,10 @@ PE_NAME '=' PE_NAME
 {
 	struct parse_events__term *term;
 
-	ABORT_ON(parse_events__new_term(&term, PARSE_EVENTS__TERM_TYPE_STR,
-		 $1, $3, 0));
+	ABORT_ON(parse_events__new_term(&term,
+					PARSE_EVENTS__TERM_TYPE_STR,
+					PARSE_EVENTS__TERM_TYPE_USER,
+					$1, $3, 0));
 	$$ = term;
 }
 |
@@ -185,8 +187,10 @@ PE_NAME '=' PE_VALUE
 {
 	struct parse_events__term *term;
 
-	ABORT_ON(parse_events__new_term(&term, PARSE_EVENTS__TERM_TYPE_NUM,
-		 $1, NULL, $3));
+	ABORT_ON(parse_events__new_term(&term,
+					PARSE_EVENTS__TERM_TYPE_NUM,
+					PARSE_EVENTS__TERM_TYPE_USER,
+					$1, NULL, $3));
 	$$ = term;
 }
 |
@@ -194,8 +198,10 @@ PE_NAME
 {
 	struct parse_events__term *term;
 
-	ABORT_ON(parse_events__new_term(&term, PARSE_EVENTS__TERM_TYPE_NUM,
-		 $1, NULL, 1));
+	ABORT_ON(parse_events__new_term(&term,
+					PARSE_EVENTS__TERM_TYPE_NUM,
+					PARSE_EVENTS__TERM_TYPE_USER,
+					$1, NULL, 1));
 	$$ = term;
 }
 |
@@ -203,7 +209,9 @@ PE_TERM '=' PE_VALUE
 {
 	struct parse_events__term *term;
 
-	ABORT_ON(parse_events__new_term(&term, $1, NULL, NULL, $3));
+	ABORT_ON(parse_events__new_term(&term,
+					PARSE_EVENTS__TERM_TYPE_NUM,
+					$1, NULL, NULL, $3));
 	$$ = term;
 }
 |
@@ -211,7 +219,9 @@ PE_TERM
 {
 	struct parse_events__term *term;
 
-	ABORT_ON(parse_events__new_term(&term, $1, NULL, NULL, 1));
+	ABORT_ON(parse_events__new_term(&term,
+					PARSE_EVENTS__TERM_TYPE_NUM,
+					$1, NULL, NULL, 1));
 	$$ = term;
 }
 
tools/perf/util/pmu.c +42 -28

104 lines not on this page · raw message

Signature · 1 line
1.7.7.6
author
#4 lore raw

PATCH 3/8 perf, tool: Add list type for event term parsing

In reply to Jiri Olsa

Adding list type for event term to hanle multiple values for single term. Term values can be now cpecified via following grammar:

event_term_list: event_term_list '|' event_term_elem
event_term_list: event_term_elem '|' event_term_elem
event_term_elem: PE_NAME | PE_VALUE

eg.:
...,term=str1|str2|num1|num2,...

This is going to be used for branch_type term in future.

---
 tools/perf/util/parse-events.c |   58 ++++++++++++++++++++++++++++-
 tools/perf/util/parse-events.h |   23 ++++++++++-
 tools/perf/util/parse-events.l |    1 +
 tools/perf/util/parse-events.y |   80 +++++++++++++++++++++++++++++++---------
 4 files changed, 140 insertions(+), 22 deletions(-)
tools/perf/util/parse-events.c +56 -2
index 37b8fc9..e910632 100644
@@ -1031,8 +1031,9 @@ int parse_events__is_hardcoded_term(struct parse_events__term *term)
 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
 }
 
-int parse_events__new_term(struct parse_events__term **_term, int type_val,
-			   int type_term, char *config, char *str, long num)
+static int new_term(struct parse_events__term **_term, int type_val,
+		    int type_term, char *config,
+		    char *str, long num, struct list_head *list)
 {
 	struct parse_events__term *term;
 
@@ -1052,6 +1053,10 @@ int parse_events__new_term(struct parse_events__term **_term, int type_val,
 	case PARSE_EVENTS__TERM_TYPE_STR:
 		term->val.str = str;
 		break;
+	case PARSE_EVENTS__TERM_TYPE_LIST:
+		INIT_LIST_HEAD(&term->val.list);
+		list_splice_tail(list, &term->val.list);
+		break;
 	default:
 		return -EINVAL;
 	}
@@ -1060,6 +1065,55 @@ int parse_events__new_term(struct parse_events__term **_term, int type_val,
 	return 0;
 }
 
+int parse_events__term_num(struct parse_events__term **term,
+			   int type_term, char *config, long num)
+{
+	return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
+			config, NULL, num, NULL);
+}
+
+int parse_events__term_str(struct parse_events__term **term,
+			   int type_term, char *config, char *str)
+{
+	return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
+			config, str, 0, NULL);
+}
+
+int parse_events__term_list(struct parse_events__term **term,
+			    int type_term, char *config,
+			    struct list_head *list)
+{
+	return new_term(term, PARSE_EVENTS__TERM_TYPE_LIST, type_term,
+			config, NULL, 0, list);
+}
+
+int parse_events__term_elem(struct parse_events__term_elem **_elem,
+				     int type_val, char *str, long num)
+{
+	struct parse_events__term_elem *elem;
+
+	elem = zalloc(sizeof(*elem));
+	if (!elem)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&elem->list);
+	elem->type_val = type_val;
+
+	switch (type_val) {
+	case PARSE_EVENTS__TERM_TYPE_NUM:
+		elem->val.num = num;
+		break;
+	case PARSE_EVENTS__TERM_TYPE_STR:
+		elem->val.str = str;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	*_elem = elem;
+	return 0;
+}
+
 void parse_events__free_terms(struct list_head *terms)
 {
 	struct parse_events__term *term, *h;
tools/perf/util/parse-events.h +21 -2
index 7053aa3..363aa22 100644
@@ -36,6 +36,7 @@ extern int parse_filter(const struct option *opt, const char *str, int unset);
 enum {
 	PARSE_EVENTS__TERM_TYPE_NUM,
 	PARSE_EVENTS__TERM_TYPE_STR,
+	PARSE_EVENTS__TERM_TYPE_LIST,
 };
 
 enum {
@@ -47,11 +48,22 @@ enum {
 	PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE,
 };
 
+
+struct parse_events__term_elem {
+	union {
+		char *str;
+		long  num;
+	} val;
+	int type_val;
+	struct list_head list;
+};
+
 struct parse_events__term {
 	char *config;
 	union {
 		char *str;
 		long  num;
+		struct list_head list;
 	} val;
 	int type_val;
 	int type_term;
@@ -59,8 +71,15 @@ struct parse_events__term {
 };
 
 int parse_events__is_hardcoded_term(struct parse_events__term *term);
-int parse_events__new_term(struct parse_events__term **_term, int type_val,
-			   int type_term, char *config, char *str, long num);
+int parse_events__term_num(struct parse_events__term **_term,
+			   int type_term, char *config, long num);
+int parse_events__term_str(struct parse_events__term **_term,
+			   int type_term, char *config, char *str);
+int parse_events__term_list(struct parse_events__term **_term,
+			    int type_term, char *config,
+			    struct list_head *list);
+int parse_events__term_elem(struct parse_events__term_elem **_e,
+			    int type_val, char *str, long num);
 void parse_events__free_terms(struct list_head *terms);
 int parse_events_modifier(struct list_head *list __used, char *str __used);
 int parse_events_add_tracepoint(struct list_head *list, int *idx,
tools/perf/util/parse-events.l +1 -0
index 05d766e..46c5b3a 100644
@@ -118,6 +118,7 @@ r{num_raw_hex}		{ return raw(); }
 ,			{ return ','; }
 :			{ return ':'; }
 =			{ return '='; }
+\|			{ return '|'; }
 
 %%
 
tools/perf/util/parse-events.y +62 -18

125 lines not on this page · raw message

Signature · 1 line
1.7.7.6
author
#5 lore raw

PATCH 4/8 perf, tool: Add hardcoded name term for pmu events

In reply to Jiri Olsa

Adding a new hardcoded term 'name' allowing to specify a name for the pmu event. The term is defined along with standard pmu terms. If no 'name' term is given, the event name become the pmu name itself ('cpu' for the example below).

running:
perf stat -e cpu/config=1,name=krava1/u ls

will produce following output:
...
Performance counter stats for 'ls':

                 0 krava1

       0.009779735 seconds time elapsed
---
 tools/perf/builtin-test.c      |   26 ++++++++++++++++++++++++++
 tools/perf/util/parse-events.c |   10 +++++++++-
 tools/perf/util/parse-events.h |    3 +++
 tools/perf/util/parse-events.l |    1 +
 tools/perf/util/parse-events.y |    8 ++++++++
 tools/perf/util/pmu.c          |   15 +++++++++++++--
 tools/perf/util/pmu.h          |    2 ++
 7 files changed, 62 insertions(+), 3 deletions(-)
tools/perf/builtin-test.c +26 -0
index 1c5b980..d522cf4 100644
@@ -986,6 +986,28 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	return 0;
 }
 
+static int test__checkevent_pmu_name(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	/* cpu/config=1,name=krava1/u */
+	evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
+	TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
+	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong config",  1 == evsel->attr.config);
+	TEST_ASSERT_VAL("wrong name", !strcmp(evsel->name, "krava"));
+
+	/* cpu/config=2/" */
+	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
+	TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
+	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong config",  2 == evsel->attr.config);
+	TEST_ASSERT_VAL("wrong name", !strcmp(evsel->name, "cpu"));
+
+	return 0;
+}
+
 static struct test__event_st {
 	const char *name;
 	__u32 type;
@@ -1091,6 +1113,10 @@ static struct test__event_st {
 		.name  = "r1,syscalls:sys_enter_open:k,1:1:hp",
 		.check = test__checkevent_list,
 	},
+	{
+		.name  = "cpu/config=1,name=krava/u,cpu/config=2/u",
+		.check = test__checkevent_pmu_name,
+	},
 };
 
 #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
tools/perf/util/parse-events.c +9 -1
index e910632..c349cf3 100644
@@ -682,7 +682,9 @@ int parse_events_add_pmu(struct list_head *list, int *idx,
 	if (perf_pmu__config(pmu, &attr, head_config))
 		return -EINVAL;
 
-	return add_event(list, idx, &attr, (char *) "pmu");
+	name = perf_pmu__event_name(name, head_config);
+
+	return add_event(list, idx, &attr, name);
 }
 
 void parse_events_update_lists(struct list_head *list_event,
@@ -1031,6 +1033,11 @@ int parse_events__is_hardcoded_term(struct parse_events__term *term)
 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
 }
 
+int parse_events__is_name_term(struct parse_events__term *term)
+{
+	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
+}
+
 static int new_term(struct parse_events__term **_term, int type_val,
 		    int type_term, char *config,
 		    char *str, long num, struct list_head *list)
@@ -1050,6 +1057,7 @@ static int new_term(struct parse_events__term **_term, int type_val,
 	case PARSE_EVENTS__TERM_TYPE_NUM:
 		term->val.num = num;
 		break;
+	case PARSE_EVENTS__TERM_TYPE_NAME:
 	case PARSE_EVENTS__TERM_TYPE_STR:
 		term->val.str = str;
 		break;
tools/perf/util/parse-events.h +3 -0
index 363aa22..cabcbc1 100644
@@ -4,6 +4,7 @@
  * Parse symbolic events/counts passed in as options:
  */
 
+#include <linux/list.h>
 #include "../../../include/linux/perf_event.h"
 
 struct list_head;
@@ -44,6 +45,7 @@ enum {
 	PARSE_EVENTS__TERM_TYPE_CONFIG,
 	PARSE_EVENTS__TERM_TYPE_CONFIG1,
 	PARSE_EVENTS__TERM_TYPE_CONFIG2,
+	PARSE_EVENTS__TERM_TYPE_NAME,
 	PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD,
 	PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE,
 };
@@ -71,6 +73,7 @@ struct parse_events__term {
 };
 
 int parse_events__is_hardcoded_term(struct parse_events__term *term);
+int parse_events__is_name_term(struct parse_events__term *term);
 int parse_events__term_num(struct parse_events__term **_term,
 			   int type_term, char *config, long num);
 int parse_events__term_str(struct parse_events__term **_term,
tools/perf/util/parse-events.l +1 -0
index 46c5b3a..cd6614d 100644
@@ -102,6 +102,7 @@ misses|miss				{ return str(PE_NAME_CACHE_OP_RESULT); }
 config			{ return term(PARSE_EVENTS__TERM_TYPE_CONFIG); }
 config1			{ return term(PARSE_EVENTS__TERM_TYPE_CONFIG1); }
 config2			{ return term(PARSE_EVENTS__TERM_TYPE_CONFIG2); }
+name			{ return term(PARSE_EVENTS__TERM_TYPE_NAME); }
 period			{ return term(PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
 branch_type		{ return term(PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
 
tools/perf/util/parse-events.y +8 -0
index 6d447b7..ef3ee2d 100644
@@ -202,6 +202,14 @@ PE_NAME
 	$$ = term;
 }
 |
+PE_TERM '=' PE_NAME
+{
+	struct parse_events__term *term;
+
+	ABORT_ON(parse_events__term_str(&term, $1, NULL, $3));
+	$$ = term;
+}
+|
 PE_TERM '=' PE_VALUE
 {
 	struct parse_events__term *term;
tools/perf/util/pmu.c +13 -2
index 8ee219b..5526446 100644
@@ -258,9 +258,9 @@ static int pmu_config_term(struct list_head *formats,
 static int pmu_config(struct list_head *formats, struct perf_event_attr *attr,
 		      struct list_head *head_terms)
 {
-	struct parse_events__term *term, *h;
+	struct parse_events__term *term;
 
-	list_for_each_entry_safe(term, h, head_terms, list)
+	list_for_each_entry(term, head_terms, list)
 		if (pmu_config_term(formats, attr, term))
 			return -EINVAL;
 
@@ -308,6 +308,17 @@ void perf_pmu__set_format(unsigned long *bits, long from, long to)
 		set_bit(b, bits);
 }
 
+char *perf_pmu__event_name(char *name, struct list_head *head_terms)
+{
+	struct parse_events__term *term;
+
+	list_for_each_entry(term, head_terms, list)
+		if (parse_events__is_name_term(term))
+			return term->val.str;
+
+	return name;
+}
+
 /* Simulated format definitions. */
 static struct test_format {
 	const char *name;
tools/perf/util/pmu.h +2 -0
index 68c0db9..f602745 100644
@@ -37,5 +37,7 @@ int perf_pmu__new_format(struct list_head *list, char *name,
 			 int config, unsigned long *bits);
 void perf_pmu__set_format(unsigned long *bits, long from, long to);
 
+char *perf_pmu__event_name(char *name, struct list_head *head_terms);
+
 int perf_pmu__test(void);
 #endif /* __PMU_H */
Signature · 1 line
1.7.7.6
author
#6 lore raw

PATCH 5/8 perf, tool: Add pmu event parse support for branch_sample_type values

In reply to Jiri Olsa

Adding a support to specify branch_type as a hardcoded term inside the pmu event definition.

It is possible to specify pmu event like:
"cpu/config=1,branch_type=hv|any_ret|1,config2=2/u"

Following string values could be used as value for branch_type:
u (PERF_SAMPLE_BRANCH_USER)
k (PERF_SAMPLE_BRANCH_KERNEL)
hv (PERF_SAMPLE_BRANCH_HV)
any (PERF_SAMPLE_BRANCH_ANY)
any_call (PERF_SAMPLE_BRANCH_ANY_CALL)
any_ret (PERF_SAMPLE_BRANCH_ANY_RETURN)
ind_call (PERF_SAMPLE_BRANCH_IND_CALL)

Also a number could be specified as value.

CC Stephane Eranian <[email protected]>
---
 tools/perf/builtin-record.c    |   31 +-----------
 tools/perf/builtin-test.c      |   27 +++++++++++
 tools/perf/util/parse-events.c |  100 ++++++++++++++++++++++++++++++++++++++--
 tools/perf/util/parse-events.h |    1 +
 4 files changed, 127 insertions(+), 32 deletions(-)
tools/perf/builtin-record.c +3 -28
index be4e1ee..87fc68d 100644
@@ -641,27 +641,6 @@ out_delete_session:
 	return err;
 }
 
-#define BRANCH_OPT(n, m) \
-	{ .name = n, .mode = (m) }
-
-#define BRANCH_END { .name = NULL }
-
-struct branch_mode {
-	const char *name;
-	int mode;
-};
-
-static const struct branch_mode branch_modes[] = {
-	BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER),
-	BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL),
-	BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV),
-	BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY),
-	BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
-	BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
-	BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
-	BRANCH_END
-};
-
 static int
 parse_branch_stack(const struct option *opt, const char *str, int unset)
 {
@@ -671,7 +650,6 @@ parse_branch_stack(const struct option *opt, const char *str, int unset)
 	 PERF_SAMPLE_BRANCH_HV)
 
 	uint64_t *mode = (uint64_t *)opt->value;
-	const struct branch_mode *br;
 	char *s, *os = NULL, *p;
 	int ret = -1;
 
@@ -692,21 +670,18 @@ parse_branch_stack(const struct option *opt, const char *str, int unset)
 			return -1;
 
 		for (;;) {
+			u64 bmode;
 			p = strchr(s, ',');
 			if (p)
 				*p = '\0';
 
-			for (br = branch_modes; br->name; br++) {
-				if (!strcasecmp(s, br->name))
-					break;
-			}
-			if (!br->name) {
+			if (parse_events__branch_type(&bmode, s)) {
 				ui__warning("unknown branch filter %s,"
 					    " check man page\n", s);
 				goto error;
 			}
 
-			*mode |= br->mode;
+			*mode |= bmode;
 
 			if (!p)
 				break;
tools/perf/builtin-test.c +27 -0
index d522cf4..2e4dc0a 100644
@@ -1008,6 +1008,29 @@ static int test__checkevent_pmu_name(struct perf_evlist *evlist)
 	return 0;
 }
 
+static int test__checkevent_pmu_branch_type(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
+	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong config",  1 == evsel->attr.config);
+	TEST_ASSERT_VAL("wrong config2", 2 == evsel->attr.config2);
+
+#define BT (PERF_SAMPLE_BRANCH_USER | \
+	    PERF_SAMPLE_BRANCH_HV | \
+	    PERF_SAMPLE_BRANCH_ANY_RETURN)
+
+	TEST_ASSERT_VAL("wrong sample_type",
+		PERF_SAMPLE_BRANCH_STACK && evsel->attr.sample_type);
+	TEST_ASSERT_VAL("wrong branch_type",
+		BT == evsel->attr.branch_sample_type);
+
+#undef BT
+	return 0;
+}
+
 static struct test__event_st {
 	const char *name;
 	__u32 type;
@@ -1117,6 +1140,10 @@ static struct test__event_st {
 		.name  = "cpu/config=1,name=krava/u,cpu/config=2/u",
 		.check = test__checkevent_pmu_name,
 	},
+	{
+		.name  = "cpu/config=1,branch_type=hv|any_ret|1,config2=2/u",
+		.check = test__checkevent_pmu_branch_type,
+	},
 };
 
 #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
tools/perf/util/parse-events.c +96 -4

112 lines not on this page · raw message

tools/perf/util/parse-events.h +1 -0
index cabcbc1..8bb673d 100644
@@ -83,6 +83,7 @@ int parse_events__term_list(struct parse_events__term **_term,
 			    struct list_head *list);
 int parse_events__term_elem(struct parse_events__term_elem **_e,
 			    int type_val, char *str, long num);
+int parse_events__branch_type(u64 *_type, char *str);
 void parse_events__free_terms(struct list_head *terms);
 int parse_events_modifier(struct list_head *list __used, char *str __used);
 int parse_events_add_tracepoint(struct list_head *list, int *idx,
Signature · 1 line
1.7.7.6
#7 lore raw

Re: [PATCH 5/8] perf, tool: Add pmu event parse support for branch_sample_type values

Quoted 15 lines · Jiri Olsa

Adding a support to specify branch_type as a hardcoded term inside the pmu event definition.

It is possible to specify pmu event like:
 "cpu/config=1,branch_type=hv|any_ret|1,config2=2/u"

Following string values could be used as value for branch_type:
 u (PERF_SAMPLE_BRANCH_USER)
 k (PERF_SAMPLE_BRANCH_KERNEL)
 hv (PERF_SAMPLE_BRANCH_HV)
 any (PERF_SAMPLE_BRANCH_ANY)
 any_call (PERF_SAMPLE_BRANCH_ANY_CALL)
 any_ret (PERF_SAMPLE_BRANCH_ANY_RETURN)
 ind_call (PERF_SAMPLE_BRANCH_IND_CALL)

Quoted 2 lines

Also a number could be specified as value.

Although it would be nice to have, the current kernel sampling buffer layout + perf would not be able to parse the RECORD_SAMPLE if you were to sample different things for different events:

perf record -e
cpu/event=0xc0,umask=1:branch_type=any/,cpu/event=0x3c,umask=0x1/

Perf report/annotate would not be able to distinguish a RECORD_SAMPLE generated by the first or the second event. That's because the RECORD_SAMPLE fixed header does not contain enough info to determine which event caused the record to be generated. You need to event ID to decode the sample. The event ID gives you the attr struct which gives you the attr->sample_type which drives the layout of the RECORD_SAMPLE variable size body. The event ID is currently saved "somewhere" in the variable-size body of the sample. You have a chicken and egg problem here.

Next week, I will post a patch to extend the existing format to support sampling on multiple events with different sample_type bitmasks.

Quoted 253 lines
CC Stephane Eranian <[email protected]>

---
 tools/perf/builtin-record.c    |   31 +----------- tools/perf/builtin-test.c      |   27 +++++++++++ tools/perf/util/parse-events.c |  100 ++++++++++++++++++++++++++++++++++++++-- tools/perf/util/parse-events.h |    1 +

 4 files changed, 127 insertions(+), 32 deletions(-)
tools/perf/builtin-record.c +0 -21
index be4e1ee..87fc68d 100644
@@ -641,27 +641,6 @@ out_delete_session:
        return err;
  }
 
-#define BRANCH_OPT(n, m) \
-       { .name = n, .mode = (m) }
-
-#define BRANCH_END { .name = NULL }
-
-struct branch_mode {
-       const char *name;
-       int mode;
-};
-
-static const struct branch_mode branch_modes[] = {
-       BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER),
-       BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL),
-       BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV),
-       BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY),
-       BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
-       BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
-       BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
-       BRANCH_END
-};
-

 static int
 parse_branch_stack(const struct option *opt, const char *str, int unset) {

tools/perf/builtin-record.c +2 -6
@@ -671,7 +650,6 @@ parse_branch_stack(const struct option *opt, const char *str, int unset)
         PERF_SAMPLE_BRANCH_HV)
 
        uint64_t *mode = (uint64_t *)opt->value;
-       const struct branch_mode *br;
        char *s, *os = NULL, *p;
        int ret = -1;
 
@@ -692,21 +670,18 @@ parse_branch_stack(const struct option *opt, const char *str, int unset)
                        return -1;
 
                for (;;) {
+                       u64 bmode;
                        p = strchr(s, ',');
                        if (p)
                                *p = '\0';
 
-                       for (br = branch_modes; br->name; br++) {
-                               if (!strcasecmp(s, br->name))
-                                       break;
-                       }
-                       if (!br->name) {
+                       if (parse_events__branch_type(&bmode, s)) {

                               ui__warning("unknown branch filter %s," " check man page\n", s); goto error;
                       }

-                       *mode |= br->mode;
+                       *mode |= bmode;

                       if (!p)
                               break;

tools/perf/builtin-test.c +27 -0
index d522cf4..2e4dc0a 100644
@@ -1008,6 +1008,29 @@ static int test__checkevent_pmu_name(struct perf_evlist *evlist)
        return 0;
  }
 
+static int test__checkevent_pmu_branch_type(struct perf_evlist *evlist)
+{
+       struct perf_evsel *evsel = list_entry(evlist->entries.next,
+                                             struct perf_evsel, node);
+
+       TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
+       TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
+       TEST_ASSERT_VAL("wrong config",  1 == evsel->attr.config);
+       TEST_ASSERT_VAL("wrong config2", 2 == evsel->attr.config2);
+
+#define BT (PERF_SAMPLE_BRANCH_USER | \
+           PERF_SAMPLE_BRANCH_HV | \
+           PERF_SAMPLE_BRANCH_ANY_RETURN)
+
+       TEST_ASSERT_VAL("wrong sample_type",
+               PERF_SAMPLE_BRANCH_STACK && evsel->attr.sample_type);
+       TEST_ASSERT_VAL("wrong branch_type",
+               BT == evsel->attr.branch_sample_type);
+
+#undef BT
+       return 0;
+}
+
  static struct test__event_st {
        const char *name;
        __u32 type;
@@ -1117,6 +1140,10 @@ static struct test__event_st {
                .name  = "cpu/config=1,name=krava/u,cpu/config=2/u",
                .check = test__checkevent_pmu_name,
        },
+       {
+               .name  = "cpu/config=1,branch_type=hv|any_ret|1,config2=2/u",
+               .check = test__checkevent_pmu_branch_type,
+       },
  };
 
  #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
tools/perf/util/parse-events.c +93 -0
index c349cf3..076b1f8 100644
@@ -590,6 +590,99 @@ int parse_events_add_breakpoint(struct list_head *list, int *idx,
        return add_event(list, idx, &attr, name);
  }
 
+#define BRANCH_OPT(n, m) { .name = n, .mode = (m) }
+#define BRANCH_END { .name = NULL }
+
+struct branch_mode {
+       const char *name;
+       int mode;
+};
+
+static const struct branch_mode branch_modes[] = {
+       BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER),
+       BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL),
+       BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV),
+       BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY),
+       BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
+       BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
+       BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
+       BRANCH_END
+};
+
+int parse_events__branch_type(u64 *_type, char *str)
+{
+       const struct branch_mode *br;
+
+       for (br = branch_modes; br->name; br++) {
+               if (!strcasecmp(str, br->name)) {
+                       *_type = br->mode;
+                       return 0;
+               }
+       }
+
+       return -EINVAL;
+}
+
+typedef int (process_term_list_cb)(struct parse_events__term_elem *elem,
+                                  void *arg);
+
+static int branch_type_cb(struct parse_events__term_elem *elem, void *arg)
+{
+       u64 *_type = arg;
+       u64 type;
+
+       switch (elem->type_val) {
+       case PARSE_EVENTS__TERM_TYPE_NUM:
+               type = elem->val.num;
+               break;
+       case PARSE_EVENTS__TERM_TYPE_STR:
+               if (parse_events__branch_type(&type, elem->val.str))
+                       return -EINVAL;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       *_type |= type;
+       return 0;
+}
+
+static int process_term_list(struct parse_events__term *term,
+                            process_term_list_cb cb, void *arg)
+{
+       struct parse_events__term_elem *elem;
+
+       list_for_each_entry(elem, &term->val.list, list)
+               if (cb(elem, arg))
+                       return -EINVAL;
+
+       return 0;
+}
+
+static int branch_type(struct parse_events__term *term, u64 *_type)
+{
+       u64 type = 0;
+
+       switch (term->type_val) {
+       case PARSE_EVENTS__TERM_TYPE_NUM:
+               type = term->val.num;
+               break;
+       case PARSE_EVENTS__TERM_TYPE_STR:
+               if (parse_events__branch_type(&type, term->val.str))
+                       return -EINVAL;
+               break;
+       case PARSE_EVENTS__TERM_TYPE_LIST:
+               if (process_term_list(term, branch_type_cb, &type))
+                       return -EINVAL;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       *_type = type;
+       return 0;
+}
+
  static int config_term(struct perf_event_attr *attr,
                       struct parse_events__term *term)
  {
@@ -617,10 +710,9 @@ do {                                                               \
                attr->sample_period = term->val.num;
                break;

       case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
-               /*
-                * TODO uncomment when the field is available
-                * attr->branch_sample_type = term->val.num;
-                */
+               attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
+               if (branch_type(term, (u64 *)&attr->branch_sample_type))
+                       return -EINVAL;
               break;
       default:
               return -EINVAL;

tools/perf/util/parse-events.h +1 -1
index cabcbc1..8bb673d 100644
@@ -83,6 +83,7 @@ int parse_events__term_list(struct parse_events__term **_term,
                            struct list_head *list);
  int parse_events__term_elem(struct parse_events__term_elem **_e,
                            int type_val, char *str, long num);
+int parse_events__branch_type(u64 *_type, char *str);
  void parse_events__free_terms(struct list_head *terms);
  int parse_events_modifier(struct list_head *list __used, char *str __used);
  int parse_events_add_tracepoint(struct list_head *list, int *idx,
--

1.7.7.6

author
#8 lore raw

Re: [PATCH 5/8] perf, tool: Add pmu event parse support for branch_sample_type values

Quoted 16 lines · Stephane Eranian
Quoted 15 lines · Jiri Olsa

Adding a support to specify branch_type as a hardcoded term inside the pmu event definition.

It is possible to specify pmu event like:
 "cpu/config=1,branch_type=hv|any_ret|1,config2=2/u"

Following string values could be used as value for branch_type:
 u (PERF_SAMPLE_BRANCH_USER)
 k (PERF_SAMPLE_BRANCH_KERNEL)
 hv (PERF_SAMPLE_BRANCH_HV)
 any (PERF_SAMPLE_BRANCH_ANY)
 any_call (PERF_SAMPLE_BRANCH_ANY_CALL)
 any_ret (PERF_SAMPLE_BRANCH_ANY_RETURN)
 ind_call (PERF_SAMPLE_BRANCH_IND_CALL)

Quoted 8 lines
Quoted 2 lines

Also a number could be specified as value.

Although it would be nice to have, the current kernel sampling buffer layout + perf would not be able to parse the RECORD_SAMPLE if you were to sample different things for different events:

perf record -e
cpu/event=0xc0,umask=1:branch_type=any/,cpu/event=0x3c,umask=0x1/

hi,
for some reason I thought there might be sense to have multiple branch types enabled in a single event.. I misread the code probably

Quoted 9 lines

Perf report/annotate would not be able to distinguish a RECORD_SAMPLE generated by the first or the second event. That's because the RECORD_SAMPLE fixed header does not contain enough info to determine which event caused the record to be generated. You need to event ID to decode the sample. The event ID gives you the attr struct which gives you the attr->sample_type which drives the layout of the RECORD_SAMPLE variable size body. The event ID is currently saved "somewhere" in the variable-size body of the sample. You have a chicken and egg problem here.

yep, I'm aware of this.. cool you're preparing fix

thanks,
jirka

#9 lore raw

Re: [PATCH 5/8] perf, tool: Add pmu event parse support for branch_sample_type values

Quoted 23 lines · Jiri Olsa
Quoted 22 lines · Stephane Eranian
Quoted 15 lines · Jiri Olsa

Adding a support to specify branch_type as a hardcoded term inside the pmu event definition.

It is possible to specify pmu event like:
 "cpu/config=1,branch_type=hv|any_ret|1,config2=2/u"

Following string values could be used as value for branch_type:
 u (PERF_SAMPLE_BRANCH_USER)
 k (PERF_SAMPLE_BRANCH_KERNEL)
 hv (PERF_SAMPLE_BRANCH_HV)
 any (PERF_SAMPLE_BRANCH_ANY)
 any_call (PERF_SAMPLE_BRANCH_ANY_CALL)
 any_ret (PERF_SAMPLE_BRANCH_ANY_RETURN)
 ind_call (PERF_SAMPLE_BRANCH_IND_CALL)

Quoted 2 lines

Also a number could be specified as value.

Although it would be nice to have, the current kernel sampling buffer layout + perf would not be able to parse the RECORD_SAMPLE if you were to sample different things for different events:

Quoted 6 lines
Quoted 2 lines

 perf record -e cpu/event=0xc0,umask=1:branch_type=any/,cpu/event=0x3c,umask=0x1/

hi,
for some reason I thought there might be sense to have multiple branch types enabled in a single event.. I misread the code probably

You can combine the branch types but I don't see how this could be really useful. As for priv levels, you can of course combine them. If they are not specificied, then the levels of the associated event are used.

Quoted 14 lines
Quoted 9 lines

Perf report/annotate would not be able to distinguish a RECORD_SAMPLE generated by the first or the second event. That's because the RECORD_SAMPLE fixed header does not contain enough info to determine which event caused the record to be generated. You need to event ID to decode the sample. The event ID gives you the attr struct which gives you the attr->sample_type which drives the layout of the RECORD_SAMPLE variable size body. The event ID is currently saved "somewhere" in the variable-size body of the sample. You have  a chicken and egg problem here.

yep, I'm aware of this.. cool you're preparing fix

thanks,
jirka

author
#10 lore raw

PATCH 6/8 perf, tool: Move parse event automated tests to separated object

In reply to Jiri Olsa

Moving event parsing specific tests into separated file:
util/parse-events-test.c

Also changing the code a bit to ease running separate tests.

---
 tools/perf/Makefile                 |    1 +
 tools/perf/builtin-test.c           |  575 +----------------------------------
 tools/perf/util/parse-events-test.c |  588 +++++++++++++++++++++++++++++++++++
 tools/perf/util/parse-events.h      |    3 +
 4 files changed, 593 insertions(+), 574 deletions(-)
 create mode 100644 tools/perf/util/parse-events-test.c
tools/perf/Makefile +1 -0
index 820371f..7546dd5 100644
@@ -353,6 +353,7 @@ LIB_OBJS += $(OUTPUT)util/help.o
 LIB_OBJS += $(OUTPUT)util/levenshtein.o
 LIB_OBJS += $(OUTPUT)util/parse-options.o
 LIB_OBJS += $(OUTPUT)util/parse-events.o
+LIB_OBJS += $(OUTPUT)util/parse-events-test.o
 LIB_OBJS += $(OUTPUT)util/path.o
 LIB_OBJS += $(OUTPUT)util/rbtree.o
 LIB_OBJS += $(OUTPUT)util/bitmap.o
tools/perf/builtin-test.c +1 -574

587 lines not on this page · raw message

tools/perf/util/parse-events-test.c new file +588 -0

588 lines not on this page · raw message

tools/perf/util/parse-events.h +3 -0
index 8bb673d..66e08f4 100644
@@ -5,6 +5,8 @@
  */
 
 #include <linux/list.h>
+#include <stdbool.h>
+#include "types.h"
 #include "../../../include/linux/perf_event.h"
 
 struct list_head;
@@ -105,6 +107,7 @@ void parse_events_update_lists(struct list_head *list_event,
 void parse_events_error(struct list_head *list_all,
 			struct list_head *list_event,
 			int *idx, char const *msg);
+int parse_events__test(void);
 
 void print_events(const char *event_glob);
 void print_events_type(u8 type);
Signature · 1 line
1.7.7.6
author
#11 lore raw

PATCH 7/8 perf, tool: Add support for displaying event parser debug info

In reply to Jiri Olsa

Adding PARSER_DEBUG Makefile variable to enable building event scanner/parser with debug enabled. This results in verbose output right out of the scanner/parser.

It's usefull for debuging the event parser. Keeping this only for event parser so far.

---
 tools/perf/Makefile            |   12 +++++++++---
 tools/perf/util/parse-events.c |    6 ++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
tools/perf/Makefile +9 -3
index 7546dd5..33bec3a 100644
@@ -116,7 +116,13 @@ ifndef PERF_DEBUG
   CFLAGS_OPTIMIZE = -O6
 endif
 
-CFLAGS = -fno-omit-frame-pointer -ggdb3 -Wall -Wextra -std=gnu99 $(CFLAGS_WERROR) $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS)
+ifdef PARSER_DEBUG
+	PARSER_DEBUG_BISON  := -t
+	PARSER_DEBUG_FLEX   := -d
+	PARSER_DEBUG_CFLAGS := -DPARSER_DEBUG
+endif
+
+CFLAGS = -fno-omit-frame-pointer -ggdb3 -Wall -Wextra -std=gnu99 $(CFLAGS_WERROR) $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) $(PARSER_DEBUG_CFLAGS)
 EXTLIBS = -lpthread -lrt -lelf -lm
 ALL_CFLAGS = $(CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
 ALL_LDFLAGS = $(LDFLAGS)
@@ -238,8 +244,8 @@ FLEX = $(CROSS_COMPILE)flex
 BISON= $(CROSS_COMPILE)bison
 
 event-parser:
-	$(QUIET_BISON)$(BISON) -v util/parse-events.y -d -o $(OUTPUT)util/parse-events-bison.c
-	$(QUIET_FLEX)$(FLEX) --header-file=$(OUTPUT)util/parse-events-flex.h -t util/parse-events.l > $(OUTPUT)util/parse-events-flex.c
+	$(QUIET_BISON)$(BISON) -v util/parse-events.y -d $(PARSER_DEBUG_BISON) -o $(OUTPUT)util/parse-events-bison.c
+	$(QUIET_FLEX)$(FLEX) --header-file=$(OUTPUT)util/parse-events-flex.h $(PARSER_DEBUG_FLEX) -t util/parse-events.l > $(OUTPUT)util/parse-events-flex.c
 
 $(OUTPUT)util/parse-events-flex.c: event-parser
 $(OUTPUT)util/parse-events-bison.c: event-parser
tools/perf/util/parse-events.c +6 -0
index 076b1f8..b6136fd 100644
@@ -23,6 +23,9 @@ struct event_symbol {
 	const char	*alias;
 };
 
+#ifdef PARSER_DEBUG
+extern int parse_events_debug;
+#endif
 int parse_events_parse(struct list_head *list, struct list_head *list_tmp,
 		       int *idx);
 
@@ -863,6 +866,9 @@ int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 
 	buffer = parse_events__scan_string(str);
 
+#ifdef PARSER_DEBUG
+	parse_events_debug = 1;
+#endif
 	ret = parse_events_parse(&list, &list_tmp, &idx);
 
 	parse_events__flush_buffer(buffer);
Signature · 1 line
1.7.7.6
author
#12 lore raw

PATCH 8/8 perf, tool: Use allocated list for each parsed event

In reply to Jiri Olsa

Switch from using static temporary event list into dynamically allocated one. This way we dont need to pass temp list to the parse_events_parse which makes the interface more clear.

---
 tools/perf/util/parse-events.c |   39 ++++++++++++++---------
 tools/perf/util/parse-events.h |   16 +++------
 tools/perf/util/parse-events.y |   69 ++++++++++++++++++++++++++++++---------
 3 files changed, 83 insertions(+), 41 deletions(-)
tools/perf/util/parse-events.c +24 -15
index b6136fd..d4abd00 100644
@@ -26,8 +26,7 @@ struct event_symbol {
 #ifdef PARSER_DEBUG
 extern int parse_events_debug;
 #endif
-int parse_events_parse(struct list_head *list, struct list_head *list_tmp,
-		       int *idx);
+int parse_events_parse(struct list_head *list, int *idx);
 
 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
@@ -358,20 +357,30 @@ const char *__event_name(int type, u64 config)
 	return "unknown";
 }
 
-static int add_event(struct list_head *list, int *idx,
+static int add_event(struct list_head **_list, int *idx,
 		     struct perf_event_attr *attr, char *name)
 {
 	struct perf_evsel *evsel;
+	struct list_head *list = *_list;
+
+	if (!list) {
+		list = malloc(sizeof(*list));
+		if (!list)
+			return -ENOMEM;
+		INIT_LIST_HEAD(list);
+	}
 
 	event_attr_init(attr);
 
 	evsel = perf_evsel__new(attr, (*idx)++);
-	if (!evsel)
+	if (!evsel) {
+		free(list);
 		return -ENOMEM;
-
-	list_add_tail(&evsel->node, list);
+	}
 
 	evsel->name = strdup(name);
+	list_add_tail(&evsel->node, list);
+	*_list = list;
 	return 0;
 }
 
@@ -393,7 +402,7 @@ static int parse_aliases(char *str, const char *names[][MAX_ALIASES], int size)
 	return -1;
 }
 
-int parse_events_add_cache(struct list_head *list, int *idx,
+int parse_events_add_cache(struct list_head **list, int *idx,
 			   char *type, char *op_result1, char *op_result2)
 {
 	struct perf_event_attr attr;
@@ -454,7 +463,7 @@ int parse_events_add_cache(struct list_head *list, int *idx,
 	return add_event(list, idx, &attr, name);
 }
 
-static int add_tracepoint(struct list_head *list, int *idx,
+static int add_tracepoint(struct list_head **list, int *idx,
 			  char *sys_name, char *evt_name)
 {
 	struct perf_event_attr attr;
@@ -491,7 +500,7 @@ static int add_tracepoint(struct list_head *list, int *idx,
 	return add_event(list, idx, &attr, name);
 }
 
-static int add_tracepoint_multi(struct list_head *list, int *idx,
+static int add_tracepoint_multi(struct list_head **list, int *idx,
 				char *sys_name, char *evt_name)
 {
 	char evt_path[MAXPATHLEN];
@@ -522,7 +531,7 @@ static int add_tracepoint_multi(struct list_head *list, int *idx,
 	return ret;
 }
 
-int parse_events_add_tracepoint(struct list_head *list, int *idx,
+int parse_events_add_tracepoint(struct list_head **list, int *idx,
 				char *sys, char *event)
 {
 	int ret;
@@ -566,7 +575,7 @@ parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
 	return 0;
 }
 
-int parse_events_add_breakpoint(struct list_head *list, int *idx,
+int parse_events_add_breakpoint(struct list_head **list, int *idx,
 				void *ptr, char *type)
 {
 	struct perf_event_attr attr;
@@ -738,7 +747,7 @@ static int config_attr(struct perf_event_attr *attr,
 	return 0;
 }
 
-int parse_events_add_numeric(struct list_head *list, int *idx,
+int parse_events_add_numeric(struct list_head **list, int *idx,
 			     unsigned long type, unsigned long config,
 			     struct list_head *head_config)
 {
@@ -756,7 +765,7 @@ int parse_events_add_numeric(struct list_head *list, int *idx,
 			 (char *) __event_name(type, config));
 }
 
-int parse_events_add_pmu(struct list_head *list, int *idx,
+int parse_events_add_pmu(struct list_head **list, int *idx,
 			 char *name, struct list_head *head_config)
 {
 	struct perf_event_attr attr;
@@ -791,7 +800,7 @@ void parse_events_update_lists(struct list_head *list_event,
 	 * list, for next event definition.
 	 */
 	list_splice_tail(list_event, list_all);
-	INIT_LIST_HEAD(list_event);
+	free(list_event);
 }
 
 int parse_events_modifier(struct list_head *list, char *str)
@@ -869,7 +878,7 @@ int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 #ifdef PARSER_DEBUG
 	parse_events_debug = 1;
 #endif
-	ret = parse_events_parse(&list, &list_tmp, &idx);
+	ret = parse_events_parse(&list, &idx);
 
 	parse_events__flush_buffer(buffer);
 	parse_events__delete_buffer(buffer);
tools/perf/util/parse-events.h +6 -10
index 66e08f4..8651757 100644
@@ -87,25 +87,21 @@ int parse_events__term_elem(struct parse_events__term_elem **_e,
 			    int type_val, char *str, long num);
 int parse_events__branch_type(u64 *_type, char *str);
 void parse_events__free_terms(struct list_head *terms);
-int parse_events_modifier(struct list_head *list __used, char *str __used);
-int parse_events_add_tracepoint(struct list_head *list, int *idx,
+int parse_events_modifier(struct list_head *list, char *str);
+int parse_events_add_tracepoint(struct list_head **list, int *idx,
 				char *sys, char *event);
-int parse_events_add_raw(struct perf_evlist *evlist, unsigned long config,
-			 unsigned long config1, unsigned long config2,
-			 char *mod);
-int parse_events_add_numeric(struct list_head *list, int *idx,
+int parse_events_add_numeric(struct list_head **list, int *idx,
 			     unsigned long type, unsigned long config,
 			     struct list_head *head_config);
-int parse_events_add_cache(struct list_head *list, int *idx,
+int parse_events_add_cache(struct list_head **list, int *idx,
 			   char *type, char *op_result1, char *op_result2);
-int parse_events_add_breakpoint(struct list_head *list, int *idx,
+int parse_events_add_breakpoint(struct list_head **list, int *idx,
 				void *ptr, char *type);
-int parse_events_add_pmu(struct list_head *list, int *idx,
+int parse_events_add_pmu(struct list_head **list, int *idx,
 			 char *pmu , struct list_head *head_config);
 void parse_events_update_lists(struct list_head *list_event,
 			       struct list_head *list_all);
 void parse_events_error(struct list_head *list_all,
-			struct list_head *list_event,
 			int *idx, char const *msg);
 int parse_events__test(void);
 
tools/perf/util/parse-events.y +53 -16

157 lines not on this page · raw message

Signature · 1 line
1.7.7.6