Skip to contentSkip to conversation

perf parse-events: Specially handle uncore event alias in small groups

PATCH Superseded [email protected] 2 participants 3 messages Apr 25, 2018 – Apr 26, 2018 perf events

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 perf parse-events: Specially handle uncore event alias in small groups

From: Kan Liang <[email protected]>

Perf stat doesn't count the uncore event aliases from the same uncore block in a group, for example:

perf stat -e '{unc_m_cas_count.all,unc_m_clockticks}' -a -I 1000
# time counts unit events

       1.000447342      <not counted>      unc_m_cas_count.all
       1.000447342      <not counted>      unc_m_clockticks
       2.000740654      <not counted>      unc_m_cas_count.all
       2.000740654      <not counted>      unc_m_clockticks

The output is very misleading. It gives a wrong impression that the uncore event doesn't work.

An uncore block could be composed by several PMUs. An uncore event alias is a joint name which means the same event runs on all PMUs of a block. Perf doesn't support mixed events from different PMUs in the same group. It is wrong to put uncore event aliases in a big group.

The right way is to split the big group into multiple small groups which only include the events from the same PMU.
Only uncore event aliases from the same uncore block should be specially handled here. It doesn't make sense to mix the uncore events with other uncore events from different blocks or even core events in a group.

With the patch:
# time counts unit events

     1.001557653            140,833      unc_m_cas_count.all
     1.001557653      1,330,231,332      unc_m_clockticks
     2.002709483             85,007      unc_m_cas_count.all
     2.002709483      1,429,494,563      unc_m_clockticks

---

Change since V2:
- Fix a core dump caused by two exactly same evens in a group. Adding a check in parse_events__set_leader_for_uncore_aliase() to distinguish the leader from other members, even they have the same event name.

 tools/perf/util/evsel.h        |   1 +
 tools/perf/util/parse-events.c | 109 +++++++++++++++++++++++++++++++++++++++--
 tools/perf/util/parse-events.h |   7 ++-
 tools/perf/util/parse-events.y |   8 +--
 4 files changed, 116 insertions(+), 9 deletions(-)
tools/perf/util/evsel.h +1 -0
index 92ec009..b13f5f2 100644
@@ -127,6 +127,7 @@ struct perf_evsel {
 	bool			precise_max;
 	bool			ignore_missing_thread;
 	bool			forced_leader;
+	bool			use_uncore_alias;
 	/* parse modifier helper */
 	int			exclude_GH;
 	int			nr_members;
tools/perf/util/parse-events.c +106 -3

156 lines not on this page · raw message

tools/perf/util/parse-events.h +5 -2
index 5015cfd..4473dac 100644
@@ -167,7 +167,9 @@ int parse_events_add_breakpoint(struct list_head *list, int *idx,
 				void *ptr, char *type, u64 len);
 int parse_events_add_pmu(struct parse_events_state *parse_state,
 			 struct list_head *list, char *name,
-			 struct list_head *head_config, bool auto_merge_stats);
+			 struct list_head *head_config,
+			 bool auto_merge_stats,
+			 bool use_alias);
 
 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
 			       char *str,
@@ -178,7 +180,8 @@ int parse_events_copy_term_list(struct list_head *old,
 
 enum perf_pmu_event_symbol_type
 perf_pmu__parse_check(const char *name);
-void parse_events__set_leader(char *name, struct list_head *list);
+void parse_events__set_leader(char *name, struct list_head *list,
+			      struct parse_events_state *parse_state);
 void parse_events_update_lists(struct list_head *list_event,
 			       struct list_head *list_all);
 void parse_events_evlist_error(struct parse_events_state *parse_state,
tools/perf/util/parse-events.y +4 -4
index d14464c..47f6399 100644
@@ -161,7 +161,7 @@ PE_NAME '{' events '}'
 	struct list_head *list = $3;
 
 	inc_group_count(list, _parse_state);
-	parse_events__set_leader($1, list);
+	parse_events__set_leader($1, list, _parse_state);
 	$$ = list;
 }
 |
@@ -170,7 +170,7 @@ PE_NAME '{' events '}'
 	struct list_head *list = $2;
 
 	inc_group_count(list, _parse_state);
-	parse_events__set_leader(NULL, list);
+	parse_events__set_leader(NULL, list, _parse_state);
 	$$ = list;
 }
 
@@ -232,7 +232,7 @@ PE_NAME '/' event_config '/'
 		YYABORT;
 
 	ALLOC_LIST(list);
