Re: [PATCH 1/3] perf: Optimize perf_install_in_event()

From: Peter Zijlstra
Date: Tue Oct 22 2019 - 06:04:07 EST


On Tue, Oct 22, 2019 at 11:20:18AM +0200, Peter Zijlstra wrote:
> Andi reported that when creating a lot of events, a lot of time is
> spend 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).

Arguably, we could possible do something like so as well, but I'm not
sure it makes sense as it will not help if IOC_ENABLE is called in
creation order. Because in that case we'll enable the group leader
before the siblings and we'll schedule them one at a time, instead of
the whole group at once.

---
diff --git a/kernel/events/core.c b/kernel/events/core.c
index f9a5d4356562..2a5e6d9654e1 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2791,6 +2791,7 @@ static void __perf_event_enable(struct perf_event *event,
static void _perf_event_enable(struct perf_event *event)
{
struct perf_event_context *ctx = event->ctx;
+ struct perf_event *leader;

raw_spin_lock_irq(&ctx->lock);
if (event->state >= PERF_EVENT_STATE_INACTIVE ||
@@ -2808,6 +2809,17 @@ static void _perf_event_enable(struct perf_event *event)
*/
if (event->state == PERF_EVENT_STATE_ERROR)
event->state = PERF_EVENT_STATE_OFF;
+
+ /*
+ * If this is a sibling event and the group leader is still OFF
+ * then there is no point in trying to schedule this event.
+ */
+ leader = event->group_leader;
+ if (leader != event && leader->state == PERF_EVENT_STATE_OFF) {
+ event->state = PERF_EVENT_STATE_INACTIVE;
+ raw_spin_unlock_irq(&ctx->lock);
+ return;
+ }
raw_spin_unlock_irq(&ctx->lock);

event_function_call(event, __perf_event_enable, NULL);