Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp3399652ybd; Tue, 25 Jun 2019 01:48:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqxKOJ+Aj2EyubTMDY7mubhP9LGZDXS/ZHtBQuRRHZgTKsHlzR+Qq00kNRKb2h0C2IfTQSns X-Received: by 2002:a17:90a:d814:: with SMTP id a20mr30685785pjv.48.1561452486712; Tue, 25 Jun 2019 01:48:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561452486; cv=none; d=google.com; s=arc-20160816; b=vp/oTTw+X5WIDvX1usxlSo2Qx+mr7vKgHyJ49MS19hGDkNLfhdsANLxHchr0eX5roE ZsNm7IAtAx24a19KELytN544sg6ObLBhnScWNSCJWB+mm+QSoFjE7iqAsnujCmlZxNyq DkSI25ILPAzpObrOu/fPg0ZrlBJCRQ6SZKudbW+IcquiwZlaSIZGVexHEsd8WhlSiDzP +Q1aGJniz+jkoGrfL3alLf2zWRgK6NvM7AtnnpoP5vfKz3UvbToFl123khHWCs+FRoyW PWUUF8eHEQKITEngoNDdBckgy8sC2XKXGug9BfFCZ52JYjXkyD/VmWaxeLbdk48GWgfQ eljw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:dkim-signature:dkim-filter; bh=UJyWbOXZasfdr0iSMfDvpdwATDMZcJ4qRcAeZiPEYnk=; b=tVSG78kscoxnytCwr/0Na3wg/Y6KMzwLhl9zX2BeIPiLFiTxzmbCgCbRlH7OdCBCGm 6AV6qW8i2NlNTOOeIdfYIbaVX/Bd3zG3QIzX2Nz23gnsWy7jV6gE+9QOIBA0TFKEtLZC Ef7QJQpat930tiahG4Qi5Jyj592mcviuD1clUyxXcVNVhjYHzC4p0cKCuBA6fP7PZerO cvWUlJ+fFzLgw/aRL8cl2/Hd6SMXjQmIXOAv6T4YsmV6nQq65Z3iXj/dxfQRJ7cnp9PL qqQjmibPmyMub+nR9NGx9FNtFcANBLqeqO7acn9ngy1L9rD9zUBQGNie+p72Y3Gi4CYB R5og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2019061801 header.b=HHlVD9LC; 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=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f1si12652418pgu.521.2019.06.25.01.47.50; Tue, 25 Jun 2019 01:48:06 -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=@zytor.com header.s=2019061801 header.b=HHlVD9LC; 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=NONE sp=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730841AbfFYIrQ (ORCPT + 99 others); Tue, 25 Jun 2019 04:47:16 -0400 Received: from terminus.zytor.com ([198.137.202.136]:57723 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726587AbfFYIrQ (ORCPT ); Tue, 25 Jun 2019 04:47:16 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x5P8hnJ33533481 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 25 Jun 2019 01:43:49 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 terminus.zytor.com x5P8hnJ33533481 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2019061801; t=1561452230; bh=UJyWbOXZasfdr0iSMfDvpdwATDMZcJ4qRcAeZiPEYnk=; h=Date:From:Cc:Reply-To:In-Reply-To:References:To:Subject:From; b=HHlVD9LC77vtf0jwXbKfmhcpZnpVrEl7e908yx/GsTqVTbVd2AI0n/adJmlh2lsT+ GMfckyn5dPkC9gK/RqtX9pm+Z7jcpNRukAdspSBveZiOi/rqLxLsYGmhx5+gd79eIx t8RTXlLi49O1XTqUEb8I0pQQ5+SKzUKALJS84mpaDL1qAwrXPVHvq9lyTpTs3DywA4 Wh7bHQ/dSAmXXP3YEiqfG/8cPQakY6+tHVRy20ioj/yOuAlSbbJ3J69n5VDGijLbMr sV23Yoc32jxsGjjJqggxgnhIXj62uybbCYtmtc///OvzDqpQHlOLSG7acK+XDgRBI1 9del7FNsdQXkg== Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x5P8hnAO3533478; Tue, 25 Jun 2019 01:43:49 -0700 Date: Tue, 25 Jun 2019 01:43:49 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Ian Rogers Message-ID: Cc: hpa@zytor.com, irogers@google.com, tglx@linutronix.de, vincent.weaver@maine.edu, peterz@infradead.org, acme@kernel.org, jolsa@redhat.com, bp@alien8.de, torvalds@linux-foundation.org, linux-kernel@vger.kernel.org, mingo@kernel.org, kan.liang@linux.intel.com, acme@redhat.com, alexander.shishkin@linux.intel.com, eranian@google.com, namhyung@kernel.org Reply-To: linux-kernel@vger.kernel.org, mingo@kernel.org, alexander.shishkin@linux.intel.com, namhyung@kernel.org, eranian@google.com, acme@redhat.com, kan.liang@linux.intel.com, vincent.weaver@maine.edu, peterz@infradead.org, hpa@zytor.com, irogers@google.com, tglx@linutronix.de, torvalds@linux-foundation.org, bp@alien8.de, acme@kernel.org, jolsa@redhat.com In-Reply-To: <20190601082722.44543-1-irogers@google.com> References: <20190601082722.44543-1-irogers@google.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf/cgroups: Don't rotate events for cgroups unnecessarily Git-Commit-ID: fd7d55172d1e2e501e6da0a5c1de25f06612dc2e X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-3.1 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: fd7d55172d1e2e501e6da0a5c1de25f06612dc2e Gitweb: https://git.kernel.org/tip/fd7d55172d1e2e501e6da0a5c1de25f06612dc2e Author: Ian Rogers AuthorDate: Sat, 1 Jun 2019 01:27:22 -0700 Committer: Ingo Molnar CommitDate: Mon, 24 Jun 2019 19:30:04 +0200 perf/cgroups: Don't rotate events for cgroups unnecessarily 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 Signed-off-by: Peter Zijlstra (Intel) Cc: Alexander Shishkin Cc: Arnaldo Carvalho de Melo Cc: Arnaldo Carvalho de Melo Cc: Borislav Petkov Cc: Jiri Olsa Cc: Kan Liang Cc: Linus Torvalds Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Thomas Gleixner Cc: Vince Weaver Link: https://lkml.kernel.org/r/20190601082722.44543-1-irogers@google.com Signed-off-by: Ingo Molnar --- include/linux/perf_event.h | 5 +++++ kernel/events/core.c | 42 ++++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 3dc01cf98e16..2ddae518dce6 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -749,6 +749,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 + * necessary to be active due to scheduling constraints, such as cgroups. + */ + int rotate_necessary; refcount_t refcount; struct task_struct *task; diff --git a/kernel/events/core.c b/kernel/events/core.c index 118ad1aef6af..23efe6792abc 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) @@ -3319,10 +3325,13 @@ static int flexible_sched_in(struct perf_event *event, void *data) return 0; if (group_can_go_on(event, sid->cpuctx, sid->can_add_hw)) { - if (!group_sched_in(event, sid->cpuctx, sid->ctx)) - list_add_tail(&event->active_list, &sid->ctx->flexible_active); - else + int ret = group_sched_in(event, sid->cpuctx, sid->ctx); + if (ret) { sid->can_add_hw = 0; + sid->ctx->rotate_necessary = 1; + return 0; + } + list_add_tail(&event->active_list, &sid->ctx->flexible_active); } return 0; @@ -3690,24 +3699,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 +3718,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 +3726,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);