Skip to contentSkip to conversation

perf tool: Add new event group management

RFC Superseded Jiri Olsa 5 participants 28 messages Mar 20, 2012 – Apr 10, 2012 perf events

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

cover perf tool: Add new event group management

hi,
adding support for creating event groups based on the way they are specified on the command line. Also attaching several fixies to current code.

attached patches:
1/3 perf, tool: Fix various casting issues for 32 bits 2/3 perf, tool: Fix modifier to be applied on correct events 3/3 perf, tool: Add new event group management

I added automated tests and run some record and stats tests based on the examples (attached in the 3/3 changelog), and so far so good..

thanks for comments,
jirka

---
 tools/perf/Makefile                  |    2 +
 tools/perf/builtin-record.c          |    8 +-
 tools/perf/builtin-stat.c            |   10 +-
 tools/perf/builtin-test.c            |  126 +++++++-
 tools/perf/builtin-top.c             |    8 +-
 tools/perf/perf.h                    |    3 +-
 tools/perf/util/evlist.c             |    4 +-
 tools/perf/util/evlist.h             |    3 +-
 tools/perf/util/evsel.c              |   32 ++-
 tools/perf/util/evsel.h              |    9 +-
 tools/perf/util/group.c              |   22 ++
 tools/perf/util/group.h              |   32 ++
 tools/perf/util/header.c             |    2 +-
 tools/perf/util/parse-events-bison.c |  675 +++++++++++++++++++---------------
 tools/perf/util/parse-events-bison.h |   15 +-
 tools/perf/util/parse-events.c       |   47 ++-
 tools/perf/util/parse-events.h       |   10 +-
 tools/perf/util/parse-events.y       |   40 ++-
 tools/perf/util/python.c             |    4 +
 tools/perf/util/top.h                |    2 +-
 20 files changed, 687 insertions(+), 367 deletions(-)
author
#1 lore raw

PATCH 1/3 perf, tool: Fix various casting issues for 32 bits

- util/parse-events.c(parse_events_add_breakpoint) need to use unsigned long instead of u64, otherwise we get following gcc error on 32 bits:
error: cast from pointer to integer of different size

- util/header.c(print_event_desc)
cannot retype to signed type, otherwise we get following gcc error on 32 bits:
error: comparison between signed and unsigned integer expressions

---
 tools/perf/util/header.c       |    2 +-
 tools/perf/util/parse-events.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/header.c +1 -1
index fcd9cf3..4c7c2d7 100644
@@ -1177,7 +1177,7 @@ static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
 		goto error;
 
 	msz = sizeof(attr);
-	if (sz < (ssize_t)msz)
+	if (sz < msz)
 		msz = sz;
 
 	for (i = 0 ; i < nre; i++) {
tools/perf/util/parse-events.c +1 -1
index 201b40f..f542a63 100644
@@ -569,7 +569,7 @@ int parse_events_add_breakpoint(struct list_head *list, int *idx,
 	char name[MAX_NAME_LEN];
 
 	memset(&attr, 0, sizeof(attr));
-	attr.bp_addr = (u64) ptr;
+	attr.bp_addr = (unsigned long) ptr;
 
 	if (parse_breakpoint_type(type, &attr))
 		return -EINVAL;
Signature · 1 line
1.7.7.6
#2 lore raw

[tip:perf/urgent] perf tools: Fix various casting issues for 32 bits

Commit-ID:  9fafd98f1bf14276f95b69f0186ad5675f1e1a18
Gitweb:     http://git.kernel.org/tip/9fafd98f1bf14276f95b69f0186ad5675f1e1a18
Author:     Jiri Olsa <[email protected]>
AuthorDate: Tue, 20 Mar 2012 19:15:39 +0100
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 22 Mar 2012 15:11:18 -0300

perf tools: Fix various casting issues for 32 bits

- util/parse-events.c(parse_events_add_breakpoint) need to use unsigned long instead of u64, otherwise we get following gcc error on 32 bits:
error: cast from pointer to integer of different size

- util/header.c(print_event_desc)
cannot retype to signed type, otherwise we get following gcc error on 32 bits:
error: comparison between signed and unsigned integer expressions

---
 tools/perf/util/header.c       |    2 +-
 tools/perf/util/parse-events.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/header.c +1 -1
index fcd9cf3..4c7c2d7 100644
@@ -1177,7 +1177,7 @@ static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
 		goto error;
 
 	msz = sizeof(attr);
-	if (sz < (ssize_t)msz)
+	if (sz < msz)
 		msz = sz;
 
 	for (i = 0 ; i < nre; i++) {
tools/perf/util/parse-events.c +1 -1
index 201b40f..f542a63 100644
@@ -569,7 +569,7 @@ int parse_events_add_breakpoint(struct list_head *list, int *idx,
 	char name[MAX_NAME_LEN];
 
 	memset(&attr, 0, sizeof(attr));
-	attr.bp_addr = (u64) ptr;
+	attr.bp_addr = (unsigned long) ptr;
 
 	if (parse_breakpoint_type(type, &attr))
 		return -EINVAL;
author
#3 lore raw

PATCH 2/3 perf, tool: Fix modifier to be applied on correct events

In reply to Jiri Olsa

The event modifier needs to be applied only on the event definition it is attached to.

The current state is that in case of multiple events definition (in single '-e' option, separated by ',') all will get modifier of the last one.

Fixing this by adding separated list for each event definition, so the modifier is applied only to proper event(s). Added automated test to catch this, plus some other modifier tests.

---
 tools/perf/builtin-test.c            |  116 ++++++-
 tools/perf/util/parse-events-bison.c |  675 +++++++++++++++++++---------------
 tools/perf/util/parse-events-bison.h |   15 +-
 tools/perf/util/parse-events.c       |   27 +-
 tools/perf/util/parse-events.h       |    7 +-
 tools/perf/util/parse-events.y       |   40 ++-
 6 files changed, 543 insertions(+), 337 deletions(-)
tools/perf/builtin-test.c +114 -2
index 8687423..1c5b980 100644
@@ -877,6 +877,58 @@ static int test__checkevent_genhw_modifier(struct perf_evlist *evlist)
 	return test__checkevent_genhw(evlist);
 }
 
+static int test__checkevent_breakpoint_modifier(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+
+	return test__checkevent_breakpoint(evlist);
+}
+
+static int test__checkevent_breakpoint_x_modifier(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+
+	return test__checkevent_breakpoint_x(evlist);
+}
+
+static int test__checkevent_breakpoint_r_modifier(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+
+	return test__checkevent_breakpoint_r(evlist);
+}
+
+static int test__checkevent_breakpoint_w_modifier(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+
+	return test__checkevent_breakpoint_w(evlist);
+}
+
 static int test__checkevent_pmu(struct perf_evlist *evlist)
 {
 
@@ -893,6 +945,47 @@ static int test__checkevent_pmu(struct perf_evlist *evlist)
 	return 0;
 }
 
+static int test__checkevent_list(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel;
+
+	TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
+
+	/* r1 */
+	evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
+	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
+	TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
+	TEST_ASSERT_VAL("wrong config2", 0 == evsel->attr.config2);
+	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+
+	/* syscalls:sys_enter_open:k */
+	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
+	TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong sample_type",
+		(PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
+		evsel->attr.sample_type);
+	TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
+	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+
+	/* 1:1:hp */
+	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
+	TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
+	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+
+	return 0;
+}
+
 static struct test__event_st {
 	const char *name;
 	__u32 type;
@@ -975,9 +1068,29 @@ static struct test__event_st {
 		.check = test__checkevent_genhw_modifier,
 	},
 	{
+		.name  = "mem:0:u",
+		.check = test__checkevent_breakpoint_modifier,
+	},
+	{
+		.name  = "mem:0:x:k",
+		.check = test__checkevent_breakpoint_x_modifier,
+	},
+	{
+		.name  = "mem:0:r:hp",
+		.check = test__checkevent_breakpoint_r_modifier,
+	},
+	{
+		.name  = "mem:0:w:up",
+		.check = test__checkevent_breakpoint_w_modifier,
+	},
+	{
 		.name  = "cpu/config=10,config1,config2=3,period=1000/u",
 		.check = test__checkevent_pmu,
 	},
+	{
+		.name  = "r1,syscalls:sys_enter_open:k,1:1:hp",
+		.check = test__checkevent_list,
+	},
 };
 
 #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
@@ -1003,10 +1116,9 @@ static int test__parse_events(void)
 		}
 
 		ret = e->check(evlist);
+		perf_evlist__delete(evlist);
 		if (ret)
 			break;
-
-		perf_evlist__delete(evlist);
 	}
 
 	return ret;
tools/perf/util/parse-events-bison.c +370 -305

1,138 lines not on this page · raw message

tools/perf/util/parse-events-bison.h +7 -8
index c58b765..0be3e5a 100644
@@ -1,9 +1,8 @@
-/* A Bison parser, made by GNU Bison 2.4.3.  */
+/* A Bison parser, made by GNU Bison 2.5.  */
 
-/* Skeleton interface for Bison's Yacc-like parsers in C
+/* Bison interface for Yacc-like parsers in C
    
-      Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
-   2009, 2010 Free Software Foundation, Inc.
+      Copyright (C) 1984, 1989-1990, 2000-2011 Free Software Foundation, Inc.
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -59,8 +58,8 @@
 typedef union YYSTYPE
 {
 
-/* Line 1685 of yacc.c  */
-#line 45 "util/parse-events.y"
+/* Line 2068 of yacc.c  */
+#line 46 "util/parse-events.y"
 
 	char *str;
 	unsigned long num;
@@ -69,8 +68,8 @@ typedef union YYSTYPE
 
 
 
-/* Line 1685 of yacc.c  */
-#line 74 "util/parse-events-bison.h"
+/* Line 2068 of yacc.c  */
+#line 73 "util/parse-events-bison.h"
 } YYSTYPE;
 # define YYSTYPE_IS_TRIVIAL 1
 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
tools/perf/util/parse-events.c +21 -6
index f542a63..5b3a0ef 100644
@@ -23,7 +23,8 @@ struct event_symbol {
 	const char	*alias;
 };
 
-int parse_events_parse(struct list_head *list, int *idx);
+int parse_events_parse(struct list_head *list, struct list_head *list_tmp,
+		       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
@@ -671,6 +672,18 @@ int parse_events_add_pmu(struct list_head *list, int *idx,
 	return add_event(list, idx, &attr, (char *) "pmu");
 }
 
+void parse_events_update_lists(struct list_head *list_event,
+			       struct list_head *list_all)
+{
+	/*
+	 * Called for single event definition. Update the
+	 * 'all event' list, and reinit the 'signle event'
+	 * list, for next event definition.
+	 */
+	list_splice_tail(list_event, list_all);
+	INIT_LIST_HEAD(list_event);
+}
+
 int parse_events_modifier(struct list_head *list, char *str)
 {
 	struct perf_evsel *evsel;
@@ -736,14 +749,14 @@ int parse_events_modifier(struct list_head *list, char *str)
 
 int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 {
-	struct perf_evsel *evsel, *h;
 	LIST_HEAD(list);
+	LIST_HEAD(list_tmp);
 	YY_BUFFER_STATE buffer;
 	int ret, idx = evlist->nr_entries;
 
 	buffer = parse_events__scan_string(str);
 
-	ret = parse_events_parse(&list, &idx);
+	ret = parse_events_parse(&list, &list_tmp, &idx);
 
 	parse_events__flush_buffer(buffer);
 	parse_events__delete_buffer(buffer);
@@ -754,9 +767,11 @@ int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 		return 0;
 	}
 
-	list_for_each_entry_safe(evsel, h, &list, node)
-		perf_evsel__delete(evsel);
-
+	/*
+	 * There are 2 users - builtin-record and builtin-test objects.
+	 * Both call perf_evlist__delete in case of error, so we dont
+	 * need to bother.
+	 */
 	fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
 	fprintf(stderr, "Run 'perf list' for a list of valid events\n");
 	return ret;
tools/perf/util/parse-events.h +5 -2
index 6d7c74b..ca069f8 100644
@@ -76,8 +76,11 @@ 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,
 			 char *pmu , struct list_head *head_config);
-void parse_events_error(struct list_head *list, int *idx,
-			char const *msg);
+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);
 
 void print_events(const char *event_glob);
 void print_events_type(u8 type);
tools/perf/util/parse-events.y +26 -14
index 3a53019..d9637da 100644
@@ -1,6 +1,7 @@
 
 %name-prefix "parse_events_"
-%parse-param {struct list_head *list}
+%parse-param {struct list_head *list_all}
+%parse-param {struct list_head *list_event}
 %parse-param {int *idx}
 
 %{
@@ -56,10 +57,19 @@ events ',' event | event
 event:
 event_def PE_MODIFIER_EVENT
 {
-	ABORT_ON(parse_events_modifier(list, $2));
+	/*
+	 * Apply modifier on all events added by single event definition
+	 * (there could be more events added for multiple tracepoint
+	 * definitions via '*?'.
+	 */
+	ABORT_ON(parse_events_modifier(list_event, $2));
+	parse_events_update_lists(list_event, list_all);
 }
 |
 event_def
+{
+	parse_events_update_lists(list_event, list_all);
+}
 
 event_def: event_pmu |
 	   event_legacy_symbol |
@@ -72,7 +82,7 @@ event_def: event_pmu |
 event_pmu:
 PE_NAME '/' event_config '/'
 {
-	ABORT_ON(parse_events_add_pmu(list, idx, $1, $3));
+	ABORT_ON(parse_events_add_pmu(list_event, idx, $1, $3));
 	parse_events__free_terms($3);
 }
 
@@ -82,7 +92,7 @@ PE_VALUE_SYM '/' event_config '/'
 	int type = $1 >> 16;
 	int config = $1 & 255;
 
-	ABORT_ON(parse_events_add_numeric(list, idx, type, config, $3));
+	ABORT_ON(parse_events_add_numeric(list_event, idx, type, config, $3));
 	parse_events__free_terms($3);
 }
 |
@@ -91,52 +101,52 @@ PE_VALUE_SYM sep_slash_dc
 	int type = $1 >> 16;
 	int config = $1 & 255;
 
-	ABORT_ON(parse_events_add_numeric(list, idx, type, config, NULL));
+	ABORT_ON(parse_events_add_numeric(list_event, idx, type, config, NULL));
 }
 
 event_legacy_cache:
 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT
 {
-	ABORT_ON(parse_events_add_cache(list, idx, $1, $3, $5));
+	ABORT_ON(parse_events_add_cache(list_event, idx, $1, $3, $5));
 }
 |
 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT
 {
-	ABORT_ON(parse_events_add_cache(list, idx, $1, $3, NULL));
+	ABORT_ON(parse_events_add_cache(list_event, idx, $1, $3, NULL));
 }
 |
 PE_NAME_CACHE_TYPE
 {
-	ABORT_ON(parse_events_add_cache(list, idx, $1, NULL, NULL));
+	ABORT_ON(parse_events_add_cache(list_event, idx, $1, NULL, NULL));
 }
 
 event_legacy_mem:
 PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc
 {
-	ABORT_ON(parse_events_add_breakpoint(list, idx, (void *) $2, $4));
+	ABORT_ON(parse_events_add_breakpoint(list_event, idx, (void *) $2, $4));
 }
 |
 PE_PREFIX_MEM PE_VALUE sep_dc
 {
-	ABORT_ON(parse_events_add_breakpoint(list, idx, (void *) $2, NULL));
+	ABORT_ON(parse_events_add_breakpoint(list_event, idx, (void *) $2, NULL));
 }
 
 event_legacy_tracepoint:
 PE_NAME ':' PE_NAME
 {
-	ABORT_ON(parse_events_add_tracepoint(list, idx, $1, $3));
+	ABORT_ON(parse_events_add_tracepoint(list_event, idx, $1, $3));
 }
 
 event_legacy_numeric:
 PE_VALUE ':' PE_VALUE
 {
-	ABORT_ON(parse_events_add_numeric(list, idx, $1, $3, NULL));
+	ABORT_ON(parse_events_add_numeric(list_event, idx, $1, $3, NULL));
 }
 
 event_legacy_raw:
 PE_RAW
 {
-	ABORT_ON(parse_events_add_numeric(list, idx, PERF_TYPE_RAW, $1, NULL));
+	ABORT_ON(parse_events_add_numeric(list_event, idx, PERF_TYPE_RAW, $1, NULL));
 }
 
 event_config:
