From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24226C43219 for ; Mon, 29 Apr 2019 14:45:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E9C3020656 for ; Mon, 29 Apr 2019 14:45:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728573AbfD2OpW (ORCPT ); Mon, 29 Apr 2019 10:45:22 -0400 Received: from mga05.intel.com ([192.55.52.43]:1324 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728560AbfD2OpV (ORCPT ); Mon, 29 Apr 2019 10:45:21 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Apr 2019 07:45:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,409,1549958400"; d="scan'208";a="154739495" Received: from otc-lr-04.jf.intel.com ([10.54.39.157]) by orsmga002.jf.intel.com with ESMTP; 29 Apr 2019 07:45:20 -0700 From: kan.liang@linux.intel.com To: peterz@infradead.org, tglx@linutronix.de, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: eranian@google.com, tj@kernel.org, ak@linux.intel.com, Kan Liang Subject: [PATCH 1/4] perf: Fix system-wide events miscounting during cgroup monitoring Date: Mon, 29 Apr 2019 07:44:02 -0700 Message-Id: <1556549045-71814-2-git-send-email-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1556549045-71814-1-git-send-email-kan.liang@linux.intel.com> References: <1556549045-71814-1-git-send-email-kan.liang@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kan Liang When counting system-wide events and cgroup events simultaneously, the value of system-wide events are miscounting. For example, perf stat -e cycles,instructions -e cycles,instructions -G cgroup1,cgroup1,cgroup2,cgroup2 -a -e cycles,instructions -I 1000 1.096265502 12,375,398,872 cycles cgroup1 1.096265502 8,396,184,503 instructions cgroup1 # 0.10 insn per cycle 1.096265502 109,609,027,112 cycles cgroup2 1.096265502 11,533,690,148 instructions cgroup2 # 0.14 insn per cycle 1.096265502 121,672,937,058 cycles 1.096265502 19,331,496,727 instructions # 0.24 insn per cycle The events are identical events for system-wide and cgroup. The value of system-wide events is less than the sum of cgroup events, which is wrong. Both system-wide and cgroup are per-cpu. They share the same cpuctx groups, cpuctx->flexible_groups/pinned_groups. In context switch, cgroup switch tries to schedule all the events in the cpuctx groups. The unmatched cgroup events can be filtered by its event->cgrp. However, system-wide events, which event->cgrp is NULL, are unconditionally switched, which causes miscounting. Introduce cgrp_switch in cpuctx to indicate the cgroup context switch. If it's system-wide event in context switch, don't try to switch it. Fixes: e5d1367f17ba ("perf: Add cgroup support") Signed-off-by: Kan Liang --- include/linux/perf_event.h | 1 + kernel/events/core.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index e47ef76..039e2f2 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -795,6 +795,7 @@ struct perf_cpu_context { #ifdef CONFIG_CGROUP_PERF struct perf_cgroup *cgrp; struct list_head cgrp_cpuctx_entry; + unsigned int cgrp_switch :1; #endif struct list_head sched_cb_entry; diff --git a/kernel/events/core.c b/kernel/events/core.c index dc7dead..388dd42 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -809,6 +809,7 @@ static void perf_cgroup_switch(struct task_struct *task, int mode) perf_ctx_lock(cpuctx, cpuctx->task_ctx); perf_pmu_disable(cpuctx->ctx.pmu); + cpuctx->cgrp_switch = true; if (mode & PERF_CGROUP_SWOUT) { cpu_ctx_sched_out(cpuctx, EVENT_ALL); @@ -832,6 +833,7 @@ static void perf_cgroup_switch(struct task_struct *task, int mode) &cpuctx->ctx); cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); } + cpuctx->cgrp_switch = false; perf_pmu_enable(cpuctx->ctx.pmu); perf_ctx_unlock(cpuctx, cpuctx->task_ctx); } @@ -2944,13 +2946,25 @@ static void ctx_sched_out(struct perf_event_context *ctx, perf_pmu_disable(ctx->pmu); if (is_active & EVENT_PINNED) { - list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list) + list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list) { +#ifdef CONFIG_CGROUP_PERF + /* Don't sched system-wide event when cgroup context switch */ + if (cpuctx->cgrp_switch && !event->cgrp) + continue; +#endif group_sched_out(event, cpuctx, ctx); + } } if (is_active & EVENT_FLEXIBLE) { - list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list) + list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list) { +#ifdef CONFIG_CGROUP_PERF + /* Don't sched system-wide event when cgroup context switch */ + if (cpuctx->cgrp_switch && !event->cgrp) + continue; +#endif group_sched_out(event, cpuctx, ctx); + } } perf_pmu_enable(ctx->pmu); } @@ -3280,6 +3294,12 @@ static int pinned_sched_in(struct perf_event *event, void *data) if (event->state <= PERF_EVENT_STATE_OFF) return 0; +#ifdef CONFIG_CGROUP_PERF + /* Don't sched system-wide event when cgroup context switch */ + if (sid->cpuctx->cgrp_switch && !event->cgrp) + return 0; +#endif + if (!event_filter_match(event)) return 0; @@ -3305,6 +3325,12 @@ static int flexible_sched_in(struct perf_event *event, void *data) if (event->state <= PERF_EVENT_STATE_OFF) return 0; +#ifdef CONFIG_CGROUP_PERF + /* Don't sched system-wide event when cgroup context switch */ + if (sid->cpuctx->cgrp_switch && !event->cgrp) + return 0; +#endif + if (!event_filter_match(event)) return 0; -- 2.7.4