-	if (parse_events_add_pmu(_parse_state, list, $1, $3, false)) {
+	if (parse_events_add_pmu(_parse_state, list, $1, $3, false, false)) {
 		struct perf_pmu *pmu = NULL;
 		int ok = 0;
 		char *pattern;
@@ -251,7 +251,7 @@ PE_NAME '/' event_config '/'
 					free(pattern);
 					YYABORT;
 				}
-				if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms, true))
+				if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms, true, false))
 					ok++;
 				parse_events_terms__delete(terms);
 			}
Signature · 1 line
2.7.4
#1 lore raw

On Wed, Apr 25, 2018 at 10:58:43AM -0700, [email protected] wrote:

SNIP

Quoted 22 lines
tools/perf/util/parse-events.c +21 -1
-void parse_events__set_leader(char *name, struct list_head *list)
+/*
+ * Check if the two uncore PMUs are from the same uncore block
+ * The format of the uncore PMU name is uncore_#blockname_#pmuidx
+ */
+static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
+{
+	char *end_a, *end_b;
+
+	end_a = strrchr(pmu_name_a, '_');
+	end_b = strrchr(pmu_name_b, '_');
+
+	if (!end_a || !end_b)
+		return false;
+
+	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
+		return false;
+
+	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
+}
+
+static int

