Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1304077ybi; Fri, 14 Jun 2019 12:11:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqwYVbTWetiYWX9KISsqU5WALlxuhpTzPKVwCKmd7U99E4FzQxnkVShusF2pvtcM78M6ediP X-Received: by 2002:aa7:8b12:: with SMTP id f18mr99495819pfd.178.1560539474329; Fri, 14 Jun 2019 12:11:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560539474; cv=none; d=google.com; s=arc-20160816; b=ibcE6EBbkVCKDp6A1BSnglJSDVTkK2svqYYftr9fLbs5eKg9OYaCnIykY29zgXM8b6 Ob6ar52y1sGGjiphGRfReVG6HOXA9fzO471yDXSDNVNB5zlLVS4RNeim4gbrbzKMkFK/ cozzmyhbAf54pt1S8S2HSZL9wyY9DOPPakGbsmDfVTxKK3z12xQxaKMBoGvuuvEcaJfo ifIL2knYBNL0Nr2droZTDb6z8S1BWOLVaXixcUsZpDDKF2Fb2Gwsgfnn+EYh8HqyDop9 0rIGHauc0/p6MrEv6FtCKr0l9KRkrihc3ijnP8od1rg/XfJ0zuon7m7Bujh/+NUD8wlc jCRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=w0I3Z4/wykFceLcnHYGRYFdVs0dZ0k9LRu5iTius9Iw=; b=VJlHQLBswL080ehfQE+tWHKvGgHFD7d6oJvtgP56gBkJZf5gGrwXfH6fEZ3AMMqnF8 Wf1fck4vG1hBjoWaY3PgskMxHEl4RbtPvZuM7B+UkVg5GyvWWQtbGWn+bYwk1qojYmfN XJ6p0dmyZA8b1NjxahgUJ8xrg65Ue5HOfDGTKrRt4dmQwc1OZ4MPJWKIbqJLaeYKh7KT TQ350KQIh+a4LevVNchiW4IQHJAjnPIH85gvtUK9H7RmYTnl93rXZWOuNetc/Rxd27n+ Sq63KD5ljIThg2MkYFjsW4FP3lRPRFMBpzBgQmmiWm/oEc+p9I6iBJySMbYUGESnAsMq UG9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="qAJ/cGYy"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f10si3292019pgg.348.2019.06.14.12.10.58; Fri, 14 Jun 2019 12:11:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="qAJ/cGYy"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726252AbfFNTKW (ORCPT + 99 others); Fri, 14 Jun 2019 15:10:22 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:44499 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725812AbfFNTKV (ORCPT ); Fri, 14 Jun 2019 15:10:21 -0400 Received: by mail-io1-f68.google.com with SMTP id s7so8027140iob.11 for ; Fri, 14 Jun 2019 12:10:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=w0I3Z4/wykFceLcnHYGRYFdVs0dZ0k9LRu5iTius9Iw=; b=qAJ/cGYyzVyAEOG3i3Ky/3nD3AJOpT6GcN5/eDRCy23jKvtMdn1X+j1LJg5CoUiWX+ IkfuIgFQVxDE1QZP5SlzgTjHzAMj6FByxlhCzxqxJ6uh2o5ZxNC2xQ7q/wNhiAQ+zscB G6QsDOdkMUvWaPV8X3xf9DsRm6AZL7FPnxtSNzrSmo5WC2jlgNw7npOyouoFsfsaT9oz 2uujIDz+6kNihtxlQABGR2tMUopkztR/VEIqKgeQOmzwTIgm7tnCzv+pg2h9lFR5YLej CsmV3ceXTRRfWLTpbg2pHroVmciXcrlZY+Ug8H68Ij3FCiqiLQ2+fWah8oxhbqvAL06D NNXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=w0I3Z4/wykFceLcnHYGRYFdVs0dZ0k9LRu5iTius9Iw=; b=rr4A8fU1XItOQRMT9khIcAadv20ar2Ipj6WuAvJ6zlx173TJTmTd2SJYMNUxuhFJ1G BO85WwMoYNiNNg0/tmCWL4ZnZ1BScb55LDyccwoXuHkwkUnbHd7UlLEHyswbNCe7CVnU d1MVvrIx7glQcYh/cz6A2DmvuGtGIXPrOwyMCLoGScfd2fQO1AE0POy+chUCrZWbF+nU UkOzN3SW5puKKiO/xF+zP4HKb3UxS0tjPbhRBeyJiJ/jaJRkevTsYF747jdbjQTE9aYE VlSXpzicCJaqG/6vVW9kNdjHgI8a9nQ+wR4dBMM/xcKI5+vq9YjA+Syo1hxDSj4YkgVY 7CvA== X-Gm-Message-State: APjAAAXIkY0Qv1AqHuPg/wiod/V8NQNeNJMFyMtNBDWPstz3GT1ByQ68 7bS4aCdiPddwUS0dGn1vDucEd15lb4H7ls1zjqKTag== X-Received: by 2002:a5d:8506:: with SMTP id q6mr1794969ion.41.1560539420401; Fri, 14 Jun 2019 12:10:20 -0700 (PDT) MIME-Version: 1.0 References: <20190601082722.44543-1-irogers@google.com> <5084acfa-59a4-996f-bb1d-69fbbac01b87@linux.intel.com> In-Reply-To: <5084acfa-59a4-996f-bb1d-69fbbac01b87@linux.intel.com> From: Stephane Eranian Date: Fri, 14 Jun 2019 12:10:08 -0700 Message-ID: Subject: Re: [PATCH] perf cgroups: Don't rotate events for cgroups unnecessarily To: "Liang, Kan" Cc: Ian Rogers , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , Jiri Olsa , Namhyung Kim , LKML , Andi Kleen Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 13, 2019 at 9:13 AM Liang, Kan wrote: > > > > On 6/1/2019 4:27 AM, Ian Rogers wrote: > > Currently perf_rotate_context assumes that if the context's nr_events != > > nr_active a rotation is necessary for perf event multiplexing. With > > cgroups, nr_events is the total count of events for all cgroups and > > nr_active will not include events in a cgroup other than the current > > task's. This makes rotation appear necessary for cgroups when it is not. > > > > Add a perf_event_context flag that is set when rotation is necessary. > > Clear the flag during sched_out and set it when a flexible sched_in > > fails due to resources. > > > > Signed-off-by: Ian Rogers > > --- > > include/linux/perf_event.h | 5 +++++ > > kernel/events/core.c | 42 +++++++++++++++++++++++--------------- > > 2 files changed, 30 insertions(+), 17 deletions(-) > > > > diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h > > index 15a82ff0aefe..7ab6c251aa3d 100644 > > --- a/include/linux/perf_event.h > > +++ b/include/linux/perf_event.h > > @@ -747,6 +747,11 @@ struct perf_event_context { > > int nr_stat; > > int nr_freq; > > int rotate_disable; > > + /* > > + * Set when nr_events != nr_active, except tolerant to events not > > + * needing to be active due to scheduling constraints, such as cgroups. > > + */ > > + int rotate_necessary; > > It looks like the rotate_necessary is only useful for cgroup and cpuctx. > Why not move it to struct perf_cpu_context and under #ifdef > CONFIG_CGROUP_PERF? > And rename it cgrp_rotate_necessary? > I am not sure I see the point here. What I'd like to see is a uniform signal for rotation needed in per-task, per-cpu or per-cgroup modes. Ian's patch does that. It does make it a lot more efficient in cgroup mode, by avoiding unnecessary rotations, and does not alter/improve on any of the other two modes. > Thanks, > Kan > > > refcount_t refcount; > > struct task_struct *task; > > > > diff --git a/kernel/events/core.c b/kernel/events/core.c > > index abbd4b3b96c2..41ae424b9f1d 100644 > > --- a/kernel/events/core.c > > +++ b/kernel/events/core.c > > @@ -2952,6 +2952,12 @@ static void ctx_sched_out(struct perf_event_context *ctx, > > if (!ctx->nr_active || !(is_active & EVENT_ALL)) > > return; > > > > + /* > > + * If we had been multiplexing, no rotations are necessary now no events > > + * are active. > > + */ > > + ctx->rotate_necessary = 0; > > + > > perf_pmu_disable(ctx->pmu); > > if (is_active & EVENT_PINNED) { > > list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list) > > @@ -3325,6 +3331,15 @@ static int flexible_sched_in(struct perf_event *event, void *data) > > sid->can_add_hw = 0; > > } > > > > + /* > > + * If the group wasn't scheduled then set that multiplexing is necessary > > + * for the context. Note, this won't be set if the event wasn't > > + * scheduled due to event_filter_match failing due to the earlier > > + * return. > > + */ > > + if (event->state == PERF_EVENT_STATE_INACTIVE) > > + sid->ctx->rotate_necessary = 1; > > + > > return 0; > > } > > > > @@ -3690,24 +3705,17 @@ ctx_first_active(struct perf_event_context *ctx) > > static bool perf_rotate_context(struct perf_cpu_context *cpuctx) > > { > > struct perf_event *cpu_event = NULL, *task_event = NULL; > > - bool cpu_rotate = false, task_rotate = false; > > - struct perf_event_context *ctx = NULL; > > + struct perf_event_context *task_ctx = NULL; > > + int cpu_rotate, task_rotate; > > > > /* > > * Since we run this from IRQ context, nobody can install new > > * events, thus the event count values are stable. > > */ > > > > - if (cpuctx->ctx.nr_events) { > > - if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) > > - cpu_rotate = true; > > - } > > - > > - ctx = cpuctx->task_ctx; > > - if (ctx && ctx->nr_events) { > > - if (ctx->nr_events != ctx->nr_active) > > - task_rotate = true; > > - } > > + cpu_rotate = cpuctx->ctx.rotate_necessary; > > + task_ctx = cpuctx->task_ctx; > > + task_rotate = task_ctx ? task_ctx->rotate_necessary : 0; > > > > if (!(cpu_rotate || task_rotate)) > > return false; > > @@ -3716,7 +3724,7 @@ static bool perf_rotate_context(struct perf_cpu_context *cpuctx) > > perf_pmu_disable(cpuctx->ctx.pmu); > > > > if (task_rotate) > > - task_event = ctx_first_active(ctx); > > + task_event = ctx_first_active(task_ctx); > > if (cpu_rotate) > > cpu_event = ctx_first_active(&cpuctx->ctx); > > > > @@ -3724,17 +3732,17 @@ static bool perf_rotate_context(struct perf_cpu_context *cpuctx) > > * As per the order given at ctx_resched() first 'pop' task flexible > > * and then, if needed CPU flexible. > > */ > > - if (task_event || (ctx && cpu_event)) > > - ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); > > + if (task_event || (task_ctx && cpu_event)) > > + ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE); > > if (cpu_event) > > cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); > > > > if (task_event) > > - rotate_ctx(ctx, task_event); > > + rotate_ctx(task_ctx, task_event); > > if (cpu_event) > > rotate_ctx(&cpuctx->ctx, cpu_event); > > > > - perf_event_sched_in(cpuctx, ctx, current); > > + perf_event_sched_in(cpuctx, task_ctx, current); > > > > perf_pmu_enable(cpuctx->ctx.pmu); > > perf_ctx_unlock(cpuctx, cpuctx->task_ctx); > >