@@ -211,7 +221,9 @@ sep_slash_dc: '/' | ':' |
 
 %%
 
-void parse_events_error(struct list_head *list __used, int *idx __used,
+void parse_events_error(struct list_head *list_all __used,
+			struct list_head *list_event __used,
+			int *idx __used,
 			char const *msg __used)
 {
 }
Signature · 1 line
1.7.7.6
#4 lore raw

[tip:perf/urgent] perf tools: Fix modifier to be applied on correct events

Commit-ID:  5d7be90ed5cfb5dd3c9ab726d7daa91b86b81747
Gitweb:     http://git.kernel.org/tip/5d7be90ed5cfb5dd3c9ab726d7daa91b86b81747
Author:     Jiri Olsa <[email protected]>
AuthorDate: Tue, 20 Mar 2012 19:15:40 +0100
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 22 Mar 2012 15:11:38 -0300

perf tools: Fix modifier to be applied on correct events

The event modifier needs to be applied only on the event definition it is attached to.

The current state is that in case of multiple events definition (in single '-e' option, separated by ',') all will get modifier of the last one.

Fixing this by adding separated list for each event definition, so the modifier is applied only to proper event(s). Added automated test to catch this, plus some other modifier tests.

---
 tools/perf/builtin-test.c            |  116 ++++++-
 tools/perf/util/parse-events-bison.c |  675 +++++++++++++++++++---------------
 tools/perf/util/parse-events-bison.h |   15 +-
 tools/perf/util/parse-events.c       |   27 +-
 tools/perf/util/parse-events.h       |    7 +-
 tools/perf/util/parse-events.y       |   40 ++-
 6 files changed, 543 insertions(+), 337 deletions(-)
tools/perf/builtin-test.c +114 -2
index 8687423..1c5b980 100644
@@ -877,6 +877,58 @@ static int test__checkevent_genhw_modifier(struct perf_evlist *evlist)
 	return test__checkevent_genhw(evlist);
 }
 
+static int test__checkevent_breakpoint_modifier(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+
+	return test__checkevent_breakpoint(evlist);
+}
+
+static int test__checkevent_breakpoint_x_modifier(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+
+	return test__checkevent_breakpoint_x(evlist);
+}
+
+static int test__checkevent_breakpoint_r_modifier(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+
+	return test__checkevent_breakpoint_r(evlist);
+}
+
+static int test__checkevent_breakpoint_w_modifier(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel = list_entry(evlist->entries.next,
+					      struct perf_evsel, node);
+
+	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+
+	return test__checkevent_breakpoint_w(evlist);
+}
+
 static int test__checkevent_pmu(struct perf_evlist *evlist)
 {
 
@@ -893,6 +945,47 @@ static int test__checkevent_pmu(struct perf_evlist *evlist)
 	return 0;
 }
 
+static int test__checkevent_list(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel;
+
+	TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
+
+	/* r1 */
+	evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
+	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
+	TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
+	TEST_ASSERT_VAL("wrong config2", 0 == evsel->attr.config2);
+	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+
+	/* syscalls:sys_enter_open:k */
+	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
+	TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong sample_type",
+		(PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
+		evsel->attr.sample_type);
+	TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
+	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+
+	/* 1:1:hp */
+	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
+	TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
+	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
+	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
+	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
+	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
+	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+
+	return 0;
+}
+
 static struct test__event_st {
 	const char *name;
 	__u32 type;
@@ -975,9 +1068,29 @@ static struct test__event_st {
 		.check = test__checkevent_genhw_modifier,
 	},
 	{
+		.name  = "mem:0:u",
+		.check = test__checkevent_breakpoint_modifier,
+	},
+	{
+		.name  = "mem:0:x:k",
+		.check = test__checkevent_breakpoint_x_modifier,
+	},
+	{
+		.name  = "mem:0:r:hp",
+		.check = test__checkevent_breakpoint_r_modifier,
+	},
+	{
+		.name  = "mem:0:w:up",
+		.check = test__checkevent_breakpoint_w_modifier,
+	},
+	{
 		.name  = "cpu/config=10,config1,config2=3,period=1000/u",
 		.check = test__checkevent_pmu,
 	},
+	{
+		.name  = "r1,syscalls:sys_enter_open:k,1:1:hp",
+		.check = test__checkevent_list,
+	},
 };
 
 #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
@@ -1003,10 +1116,9 @@ static int test__parse_events(void)
 		}
 
 		ret = e->check(evlist);
+		perf_evlist__delete(evlist);
 		if (ret)
 			break;
-
-		perf_evlist__delete(evlist);
 	}
 
 	return ret;
tools/perf/util/parse-events-bison.c +370 -305

1,138 lines not on this page · raw message

tools/perf/util/parse-events-bison.h +7 -8
index c58b765..0be3e5a 100644
@@ -1,9 +1,8 @@
-/* A Bison parser, made by GNU Bison 2.4.3.  */
+/* A Bison parser, made by GNU Bison 2.5.  */
 
-/* Skeleton interface for Bison's Yacc-like parsers in C
+/* Bison interface for Yacc-like parsers in C
    
-      Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
-   2009, 2010 Free Software Foundation, Inc.
+      Copyright (C) 1984, 1989-1990, 2000-2011 Free Software Foundation, Inc.
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -59,8 +58,8 @@
 typedef union YYSTYPE
 {
 
-/* Line 1685 of yacc.c  */
-#line 45 "util/parse-events.y"
+/* Line 2068 of yacc.c  */
+#line 46 "util/parse-events.y"
 
 	char *str;
 	unsigned long num;
@@ -69,8 +68,8 @@ typedef union YYSTYPE
 
 
 
-/* Line 1685 of yacc.c  */
-#line 74 "util/parse-events-bison.h"
+/* Line 2068 of yacc.c  */
+#line 73 "util/parse-events-bison.h"
 } YYSTYPE;
 # define YYSTYPE_IS_TRIVIAL 1
 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
tools/perf/util/parse-events.c +21 -6
index f542a63..5b3a0ef 100644
@@ -23,7 +23,8 @@ struct event_symbol {
 	const char	*alias;
 };
 
-int parse_events_parse(struct list_head *list, int *idx);
+int parse_events_parse(struct list_head *list, struct list_head *list_tmp,
+		       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
@@ -671,6 +672,18 @@ int parse_events_add_pmu(struct list_head *list, int *idx,
 	return add_event(list, idx, &attr, (char *) "pmu");
 }
 
+void parse_events_update_lists(struct list_head *list_event,
+			       struct list_head *list_all)
+{
+	/*
+	 * Called for single event definition. Update the
+	 * 'all event' list, and reinit the 'signle event'
+	 * list, for next event definition.
+	 */
+	list_splice_tail(list_event, list_all);
+	INIT_LIST_HEAD(list_event);
+}
+
 int parse_events_modifier(struct list_head *list, char *str)
 {
 	struct perf_evsel *evsel;
@@ -736,14 +749,14 @@ int parse_events_modifier(struct list_head *list, char *str)
 
 int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 {
-	struct perf_evsel *evsel, *h;
 	LIST_HEAD(list);
+	LIST_HEAD(list_tmp);
 	YY_BUFFER_STATE buffer;
 	int ret, idx = evlist->nr_entries;
 
 	buffer = parse_events__scan_string(str);
 
-	ret = parse_events_parse(&list, &idx);
+	ret = parse_events_parse(&list, &list_tmp, &idx);
 
 	parse_events__flush_buffer(buffer);
 	parse_events__delete_buffer(buffer);
@@ -754,9 +767,11 @@ int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 		return 0;
 	}
 
-	list_for_each_entry_safe(evsel, h, &list, node)
-		perf_evsel__delete(evsel);
-
+	/*
+	 * There are 2 users - builtin-record and builtin-test objects.
+	 * Both call perf_evlist__delete in case of error, so we dont
+	 * need to bother.
+	 */
 	fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
 	fprintf(stderr, "Run 'perf list' for a list of valid events\n");
 	return ret;
tools/perf/util/parse-events.h +5 -2
index 6d7c74b..ca069f8 100644
@@ -76,8 +76,11 @@ 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,
 			 char *pmu , struct list_head *head_config);
-void parse_events_error(struct list_head *list, int *idx,
-			char const *msg);
+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);
 
 void print_events(const char *event_glob);
 void print_events_type(u8 type);
tools/perf/util/parse-events.y +26 -14
index 3a53019..d9637da 100644
@@ -1,6 +1,7 @@
 
 %name-prefix "parse_events_"
-%parse-param {struct list_head *list}
+%parse-param {struct list_head *list_all}
+%parse-param {struct list_head *list_event}
 %parse-param {int *idx}
 
 %{
@@ -56,10 +57,19 @@ events ',' event | event
 event:
 event_def PE_MODIFIER_EVENT
 {
-	ABORT_ON(parse_events_modifier(list, $2));
+	/*
+	 * Apply modifier on all events added by single event definition
+	 * (there could be more events added for multiple tracepoint
+	 * definitions via '*?'.
+	 */
+	ABORT_ON(parse_events_modifier(list_event, $2));
+	parse_events_update_lists(list_event, list_all);
 }
 |
 event_def
+{
+	parse_events_update_lists(list_event, list_all);
+}
 
 event_def: event_pmu |
 	   event_legacy_symbol |
@@ -72,7 +82,7 @@ event_def: event_pmu |
 event_pmu:
 PE_NAME '/' event_config '/'
 {
-	ABORT_ON(parse_events_add_pmu(list, idx, $1, $3));
+	ABORT_ON(parse_events_add_pmu(list_event, idx, $1, $3));
 	parse_events__free_terms($3);
 }
 
@@ -82,7 +92,7 @@ PE_VALUE_SYM '/' event_config '/'
 	int type = $1 >> 16;
 	int config = $1 & 255;
 
-	ABORT_ON(parse_events_add_numeric(list, idx, type, config, $3));
+	ABORT_ON(parse_events_add_numeric(list_event, idx, type, config, $3));
 	parse_events__free_terms($3);
 }
 |
@@ -91,52 +101,52 @@ PE_VALUE_SYM sep_slash_dc
 	int type = $1 >> 16;
 	int config = $1 & 255;
 
-	ABORT_ON(parse_events_add_numeric(list, idx, type, config, NULL));
+	ABORT_ON(parse_events_add_numeric(list_event, idx, type, config, NULL));
 }
 
 event_legacy_cache:
 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT
 {
-	ABORT_ON(parse_events_add_cache(list, idx, $1, $3, $5));
+	ABORT_ON(parse_events_add_cache(list_event, idx, $1, $3, $5));
 }
 |
 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT
 {
-	ABORT_ON(parse_events_add_cache(list, idx, $1, $3, NULL));
+	ABORT_ON(parse_events_add_cache(list_event, idx, $1, $3, NULL));
 }
 |
 PE_NAME_CACHE_TYPE
 {
-	ABORT_ON(parse_events_add_cache(list, idx, $1, NULL, NULL));
+	ABORT_ON(parse_events_add_cache(list_event, idx, $1, NULL, NULL));
 }
 
 event_legacy_mem:
 PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc
 {
-	ABORT_ON(parse_events_add_breakpoint(list, idx, (void *) $2, $4));
+	ABORT_ON(parse_events_add_breakpoint(list_event, idx, (void *) $2, $4));
 }
 |
 PE_PREFIX_MEM PE_VALUE sep_dc
 {
-	ABORT_ON(parse_events_add_breakpoint(list, idx, (void *) $2, NULL));
+	ABORT_ON(parse_events_add_breakpoint(list_event, idx, (void *) $2, NULL));
 }
 
 event_legacy_tracepoint:
 PE_NAME ':' PE_NAME
 {
-	ABORT_ON(parse_events_add_tracepoint(list, idx, $1, $3));
+	ABORT_ON(parse_events_add_tracepoint(list_event, idx, $1, $3));
 }
 
 event_legacy_numeric:
 PE_VALUE ':' PE_VALUE
 {
-	ABORT_ON(parse_events_add_numeric(list, idx, $1, $3, NULL));
+	ABORT_ON(parse_events_add_numeric(list_event, idx, $1, $3, NULL));
 }
 
 event_legacy_raw:
 PE_RAW
 {
-	ABORT_ON(parse_events_add_numeric(list, idx, PERF_TYPE_RAW, $1, NULL));
+	ABORT_ON(parse_events_add_numeric(list_event, idx, PERF_TYPE_RAW, $1, NULL));
 }
 
 event_config:
@@ -211,7 +221,9 @@ sep_slash_dc: '/' | ':' |
 
 %%
 
-void parse_events_error(struct list_head *list __used, int *idx __used,
+void parse_events_error(struct list_head *list_all __used,
+			struct list_head *list_event __used,
+			int *idx __used,
 			char const *msg __used)
 {
 }
author
#5 lore raw

PATCH 3/3 perf, tool: Add new event group management

In reply to Jiri Olsa

The current event grouping is basic. If you specify the '--group' option for record/stat/top command, all the specified events become members of a single group with the first event as a group leader.

This patch adds a functionality that allows to create event groups based on the way they are specified on the command line. Extending the '--group/-g' option power while preserving the current behaviour.

It is now possible to use '--group/-g' option with 'parsed' value, which will create groups based on the command line events layout.

With the '--group/-g parsed' option specified, all events separated by ',' within a single '-e' option now become members of a group with the first event specified as a group leader. Another '-e' option with multiple events creates separate group.

All groups are created with regards to threads and cpus. Thus recording an event group within a 2 threads on server with 4 CPUs will create 8 separate groups.

Examples (first event in brackets is group leader):

  # 1 group (cpu-clock,task-clock)
  perf record --group -e cpu-clock,task-clock ls
  perf record --group parsed -e cpu-clock,task-clock ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults,instructions)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

Updated automated test to check on group_leader settings.

---
 tools/perf/Makefile            |    2 ++
 tools/perf/builtin-record.c    |    8 +++++---
 tools/perf/builtin-stat.c      |   10 ++++++----
 tools/perf/builtin-test.c      |   10 ++++++++++
 tools/perf/builtin-top.c       |    8 +++++---
 tools/perf/perf.h              |    3 ++-
 tools/perf/util/evlist.c       |    4 ++--
 tools/perf/util/evlist.h       |    3 ++-
 tools/perf/util/evsel.c        |   32 +++++++++++++++++++++++++-------
 tools/perf/util/evsel.h        |    9 ++++++---
 tools/perf/util/group.c        |   22 ++++++++++++++++++++++
 tools/perf/util/group.h        |   32 ++++++++++++++++++++++++++++++++
 tools/perf/util/parse-events.c |   18 ++++++++++++++++++
 tools/perf/util/parse-events.h |    3 ---
 tools/perf/util/python.c       |    4 ++++
 tools/perf/util/top.h          |    2 +-
 16 files changed, 142 insertions(+), 28 deletions(-)
 create mode 100644 tools/perf/util/group.c
 create mode 100644 tools/perf/util/group.h
tools/perf/Makefile +2 -0
index b492e3a..ea10b29 100644
@@ -317,6 +317,7 @@ LIB_H += util/cpumap.h
 LIB_H += util/top.h
 LIB_H += $(ARCH_INCLUDE)
 LIB_H += util/cgroup.h
+LIB_H += util/group.h
 
 LIB_OBJS += $(OUTPUT)util/abspath.o
 LIB_OBJS += $(OUTPUT)util/alias.o
@@ -378,6 +379,7 @@ LIB_OBJS += $(OUTPUT)util/util.o
 LIB_OBJS += $(OUTPUT)util/xyarray.o
 LIB_OBJS += $(OUTPUT)util/cpumap.o
 LIB_OBJS += $(OUTPUT)util/cgroup.o