tools/perf/util/parse-events.c int parse_events__modifier_group(struct list_head *list, · line 1380

Quoted 10 lines
tools/perf/util/parse-events.c +10 -0
+parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
+					   struct parse_events_state *parse_state)
+{
+	struct perf_evsel *evsel, *leader;
+	uintptr_t *leaders;
+	bool is_leader = true;
+	int i = 0, nr_pmu = 0, total_members, ret = 0;
+
+	leader = list_entry(list->next, struct perf_evsel, node);
+	evsel = list_entry(list->prev, struct perf_evsel, node);

could you please use list_last_entry and list_first_entry in here?

tools/perf/util/parse-events.c int parse_events__modifier_group(struct list_head *list, · line 1385

Quoted 5 lines
tools/perf/util/parse-events.c +5 -0
+	total_members = evsel->idx - leader->idx + 1;
+
+	leaders = calloc(total_members, sizeof(uintptr_t));
+	if (!leaders)
+		return ret;

returns 0 in here

Quoted 10 lines
tools/perf/util/parse-events.c +10 -0
+
+	 __evlist__for_each_entry(list, evsel) {
+
+		/* Only split the uncore group which members use alias */
+		if (!evsel->use_uncore_alias)
+			goto out;
+
+		/* The events must be from the same uncore block */
+		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
+			goto out;

tools/perf/util/parse-events.c int parse_events__modifier_group(struct list_head *list, · line 1405

Quoted 10 lines
tools/perf/util/parse-events.c +10 -0
+
+		if (!is_leader)
+			continue;
+		/*
+		 * If the event's PMU name starts to repeat, it must be a new
+		 * event. That can be used to distinguish the leader from
+		 * other members, even they have the same event name.
+		 */
+		if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
+			is_leader = false;

setting "is_leader = false" basically breaks the loop, because of that !leader check above, so why continue?

Quoted 9 lines
tools/perf/util/parse-events.c +9 -0
+			continue;
+		}
+		/* The name is always alias name */
+		WARN_ON(strcmp(leader->name, evsel->name));
+
+		leaders[nr_pmu++] = (uintptr_t) evsel;
+	}
+
+	/* only one event alias */

tools/perf/util/parse-events.c int parse_events__modifier_group(struct list_head *list, · line 1424

Quoted 10 lines
tools/perf/util/parse-events.c +10 -0
+	if (nr_pmu == total_members) {
+		parse_state->nr_groups--;
+		goto handled;
+	}
+
+	__evlist__for_each_entry(list, evsel) {
+		if (i >= nr_pmu)
+			i = 0;
+		evsel->leader = (struct perf_evsel *) leaders[i++];
+	}

it's not so obvious from the code how the groups are set.. please describe that logic in the comment

thanks,
jirka

Quoted 15 lines
tools/perf/util/parse-events.c +15 -0
+
+	for (i = 0; i < nr_pmu; i++) {
+		evsel = (struct perf_evsel *) leaders[i];
+		evsel->nr_members = total_members / nr_pmu;
+		evsel->group_name = name ? strdup(name) : NULL;
+	}
+
+	parse_state->nr_groups += nr_pmu - 1;
+
+handled:
+	ret = 1;
+out:
+	free(leaders);
+	return ret;
+}
author
#2 lore raw
Quoted 30 lines · Jiri Olsa

On Wed, Apr 25, 2018 at 10:58:43AM -0700, [email protected] wrote:

SNIP

Quoted 26 lines
tools/perf/util/parse-events.c +25 -1
-void parse_events__set_leader(char *name, struct list_head *list)
+/*
+ * Check if the two uncore PMUs are from the same uncore block
+ * The format of the uncore PMU name is uncore_#blockname_#pmuidx
+ */
+static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
+{
+	char *end_a, *end_b;
+
+	end_a = strrchr(pmu_name_a, '_');
+	end_b = strrchr(pmu_name_b, '_');
+
+	if (!end_a || !end_b)
+		return false;
+
+	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
+		return false;
+
+	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
+}
+
+static int
+parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
+					   struct parse_events_state *parse_state)
+{
+	struct perf_evsel *evsel, *leader;
Quoted 9 lines

tools/perf/util/parse-events.c int parse_events__modifier_group(struct list_head *list, · line 1380

Quoted 6 lines
tools/perf/util/parse-events.c +6 -0
+	uintptr_t *leaders;
+	bool is_leader = true;
+	int i = 0, nr_pmu = 0, total_members, ret = 0;
+
+	leader = list_entry(list->next, struct perf_evsel, node);
+	evsel = list_entry(list->prev, struct perf_evsel, node);

could you please use list_last_entry and list_first_entry in here?

Sure.

Quoted 7 lines

tools/perf/util/parse-events.c int parse_events__modifier_group(struct list_head *list, · line 1385

Quoted 5 lines
tools/perf/util/parse-events.c +5 -0
+	total_members = evsel->idx - leader->idx + 1;
+
+	leaders = calloc(total_members, sizeof(uintptr_t));
+	if (!leaders)
+		return ret;

returns 0 in here

OK

Quoted 16 lines
Quoted 16 lines
tools/perf/util/parse-events.c +15 -0
+
+	 __evlist__for_each_entry(list, evsel) {
+
+		/* Only split the uncore group which members use alias */
+		if (!evsel->use_uncore_alias)
+			goto out;
+
+		/* The events must be from the same uncore block */
+		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
+			goto out;
+
+		if (!is_leader)
+			continue;
+		/*
+		 * If the event's PMU name starts to repeat, it must be a new
Quoted 9 lines

tools/perf/util/parse-events.c int parse_events__modifier_group(struct list_head *list, · line 1405

Quoted 5 lines
tools/perf/util/parse-events.c +5 -0
+		 * event. That can be used to distinguish the leader from
+		 * other members, even they have the same event name.
+		 */
+		if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
+			is_leader = false;

setting "is_leader = false" basically breaks the loop, because of that !leader check above, so why continue?

Because the evsel doesn't belong to leader anymore. We should not add it into leaders[].

Quoted 14 lines
Quoted 14 lines
tools/perf/util/parse-events.c +14 -0
+			continue;
+		}
+		/* The name is always alias name */
+		WARN_ON(strcmp(leader->name, evsel->name));
+
+		leaders[nr_pmu++] = (uintptr_t) evsel;
+	}
+
+	/* only one event alias */
+	if (nr_pmu == total_members) {
+		parse_state->nr_groups--;
+		goto handled;
+	}
+
Quoted 9 lines

tools/perf/util/parse-events.c int parse_events__modifier_group(struct list_head *list, · line 1424

Quoted 5 lines
tools/perf/util/parse-events.c +5 -0
+	__evlist__for_each_entry(list, evsel) {
+		if (i >= nr_pmu)
+			i = 0;
+		evsel->leader = (struct perf_evsel *) leaders[i++];
+	}

it's not so obvious from the code how the groups are set.. please describe that logic in the comment

Sure. I will add more comments.

Thanks,
Kan

Quoted 18 lines

thanks,
jirka

Quoted 15 lines
tools/perf/util/parse-events.c +15 -0
+
+	for (i = 0; i < nr_pmu; i++) {
+		evsel = (struct perf_evsel *) leaders[i];
+		evsel->nr_members = total_members / nr_pmu;
+		evsel->group_name = name ? strdup(name) : NULL;
+	}
+
+	parse_state->nr_groups += nr_pmu - 1;
+
+handled:
+	ret = 1;
+out:
+	free(leaders);
+	return ret;
+}