Skip to contentSkip to conversation

perf: Reset detached siblings' group_flags

PATCH In review Namhyung Kim 3 participants 8 messages Mar 7, 2013 – Mar 11, 2013 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/2 perf: Reset detached siblings' group_flags

From: Namhyung Kim <[email protected]>

Currently if a group_leader event is deleted, the sibling events are upgraded to singleton events of a same group list. At this time, the siblings inherit the leader's group_flags.

However, if the group has mixed hw/sw events the leader's group_flag does not contain PERF_GROUP_SOFTWARE so sibling sw events will miss the flag also. Fix it.

Cc Jiri Olsa <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/events/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/events/core.c +3 -2
index 5c75791d7269..007dfe846d4d 100644
@@ -1107,8 +1107,9 @@ static void perf_group_detach(struct perf_event *event)
 			list_move_tail(&sibling->group_entry, list);
 		sibling->group_leader = sibling;
 
-		/* Inherit group flags from the previous leader */
-		sibling->group_flags = event->group_flags;
+		/* Reset group flags for each siblings */
+		sibling->group_flags = is_software_event(sibling) ?
+						PERF_GROUP_SOFTWARE : 0;
 	}
 
 out:
Signature · 1 line
1.7.11.7
author maintainer
#1 lore raw

PATCH 2/2 perf: Fix mixed hw/sw event group initialization

From: Namhyung Kim <[email protected]>

There's a problem with mixed hw/sw group when the leader is a software event. For instance:

  $ perf stat -e '{task-clock,cycles,faults}' sleep 1

Performance counter stats for 'sleep 1':

            0.273436 task-clock                #    0.000 CPUs utilized
             962,965 cycles                    #    3.522 GHz

<not supported> faults

1.000804279 seconds time elapsed

Jiri's patch 0231bb533675 ("perf: Fix event group context move") fixed a part of problem but there's a devil still..

The problem arose when a sw event is added to already moved (to hw context) group whose leader also is a sw event. In the above example

1. task-clock (sw event) is a group leader (has PERF_GROUP_SOFTWARE)
2. cycles (hw event) is added, so the leader moved to the hw context
3. faults (sw event) is added but the leader also is a sw event
4. after find_get_context(), ctx is not same as leader->ctx since the leader had moved to the hw context (-EINVAL)

Fix it by adding new PERF_GROUP_MIXED flag and use leader's ctx->pmu if it's set.

  $ perf -state -e '{task-clock,cycles,faults}' sleep 1

Performance counter stats for 'sleep 1':

            0.670405 task-clock                #    0.001 CPUs utilized
             933,264 cycles                    #    1.392 GHz
                 176 faults                    #    0.263 M/sec
         1.001506178 seconds time elapsed
Cc Jiri Olsa <[email protected]>
Cc Vince Weaver <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 37 ++++++++++++++++++++++---------------
 2 files changed, 23 insertions(+), 15 deletions(-)