+LIB_OBJS += $(OUTPUT)util/group.o
 
 BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
 
tools/perf/builtin-record.c +5 -3
index be4e1ee..a2c7dc2 100644
@@ -26,6 +26,7 @@
 #include "util/symbol.h"
 #include "util/cpumap.h"
 #include "util/thread_map.h"
+#include "util/group.h"
 
 #include <unistd.h>
 #include <sched.h>
@@ -203,7 +204,7 @@ static void perf_record__open(struct perf_record *rec)
 		 */
 		bool time_needed = attr->sample_type & PERF_SAMPLE_TIME;
 
-		if (opts->group && pos != first)
+		if (group_is_single(opts->group) && pos != first)
 			group_fd = first->fd;
 fallback_missing_features:
 		if (opts->exclude_guest_missing)
@@ -791,8 +792,9 @@ const struct option record_options[] = {
 	OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
 	OPT_UINTEGER('m', "mmap-pages", &record.opts.mmap_pages,
 		     "number of mmap data pages"),
-	OPT_BOOLEAN(0, "group", &record.opts.group,
-		    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT(0, "group", &record.opts.group, "[parsed]",
+		    "put the counters into a counter group",
+		     group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('g', "call-graph", &record.opts.call_graph,
 		    "do call-graph (stack chain/backtrace) recording"),
 	OPT_INCR('v', "verbose", &verbose,
tools/perf/builtin-stat.c +6 -4
index c941bb6..d0d3859 100644
@@ -55,6 +55,7 @@
 #include "util/cpumap.h"
 #include "util/thread.h"
 #include "util/thread_map.h"
+#include "util/group.h"
 
 #include <sys/prctl.h>
 #include <math.h>
@@ -193,7 +194,7 @@ static int			big_num_opt			=  -1;
 static const char		*cpu_list;
 static const char		*csv_sep			= NULL;
 static bool			csv_output			= false;
-static bool			group				= false;
+static enum perf_group_opt	group;
 static const char		*output_name			= NULL;
 static FILE			*output				= NULL;
 static int			output_fd;
@@ -284,7 +285,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel,
 	struct perf_event_attr *attr = &evsel->attr;
 	struct xyarray *group_fd = NULL;
 
-	if (group && evsel != first)
+	if (group_is_single(group) && evsel != first)
 		group_fd = first->fd;
 
 	if (scale)
@@ -1055,8 +1056,9 @@ static const struct option options[] = {
 		   "stat events on existing thread id"),
 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
 		    "system-wide collection from all CPUs"),
-	OPT_BOOLEAN('g', "group", &group,
-		    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT('g', "group", &group, "[parsed]",
+		    "put the counters into a counter group",
+		     group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('c', "scale", &scale,
 		    "scale/normalize counters"),
 	OPT_INCR('v', "verbose", &verbose,
tools/perf/builtin-test.c +10 -0
index 1c5b980..babdd77 100644
@@ -14,6 +14,7 @@
 #include "util/symbol.h"
 #include "util/thread_map.h"
 #include "util/pmu.h"
+#include "util/group.h"
 #include "../../include/linux/hw_breakpoint.h"
 
 #include <sys/mman.h>
@@ -948,11 +949,14 @@ static int test__checkevent_pmu(struct perf_evlist *evlist)
 static int test__checkevent_list(struct perf_evlist *evlist)
 {
 	struct perf_evsel *evsel;
+	struct perf_evsel *parsed_leader;
 
 	TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
 
 	/* r1 */
 	evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
+	parsed_leader = evsel;
+
 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
 	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
 	TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
@@ -961,6 +965,7 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader", parsed_leader);
 
 	/* syscalls:sys_enter_open:k */
 	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
@@ -973,6 +978,8 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader",
+			evsel->parsed_leader == parsed_leader);
 
 	/* 1:1:hp */
 	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
@@ -982,6 +989,8 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader",
+			evsel->parsed_leader == parsed_leader);
 
 	return 0;
 }
@@ -1168,6 +1177,7 @@ static int test__PERF_RECORD(void)
 		.no_delay   = true,
 		.freq	    = 10,
 		.mmap_pages = 256,
+		.group      = PERF_GROUP_NONE,
 	};
 	cpu_set_t *cpu_mask = NULL;
 	size_t cpu_mask_size = 0;
tools/perf/builtin-top.c +5 -3
index e3c63ae..fd04361 100644
@@ -38,6 +38,7 @@
 #include "util/cpumap.h"
 #include "util/xyarray.h"
 #include "util/sort.h"
+#include "util/group.h"
 
 #include "util/debug.h"
 
@@ -850,7 +851,7 @@ static void perf_top__start_counters(struct perf_top *top)
 		struct perf_event_attr *attr = &counter->attr;
 		struct xyarray *group_fd = NULL;
 
-		if (top->group && counter != first)
+		if (group_is_single(top->group) && counter != first)
 			group_fd = first->fd;
 
 		attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
@@ -1153,8 +1154,9 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 			    "dump the symbol table used for profiling"),
 	OPT_INTEGER('f', "count-filter", &top.count_filter,
 		    "only display functions with more events than this"),
-	OPT_BOOLEAN('g', "group", &top.group,
-			    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT('g', "group", &top.group, "[parsed]",
+		    "put the counters into a counter group",
+		    group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('i', "inherit", &top.inherit,
 		    "child tasks inherit counters"),
 	OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
tools/perf/perf.h +2 -1
index 89e3355..b80b69d 100644
@@ -108,6 +108,7 @@ void get_term_dimensions(struct winsize *ws);
 
 #include "../../include/linux/perf_event.h"
 #include "util/types.h"
+#include "util/group.h"
 #include <stdbool.h>
 
 struct perf_mmap {
@@ -212,7 +213,6 @@ struct perf_record_opts {
 	const char   *target_tid;
 	uid_t	     uid;
 	bool	     call_graph;
-	bool	     group;
 	bool	     inherit_stat;
 	bool	     no_delay;
 	bool	     no_inherit;
@@ -232,6 +232,7 @@ struct perf_record_opts {
 	u64	     default_interval;
 	u64	     user_interval;
 	const char   *cpu_list;
+	enum perf_group_opt group;
 };
 
 #endif
tools/perf/util/evlist.c +2 -2
index 1986d80..c0bcffd 100644
@@ -740,7 +740,7 @@ void perf_evlist__set_selected(struct perf_evlist *evlist,
 	evlist->selected = evsel;
 }
 
-int perf_evlist__open(struct perf_evlist *evlist, bool group)
+int perf_evlist__open(struct perf_evlist *evlist, enum perf_group_opt group)
 {
 	struct perf_evsel *evsel, *first;
 	int err, ncpus, nthreads;
@@ -750,7 +750,7 @@ int perf_evlist__open(struct perf_evlist *evlist, bool group)
 	list_for_each_entry(evsel, &evlist->entries, node) {
 		struct xyarray *group_fd = NULL;
 
-		if (group && evsel != first)
+		if (group_is_single(group) && evsel != first)
 			group_fd = first->fd;
 
 		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads,
tools/perf/util/evlist.h +2 -1
index 21f1c9e..3115e8d 100644
@@ -6,6 +6,7 @@
 #include "../perf.h"
 #include "event.h"
 #include "util.h"
+#include "group.h"
 #include <unistd.h>
 
 struct pollfd;
@@ -78,7 +79,7 @@ struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id);
 
 union perf_event *perf_evlist__mmap_read(struct perf_evlist *self, int idx);
 
-int perf_evlist__open(struct perf_evlist *evlist, bool group);
+int perf_evlist__open(struct perf_evlist *evlist, enum perf_group_opt group);
 
 void perf_evlist__config_attrs(struct perf_evlist *evlist,
 			       struct perf_record_opts *opts);
tools/perf/util/evsel.c +25 -7
index 0221700..50a305d 100644
@@ -14,6 +14,7 @@
 #include "util.h"
 #include "cpumap.h"
 #include "thread_map.h"
+#include "group.h"
 
 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
@@ -136,7 +137,7 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
 	attr->comm = track;
 
 	if (!opts->target_pid && !opts->target_tid && !opts->system_wide &&
-	    (!opts->group || evsel == first)) {
+	    (group_is_none(opts->group) || evsel == first)) {
 		attr->disabled = 1;
 		attr->enable_on_exec = 1;
 	}
@@ -293,8 +294,17 @@ int __perf_evsel__read(struct perf_evsel *evsel,
 	return 0;
 }
 
+static int parsed_leader_fd(struct perf_evsel *evsel,
+			    int cpu, int thread)
+{
+	BUG_ON(!evsel->parsed_leader);
+	/* The evsel->fd xyarray is initialized to -1. */
+	return FD(evsel->parsed_leader, cpu, thread);
+}
+
 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-			      struct thread_map *threads, bool group,
+			      struct thread_map *threads,
+			      enum perf_group_opt group,
 			      struct xyarray *group_fds)
 {
 	int cpu, thread;
@@ -311,13 +321,19 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 	}
 
 	for (cpu = 0; cpu < cpus->nr; cpu++) {
-		int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
+		int group_fd = -1;
+
+		if (group_is_single(group))
+			group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
 
 		for (thread = 0; thread < threads->nr; thread++) {
 
 			if (!evsel->cgrp)
 				pid = threads->map[thread];
 
+			if (group_is_parsed(group))
+				group_fd = parsed_leader_fd(evsel, cpu, thread);
+
 			FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
 								     pid,
 								     cpus->map[cpu],
@@ -327,7 +343,7 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 				goto out_close;
 			}
 
-			if (group && group_fd == -1)
+			if (group_is_single(group) && group_fd == -1)
 				group_fd = FD(evsel, cpu, thread);
 		}
 	}
@@ -372,7 +388,7 @@ static struct {
 };
 
 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-		     struct thread_map *threads, bool group,
+		     struct thread_map *threads, enum perf_group_opt group,
 		     struct xyarray *group_fd)
 {
 	if (cpus == NULL) {
@@ -387,7 +403,8 @@ int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
-			     struct cpu_map *cpus, bool group,
+			     struct cpu_map *cpus,
+			     enum perf_group_opt group,
 			     struct xyarray *group_fd)
 {
 	return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
@@ -395,7 +412,8 @@ int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
 }
 
 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
-				struct thread_map *threads, bool group,
+				struct thread_map *threads,
+				enum perf_group_opt group,
 				struct xyarray *group_fd)
 {
 	return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
tools/perf/util/evsel.h +6 -3
index 3158ca3..ae4023f 100644
@@ -66,6 +66,7 @@ struct perf_evsel {
 		void		*data;
 	} handler;
 	bool 			supported;
+	struct perf_evsel       *parsed_leader;
 };
 
 struct cpu_map;
@@ -91,13 +92,15 @@ void perf_evsel__free_id(struct perf_evsel *evsel);
 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
-			     struct cpu_map *cpus, bool group,
+			     struct cpu_map *cpus,
+			     enum perf_group_opt group,
 			     struct xyarray *group_fds);
 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
-				struct thread_map *threads, bool group,
+				struct thread_map *threads,
+				enum perf_group_opt group,
 				struct xyarray *group_fds);
 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-		     struct thread_map *threads, bool group,
+		     struct thread_map *threads, enum perf_group_opt group,
 		     struct xyarray *group_fds);
 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads);
 
tools/perf/util/group.c new file +22 -0
new file mode 100644
index 0000000..68d18a2
@@ -0,0 +1,22 @@
+#include <linux/compiler.h>
+#include "types.h"
+#include "util.h"
+#include "parse-options.h"
+#include "group.h"
+
+int group_parse(const struct option *opt, const char *str,
+		int unset __used)
+{
+	int *group_opt = (int *) opt->value;
+
+	if (!str)
+		*group_opt = PERF_GROUP_SINGLE;
+	else if (!strcmp(str, "parsed"))
+		*group_opt = PERF_GROUP_PARSED;
+	else {
+		fprintf(stderr, "unknown group option value\n");
+		return -1;
+	}
+
+	return 0;
+}
tools/perf/util/group.h new file +32 -0
new file mode 100644
index 0000000..f97520a
@@ -0,0 +1,32 @@
+#ifndef __GROUP_H__
+#define __GROUP_H__
+
+#include <stdbool.h>
+
+struct option;
+
+enum perf_group_opt {
+	PERF_GROUP_NONE,
+	PERF_GROUP_SINGLE,
+	PERF_GROUP_PARSED
+};
+
+int group_parse(const struct option *opt, const char *str,
+		int unset);
+
+static inline bool group_is_none(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_NONE;
+}
+
+static inline bool group_is_single(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_SINGLE;
+}
+
+static inline bool group_is_parsed(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_PARSED;
+}
+
+#endif /* __GROUP_H__ */
tools/perf/util/parse-events.c +18 -0
index 5b3a0ef..b6bccab98 100644
@@ -747,6 +747,18 @@ int parse_events_modifier(struct list_head *list, char *str)
 	return 0;
 }
 
