Skip to content

Commit db0503e

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/core: Optimize perf_install_in_event()
Andi reported that when creating a lot of events, a lot of time is spent in IPIs and asked if it would be possible to elide some of that. Now when, as for example the perf-tool always does, events are created disabled, then these events will not need to be scheduled when added to the context (they're still disable) and therefore the IPI is not required -- except for the very first event, that will need to set ctx->is_active. ( It might be possible to set ctx->is_active remotely for cpu_ctx, but we really need the IPI for task_ctx, so lets not make that distinction. ) Also use __perf_effective_state() since group events depend on the state of the leader, if the leader is OFF, the whole group is OFF. So when sibling events are created enabled (XXX check tool) then we only need a single IPI to create and enable the whole group (+ that initial IPI to initialize the context). Suggested-by: Andi Kleen <[email protected]> Reported-by: Andi Kleen <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vince Weaver <[email protected]> Cc: [email protected] Cc: [email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent c2b98a8 commit db0503e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

kernel/events/core.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,25 @@ perf_install_in_context(struct perf_event_context *ctx,
26662666
*/
26672667
smp_store_release(&event->ctx, ctx);
26682668

2669+
/*
2670+
* perf_event_attr::disabled events will not run and can be initialized
2671+
* without IPI. Except when this is the first event for the context, in
2672+
* that case we need the magic of the IPI to set ctx->is_active.
2673+
*
2674+
* The IOC_ENABLE that is sure to follow the creation of a disabled
2675+
* event will issue the IPI and reprogram the hardware.
2676+
*/
2677+
if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && ctx->nr_events) {
2678+
raw_spin_lock_irq(&ctx->lock);
2679+
if (ctx->task == TASK_TOMBSTONE) {
2680+
raw_spin_unlock_irq(&ctx->lock);
2681+
return;
2682+
}
2683+
add_event_to_ctx(event, ctx);
2684+
raw_spin_unlock_irq(&ctx->lock);
2685+
return;
2686+
}
2687+
26692688
if (!task) {
26702689
cpu_function_call(cpu, __perf_install_in_context, event);
26712690
return;

0 commit comments

Comments
 (0)