Skip to contentSkip to conversation

perf: Enable group siblings on exec if attr::enable_on_exec set

RFC PATCH In review Lin Ming 2 participants 6 messages Nov 10, 2010

lore
Share & tools
lore

Lin Ming

author
#0 lore raw

RFC PATCH 1/2 perf: Enable group siblings on exec if attr::enable_on_exec set

Currently, only group leader is enabled on exec.
This patch enables all group events on exec if attr::enable_on_exec is set.

This is needed by next patch.

---
 kernel/perf_event.c |   24 +++++++++++++++++++++---
 1 files changed, 21 insertions(+), 3 deletions(-)
kernel/perf_event.c +21 -3
index 517d827..b94c18d 100644
@@ -1700,11 +1700,29 @@ static int event_enable_on_exec(struct perf_event *event,
 	if (event->state >= PERF_EVENT_STATE_INACTIVE)
 		return 0;
 
-	__perf_event_mark_enabled(event, ctx);
+	event->state = PERF_EVENT_STATE_INACTIVE;
 
 	return 1;
 }
 
+static int group_enable_on_exec(struct perf_event *event,
+				struct perf_event_context *ctx)
+{
+	int enabled = 0;
+	struct perf_event *sub;
+
+	list_for_each_entry(sub, &event->sibling_list, group_entry)
+		if (event_enable_on_exec(sub, ctx))
+			enabled++;
+
+	if (event_enable_on_exec(event, ctx))
+		enabled++;
+
+	__perf_event_mark_enabled(event, ctx);
+
+	return enabled;
+}
+
 /*
  * Enable all of a task's events that have been marked enable-on-exec.
  * This expects task == current.
@@ -1725,13 +1743,13 @@ static void perf_event_enable_on_exec(struct perf_event_context *ctx)
 	raw_spin_lock(&ctx->lock);
 
 	list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
-		ret = event_enable_on_exec(event, ctx);
+		ret = group_enable_on_exec(event, ctx);
 		if (ret)
 			enabled = 1;
 	}
 
 	list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
-		ret = event_enable_on_exec(event, ctx);
+		ret = group_enable_on_exec(event, ctx);
 		if (ret)
 			enabled = 1;
 	}
Signature · 1 line
1.7.1
#1 lore raw
Quoted 1 line · Lin Ming

Currently, only group leader is enabled on exec.

That's enough, right? If all sibling events are already enabled enabling the group leader makes the whole thing go.

author
#2 lore raw
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Lin Ming

Currently, only group leader is enabled on exec.

That's enough, right? If all sibling events are already enabled enabling the group leader makes the whole thing go.

No.

If the event group is disabled by default("perf stat" case) and will be enabled at next exec, then actually only the group leader will be enabled , because all siblings are explicitly disabled(->state == PERF_EVENT_STATE_OFF). So the siblings will never be enabled.

/*
* Put a event into inactive state and update time fields.
* Enabling the leader of a group effectively enables all
* the group members that aren't explicitly disabled, so we
* have to update their ->tstamp_enabled also.
* Note: this works for group members as well as group leaders
* since the non-leader members' sibling_lists will be empty. */
static void __perf_event_mark_enabled(struct perf_event *event,
struct perf_event_context *ctx)

{
        struct perf_event *sub;

        event->state = PERF_EVENT_STATE_INACTIVE;
        event->tstamp_enabled = ctx->time - event->total_time_enabled;
        list_for_each_entry(sub, &event->sibling_list, group_entry)
                if (sub->state >= PERF_EVENT_STATE_INACTIVE)
                        sub->tstamp_enabled =  
                                ctx->time - sub->total_time_enabled;
}
#3 lore raw
Quoted 7 lines · Lin Ming
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Lin Ming

Currently, only group leader is enabled on exec.

That's enough, right? If all sibling events are already enabled enabling the group leader makes the whole thing go.

Quoted 6 lines

No.

If the event group is disabled by default("perf stat" case) and will be enabled at next exec, then actually only the group leader will be enabled , because all siblings are explicitly disabled(->state == PERF_EVENT_STATE_OFF). So the siblings will never be enabled.

Right, so what I was saying is, don't disable all the siblings by default, so that only the group leader will be disabled. In that case nothing will get scheduled because the group leader is not enabled.

So: {disabled, enabled, enabled} will not actually get scheduled and the group as a whole is effectively disabled. Once the enabled_on_exec flips the group leader to 'enabled' the whole group becomes enabled.

I don't mind the patch too much (although I would like to avoid that double sibling iteration in both group_enable_on_exec() and __perf_event_mark_enabled()), but I think it can already work as is.

author
#4 lore raw
Quoted 19 lines · Peter Zijlstra
Quoted 7 lines · Lin Ming
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Lin Ming

Currently, only group leader is enabled on exec.

That's enough, right? If all sibling events are already enabled enabling the group leader makes the whole thing go.

Quoted 6 lines

No.

If the event group is disabled by default("perf stat" case) and will be enabled at next exec, then actually only the group leader will be enabled , because all siblings are explicitly disabled(->state == PERF_EVENT_STATE_OFF). So the siblings will never be enabled.

Right, so what I was saying is, don't disable all the siblings by default, so that only the group leader will be disabled. In that case nothing will get scheduled because the group leader is not enabled.

Quoted 7 lines

So: {disabled, enabled, enabled} will not actually get scheduled and the group as a whole is effectively disabled. Once the enabled_on_exec flips the group leader to 'enabled' the whole group becomes enabled.

I don't mind the patch too much (although I would like to avoid that double sibling iteration in both group_enable_on_exec() and __perf_event_mark_enabled()), but I think it can already work as is.

I got it now!

Another stupid patch, sorry.

#5 lore raw
Quoted 7 lines · Lin Ming
Quoted 3 lines

I don't mind the patch too much (although I would like to avoid that double sibling iteration in both group_enable_on_exec() and __perf_event_mark_enabled()), but I think it can already work as is.

I got it now!

Another stupid patch, sorry.

Well, I wouldn't call it a stupid patch, it does fix something that is evidently non-obvious so it might be worth considering it on that merit.

I'm simply pointing out that what you're trying to achieve might be possible without changing code.