+static void update_parsed_leader(struct list_head *list)
+{
+	struct perf_evsel *evsel, *parsed_leader = NULL;
+
+	list_for_each_entry(evsel, list, node) {
+		if (!parsed_leader)
+			parsed_leader = evsel;
+
+		evsel->parsed_leader = parsed_leader;
+	}
+}
+
 int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 {
 	LIST_HEAD(list);
@@ -761,6 +773,12 @@ int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 	parse_events__flush_buffer(buffer);
 	parse_events__delete_buffer(buffer);
 
+	/*
+	 * Populate the parsed_leader even if we failed,
+	 * just to have complete data, because who knows.. ;)
+	 */
+	update_parsed_leader(&list);
+
 	if (!ret) {
 		int entries = idx - evlist->nr_entries;
 		perf_evlist__splice_list_tail(evlist, &list, entries);
tools/perf/util/parse-events.h +0 -3
index ca069f8..d6c4d5d 100644
@@ -64,9 +64,6 @@ 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,
 				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,
 			     unsigned long type, unsigned long config,
 			     struct list_head *head_config);
tools/perf/util/python.c +4 -0
index e03b58a..6030758 100644
@@ -7,6 +7,7 @@
 #include "event.h"
 #include "cpumap.h"
 #include "thread_map.h"
+#include "group.h"
 
 /* Define PyVarObject_HEAD_INIT for python 2.5 */
 #ifndef PyVarObject_HEAD_INIT
@@ -828,6 +829,9 @@ static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
 		return NULL;
 
+	if (group)
+		group = PERF_GROUP_SINGLE;
+
 	if (perf_evlist__open(evlist, group) < 0) {
 		PyErr_SetFromErrno(PyExc_OSError);
 		return NULL;
tools/perf/util/top.h +1 -1
index ce61cb2..0d2cf8b 100644
@@ -33,7 +33,7 @@ struct perf_top {
 	bool		   kptr_restrict_warned;
 	bool		   vmlinux_warned;
 	bool		   inherit;
-	bool		   group;
+	enum perf_group_opt group;
 	bool		   sample_id_all_missing;
 	bool		   exclude_guest_missing;
 	bool		   dump_symtab;
Signature · 1 line
1.7.7.6
#6 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 14 lines · Jiri Olsa

Examples (first event in brackets is group leader):

  # 1 group (cpu-clock,task-clock)
  perf record --group -e cpu-clock,task-clock ls
  perf record --group parsed -e cpu-clock,task-clock ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls
Quoted 7 lines
  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults,instructions)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls 

I can't help but dislike the --group/--group parsed thing...

How about something like:

event_group = "{", events, "}" , [ ":", event_group_mod ]

Such that you can write things like:

perf record -e "{cpu-clock,cache-misses,cache-references}"

perf stat -e "{cpu-clock,cycles},{cpu-clock,cache-misses,cache-references}" perf stat -e "{cpu-clock,cycles}" -e "{cpu-clock,cache-misses,cache-references}"

I'm not sure what the current behaviour of --group is, if you create a group like this, do they all sample?

If so, we need some option like:

perf record -e "{cpu-clock,cache-misses,cache-references}:1"

to mean, only sample on cpu-clock but use PERF_SAMPLE_READ and PERF_FORMAT_GROUP to read all siblings on every cpu-clock sample.

Now the disadvantage is that {} needs quotes on bash, the advantage is that its completely natural on how to construct groups, without weird --group/--group parsed. Also it provides a place for group modifiers.

#7 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 1 line · Peter Zijlstra

Now the disadvantage is that {} needs quotes on bash, [...]

Not on my version of Bash:

earth5:~> perf stat {}
{}: No such file or directory
Terminated
earth5:~> perf stat true {}

Performance counter stats for 'true {}':

it's passed in literally, AFAICS.

Thanks,

Ingo

#8 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Em Tue, Mar 20, 2012 at 09:46:25PM +0100, Ingo Molnar escreveu:

Quoted 7 lines · Peter Zijlstra
Quoted 2 lines

Now the disadvantage is that {} needs quotes on bash, [...]

Not on my version of Bash:

Quoted 8 lines

earth5:~> perf stat {}
{}: No such file or directory
Terminated
earth5:~> perf stat true {}

Performance counter stats for 'true {}':

it's passed in literally, AFAICS.

I like it, but...

[acme@felicio ~]$ perf stat -e {cycles,cache-misses} ls
cache-misses: No such file or directory Terminated
[acme@felicio ~]$ perf stat -e \{cycles,cache-misses\} ls invalid or unsupported event: '{cycles,cache-misses}' Run 'perf list' for a list of valid events

but:

[acme@felicio ~]$ perf stat -e [cycles,instructions] ls perf stat -e [cycles,instructions] ls
invalid or unsupported event: '[cycles,instructions]' Run 'perf list' for a list of valid events

[acme@felicio ~]$ perf stat -e [cycles,cache-misses]:u ls invalid or unsupported event: '[cycles,cache-misses]:u' Run 'perf list' for a list of valid events

And:

[acme@felicio ~]$ perf stat -e (cycles,cache-misses) ls
bash: syntax error near unexpected token `(' [acme@felicio ~]$

- Arnaldo

#9 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

In reply to Ingo Molnar
Quoted 6 lines · Ingo Molnar
Quoted 1 line · Peter Zijlstra

Now the disadvantage is that {} needs quotes on bash, [...]

Not on my version of Bash:

Quoted 8 lines

earth5:~> perf stat {}
{}: No such file or directory
Terminated
earth5:~> perf stat true {}

Performance counter stats for 'true {}':

it's passed in literally, AFAICS.

# echo {}
{}
# echo {en,dis}able
enable disable

It somehow special cases {}, which is horrible.

#10 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 7 lines · Peter Zijlstra
# echo {}
{}
# echo {en,dis}able
enable disable

It somehow special cases {}, which is horrible.

Oh, indeed: brace expansion and sequence expressions both use curly braces:

 $ echo foo-{a,b,c}-bar
 foo-a-bar foo-b-bar foo-c-bar
 $ echo {1..10}
 1 2 3 4 5 6 7 8 9 10

Too bad, it would be rather intuitive. All the brace characters are taken by Bash.

Maybe something like:

 $ echo /minor-faults,major-faults/
 /minor-faults,major-faults/

although it looks a bit weird.

So ... how about using another grouping operator, such as '+'?

Something like:

-e minor-faults+major-faults

While when comma separated they are not grouped, or so.

?

Thanks,

Ingo

#11 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 29 lines · Ingo Molnar
Quoted 7 lines · Peter Zijlstra
# echo {}
{}
# echo {en,dis}able
enable disable

It somehow special cases {}, which is horrible.

Oh, indeed: brace expansion and sequence expressions both use curly braces:

 $ echo foo-{a,b,c}-bar
 foo-a-bar foo-b-bar foo-c-bar
 $ echo {1..10}
 1 2 3 4 5 6 7 8 9 10

Too bad, it would be rather intuitive. All the brace characters are taken by Bash.

Maybe something like:

 $ echo /minor-faults,major-faults/
 /minor-faults,major-faults/

although it looks a bit weird.

Quoted 7 lines

So ... how about using another grouping operator, such as '+'?

Something like:

-e minor-faults+major-faults

While when comma separated they are not grouped, or so.

I would much prefer a syntax that's more natural but requires quoting than one that's quirky and tailor made to avoid whatever current bash does. For one, there's other shells out there that might have different quoting needs and bash is of course free to extend its syntax.

#12 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 36 lines · Peter Zijlstra
Quoted 35 lines · Ingo Molnar
Quoted 7 lines · Peter Zijlstra
# echo {}
{}
# echo {en,dis}able
enable disable

It somehow special cases {}, which is horrible.

Oh, indeed: brace expansion and sequence expressions both use curly braces:

 $ echo foo-{a,b,c}-bar
 foo-a-bar foo-b-bar foo-c-bar
 $ echo {1..10}
 1 2 3 4 5 6 7 8 9 10

Too bad, it would be rather intuitive. All the brace characters are taken by Bash.

Maybe something like:

 $ echo /minor-faults,major-faults/
 /minor-faults,major-faults/

although it looks a bit weird.

So ... how about using another grouping operator, such as '+'?

Something like:

-e minor-faults+major-faults

Quoted 7 lines
Quoted 1 line

While when comma separated they are not grouped, or so.

I would much prefer a syntax that's more natural but requires quoting than one that's quirky and tailor made to avoid whatever current bash does. For one, there's other shells out there that might have different quoting needs and bash is of course free to extend its syntax.

Well, they are unlikely to extend to '+', it would break a boatload of scripts I suspect.

So the question would be, is a+b+c as event grouping a natural syntax? If not then lets use a quoted one that is.

Thanks,

Ingo

#13 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Em Wed, Mar 21, 2012 at 01:15:10PM +0100, Ingo Molnar escreveu:

Quoted 7 lines · Peter Zijlstra
Quoted 5 lines

I would much prefer a syntax that's more natural but requires quoting than one that's quirky and tailor made to avoid whatever current bash does. For one, there's other shells out there that might have different quoting needs and bash is of course free to extend its syntax.

Quoted 5 lines

Well, they are unlikely to extend to '+', it would break a boatload of scripts I suspect.

So the question would be, is a+b+c as event grouping a natural syntax? If not then lets use a quoted one that is.

-e groupname=event1,event2,event3

Seems intuitive, no?

- Arnaldo

#14 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 11 lines · Arnaldo Carvalho de Melo

Em Wed, Mar 21, 2012 at 01:15:10PM +0100, Ingo Molnar escreveu:

Quoted 10 lines · Peter Zijlstra
Quoted 5 lines

I would much prefer a syntax that's more natural but requires quoting than one that's quirky and tailor made to avoid whatever current bash does. For one, there's other shells out there that might have different quoting needs and bash is of course free to extend its syntax.

Well, they are unlikely to extend to '+', it would break a boatload of scripts I suspect.

Quoted 6 lines
Quoted 2 lines

So the question would be, is a+b+c as event grouping a natural syntax? If not then lets use a quoted one that is.

-e groupname=event1,event2,event3

Seems intuitive, no?

Hm, if there's no use for 'groupname' later on then it's a needlessly unspecified dimension. If this variant is picked then I'd suggest to make it a fixed:

-e group=event1,event2,event3

kind of thing instead.

Thanks,

Ingo

#15 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Em Thu, Mar 22, 2012 at 08:56:34AM +0100, Ingo Molnar escreveu:

Quoted 21 lines · Arnaldo Carvalho de Melo
Quoted 12 lines

Em Wed, Mar 21, 2012 at 01:15:10PM +0100, Ingo Molnar escreveu:

Quoted 10 lines · Peter Zijlstra
Quoted 5 lines

I would much prefer a syntax that's more natural but requires quoting than one that's quirky and tailor made to avoid whatever current bash does. For one, there's other shells out there that might have different quoting needs and bash is of course free to extend its syntax.

Well, they are unlikely to extend to '+', it would break a boatload of scripts I suspect.

Quoted 6 lines
Quoted 2 lines

So the question would be, is a+b+c as event grouping a natural syntax? If not then lets use a quoted one that is.

-e groupname=event1,event2,event3

Seems intuitive, no?

Quoted 7 lines

Hm, if there's no use for 'groupname' later on then it's a needlessly unspecified dimension. If this variant is picked then I'd suggest to make it a fixed:

-e group=event1,event2,event3

kind of thing instead.

Jiri mentioned a use for the group name, no?

- Arnaldo

#16 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 26 lines · Arnaldo Carvalho de Melo

Em Thu, Mar 22, 2012 at 08:56:34AM +0100, Ingo Molnar escreveu:

Quoted 25 lines · Arnaldo Carvalho de Melo
Quoted 12 lines

Em Wed, Mar 21, 2012 at 01:15:10PM +0100, Ingo Molnar escreveu:

Quoted 10 lines · Peter Zijlstra
Quoted 5 lines

I would much prefer a syntax that's more natural but requires quoting than one that's quirky and tailor made to avoid whatever current bash does. For one, there's other shells out there that might have different quoting needs and bash is of course free to extend its syntax.

Well, they are unlikely to extend to '+', it would break a boatload of scripts I suspect.

Quoted 6 lines
Quoted 2 lines

So the question would be, is a+b+c as event grouping a natural syntax? If not then lets use a quoted one that is.

-e groupname=event1,event2,event3

Seems intuitive, no?

Hm, if there's no use for 'groupname' later on then it's a needlessly unspecified dimension. If this variant is picked then I'd suggest to make it a fixed:

Quoted 5 lines
Quoted 3 lines

-e group=event1,event2,event3

kind of thing instead.

Jiri mentioned a use for the group name, no?

Only for perf stat output, right?

Thanks,

Ingo

author
#17 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 29 lines · Ingo Molnar
Quoted 26 lines · Arnaldo Carvalho de Melo

Em Thu, Mar 22, 2012 at 08:56:34AM +0100, Ingo Molnar escreveu:

Quoted 25 lines · Arnaldo Carvalho de Melo
Quoted 12 lines

Em Wed, Mar 21, 2012 at 01:15:10PM +0100, Ingo Molnar escreveu:

Quoted 10 lines · Peter Zijlstra
Quoted 5 lines

I would much prefer a syntax that's more natural but requires quoting than one that's quirky and tailor made to avoid whatever current bash does. For one, there's other shells out there that might have different quoting needs and bash is of course free to extend its syntax.

Well, they are unlikely to extend to '+', it would break a boatload of scripts I suspect.

Quoted 6 lines
Quoted 2 lines

So the question would be, is a+b+c as event grouping a natural syntax? If not then lets use a quoted one that is.

-e groupname=event1,event2,event3

Seems intuitive, no?

Hm, if there's no use for 'groupname' later on then it's a needlessly unspecified dimension. If this variant is picked then I'd suggest to make it a fixed:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

-e group=event1,event2,event3

kind of thing instead.

Jiri mentioned a use for the group name, no?

Only for perf stat output, right?

right.. hm, using it in perf report would need to have it stored in the data file.. not sure thats worth the effort

maybe let's start with 'group:mod=' and we can add later something like 'group/name:mod=' if there's need to see group name in output

I think it's better to have one event syntax for all commands

jirka

author
#18 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

In reply to Peter Zijlstra
Quoted 27 lines · Peter Zijlstra
Quoted 14 lines · Jiri Olsa

Examples (first event in brackets is group leader):

  # 1 group (cpu-clock,task-clock)
  perf record --group -e cpu-clock,task-clock ls
  perf record --group parsed -e cpu-clock,task-clock ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls
Quoted 7 lines
  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults,instructions)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls 

I can't help but dislike the --group/--group parsed thing...

How about something like:

Quoted 8 lines

event_group = "{", events, "}" , [ ":", event_group_mod ]

Such that you can write things like:

perf record -e "{cpu-clock,cache-misses,cache-references}"

perf stat -e "{cpu-clock,cycles},{cpu-clock,cache-misses,cache-references}" perf stat -e "{cpu-clock,cycles}" -e "{cpu-clock,cache-misses,cache-references}"

there should be no problem with that

Quoted 3 lines

I'm not sure what the current behaviour of --group is, if you create a group like this, do they all sample?

I think so.. the --group switch just set the group_fd for the event syscall, nothing else

Quoted 7 lines

If so, we need some option like:

perf record -e "{cpu-clock,cache-misses,cache-references}:1"

to mean, only sample on cpu-clock but use PERF_SAMPLE_READ and PERF_FORMAT_GROUP to read all siblings on every cpu-clock sample.

never happy, are you.. ;) seems like nice feature, will check

Quoted 4 lines

Now the disadvantage is that {} needs quotes on bash, the advantage is that its completely natural on how to construct groups, without weird --group/--group parsed. Also it provides a place for group modifiers.

looks like Arnaldo could use [] but I get different results:

[jolsa@dhcp-26-214 perf]$ ./perf stat -e [cycles,instructions] ls invalid or unsupported event: 't'

need to check.. maybe escaping is small price for good readability, but I'll think about some other way as well.

thanks for comments,
jirka

#19 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 8 lines · Jiri Olsa
Quoted 6 lines

If so, we need some option like:

perf record -e "{cpu-clock,cache-misses,cache-references}:1"

to mean, only sample on cpu-clock but use PERF_SAMPLE_READ and PERF_FORMAT_GROUP to read all siblings on every cpu-clock sample.

never happy, are you.. ;) seems like nice feature, will check

Hehe, its a long standing feature request. Also tglx asked for it only a few days ago.

I just saw a good opportunity ;-)

author
#20 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

In reply to Peter Zijlstra
Quoted 41 lines · Peter Zijlstra
Quoted 14 lines · Jiri Olsa

Examples (first event in brackets is group leader):

  # 1 group (cpu-clock,task-clock)
  perf record --group -e cpu-clock,task-clock ls
  perf record --group parsed -e cpu-clock,task-clock ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls
Quoted 7 lines
  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults,instructions)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls 

I can't help but dislike the --group/--group parsed thing...

How about something like:

event_group = "{", events, "}" , [ ":", event_group_mod ]

Such that you can write things like:

perf record -e "{cpu-clock,cache-misses,cache-references}"

perf stat -e "{cpu-clock,cycles},{cpu-clock,cache-misses,cache-references}" perf stat -e "{cpu-clock,cycles}" -e "{cpu-clock,cache-misses,cache-references}"

I'm not sure what the current behaviour of --group is, if you create a group like this, do they all sample?

If so, we need some option like:

Quoted 8 lines

perf record -e "{cpu-clock,cache-misses,cache-references}:1"

to mean, only sample on cpu-clock but use PERF_SAMPLE_READ and PERF_FORMAT_GROUP to read all siblings on every cpu-clock sample.

Now the disadvantage is that {} needs quotes on bash, the advantage is that its completely natural on how to construct groups, without weird --group/--group parsed. Also it provides a place for group modifiers.

how about '=group' keyword followed by ':' modifier
perf record -e "cpu-clock,cache-misses,cache-references=group:1"

or '=$groupname' and use the group name in output like
perf stat -e "task-clock,context-switches=krava:1" ls

Performance counter stats for 'ls':

1.003695 task-clock/krava # 0.083 CPUs utilized
0 context-switches/krava # 0.000 K/sec

jirka

#21 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Em Wed, Mar 21, 2012 at 11:42:14AM +0100, Jiri Olsa escreveu:

Quoted 15 lines · Peter Zijlstra
Quoted 10 lines

If so, we need some option like:

perf record -e "{cpu-clock,cache-misses,cache-references}:1"

to mean, only sample on cpu-clock but use PERF_SAMPLE_READ and PERF_FORMAT_GROUP to read all siblings on every cpu-clock sample.

Now the disadvantage is that {} needs quotes on bash, the advantage is that its completely natural on how to construct groups, without weird --group/--group parsed. Also it provides a place for group modifiers.

how about '=group' keyword followed by ':' modifier
perf record -e "cpu-clock,cache-misses,cache-references=group:1"

Quoted 8 lines

or '=$groupname' and use the group name in output like
perf stat -e "task-clock,context-switches=krava:1" ls

Performance counter stats for 'ls':

1.003695 task-clock/krava # 0.083 CPUs utilized
0 context-switches/krava # 0.000 K/sec

Humm, interesting, perhaps putting the group name before the =event_list will be more intuitive?

I.e.:

perf stat -e "krava=task-clock,context-switches:1" ls

- Arnaldo

author
#22 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 25 lines · Arnaldo Carvalho de Melo

Em Wed, Mar 21, 2012 at 11:42:14AM +0100, Jiri Olsa escreveu:

Quoted 15 lines · Peter Zijlstra
Quoted 10 lines

If so, we need some option like:

perf record -e "{cpu-clock,cache-misses,cache-references}:1"

to mean, only sample on cpu-clock but use PERF_SAMPLE_READ and PERF_FORMAT_GROUP to read all siblings on every cpu-clock sample.

Now the disadvantage is that {} needs quotes on bash, the advantage is that its completely natural on how to construct groups, without weird --group/--group parsed. Also it provides a place for group modifiers.

how about '=group' keyword followed by ':' modifier
perf record -e "cpu-clock,cache-misses,cache-references=group:1"

Quoted 8 lines

or '=$groupname' and use the group name in output like
perf stat -e "task-clock,context-switches=krava:1" ls

Performance counter stats for 'ls':

1.003695 task-clock/krava # 0.083 CPUs utilized
0 context-switches/krava # 0.000 K/sec

Quoted 6 lines

Humm, interesting, perhaps putting the group name before the =event_list will be more intuitive?

I.e.:

perf stat -e "krava=task-clock,context-switches:1" ls

yep, I put it at the end because of the ':' modifier stuff. We have it for each event, and it'd be in conflict having it at the end..

maybe we could put it like:
perf stat -e "krava:1=task-clock,context-switches" ls

or put some other separator instead of ':' ... but ':' is the best ;)

jirka

#23 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Em Wed, Mar 21, 2012 at 11:27:24PM +0100, Jiri Olsa escreveu:

Quoted 33 lines · Arnaldo Carvalho de Melo
Quoted 25 lines

Em Wed, Mar 21, 2012 at 11:42:14AM +0100, Jiri Olsa escreveu:

Quoted 15 lines · Peter Zijlstra
Quoted 10 lines

If so, we need some option like:

perf record -e "{cpu-clock,cache-misses,cache-references}:1"

to mean, only sample on cpu-clock but use PERF_SAMPLE_READ and PERF_FORMAT_GROUP to read all siblings on every cpu-clock sample.

Now the disadvantage is that {} needs quotes on bash, the advantage is that its completely natural on how to construct groups, without weird --group/--group parsed. Also it provides a place for group modifiers.

how about '=group' keyword followed by ':' modifier
perf record -e "cpu-clock,cache-misses,cache-references=group:1"

Quoted 8 lines

or '=$groupname' and use the group name in output like
perf stat -e "task-clock,context-switches=krava:1" ls

Performance counter stats for 'ls':

1.003695 task-clock/krava # 0.083 CPUs utilized
0 context-switches/krava # 0.000 K/sec

Quoted 6 lines

Humm, interesting, perhaps putting the group name before the =event_list will be more intuitive?

I.e.:

perf stat -e "krava=task-clock,context-switches:1" ls

Quoted 8 lines

yep, I put it at the end because of the ':' modifier stuff. We have it for each event, and it'd be in conflict having it at the end..

maybe we could put it like:
perf stat -e "krava:1=task-clock,context-switches" ls

or put some other separator instead of ':' ... but ':' is the best ;)

That thought crossed my mind as well, indeed it seems more intuitive:
everything before the '=' defines the group wide attributes (name, modifiers, whatevever else we need later) after it, the evlist, where each evsel can have its own attributes.

- Arnaldo

author
#24 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

In reply to Peter Zijlstra
Quoted 39 lines · Peter Zijlstra
Quoted 14 lines · Jiri Olsa

Examples (first event in brackets is group leader):

  # 1 group (cpu-clock,task-clock)
  perf record --group -e cpu-clock,task-clock ls
  perf record --group parsed -e cpu-clock,task-clock ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls
Quoted 7 lines
  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults,instructions)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls 

I can't help but dislike the --group/--group parsed thing...

How about something like:

event_group = "{", events, "}" , [ ":", event_group_mod ]

Such that you can write things like:

perf record -e "{cpu-clock,cache-misses,cache-references}"

perf stat -e "{cpu-clock,cycles},{cpu-clock,cache-misses,cache-references}" perf stat -e "{cpu-clock,cycles}" -e "{cpu-clock,cache-misses,cache-references}"

I'm not sure what the current behaviour of --group is, if you create a group like this, do they all sample?

Quoted 6 lines

If so, we need some option like:

perf record -e "{cpu-clock,cache-misses,cache-references}:1"

to mean, only sample on cpu-clock but use PERF_SAMPLE_READ and PERF_FORMAT_GROUP to read all siblings on every cpu-clock sample.

hi,

I'm looking on how to present this data in perf and it seems we need to reset all siblings once we read/store them (in kernel) to the leader sample.

My current thinking is to store siblings' sum values for each hists entry of the sample (perf report count unit) .. and display them in similar way we display callchains: for each hists entry display the sum value for each sibling.

Could you provide more of your world examples? Your expectations about presenting this..

Maybe we want to make the reset optional, and do some do some other math with siblings' values..?

Thoughts? ;)

thanks,
jirka

#25 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

In reply to Jiri Olsa

Em Tue, Mar 20, 2012 at 07:15:41PM +0100, Jiri Olsa escreveu:

Quoted 11 lines

The current event grouping is basic. If you specify the '--group' option for record/stat/top command, all the specified events become members of a single group with the first event as a group leader.

This patch adds a functionality that allows to create event groups based on the way they are specified on the command line. Extending the '--group/-g' option power while preserving the current behaviour.

It is now possible to use '--group/-g' option with 'parsed' value, which will create groups based on the command line events layout.

Quoted 8 lines

With the '--group/-g parsed' option specified, all events separated by ',' within a single '-e' option now become members of a group with the first event specified as a group leader. Another '-e' option with multiple events creates separate group.

All groups are created with regards to threads and cpus. Thus recording an event group within a 2 threads on server with 4 CPUs will create 8 separate groups.

Why not just set a rule that if -g is present then each -e CSV is a separate group?

- Arnaldo

Quoted 589 lines

Examples (first event in brackets is group leader):

  # 1 group (cpu-clock,task-clock)
  perf record --group -e cpu-clock,task-clock ls
  perf record --group parsed -e cpu-clock,task-clock ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults,instructions)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

Updated automated test to check on group_leader settings.

---
 tools/perf/Makefile            |    2 ++
 tools/perf/builtin-record.c    |    8 +++++---
 tools/perf/builtin-stat.c      |   10 ++++++----
 tools/perf/builtin-test.c      |   10 ++++++++++
 tools/perf/builtin-top.c       |    8 +++++---
 tools/perf/perf.h              |    3 ++-
 tools/perf/util/evlist.c       |    4 ++--
 tools/perf/util/evlist.h       |    3 ++-
 tools/perf/util/evsel.c        |   32 +++++++++++++++++++++++++-------
 tools/perf/util/evsel.h        |    9 ++++++---
 tools/perf/util/group.c        |   22 ++++++++++++++++++++++
 tools/perf/util/group.h        |   32 ++++++++++++++++++++++++++++++++
 tools/perf/util/parse-events.c |   18 ++++++++++++++++++
 tools/perf/util/parse-events.h |    3 ---
 tools/perf/util/python.c       |    4 ++++
 tools/perf/util/top.h          |    2 +-
 16 files changed, 142 insertions(+), 28 deletions(-)
 create mode 100644 tools/perf/util/group.c
 create mode 100644 tools/perf/util/group.h
tools/perf/Makefile +2 -0
index b492e3a..ea10b29 100644
@@ -317,6 +317,7 @@ LIB_H += util/cpumap.h
 LIB_H += util/top.h
 LIB_H += $(ARCH_INCLUDE)
 LIB_H += util/cgroup.h
+LIB_H += util/group.h
 
 LIB_OBJS += $(OUTPUT)util/abspath.o
 LIB_OBJS += $(OUTPUT)util/alias.o
@@ -378,6 +379,7 @@ LIB_OBJS += $(OUTPUT)util/util.o
 LIB_OBJS += $(OUTPUT)util/xyarray.o
 LIB_OBJS += $(OUTPUT)util/cpumap.o
 LIB_OBJS += $(OUTPUT)util/cgroup.o
+LIB_OBJS += $(OUTPUT)util/group.o
 
 BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
 
tools/perf/builtin-record.c +5 -3
index be4e1ee..a2c7dc2 100644
@@ -26,6 +26,7 @@
 #include "util/symbol.h"
 #include "util/cpumap.h"
 #include "util/thread_map.h"
+#include "util/group.h"
 
 #include <unistd.h>
 #include <sched.h>
@@ -203,7 +204,7 @@ static void perf_record__open(struct perf_record *rec)
 		 */
 		bool time_needed = attr->sample_type & PERF_SAMPLE_TIME;
 
-		if (opts->group && pos != first)
+		if (group_is_single(opts->group) && pos != first)
 			group_fd = first->fd;
 fallback_missing_features:
 		if (opts->exclude_guest_missing)
@@ -791,8 +792,9 @@ const struct option record_options[] = {
 	OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
 	OPT_UINTEGER('m', "mmap-pages", &record.opts.mmap_pages,
 		     "number of mmap data pages"),
-	OPT_BOOLEAN(0, "group", &record.opts.group,
-		    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT(0, "group", &record.opts.group, "[parsed]",
+		    "put the counters into a counter group",
+		     group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('g', "call-graph", &record.opts.call_graph,
 		    "do call-graph (stack chain/backtrace) recording"),
 	OPT_INCR('v', "verbose", &verbose,
tools/perf/builtin-stat.c +6 -4
index c941bb6..d0d3859 100644
@@ -55,6 +55,7 @@
 #include "util/cpumap.h"
 #include "util/thread.h"
 #include "util/thread_map.h"
+#include "util/group.h"
 
 #include <sys/prctl.h>
 #include <math.h>
@@ -193,7 +194,7 @@ static int			big_num_opt			=  -1;
 static const char		*cpu_list;
 static const char		*csv_sep			= NULL;
 static bool			csv_output			= false;
-static bool			group				= false;
+static enum perf_group_opt	group;
 static const char		*output_name			= NULL;
 static FILE			*output				= NULL;
 static int			output_fd;
@@ -284,7 +285,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel,
 	struct perf_event_attr *attr = &evsel->attr;
 	struct xyarray *group_fd = NULL;
 
-	if (group && evsel != first)
+	if (group_is_single(group) && evsel != first)
 		group_fd = first->fd;
 
 	if (scale)
@@ -1055,8 +1056,9 @@ static const struct option options[] = {
 		   "stat events on existing thread id"),
 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
 		    "system-wide collection from all CPUs"),
-	OPT_BOOLEAN('g', "group", &group,
-		    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT('g', "group", &group, "[parsed]",
+		    "put the counters into a counter group",
+		     group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('c', "scale", &scale,
 		    "scale/normalize counters"),
 	OPT_INCR('v', "verbose", &verbose,
tools/perf/builtin-test.c +10 -0
index 1c5b980..babdd77 100644
@@ -14,6 +14,7 @@
 #include "util/symbol.h"
 #include "util/thread_map.h"
 #include "util/pmu.h"
+#include "util/group.h"
 #include "../../include/linux/hw_breakpoint.h"
 
 #include <sys/mman.h>
@@ -948,11 +949,14 @@ static int test__checkevent_pmu(struct perf_evlist *evlist)
 static int test__checkevent_list(struct perf_evlist *evlist)
 {
 	struct perf_evsel *evsel;
+	struct perf_evsel *parsed_leader;
 
 	TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
 
 	/* r1 */
 	evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
+	parsed_leader = evsel;
+
 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
 	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
 	TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
@@ -961,6 +965,7 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader", parsed_leader);
 
 	/* syscalls:sys_enter_open:k */
 	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
@@ -973,6 +978,8 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader",
+			evsel->parsed_leader == parsed_leader);
 
 	/* 1:1:hp */
 	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
@@ -982,6 +989,8 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader",
+			evsel->parsed_leader == parsed_leader);
 
 	return 0;
 }
@@ -1168,6 +1177,7 @@ static int test__PERF_RECORD(void)
 		.no_delay   = true,
 		.freq	    = 10,
 		.mmap_pages = 256,
+		.group      = PERF_GROUP_NONE,
 	};
 	cpu_set_t *cpu_mask = NULL;
 	size_t cpu_mask_size = 0;
tools/perf/builtin-top.c +5 -3
index e3c63ae..fd04361 100644
@@ -38,6 +38,7 @@
 #include "util/cpumap.h"
 #include "util/xyarray.h"
 #include "util/sort.h"
+#include "util/group.h"
 
 #include "util/debug.h"
 
@@ -850,7 +851,7 @@ static void perf_top__start_counters(struct perf_top *top)
 		struct perf_event_attr *attr = &counter->attr;
 		struct xyarray *group_fd = NULL;
 
-		if (top->group && counter != first)
+		if (group_is_single(top->group) && counter != first)
 			group_fd = first->fd;
 
 		attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
@@ -1153,8 +1154,9 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 			    "dump the symbol table used for profiling"),
 	OPT_INTEGER('f', "count-filter", &top.count_filter,
 		    "only display functions with more events than this"),
-	OPT_BOOLEAN('g', "group", &top.group,
-			    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT('g', "group", &top.group, "[parsed]",
+		    "put the counters into a counter group",
+		    group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('i', "inherit", &top.inherit,
 		    "child tasks inherit counters"),
 	OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
tools/perf/perf.h +2 -1
index 89e3355..b80b69d 100644
@@ -108,6 +108,7 @@ void get_term_dimensions(struct winsize *ws);
 
 #include "../../include/linux/perf_event.h"
 #include "util/types.h"
+#include "util/group.h"
 #include <stdbool.h>
 
 struct perf_mmap {
@@ -212,7 +213,6 @@ struct perf_record_opts {
 	const char   *target_tid;
 	uid_t	     uid;
 	bool	     call_graph;
-	bool	     group;
 	bool	     inherit_stat;
 	bool	     no_delay;
 	bool	     no_inherit;
@@ -232,6 +232,7 @@ struct perf_record_opts {
 	u64	     default_interval;
 	u64	     user_interval;
 	const char   *cpu_list;
+	enum perf_group_opt group;
 };
 
 #endif
tools/perf/util/evlist.c +2 -2
index 1986d80..c0bcffd 100644
@@ -740,7 +740,7 @@ void perf_evlist__set_selected(struct perf_evlist *evlist,
 	evlist->selected = evsel;
 }
 
-int perf_evlist__open(struct perf_evlist *evlist, bool group)
+int perf_evlist__open(struct perf_evlist *evlist, enum perf_group_opt group)
 {
 	struct perf_evsel *evsel, *first;
 	int err, ncpus, nthreads;
@@ -750,7 +750,7 @@ int perf_evlist__open(struct perf_evlist *evlist, bool group)
 	list_for_each_entry(evsel, &evlist->entries, node) {
 		struct xyarray *group_fd = NULL;
 
-		if (group && evsel != first)
+		if (group_is_single(group) && evsel != first)
 			group_fd = first->fd;
 
 		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads,
tools/perf/util/evlist.h +2 -1
index 21f1c9e..3115e8d 100644
@@ -6,6 +6,7 @@
 #include "../perf.h"
 #include "event.h"
 #include "util.h"
+#include "group.h"
 #include <unistd.h>
 
 struct pollfd;
@@ -78,7 +79,7 @@ struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id);
 
 union perf_event *perf_evlist__mmap_read(struct perf_evlist *self, int idx);
 
-int perf_evlist__open(struct perf_evlist *evlist, bool group);
+int perf_evlist__open(struct perf_evlist *evlist, enum perf_group_opt group);
 
 void perf_evlist__config_attrs(struct perf_evlist *evlist,
 			       struct perf_record_opts *opts);
tools/perf/util/evsel.c +25 -7
index 0221700..50a305d 100644
@@ -14,6 +14,7 @@
 #include "util.h"
 #include "cpumap.h"
 #include "thread_map.h"
+#include "group.h"
 
 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
@@ -136,7 +137,7 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
 	attr->comm = track;
 
 	if (!opts->target_pid && !opts->target_tid && !opts->system_wide &&
-	    (!opts->group || evsel == first)) {
+	    (group_is_none(opts->group) || evsel == first)) {
 		attr->disabled = 1;
 		attr->enable_on_exec = 1;
 	}
@@ -293,8 +294,17 @@ int __perf_evsel__read(struct perf_evsel *evsel,
 	return 0;
 }
 
+static int parsed_leader_fd(struct perf_evsel *evsel,
+			    int cpu, int thread)
+{
+	BUG_ON(!evsel->parsed_leader);
+	/* The evsel->fd xyarray is initialized to -1. */
+	return FD(evsel->parsed_leader, cpu, thread);
+}
+
 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-			      struct thread_map *threads, bool group,
+			      struct thread_map *threads,
+			      enum perf_group_opt group,
 			      struct xyarray *group_fds)
 {
 	int cpu, thread;
@@ -311,13 +321,19 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 	}
 
 	for (cpu = 0; cpu < cpus->nr; cpu++) {
-		int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
+		int group_fd = -1;
+
+		if (group_is_single(group))
+			group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
 
 		for (thread = 0; thread < threads->nr; thread++) {
 
 			if (!evsel->cgrp)
 				pid = threads->map[thread];
 
+			if (group_is_parsed(group))
+				group_fd = parsed_leader_fd(evsel, cpu, thread);
+
 			FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
 								     pid,
 								     cpus->map[cpu],
@@ -327,7 +343,7 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 				goto out_close;
 			}
 
-			if (group && group_fd == -1)
+			if (group_is_single(group) && group_fd == -1)
 				group_fd = FD(evsel, cpu, thread);
 		}
 	}
@@ -372,7 +388,7 @@ static struct {
 };
 
 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-		     struct thread_map *threads, bool group,
+		     struct thread_map *threads, enum perf_group_opt group,
 		     struct xyarray *group_fd)
 {
 	if (cpus == NULL) {
@@ -387,7 +403,8 @@ int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
-			     struct cpu_map *cpus, bool group,
+			     struct cpu_map *cpus,
+			     enum perf_group_opt group,
 			     struct xyarray *group_fd)
 {
 	return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
@@ -395,7 +412,8 @@ int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
 }
 
 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
-				struct thread_map *threads, bool group,
+				struct thread_map *threads,
+				enum perf_group_opt group,
 				struct xyarray *group_fd)
 {
 	return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
tools/perf/util/evsel.h +6 -3
index 3158ca3..ae4023f 100644
@@ -66,6 +66,7 @@ struct perf_evsel {
 		void		*data;
 	} handler;
 	bool 			supported;
+	struct perf_evsel       *parsed_leader;
 };
 
 struct cpu_map;
@@ -91,13 +92,15 @@ void perf_evsel__free_id(struct perf_evsel *evsel);
 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
-			     struct cpu_map *cpus, bool group,
+			     struct cpu_map *cpus,
+			     enum perf_group_opt group,
 			     struct xyarray *group_fds);
 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
-				struct thread_map *threads, bool group,
+				struct thread_map *threads,
+				enum perf_group_opt group,
 				struct xyarray *group_fds);
 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-		     struct thread_map *threads, bool group,
+		     struct thread_map *threads, enum perf_group_opt group,
 		     struct xyarray *group_fds);
 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads);
 
tools/perf/util/group.c new file +22 -0
new file mode 100644
index 0000000..68d18a2
@@ -0,0 +1,22 @@
+#include <linux/compiler.h>
+#include "types.h"
+#include "util.h"
+#include "parse-options.h"
+#include "group.h"
+
+int group_parse(const struct option *opt, const char *str,
+		int unset __used)
+{
+	int *group_opt = (int *) opt->value;
+
+	if (!str)
+		*group_opt = PERF_GROUP_SINGLE;
+	else if (!strcmp(str, "parsed"))
+		*group_opt = PERF_GROUP_PARSED;
+	else {
+		fprintf(stderr, "unknown group option value\n");
+		return -1;
+	}
+
+	return 0;
+}
tools/perf/util/group.h new file +32 -0
new file mode 100644
index 0000000..f97520a
@@ -0,0 +1,32 @@
+#ifndef __GROUP_H__
+#define __GROUP_H__
+
+#include <stdbool.h>
+
+struct option;
+
+enum perf_group_opt {
+	PERF_GROUP_NONE,
+	PERF_GROUP_SINGLE,
+	PERF_GROUP_PARSED
+};
+
+int group_parse(const struct option *opt, const char *str,
+		int unset);
+
+static inline bool group_is_none(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_NONE;
+}
+
+static inline bool group_is_single(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_SINGLE;
+}
+
+static inline bool group_is_parsed(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_PARSED;
+}
+
+#endif /* __GROUP_H__ */
tools/perf/util/parse-events.c +18 -0
index 5b3a0ef..b6bccab98 100644
@@ -747,6 +747,18 @@ int parse_events_modifier(struct list_head *list, char *str)
 	return 0;
 }
 
+static void update_parsed_leader(struct list_head *list)
+{
+	struct perf_evsel *evsel, *parsed_leader = NULL;
+
+	list_for_each_entry(evsel, list, node) {
+		if (!parsed_leader)
+			parsed_leader = evsel;
+
+		evsel->parsed_leader = parsed_leader;
+	}
+}
+
 int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 {
 	LIST_HEAD(list);
@@ -761,6 +773,12 @@ int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 	parse_events__flush_buffer(buffer);
 	parse_events__delete_buffer(buffer);
 
+	/*
+	 * Populate the parsed_leader even if we failed,
+	 * just to have complete data, because who knows.. ;)
+	 */
+	update_parsed_leader(&list);
+
 	if (!ret) {
 		int entries = idx - evlist->nr_entries;
 		perf_evlist__splice_list_tail(evlist, &list, entries);
tools/perf/util/parse-events.h +0 -3
index ca069f8..d6c4d5d 100644
@@ -64,9 +64,6 @@ 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,
 				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,
 			     unsigned long type, unsigned long config,
 			     struct list_head *head_config);
tools/perf/util/python.c +4 -0
index e03b58a..6030758 100644
@@ -7,6 +7,7 @@
 #include "event.h"
 #include "cpumap.h"
 #include "thread_map.h"
+#include "group.h"
 
 /* Define PyVarObject_HEAD_INIT for python 2.5 */
 #ifndef PyVarObject_HEAD_INIT
@@ -828,6 +829,9 @@ static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
 		return NULL;
 
+	if (group)
+		group = PERF_GROUP_SINGLE;
+
 	if (perf_evlist__open(evlist, group) < 0) {
 		PyErr_SetFromErrno(PyExc_OSError);
 		return NULL;
tools/perf/util/top.h +1 -1
index ce61cb2..0d2cf8b 100644
@@ -33,7 +33,7 @@ struct perf_top {
 	bool		   kptr_restrict_warned;
 	bool		   vmlinux_warned;
 	bool		   inherit;
-	bool		   group;
+	enum perf_group_opt group;
 	bool		   sample_id_all_missing;
 	bool		   exclude_guest_missing;
 	bool		   dump_symtab;
Signature · 1 line
1.7.7.6
author
#26 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

Quoted 17 lines · Arnaldo Carvalho de Melo

Em Tue, Mar 20, 2012 at 07:15:41PM +0100, Jiri Olsa escreveu:

Quoted 16 lines

The current event grouping is basic. If you specify the '--group' option for record/stat/top command, all the specified events become members of a single group with the first event as a group leader.

This patch adds a functionality that allows to create event groups based on the way they are specified on the command line. Extending the '--group/-g' option power while preserving the current behaviour.

It is now possible to use '--group/-g' option with 'parsed' value, which will create groups based on the command line events layout.

With the '--group/-g parsed' option specified, all events separated by ',' within a single '-e' option now become members of a group with the first event specified as a group leader. Another '-e' option with multiple events creates separate group.

Quoted 6 lines
Quoted 3 lines

All groups are created with regards to threads and cpus. Thus recording an event group within a 2 threads on server with 4 CPUs will create 8 separate groups.

Why not just set a rule that if -g is present then each -e CSV is a separate group?

hm, for some reason I thought I need to keep the old '-g,--group" interface behaviour intact.. ;) no problem..

jirka

Quoted 592 lines

- Arnaldo

Quoted 589 lines

Examples (first event in brackets is group leader):

  # 1 group (cpu-clock,task-clock)
  perf record --group -e cpu-clock,task-clock ls
  perf record --group parsed -e cpu-clock,task-clock ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults ls

  # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
  perf record --group parsed -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

  # 1 group (cpu-clock,task-clock,minor-faults,major-faults,instructions)
  perf record --group -e cpu-clock,task-clock \
   -e minor-faults,major-faults -e instructions ls

Updated automated test to check on group_leader settings.

---
 tools/perf/Makefile            |    2 ++
 tools/perf/builtin-record.c    |    8 +++++---
 tools/perf/builtin-stat.c      |   10 ++++++----
 tools/perf/builtin-test.c      |   10 ++++++++++
 tools/perf/builtin-top.c       |    8 +++++---
 tools/perf/perf.h              |    3 ++-
 tools/perf/util/evlist.c       |    4 ++--
 tools/perf/util/evlist.h       |    3 ++-
 tools/perf/util/evsel.c        |   32 +++++++++++++++++++++++++-------
 tools/perf/util/evsel.h        |    9 ++++++---
 tools/perf/util/group.c        |   22 ++++++++++++++++++++++
 tools/perf/util/group.h        |   32 ++++++++++++++++++++++++++++++++
 tools/perf/util/parse-events.c |   18 ++++++++++++++++++
 tools/perf/util/parse-events.h |    3 ---
 tools/perf/util/python.c       |    4 ++++
 tools/perf/util/top.h          |    2 +-
 16 files changed, 142 insertions(+), 28 deletions(-)
 create mode 100644 tools/perf/util/group.c
 create mode 100644 tools/perf/util/group.h
tools/perf/Makefile +2 -0
index b492e3a..ea10b29 100644
@@ -317,6 +317,7 @@ LIB_H += util/cpumap.h
 LIB_H += util/top.h
 LIB_H += $(ARCH_INCLUDE)
 LIB_H += util/cgroup.h
+LIB_H += util/group.h
 
 LIB_OBJS += $(OUTPUT)util/abspath.o
 LIB_OBJS += $(OUTPUT)util/alias.o
@@ -378,6 +379,7 @@ LIB_OBJS += $(OUTPUT)util/util.o
 LIB_OBJS += $(OUTPUT)util/xyarray.o
 LIB_OBJS += $(OUTPUT)util/cpumap.o
 LIB_OBJS += $(OUTPUT)util/cgroup.o
+LIB_OBJS += $(OUTPUT)util/group.o
 
 BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
 
tools/perf/builtin-record.c +5 -3
index be4e1ee..a2c7dc2 100644
@@ -26,6 +26,7 @@
 #include "util/symbol.h"
 #include "util/cpumap.h"
 #include "util/thread_map.h"
+#include "util/group.h"
 
 #include <unistd.h>
 #include <sched.h>
@@ -203,7 +204,7 @@ static void perf_record__open(struct perf_record *rec)
 		 */
 		bool time_needed = attr->sample_type & PERF_SAMPLE_TIME;
 
-		if (opts->group && pos != first)
+		if (group_is_single(opts->group) && pos != first)
 			group_fd = first->fd;
 fallback_missing_features:
 		if (opts->exclude_guest_missing)
@@ -791,8 +792,9 @@ const struct option record_options[] = {
 	OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
 	OPT_UINTEGER('m', "mmap-pages", &record.opts.mmap_pages,
 		     "number of mmap data pages"),
-	OPT_BOOLEAN(0, "group", &record.opts.group,
-		    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT(0, "group", &record.opts.group, "[parsed]",
+		    "put the counters into a counter group",
+		     group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('g', "call-graph", &record.opts.call_graph,
 		    "do call-graph (stack chain/backtrace) recording"),
 	OPT_INCR('v', "verbose", &verbose,
tools/perf/builtin-stat.c +6 -4
index c941bb6..d0d3859 100644
@@ -55,6 +55,7 @@
 #include "util/cpumap.h"
 #include "util/thread.h"
 #include "util/thread_map.h"
+#include "util/group.h"
 
 #include <sys/prctl.h>
 #include <math.h>
@@ -193,7 +194,7 @@ static int			big_num_opt			=  -1;
 static const char		*cpu_list;
 static const char		*csv_sep			= NULL;
 static bool			csv_output			= false;
-static bool			group				= false;
+static enum perf_group_opt	group;
 static const char		*output_name			= NULL;
 static FILE			*output				= NULL;
 static int			output_fd;
@@ -284,7 +285,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel,
 	struct perf_event_attr *attr = &evsel->attr;
 	struct xyarray *group_fd = NULL;
 
-	if (group && evsel != first)
+	if (group_is_single(group) && evsel != first)
 		group_fd = first->fd;
 
 	if (scale)
@@ -1055,8 +1056,9 @@ static const struct option options[] = {
 		   "stat events on existing thread id"),
 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
 		    "system-wide collection from all CPUs"),
-	OPT_BOOLEAN('g', "group", &group,
-		    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT('g', "group", &group, "[parsed]",
+		    "put the counters into a counter group",
+		     group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('c', "scale", &scale,
 		    "scale/normalize counters"),
 	OPT_INCR('v', "verbose", &verbose,
tools/perf/builtin-test.c +10 -0
index 1c5b980..babdd77 100644
@@ -14,6 +14,7 @@
 #include "util/symbol.h"
 #include "util/thread_map.h"
 #include "util/pmu.h"
+#include "util/group.h"
 #include "../../include/linux/hw_breakpoint.h"
 
 #include <sys/mman.h>
@@ -948,11 +949,14 @@ static int test__checkevent_pmu(struct perf_evlist *evlist)
 static int test__checkevent_list(struct perf_evlist *evlist)
 {
 	struct perf_evsel *evsel;
+	struct perf_evsel *parsed_leader;
 
 	TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
 
 	/* r1 */
 	evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
+	parsed_leader = evsel;
+
 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
 	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
 	TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
@@ -961,6 +965,7 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader", parsed_leader);
 
 	/* syscalls:sys_enter_open:k */
 	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
@@ -973,6 +978,8 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader",
+			evsel->parsed_leader == parsed_leader);
 
 	/* 1:1:hp */
 	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
@@ -982,6 +989,8 @@ static int test__checkevent_list(struct perf_evlist *evlist)
 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader",
+			evsel->parsed_leader == parsed_leader);
 
 	return 0;
 }
@@ -1168,6 +1177,7 @@ static int test__PERF_RECORD(void)
 		.no_delay   = true,
 		.freq	    = 10,
 		.mmap_pages = 256,
+		.group      = PERF_GROUP_NONE,
 	};
 	cpu_set_t *cpu_mask = NULL;
 	size_t cpu_mask_size = 0;
tools/perf/builtin-top.c +5 -3
index e3c63ae..fd04361 100644
@@ -38,6 +38,7 @@
 #include "util/cpumap.h"
 #include "util/xyarray.h"
 #include "util/sort.h"
+#include "util/group.h"
 
 #include "util/debug.h"
 
@@ -850,7 +851,7 @@ static void perf_top__start_counters(struct perf_top *top)
 		struct perf_event_attr *attr = &counter->attr;
 		struct xyarray *group_fd = NULL;
 
-		if (top->group && counter != first)
+		if (group_is_single(top->group) && counter != first)
 			group_fd = first->fd;
 
 		attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
@@ -1153,8 +1154,9 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 			    "dump the symbol table used for profiling"),
 	OPT_INTEGER('f', "count-filter", &top.count_filter,
 		    "only display functions with more events than this"),
-	OPT_BOOLEAN('g', "group", &top.group,
-			    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT('g', "group", &top.group, "[parsed]",
+		    "put the counters into a counter group",
+		    group_parse, PERF_GROUP_NONE),
 	OPT_BOOLEAN('i', "inherit", &top.inherit,
 		    "child tasks inherit counters"),
 	OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
tools/perf/perf.h +2 -1
index 89e3355..b80b69d 100644
@@ -108,6 +108,7 @@ void get_term_dimensions(struct winsize *ws);
 
 #include "../../include/linux/perf_event.h"
 #include "util/types.h"
+#include "util/group.h"
 #include <stdbool.h>
 
 struct perf_mmap {
@@ -212,7 +213,6 @@ struct perf_record_opts {
 	const char   *target_tid;
 	uid_t	     uid;
 	bool	     call_graph;
-	bool	     group;
 	bool	     inherit_stat;
 	bool	     no_delay;
 	bool	     no_inherit;
@@ -232,6 +232,7 @@ struct perf_record_opts {
 	u64	     default_interval;
 	u64	     user_interval;
 	const char   *cpu_list;
+	enum perf_group_opt group;
 };
 
 #endif
tools/perf/util/evlist.c +2 -2
index 1986d80..c0bcffd 100644
@@ -740,7 +740,7 @@ void perf_evlist__set_selected(struct perf_evlist *evlist,
 	evlist->selected = evsel;
 }
 
-int perf_evlist__open(struct perf_evlist *evlist, bool group)
+int perf_evlist__open(struct perf_evlist *evlist, enum perf_group_opt group)
 {
 	struct perf_evsel *evsel, *first;
 	int err, ncpus, nthreads;
@@ -750,7 +750,7 @@ int perf_evlist__open(struct perf_evlist *evlist, bool group)
 	list_for_each_entry(evsel, &evlist->entries, node) {
 		struct xyarray *group_fd = NULL;
 
-		if (group && evsel != first)
+		if (group_is_single(group) && evsel != first)
 			group_fd = first->fd;
 
 		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads,
tools/perf/util/evlist.h +2 -1
index 21f1c9e..3115e8d 100644
@@ -6,6 +6,7 @@
 #include "../perf.h"
 #include "event.h"
 #include "util.h"
+#include "group.h"
 #include <unistd.h>
 
 struct pollfd;
@@ -78,7 +79,7 @@ struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id);
 
 union perf_event *perf_evlist__mmap_read(struct perf_evlist *self, int idx);
 
-int perf_evlist__open(struct perf_evlist *evlist, bool group);
+int perf_evlist__open(struct perf_evlist *evlist, enum perf_group_opt group);
 
 void perf_evlist__config_attrs(struct perf_evlist *evlist,
 			       struct perf_record_opts *opts);
tools/perf/util/evsel.c +25 -7
index 0221700..50a305d 100644
@@ -14,6 +14,7 @@
 #include "util.h"
 #include "cpumap.h"
 #include "thread_map.h"
+#include "group.h"
 
 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
@@ -136,7 +137,7 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
 	attr->comm = track;
 
 	if (!opts->target_pid && !opts->target_tid && !opts->system_wide &&
-	    (!opts->group || evsel == first)) {
+	    (group_is_none(opts->group) || evsel == first)) {
 		attr->disabled = 1;
 		attr->enable_on_exec = 1;
 	}
@@ -293,8 +294,17 @@ int __perf_evsel__read(struct perf_evsel *evsel,
 	return 0;
 }
 
+static int parsed_leader_fd(struct perf_evsel *evsel,
+			    int cpu, int thread)
+{
+	BUG_ON(!evsel->parsed_leader);
+	/* The evsel->fd xyarray is initialized to -1. */
+	return FD(evsel->parsed_leader, cpu, thread);
+}
+
 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-			      struct thread_map *threads, bool group,
+			      struct thread_map *threads,
+			      enum perf_group_opt group,
 			      struct xyarray *group_fds)
 {
 	int cpu, thread;
@@ -311,13 +321,19 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 	}
 
 	for (cpu = 0; cpu < cpus->nr; cpu++) {
-		int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
+		int group_fd = -1;
+
+		if (group_is_single(group))
+			group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
 
 		for (thread = 0; thread < threads->nr; thread++) {
 
 			if (!evsel->cgrp)
 				pid = threads->map[thread];
 
+			if (group_is_parsed(group))
+				group_fd = parsed_leader_fd(evsel, cpu, thread);
+
 			FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
 								     pid,
 								     cpus->map[cpu],
@@ -327,7 +343,7 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 				goto out_close;
 			}
 
-			if (group && group_fd == -1)
+			if (group_is_single(group) && group_fd == -1)
 				group_fd = FD(evsel, cpu, thread);
 		}
 	}
@@ -372,7 +388,7 @@ static struct {
 };
 
 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-		     struct thread_map *threads, bool group,
+		     struct thread_map *threads, enum perf_group_opt group,
 		     struct xyarray *group_fd)
 {
 	if (cpus == NULL) {
@@ -387,7 +403,8 @@ int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
-			     struct cpu_map *cpus, bool group,
+			     struct cpu_map *cpus,
+			     enum perf_group_opt group,
 			     struct xyarray *group_fd)
 {
 	return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
@@ -395,7 +412,8 @@ int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
 }
 
 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
-				struct thread_map *threads, bool group,
+				struct thread_map *threads,
+				enum perf_group_opt group,
 				struct xyarray *group_fd)
 {
 	return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
tools/perf/util/evsel.h +6 -3
index 3158ca3..ae4023f 100644
@@ -66,6 +66,7 @@ struct perf_evsel {
 		void		*data;
 	} handler;
 	bool 			supported;
+	struct perf_evsel       *parsed_leader;
 };
 
 struct cpu_map;
@@ -91,13 +92,15 @@ void perf_evsel__free_id(struct perf_evsel *evsel);
 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
-			     struct cpu_map *cpus, bool group,
+			     struct cpu_map *cpus,
+			     enum perf_group_opt group,
 			     struct xyarray *group_fds);
 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
-				struct thread_map *threads, bool group,
+				struct thread_map *threads,
+				enum perf_group_opt group,
 				struct xyarray *group_fds);
 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-		     struct thread_map *threads, bool group,
+		     struct thread_map *threads, enum perf_group_opt group,
 		     struct xyarray *group_fds);
 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads);
 
tools/perf/util/group.c new file +22 -0
new file mode 100644
index 0000000..68d18a2
@@ -0,0 +1,22 @@
+#include <linux/compiler.h>
+#include "types.h"
+#include "util.h"
+#include "parse-options.h"
+#include "group.h"
+
+int group_parse(const struct option *opt, const char *str,
+		int unset __used)
+{
+	int *group_opt = (int *) opt->value;
+
+	if (!str)
+		*group_opt = PERF_GROUP_SINGLE;
+	else if (!strcmp(str, "parsed"))
+		*group_opt = PERF_GROUP_PARSED;
+	else {
+		fprintf(stderr, "unknown group option value\n");
+		return -1;
+	}
+
+	return 0;
+}
tools/perf/util/group.h new file +32 -0
new file mode 100644
index 0000000..f97520a
@@ -0,0 +1,32 @@
+#ifndef __GROUP_H__
+#define __GROUP_H__
+
+#include <stdbool.h>
+
+struct option;
+
+enum perf_group_opt {
+	PERF_GROUP_NONE,
+	PERF_GROUP_SINGLE,
+	PERF_GROUP_PARSED
+};
+
+int group_parse(const struct option *opt, const char *str,
+		int unset);
+
+static inline bool group_is_none(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_NONE;
+}
+
+static inline bool group_is_single(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_SINGLE;
+}
+
+static inline bool group_is_parsed(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_PARSED;
+}
+
+#endif /* __GROUP_H__ */
tools/perf/util/parse-events.c +18 -0
index 5b3a0ef..b6bccab98 100644
@@ -747,6 +747,18 @@ int parse_events_modifier(struct list_head *list, char *str)
 	return 0;
 }
 
+static void update_parsed_leader(struct list_head *list)
+{
+	struct perf_evsel *evsel, *parsed_leader = NULL;
+
+	list_for_each_entry(evsel, list, node) {
+		if (!parsed_leader)
+			parsed_leader = evsel;
+
+		evsel->parsed_leader = parsed_leader;
+	}
+}
+
 int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 {
 	LIST_HEAD(list);
@@ -761,6 +773,12 @@ int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
 	parse_events__flush_buffer(buffer);
 	parse_events__delete_buffer(buffer);
 
+	/*
+	 * Populate the parsed_leader even if we failed,
+	 * just to have complete data, because who knows.. ;)
+	 */
+	update_parsed_leader(&list);
+
 	if (!ret) {
 		int entries = idx - evlist->nr_entries;
 		perf_evlist__splice_list_tail(evlist, &list, entries);
tools/perf/util/parse-events.h +0 -3
index ca069f8..d6c4d5d 100644
@@ -64,9 +64,6 @@ 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,
 				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,
 			     unsigned long type, unsigned long config,
 			     struct list_head *head_config);
tools/perf/util/python.c +4 -0
index e03b58a..6030758 100644
@@ -7,6 +7,7 @@
 #include "event.h"
 #include "cpumap.h"
 #include "thread_map.h"
+#include "group.h"
 
 /* Define PyVarObject_HEAD_INIT for python 2.5 */
 #ifndef PyVarObject_HEAD_INIT
@@ -828,6 +829,9 @@ static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
 		return NULL;
 
+	if (group)
+		group = PERF_GROUP_SINGLE;
+
 	if (perf_evlist__open(evlist, group) < 0) {
 		PyErr_SetFromErrno(PyExc_OSError);
 		return NULL;
tools/perf/util/top.h +1 -1
index ce61cb2..0d2cf8b 100644
@@ -33,7 +33,7 @@ struct perf_top {
 	bool		   kptr_restrict_warned;
 	bool		   vmlinux_warned;
 	bool		   inherit;
-	bool		   group;
+	enum perf_group_opt group;
 	bool		   sample_id_all_missing;
 	bool		   exclude_guest_missing;
 	bool		   dump_symtab;
Signature · 1 line
1.7.7.6
#27 lore raw

Re: [PATCH 3/3] perf, tool: Add new event group management

In reply to Jiri Olsa

Hi,

Quoted 141 lines · Jiri Olsa

The current event grouping is basic. If you specify the '--group' option for record/stat/top command, all the specified events become members of a single group with the first event as a group leader.

This patch adds a functionality that allows to create event groups based on the way they are specified on the command line. Extending the '--group/-g' option power while preserving the current behaviour.

It is now possible to use '--group/-g' option with 'parsed' value, which will create groups based on the command line events layout.

With the '--group/-g parsed' option specified, all events separated by ',' within a single '-e' option now become members of a group with the first event specified as a group leader. Another '-e' option with multiple events creates separate group.

All groups are created with regards to threads and cpus. Thus recording an event group within a 2 threads on server with 4 CPUs will create 8 separate groups.

Examples (first event in brackets is group leader):

   # 1 group (cpu-clock,task-clock)
   perf record --group -e cpu-clock,task-clock ls
   perf record --group parsed -e cpu-clock,task-clock ls

   # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
   perf record --group parsed -e cpu-clock,task-clock \
    -e minor-faults,major-faults ls

   # 1 group (cpu-clock,task-clock,minor-faults,major-faults)
   perf record --group -e cpu-clock,task-clock \
    -e minor-faults,major-faults ls

   # 2 groups (cpu-clock,task-clock) (minor-faults,major-faults)
   perf record --group parsed -e cpu-clock,task-clock \
    -e minor-faults,major-faults -e instructions ls

   # 1 group (cpu-clock,task-clock,minor-faults,major-faults,instructions)
   perf record --group -e cpu-clock,task-clock \
    -e minor-faults,major-faults -e instructions ls

Updated automated test to check on group_leader settings.

---
  tools/perf/Makefile            |    2 ++
  tools/perf/builtin-record.c    |    8 +++++---
  tools/perf/builtin-stat.c      |   10 ++++++----
  tools/perf/builtin-test.c      |   10 ++++++++++
  tools/perf/builtin-top.c       |    8 +++++---
  tools/perf/perf.h              |    3 ++-
  tools/perf/util/evlist.c       |    4 ++--
  tools/perf/util/evlist.h       |    3 ++-
  tools/perf/util/evsel.c        |   32 +++++++++++++++++++++++++-------
  tools/perf/util/evsel.h        |    9 ++++++---
  tools/perf/util/group.c        |   22 ++++++++++++++++++++++
  tools/perf/util/group.h        |   32 ++++++++++++++++++++++++++++++++
  tools/perf/util/parse-events.c |   18 ++++++++++++++++++
  tools/perf/util/parse-events.h |    3 ---
  tools/perf/util/python.c       |    4 ++++
  tools/perf/util/top.h          |    2 +-
  16 files changed, 142 insertions(+), 28 deletions(-)

create mode 100644 tools/perf/util/group.c
create mode 100644 tools/perf/util/group.h

tools/perf/Makefile +2 -0
index b492e3a..ea10b29 100644
@@ -317,6 +317,7 @@ LIB_H += util/cpumap.h
  LIB_H += util/top.h
  LIB_H += $(ARCH_INCLUDE)
  LIB_H += util/cgroup.h
+LIB_H += util/group.h
 
  LIB_OBJS += $(OUTPUT)util/abspath.o
  LIB_OBJS += $(OUTPUT)util/alias.o
@@ -378,6 +379,7 @@ LIB_OBJS += $(OUTPUT)util/util.o
  LIB_OBJS += $(OUTPUT)util/xyarray.o
  LIB_OBJS += $(OUTPUT)util/cpumap.o
  LIB_OBJS += $(OUTPUT)util/cgroup.o
+LIB_OBJS += $(OUTPUT)util/group.o
 
  BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
 
tools/perf/builtin-record.c +5 -3
index be4e1ee..a2c7dc2 100644
@@ -26,6 +26,7 @@
  #include "util/symbol.h"
  #include "util/cpumap.h"
  #include "util/thread_map.h"
+#include "util/group.h"
 
  #include<unistd.h>
  #include<sched.h>
@@ -203,7 +204,7 @@ static void perf_record__open(struct perf_record *rec)
  		 */
  		bool time_needed = attr->sample_type&  PERF_SAMPLE_TIME;
 
-		if (opts->group&&  pos != first)
+		if (group_is_single(opts->group)&&  pos != first)
  			group_fd = first->fd;
  fallback_missing_features:
  		if (opts->exclude_guest_missing)
@@ -791,8 +792,9 @@ const struct option record_options[] = {
  	OPT_UINTEGER('F', "freq",&record.opts.user_freq, "profile at this frequency"),
  	OPT_UINTEGER('m', "mmap-pages",&record.opts.mmap_pages,
  		     "number of mmap data pages"),
-	OPT_BOOLEAN(0, "group",&record.opts.group,
-		    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT(0, "group",&record.opts.group, "[parsed]",
+		    "put the counters into a counter group",
+		     group_parse, PERF_GROUP_NONE),
  	OPT_BOOLEAN('g', "call-graph",&record.opts.call_graph,
  		    "do call-graph (stack chain/backtrace) recording"),
  	OPT_INCR('v', "verbose",&verbose,
tools/perf/builtin-stat.c +2 -1
index c941bb6..d0d3859 100644
@@ -55,6 +55,7 @@
  #include "util/cpumap.h"
  #include "util/thread.h"
  #include "util/thread_map.h"
+#include "util/group.h"
 
  #include<sys/prctl.h>
  #include<math.h>
@@ -193,7 +194,7 @@ static int			big_num_opt			=  -1;
  static const char		*cpu_list;
  static const char		*csv_sep			= NULL;
  static bool			csv_output			= false;
-static bool			group				= false;
+static enum perf_group_opt	group;
  static const char		*output_name			= NULL;
  static FILE			*output				= NULL;
  static int			output_fd;

tools/perf/builtin-stat.c static int create_perf_stat_counter(struct perf_evsel *evsel, · line 289

Quoted 6 lines
tools/perf/builtin-stat.c +1 -1
@@ -284,7 +285,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel,
  	struct perf_event_attr *attr =&evsel->attr;
  	struct xyarray *group_fd = NULL;
 
-	if (group && evsel != first)
+	if (group_is_single(group) && evsel != first)
  		group_fd = first->fd;

I think it'd be better if we had a something like is_group_leader(), so that it can be converted like following:

	if (!is_group_leader(evsel))
		group_fd = evsel->group_leader->fd;
Quoted 88 lines
tools/perf/builtin-stat.c +3 -2
  	if (scale)
@@ -1055,8 +1056,9 @@ static const struct option options[] = {
  		   "stat events on existing thread id"),
  	OPT_BOOLEAN('a', "all-cpus",&system_wide,
  		    "system-wide collection from all CPUs"),
-	OPT_BOOLEAN('g', "group",&group,
-		    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT('g', "group",&group, "[parsed]",
+		    "put the counters into a counter group",
+		     group_parse, PERF_GROUP_NONE),
  	OPT_BOOLEAN('c', "scale",&scale,
  		    "scale/normalize counters"),
  	OPT_INCR('v', "verbose",&verbose,
tools/perf/builtin-test.c +10 -0
index 1c5b980..babdd77 100644
@@ -14,6 +14,7 @@
  #include "util/symbol.h"
  #include "util/thread_map.h"
  #include "util/pmu.h"
+#include "util/group.h"
  #include "../../include/linux/hw_breakpoint.h"
 
  #include<sys/mman.h>
@@ -948,11 +949,14 @@ static int test__checkevent_pmu(struct perf_evlist *evlist)
  static int test__checkevent_list(struct perf_evlist *evlist)
  {
  	struct perf_evsel *evsel;
+	struct perf_evsel *parsed_leader;
 
  	TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
 
  	/* r1 */
  	evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
+	parsed_leader = evsel;
+
  	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  	TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
@@ -961,6 +965,7 @@ static int test__checkevent_list(struct perf_evlist *evlist)
  	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader", parsed_leader);
 
  	/* syscalls:sys_enter_open:k */
  	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
@@ -973,6 +978,8 @@ static int test__checkevent_list(struct perf_evlist *evlist)
  	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader",
+			evsel->parsed_leader == parsed_leader);
 
  	/* 1:1:hp */
  	evsel = list_entry(evsel->node.next, struct perf_evsel, node);
@@ -982,6 +989,8 @@ static int test__checkevent_list(struct perf_evlist *evlist)
  	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
+	TEST_ASSERT_VAL("wrong parsed_leader",
+			evsel->parsed_leader == parsed_leader);
 
  	return 0;
  }
@@ -1168,6 +1177,7 @@ static int test__PERF_RECORD(void)
  		.no_delay   = true,
  		.freq	    = 10,
  		.mmap_pages = 256,
+		.group      = PERF_GROUP_NONE,
  	};
  	cpu_set_t *cpu_mask = NULL;
  	size_t cpu_mask_size = 0;
tools/perf/builtin-top.c +1 -0
index e3c63ae..fd04361 100644
@@ -38,6 +38,7 @@
  #include "util/cpumap.h"
  #include "util/xyarray.h"
  #include "util/sort.h"
+#include "util/group.h"
 
  #include "util/debug.h"
 

tools/perf/builtin-top.c static void perf_top__start_counters(struct perf_top *top) · line 855

Quoted 6 lines
tools/perf/builtin-top.c +1 -1
@@ -850,7 +851,7 @@ static void perf_top__start_counters(struct perf_top *top)
  		struct perf_event_attr *attr = &counter->attr;
  		struct xyarray *group_fd = NULL;
 
-		if (top->group && counter != first)
+		if (group_is_single(top->group) && counter != first)
  			group_fd = first->fd;

Same here.

Quoted 56 lines
tools/perf/builtin-top.c +3 -2
  		attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
@@ -1153,8 +1154,9 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
  			    "dump the symbol table used for profiling"),
  	OPT_INTEGER('f', "count-filter",&top.count_filter,
  		    "only display functions with more events than this"),
-	OPT_BOOLEAN('g', "group",&top.group,
-			    "put the counters into a counter group"),
+	OPT_CALLBACK_DEFAULT('g', "group",&top.group, "[parsed]",
+		    "put the counters into a counter group",
+		    group_parse, PERF_GROUP_NONE),
  	OPT_BOOLEAN('i', "inherit",&top.inherit,
  		    "child tasks inherit counters"),
  	OPT_STRING(0, "sym-annotate",&top.sym_filter, "symbol name",
tools/perf/perf.h +2 -1
index 89e3355..b80b69d 100644
@@ -108,6 +108,7 @@ void get_term_dimensions(struct winsize *ws);
 
  #include "../../include/linux/perf_event.h"
  #include "util/types.h"
+#include "util/group.h"
  #include<stdbool.h>
 
  struct perf_mmap {
@@ -212,7 +213,6 @@ struct perf_record_opts {
  	const char   *target_tid;
  	uid_t	     uid;
  	bool	     call_graph;
-	bool	     group;
  	bool	     inherit_stat;
  	bool	     no_delay;
  	bool	     no_inherit;
@@ -232,6 +232,7 @@ struct perf_record_opts {
  	u64	     default_interval;
  	u64	     user_interval;
  	const char   *cpu_list;
+	enum perf_group_opt group;
  };
 
  #endif
tools/perf/util/evlist.c +1 -1
index 1986d80..c0bcffd 100644
@@ -740,7 +740,7 @@ void perf_evlist__set_selected(struct perf_evlist *evlist,
  	evlist->selected = evsel;
  }
 
-int perf_evlist__open(struct perf_evlist *evlist, bool group)
+int perf_evlist__open(struct perf_evlist *evlist, enum perf_group_opt group)
  {
  	struct perf_evsel *evsel, *first;
  	int err, ncpus, nthreads;

tools/perf/util/evlist.c int perf_evlist__open(struct perf_evlist *evlist, bool group) · line 754

Quoted 6 lines
tools/perf/util/evlist.c +1 -1
@@ -750,7 +750,7 @@ int perf_evlist__open(struct perf_evlist *evlist, bool group)
  	list_for_each_entry(evsel,&evlist->entries, node) {
  		struct xyarray *group_fd = NULL;
 
-		if (group && evsel != first)
+		if (group_is_single(group) && evsel != first)
  			group_fd = first->fd;

Same here.

Quoted 36 lines
tools/perf/util/evlist.c +0 -0
  		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads,
tools/perf/util/evlist.h +2 -1
index 21f1c9e..3115e8d 100644
@@ -6,6 +6,7 @@
  #include "../perf.h"
  #include "event.h"
  #include "util.h"
+#include "group.h"
  #include<unistd.h>
 
  struct pollfd;
@@ -78,7 +79,7 @@ struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id);
 
  union perf_event *perf_evlist__mmap_read(struct perf_evlist *self, int idx);
 
-int perf_evlist__open(struct perf_evlist *evlist, bool group);
+int perf_evlist__open(struct perf_evlist *evlist, enum perf_group_opt group);
 
  void perf_evlist__config_attrs(struct perf_evlist *evlist,
  			       struct perf_record_opts *opts);
tools/perf/util/evsel.c +1 -0
index 0221700..50a305d 100644
@@ -14,6 +14,7 @@
  #include "util.h"
  #include "cpumap.h"
  #include "thread_map.h"
+#include "group.h"
 
  #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))

tools/perf/util/evsel.c void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts, · line 140

Quoted 5 lines
tools/perf/util/evsel.c +1 -1
@@ -136,7 +137,7 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
  	attr->comm = track;
 
  	if (!opts->target_pid && !opts->target_tid && !opts->system_wide &&
-	    (!opts->group || evsel == first)) {
+	    (group_is_none(opts->group) || evsel == first)) {

This can be bit simpler:

	    (is_group_leader(evsel)) {
Quoted 30 lines
tools/perf/util/evsel.c +13 -2
  		attr->disabled = 1;
  		attr->enable_on_exec = 1;
  	}
@@ -293,8 +294,17 @@ int __perf_evsel__read(struct perf_evsel *evsel,
  	return 0;
  }
 
+static int parsed_leader_fd(struct perf_evsel *evsel,
+			    int cpu, int thread)
+{
+	BUG_ON(!evsel->parsed_leader);
+	/* The evsel->fd xyarray is initialized to -1. */
+	return FD(evsel->parsed_leader, cpu, thread);
+}
+
  static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-			      struct thread_map *threads, bool group,
+			      struct thread_map *threads,
+			      enum perf_group_opt group,
  			      struct xyarray *group_fds)
  {
  	int cpu, thread;
@@ -311,13 +321,19 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  	}
 
  	for (cpu = 0; cpu<  cpus->nr; cpu++) {
-		int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
+		int group_fd = -1;
+
+		if (group_is_single(group))

tools/perf/util/evsel.c static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, · line 336

Quoted 10 lines
tools/perf/util/evsel.c +4 -0
+			group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
 
  		for (thread = 0; thread<  threads->nr; thread++) {
 
  			if (!evsel->cgrp)
  				pid = threads->map[thread];
 
+			if (group_is_parsed(group))
+				group_fd = parsed_leader_fd(evsel, cpu, thread);
+

This can be:

			if (!is_group_leader(evsel))
				group_fd = FD(evsel->group_leader, cpu, thread);

Thanks,
Namhyung

Quoted 223 lines
tools/perf/util/evsel.c +6 -4
  			FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  								     pid,
  								     cpus->map[cpu],
@@ -327,7 +343,7 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  				goto out_close;
  			}
 
-			if (group&&  group_fd == -1)
+			if (group_is_single(group)&&  group_fd == -1)
  				group_fd = FD(evsel, cpu, thread);
  		}
  	}
@@ -372,7 +388,7 @@ static struct {
  };
 
  int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-		     struct thread_map *threads, bool group,
+		     struct thread_map *threads, enum perf_group_opt group,
  		     struct xyarray *group_fd)
  {
  	if (cpus == NULL) {
@@ -387,7 +403,8 @@ int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  }
 
  int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
-			     struct cpu_map *cpus, bool group,
+			     struct cpu_map *cpus,
+			     enum perf_group_opt group,
  			     struct xyarray *group_fd)
  {
  	return __perf_evsel__open(evsel, cpus,&empty_thread_map.map, group,
@@ -395,7 +412,8 @@ int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  }
 
  int perf_evsel__open_per_thread(struct perf_evsel *evsel,
-				struct thread_map *threads, bool group,
+				struct thread_map *threads,
+				enum perf_group_opt group,
  				struct xyarray *group_fd)
  {
  	return __perf_evsel__open(evsel,&empty_cpu_map.map, threads, group,
tools/perf/util/evsel.h +6 -3
index 3158ca3..ae4023f 100644
@@ -66,6 +66,7 @@ struct perf_evsel {
  		void		*data;
  	} handler;
  	bool 			supported;
+	struct perf_evsel       *parsed_leader;
  };
 
  struct cpu_map;
@@ -91,13 +92,15 @@ void perf_evsel__free_id(struct perf_evsel *evsel);
  void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
 
  int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
-			     struct cpu_map *cpus, bool group,
+			     struct cpu_map *cpus,
+			     enum perf_group_opt group,
  			     struct xyarray *group_fds);
  int perf_evsel__open_per_thread(struct perf_evsel *evsel,
-				struct thread_map *threads, bool group,
+				struct thread_map *threads,
+				enum perf_group_opt group,
  				struct xyarray *group_fds);
  int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
-		     struct thread_map *threads, bool group,
+		     struct thread_map *threads, enum perf_group_opt group,
  		     struct xyarray *group_fds);
  void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads);
 
tools/perf/util/group.c new file +22 -0
new file mode 100644
index 0000000..68d18a2
@@ -0,0 +1,22 @@
+#include<linux/compiler.h>
+#include "types.h"
+#include "util.h"
+#include "parse-options.h"
+#include "group.h"
+
+int group_parse(const struct option *opt, const char *str,
+		int unset __used)
+{
+	int *group_opt = (int *) opt->value;
+
+	if (!str)
+		*group_opt = PERF_GROUP_SINGLE;
+	else if (!strcmp(str, "parsed"))
+		*group_opt = PERF_GROUP_PARSED;
+	else {
+		fprintf(stderr, "unknown group option value\n");
+		return -1;
+	}
+
+	return 0;
+}
tools/perf/util/group.h new file +32 -0
new file mode 100644
index 0000000..f97520a
@@ -0,0 +1,32 @@
+#ifndef __GROUP_H__
+#define __GROUP_H__
+
+#include<stdbool.h>
+
+struct option;
+
+enum perf_group_opt {
+	PERF_GROUP_NONE,
+	PERF_GROUP_SINGLE,
+	PERF_GROUP_PARSED
+};
+
+int group_parse(const struct option *opt, const char *str,
+		int unset);
+
+static inline bool group_is_none(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_NONE;
+}
+
+static inline bool group_is_single(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_SINGLE;
+}
+
+static inline bool group_is_parsed(enum perf_group_opt group)
+{
+	return group == PERF_GROUP_PARSED;
+}
+
+#endif /* __GROUP_H__ */
tools/perf/util/parse-events.c +18 -0
index 5b3a0ef..b6bccab98 100644
@@ -747,6 +747,18 @@ int parse_events_modifier(struct list_head *list, char *str)
  	return 0;
  }
 
+static void update_parsed_leader(struct list_head *list)
+{
+	struct perf_evsel *evsel, *parsed_leader = NULL;
+
+	list_for_each_entry(evsel, list, node) {
+		if (!parsed_leader)
+			parsed_leader = evsel;
+
+		evsel->parsed_leader = parsed_leader;
+	}
+}
+
  int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
  {
  	LIST_HEAD(list);
@@ -761,6 +773,12 @@ int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
  	parse_events__flush_buffer(buffer);
  	parse_events__delete_buffer(buffer);
 
+	/*
+	 * Populate the parsed_leader even if we failed,
+	 * just to have complete data, because who knows.. ;)
+	 */
+	update_parsed_leader(&list);
+
  	if (!ret) {
  		int entries = idx - evlist->nr_entries;
  		perf_evlist__splice_list_tail(evlist,&list, entries);
tools/perf/util/parse-events.h +0 -3
index ca069f8..d6c4d5d 100644
@@ -64,9 +64,6 @@ 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,
  				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,
  			     unsigned long type, unsigned long config,
  			     struct list_head *head_config);
tools/perf/util/python.c +4 -0
index e03b58a..6030758 100644
@@ -7,6 +7,7 @@
  #include "event.h"
  #include "cpumap.h"
  #include "thread_map.h"
+#include "group.h"
 
  /* Define PyVarObject_HEAD_INIT for python 2.5 */
  #ifndef PyVarObject_HEAD_INIT
@@ -828,6 +829,9 @@ static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,&group))
  		return NULL;
 
+	if (group)
+		group = PERF_GROUP_SINGLE;
+
  	if (perf_evlist__open(evlist, group)<  0) {
  		PyErr_SetFromErrno(PyExc_OSError);
  		return NULL;
tools/perf/util/top.h +1 -1
index ce61cb2..0d2cf8b 100644
@@ -33,7 +33,7 @@ struct perf_top {
  	bool		   kptr_restrict_warned;
  	bool		   vmlinux_warned;
  	bool		   inherit;
-	bool		   group;
+	enum perf_group_opt group;
  	bool		   sample_id_all_missing;
  	bool		   exclude_guest_missing;
  	bool		   dump_symtab;