include/linux/perf_event.h +1 -0
index e47ee462c2f2..001a3b64fe61 100644
@@ -285,6 +285,7 @@ typedef void (*perf_overflow_handler_t)(struct perf_event *,
 
 enum perf_group_flag {
 	PERF_GROUP_SOFTWARE		= 0x1,
+	PERF_GROUP_MIXED		= 0x2,
 };
 
 #define SWEVENT_HLIST_BITS		8
kernel/events/core.c +22 -15
index 007dfe846d4d..06266d5ed500 100644
@@ -6441,6 +6441,8 @@ out:
  * @pid:		target pid
  * @cpu:		target cpu
  * @group_fd:		group leader event fd
+ * @flags:		flags which controls the meaning of arguments.
+ * 			see PERF_FLAG_*
  */
 SYSCALL_DEFINE5(perf_event_open,
 		struct perf_event_attr __user *, attr_uptr,
@@ -6536,26 +6538,30 @@ SYSCALL_DEFINE5(perf_event_open,
 	 */
 	pmu = event->pmu;
 
-	if (group_leader &&
-	    (is_software_event(event) != is_software_event(group_leader))) {
-		if (is_software_event(event)) {
-			/*
-			 * If event and group_leader are not both a software
-			 * event, and event is, then group leader is not.
-			 *
-			 * Allow the addition of software events to !software
-			 * groups, this is safe because software events never
-			 * fail to schedule.
-			 */
-			pmu = group_leader->pmu;
-		} else if (is_software_event(group_leader) &&
-			   (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
+	if (group_leader) {
+		if (group_leader->group_flags & PERF_GROUP_SOFTWARE) {
 			/*
 			 * In case the group is a pure software group, and we
 			 * try to add a hardware event, move the whole group to
 			 * the hardware context.
 			 */
-			move_group = 1;
+			if (!is_software_event(event))
+				move_group = 1;
+		} else if (group_leader->group_flags & PERF_GROUP_MIXED) {
+			/*
+			 * The group leader was moved on to a hardware context,
+			 * so move this event also.
+			 */
+			if (is_software_event(event))
+				pmu = group_leader->ctx->pmu;
+		} else if (!is_software_event(group_leader)) {
+			/*
+			 * Allow the addition of software events to !software
+			 * groups, this is safe because software events never
+			 * fail to schedule.
+			 */
+			if (is_software_event(event))
+				pmu = group_leader->pmu;
 		}
 	}
 
@@ -6650,6 +6656,7 @@ SYSCALL_DEFINE5(perf_event_open,
 			perf_install_in_context(ctx, sibling, event->cpu);
 			get_ctx(ctx);
 		}
+		group_leader->group_flags = PERF_GROUP_MIXED;
 	}
 
 	perf_install_in_context(ctx, event, event->cpu);
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [PATCH 2/2] perf: Fix mixed hw/sw event group initialization

Quoted 102 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

There's a problem with mixed hw/sw group when the leader is a software event. For instance:

Jiri's patch 0231bb533675 ("perf: Fix event group context move") fixed a part of problem but there's a devil still..

The problem arose when a sw event is added to already moved (to hw context) group whose leader also is a sw event. In the above example

1. task-clock (sw event) is a group leader (has PERF_GROUP_SOFTWARE)
2. cycles (hw event) is added, so the leader moved to the hw context
3. faults (sw event) is added but the leader also is a sw event
4. after find_get_context(), ctx is not same as leader->ctx since the leader had moved to the hw context (-EINVAL)

Fix it by adding new PERF_GROUP_MIXED flag and use leader's ctx->pmu if it's set.

Cc Jiri Olsa <[email protected]>
Cc Vince Weaver <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 37 ++++++++++++++++++++++---------------
 2 files changed, 23 insertions(+), 15 deletions(-)
include/linux/perf_event.h +1 -0
index e47ee462c2f2..001a3b64fe61 100644
@@ -285,6 +285,7 @@ typedef void (*perf_overflow_handler_t)(struct perf_event *,
 
 enum perf_group_flag {
 	PERF_GROUP_SOFTWARE		= 0x1,
+	PERF_GROUP_MIXED		= 0x2,
 };
 
 #define SWEVENT_HLIST_BITS		8
kernel/events/core.c +21 -15
index 007dfe846d4d..06266d5ed500 100644
@@ -6441,6 +6441,8 @@ out:
  * @pid:		target pid
  * @cpu:		target cpu
  * @group_fd:		group leader event fd
+ * @flags:		flags which controls the meaning of arguments.
+ * 			see PERF_FLAG_*
  */
 SYSCALL_DEFINE5(perf_event_open,
 		struct perf_event_attr __user *, attr_uptr,
@@ -6536,26 +6538,30 @@ SYSCALL_DEFINE5(perf_event_open,
 	 */
 	pmu = event->pmu;
 
-	if (group_leader &&
-	    (is_software_event(event) != is_software_event(group_leader))) {
-		if (is_software_event(event)) {
-			/*
-			 * If event and group_leader are not both a software
-			 * event, and event is, then group leader is not.
-			 *
-			 * Allow the addition of software events to !software
-			 * groups, this is safe because software events never
-			 * fail to schedule.
-			 */
-			pmu = group_leader->pmu;
-		} else if (is_software_event(group_leader) &&
-			   (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
+	if (group_leader) {
+		if (group_leader->group_flags & PERF_GROUP_SOFTWARE) {
 			/*
 			 * In case the group is a pure software group, and we
 			 * try to add a hardware event, move the whole group to
 			 * the hardware context.
 			 */
-			move_group = 1;
+			if (!is_software_event(event))
+				move_group = 1;
+		} else if (group_leader->group_flags & PERF_GROUP_MIXED) {
+			/*
+			 * The group leader was moved on to a hardware context,
+			 * so move this event also.
+			 */
+			if (is_software_event(event))
+				pmu = group_leader->ctx->pmu;
+		} else if (!is_software_event(group_leader)) {
+			/*
+			 * Allow the addition of software events to !software
+			 * groups, this is safe because software events never
+			 * fail to schedule.
+			 */
+			if (is_software_event(event))
+				pmu = group_leader->pmu;
 		}
 	}
 

kernel/events/core.c SYSCALL_DEFINE5(perf_event_open, · line 6662

Quoted 7 lines
kernel/events/core.c +1 -0
@@ -6650,6 +6656,7 @@ SYSCALL_DEFINE5(perf_event_open,
 			perf_install_in_context(ctx, sibling, event->cpu);
 			get_ctx(ctx);
 		}
+		group_leader->group_flags = PERF_GROUP_MIXED;
 	}
 
 	perf_install_in_context(ctx, event, event->cpu);

This seems reasonable, but I think the perf_group_detach thing needs to migrate events between pmu's as well to complete the whole mess.

author maintainer
#3 lore raw

Re: [PATCH 2/2] perf: Fix mixed hw/sw event group initialization

Hi Peter,

Quoted 109 lines · Peter Zijlstra
Quoted 108 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

There's a problem with mixed hw/sw group when the leader is a software event. For instance:

Jiri's patch 0231bb533675 ("perf: Fix event group context move") fixed a part of problem but there's a devil still..

The problem arose when a sw event is added to already moved (to hw context) group whose leader also is a sw event. In the above example

1. task-clock (sw event) is a group leader (has PERF_GROUP_SOFTWARE)
2. cycles (hw event) is added, so the leader moved to the hw context
3. faults (sw event) is added but the leader also is a sw event
4. after find_get_context(), ctx is not same as leader->ctx since the leader had moved to the hw context (-EINVAL)

Fix it by adding new PERF_GROUP_MIXED flag and use leader's ctx->pmu if it's set.

Cc Jiri Olsa <[email protected]>
Cc Vince Weaver <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 37 ++++++++++++++++++++++---------------
 2 files changed, 23 insertions(+), 15 deletions(-)
include/linux/perf_event.h +1 -0
index e47ee462c2f2..001a3b64fe61 100644
@@ -285,6 +285,7 @@ typedef void (*perf_overflow_handler_t)(struct perf_event *,
 
 enum perf_group_flag {
 	PERF_GROUP_SOFTWARE		= 0x1,
+	PERF_GROUP_MIXED		= 0x2,
 };
 
 #define SWEVENT_HLIST_BITS		8
kernel/events/core.c +22 -15
index 007dfe846d4d..06266d5ed500 100644
@@ -6441,6 +6441,8 @@ out:
  * @pid:		target pid
  * @cpu:		target cpu
  * @group_fd:		group leader event fd
+ * @flags:		flags which controls the meaning of arguments.
+ * 			see PERF_FLAG_*
  */
 SYSCALL_DEFINE5(perf_event_open,
 		struct perf_event_attr __user *, attr_uptr,
@@ -6536,26 +6538,30 @@ SYSCALL_DEFINE5(perf_event_open,
 	 */
 	pmu = event->pmu;
 
-	if (group_leader &&
-	    (is_software_event(event) != is_software_event(group_leader))) {
-		if (is_software_event(event)) {
-			/*
-			 * If event and group_leader are not both a software
-			 * event, and event is, then group leader is not.
-			 *
-			 * Allow the addition of software events to !software
-			 * groups, this is safe because software events never
-			 * fail to schedule.
-			 */
-			pmu = group_leader->pmu;
-		} else if (is_software_event(group_leader) &&
-			   (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
+	if (group_leader) {
+		if (group_leader->group_flags & PERF_GROUP_SOFTWARE) {
 			/*
 			 * In case the group is a pure software group, and we
 			 * try to add a hardware event, move the whole group to
 			 * the hardware context.
 			 */
-			move_group = 1;
+			if (!is_software_event(event))
+				move_group = 1;
+		} else if (group_leader->group_flags & PERF_GROUP_MIXED) {
+			/*
+			 * The group leader was moved on to a hardware context,
+			 * so move this event also.
+			 */
+			if (is_software_event(event))
+				pmu = group_leader->ctx->pmu;
+		} else if (!is_software_event(group_leader)) {
+			/*
+			 * Allow the addition of software events to !software
+			 * groups, this is safe because software events never
+			 * fail to schedule.
+			 */
+			if (is_software_event(event))
+				pmu = group_leader->pmu;
 		}
 	}
 
@@ -6650,6 +6656,7 @@ SYSCALL_DEFINE5(perf_event_open,
 			perf_install_in_context(ctx, sibling, event->cpu);
 			get_ctx(ctx);
 		}
+		group_leader->group_flags = PERF_GROUP_MIXED;
 	}
 
Quoted 5 lines

kernel/events/core.c SYSCALL_DEFINE5(perf_event_open, · line 6662

Quoted 1 line
kernel/events/core.c +0 -0
 	perf_install_in_context(ctx, event, event->cpu);

This seems reasonable, but I think the perf_group_detach thing needs to migrate events between pmu's as well to complete the whole mess.

Hmm.. it seems that it needs whole perf_{remove_from,install_in}_context step to migrate them, right? Looks like not a simple job for me. :(

Any advice?

Thanks,
Namhyung

#4 lore raw

PATCH 1/1 perf tests: Add automated test for mixed type event groups

In reply to Namhyung Kim
Quoted 7 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

There's a problem with mixed hw/sw group when the leader is a software event. For instance:

  $ perf stat -e '{task-clock,cycles,faults}' sleep 1
Quoted 8 lines
   Performance counter stats for 'sleep 1':

            0.273436 task-clock                #    0.000 CPUs utilized
             962,965 cycles                    #    3.522 GHz
     <not supported> faults

         1.000804279 seconds time elapsed

as discussed on irc, here's automated test for this, feel free to change it in any way

jirka

---
Adding automated test for mixed type event groups.

---
 tools/perf/Makefile             |    1 +
 tools/perf/tests/builtin-test.c |    4 ++
 tools/perf/tests/mixed-groups.c |  104 +++++++++++++++++++++++++++++++++++++++
 tools/perf/tests/tests.h        |    1 +
 4 files changed, 110 insertions(+), 0 deletions(-)
 create mode 100644 tools/perf/tests/mixed-groups.c
tools/perf/Makefile +1 -0
index a2108ca..6eb5930 100644
@@ -497,6 +497,7 @@ LIB_OBJS += $(OUTPUT)tests/evsel-tp-sched.o
 LIB_OBJS += $(OUTPUT)tests/pmu.o
 LIB_OBJS += $(OUTPUT)tests/hists_link.o
 LIB_OBJS += $(OUTPUT)tests/python-use.o
+LIB_OBJS += $(OUTPUT)tests/mixed-groups.o
 
 BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
 BUILTIN_OBJS += $(OUTPUT)builtin-bench.o
tools/perf/tests/builtin-test.c +4 -0
index acb98e0..aaec91d 100644
@@ -78,6 +78,10 @@ static struct test {
 		.func = test__python_use,
 	},
 	{
+		.desc = "Test event mixed groups",
+		.func = test__mixed_groups,
+	},
+	{
 		.func = NULL,
 	},
 };
tools/perf/tests/mixed-groups.c new file +104 -0

104 lines not on this page · raw message

tools/perf/tests/tests.h +1 -0
index 5de0be1..c4a2f05 100644
@@ -23,5 +23,6 @@ int test__dso_data(void);
 int test__parse_events(void);
 int test__hists_link(void);
 int test__python_use(void);
+int test__mixed_groups(void);
 
 #endif /* TESTS_H */
Signature · 1 line
1.7.7.6
#5 lore raw
In reply to Namhyung Kim
Quoted 22 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

Currently if a group_leader event is deleted, the sibling events are upgraded to singleton events of a same group list. At this time, the siblings inherit the leader's group_flags.

However, if the group has mixed hw/sw events the leader's group_flag does not contain PERF_GROUP_SOFTWARE so sibling sw events will miss the flag also. Fix it.

Cc Jiri Olsa <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/events/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/events/core.c +0 -0
index 5c75791d7269..007dfe846d4d 100644

kernel/events/core.c static void perf_group_detach(struct perf_event *event) · line 1113

Quoted 9 lines
kernel/events/core.c +3 -2
@@ -1107,8 +1107,9 @@ static void perf_group_detach(struct perf_event *event)
 			list_move_tail(&sibling->group_entry, list);
 		sibling->group_leader = sibling;
 
-		/* Inherit group flags from the previous leader */
-		sibling->group_flags = event->group_flags;
+		/* Reset group flags for each siblings */
+		sibling->group_flags = is_software_event(sibling) ?
+						PERF_GROUP_SOFTWARE : 0;
 	}

In such a case, does the event not continue to live on the hw pmu? That is, after this patch we'll have a software event on the hardware pmu list with PERF_GROUP_SOFTWARE set, right? Seems odd.

author maintainer
#6 lore raw
Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

Currently if a group_leader event is deleted, the sibling events are upgraded to singleton events of a same group list. At this time, the siblings inherit the leader's group_flags.

However, if the group has mixed hw/sw events the leader's group_flag does not contain PERF_GROUP_SOFTWARE so sibling sw events will miss the flag also. Fix it.

Cc Jiri Olsa <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/events/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/events/core.c +0 -0
index 5c75791d7269..007dfe846d4d 100644
@@ -1107,8 +1107,9 @@ static void perf_group_detach(struct perf_event *event)
 			list_move_tail(&sibling->group_entry, list);
 		sibling->group_leader = sibling;
 
Quoted 11 lines

kernel/events/core.c static void perf_group_detach(struct perf_event *event) · line 1113

Quoted 6 lines
kernel/events/core.c +3 -2
-		/* Inherit group flags from the previous leader */
-		sibling->group_flags = event->group_flags;
+		/* Reset group flags for each siblings */
+		sibling->group_flags = is_software_event(sibling) ?
+						PERF_GROUP_SOFTWARE : 0;
 	}

In such a case, does the event not continue to live on the hw pmu? That is, after this patch we'll have a software event on the hardware pmu list with PERF_GROUP_SOFTWARE set, right? Seems odd.

Right.

Just curious that dropping this one and applying just patch 2 will set the PERF_GROUP_MIXED for them and it'll fix the mixed group problem without migrating those events. What do you think about this approach?

Thanks,
Namhyung

author maintainer
#7 lore raw
In reply to Peter Zijlstra

While looking at the code again, I found that the detached siblings should call perf_event__header_size() for themselves in the loop since after moving them to the list the previous leader will have empty sibling_list. Something like below is needed IMHO:

kernel/events/core.c +7 -0
index dc96fee942a6..53ac21cc4012 100644
@@ -1100,6 +1100,13 @@ static void perf_group_detach(struct perf_event *event)
 
 		/* Inherit group flags from the previous leader */
 		sibling->group_flags = event->group_flags;
+
+		/*
+		 * The leader will forget about siblings from now on.
+		 * So below loop to update header size of each sibling won't
+		 * work for this case. Do it directly.
+		 */
+		perf_event__header_size(sibling);
 	}
 
 out: