Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp485426rdf; Tue, 21 Nov 2023 07:57:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNnZfChhAJpKr4HMZKaM7nG98wUTco7FknetOK0kzO8B2uMFRsbnw2WqRpEViDJ0XEZ5dZ X-Received: by 2002:a05:6808:158a:b0:3b8:3399:8731 with SMTP id t10-20020a056808158a00b003b833998731mr2456662oiw.57.1700582267381; Tue, 21 Nov 2023 07:57:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700582267; cv=none; d=google.com; s=arc-20160816; b=CyFRlXgcyFqNSOuaZdLlGtKvTnyUFg+won4eNdChbd5H/dNTKLdE01u+YqU0iXcv4i E5APkalfzhqsyGgLnvI9VBuI0JgDFTXpnKJAhAuE44UF75EasXZWTxpa4fvIVhIyoAaH gUMZrmdomrkuVsep1Fo6AaoTs98R4GE6jvHEfB1l+LtR0yMggnXqO5aD45hqBkUYZNYy cMA8pC59wc8TwPegZXxRHC00+qD3WJGsj10mRKhT9mKscB7yh+zpW2qxS5b/zRokz49R LjA46WuoC0r7lHvdI1YpU7/hxkZMNwX7fa9mFS2BjYYVCul83KmxXLEprpEw2V1ACc2Q iYmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=183KfsyJKSaYl3a0H26WQMDrWbyFhEe84LH1Br4wqCM=; fh=IFOIc08B6+2JaulQgZQZMGYRAf2eC3sle3aBU+yMoqg=; b=XFxC69EtbHnEBHVOjUPRFxl83q4JQV2OY9GetwiUmpMXCCr5+kEDIwDxnjoGsF/igN RcQPhBbJQOC03ZW9ez0AT5Yr+d43QDMorAY7rWhb6rcja0dO3QAvBJzqXaZ2Znb3pfmq yc8FWVrDQWHn/CAD8xgLhh5M4+913wajvZpJ6NQflvL9bbKWpkV7udNvHI1Cm2fk4AUq X9T5VEKDPVBDT/nyB++xzxCI0016qzrW0rH4hmsV8xVnagDRcIttbQrRHHqZVvyUtBrP 0QRjrl5ewh0pF7oAMWcjxjGAuS7VALmPQWJu39XPZ2PPi+k12Vd5zNqPtflAg1NpEVNl BuvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GVzMbo4O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id by8-20020a056a02058800b005c1b59f008bsi11187143pgb.617.2023.11.21.07.57.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 07:57:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GVzMbo4O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id B95F9802FA73; Tue, 21 Nov 2023 07:57:38 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234646AbjKUP5g (ORCPT + 99 others); Tue, 21 Nov 2023 10:57:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232517AbjKUP5f (ORCPT ); Tue, 21 Nov 2023 10:57:35 -0500 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1934E139 for ; Tue, 21 Nov 2023 07:57:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700582252; x=1732118252; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=eMXSOw1Tlz4fFt+3gazcD/G08h/FvUpyW6nMRPXwiFs=; b=GVzMbo4O2uOXwjPdvP5KRIxJHJGfg4WOVIIRgZL2JZE4AgKI1pf8WSAl C1Zq2LZuNn0JxWh0BtT3JXBTQ42Pxe7eEdBfKqBgMSen+Amv8ON/g0Mlq Br1MFjsq23Vf/flekVuGY4a9FnUh8/0GxTOAGMaLA07/I8+8Irpi6x0DF PuQtg6EteYhloelW2NM9xoCSo1gODRi1H5Ez6FsA+BDwIHtfVeiukDwfm Ins0aZ/3BIQNvOe8ZFgMTbGJoF3tasAR7FzKor0Z59hxAYvpDdfym04GU ch7DTzx2OPEbL3sFAS9aEx5FW9oIPRM2YdAO/87eRwwi3pvlcdrLaHT5s g==; X-IronPort-AV: E=McAfee;i="6600,9927,10901"; a="395791732" X-IronPort-AV: E=Sophos;i="6.04,216,1695711600"; d="scan'208";a="395791732" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2023 07:57:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10901"; a="743087677" X-IronPort-AV: E=Sophos;i="6.04,216,1695711600"; d="scan'208";a="743087677" Received: from linux.intel.com ([10.54.29.200]) by orsmga006.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2023 07:57:31 -0800 Received: from [10.209.157.143] (kliang2-mobl1.ccr.corp.intel.com [10.209.157.143]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by linux.intel.com (Postfix) with ESMTPS id D6A53580BB9; Tue, 21 Nov 2023 07:57:29 -0800 (PST) Message-ID: <46b5cc1b-d703-4ffb-847f-bc14d7884cfa@linux.intel.com> Date: Tue, 21 Nov 2023 10:57:28 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/3] perf/core: Update perf_adjust_freq_unthr_context() Content-Language: en-US To: Namhyung Kim , Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Ian Rogers , Mingwei Zhang References: <20231120221932.213710-1-namhyung@kernel.org> From: "Liang, Kan" In-Reply-To: <20231120221932.213710-1-namhyung@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 21 Nov 2023 07:57:39 -0800 (PST) On 2023-11-20 5:19 p.m., Namhyung Kim wrote: > It was unnecessarily disabling and enabling PMUs for each event. It > should be done at PMU level. Add pmu_ctx->nr_freq counter to check it > at each PMU. As pmu context has separate active lists for pinned group > and flexible group, factor out a new function to do the job. > > Another minor optimization is that it can skip PMUs w/ CAP_NO_INTERRUPT > even if it needs to unthrottle sampling events. > > Signed-off-by: Namhyung Kim Reviewed-by: Kan Liang Thanks, Kan > --- > include/linux/perf_event.h | 1 + > kernel/events/core.c | 68 +++++++++++++++++++++++--------------- > 2 files changed, 43 insertions(+), 26 deletions(-) > > diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h > index 0367d748fae0..3eb17dc89f5e 100644 > --- a/include/linux/perf_event.h > +++ b/include/linux/perf_event.h > @@ -879,6 +879,7 @@ struct perf_event_pmu_context { > > unsigned int nr_events; > unsigned int nr_cgroups; > + unsigned int nr_freq; > > atomic_t refcount; /* event <-> epc */ > struct rcu_head rcu_head; > diff --git a/kernel/events/core.c b/kernel/events/core.c > index 3eb26c2c6e65..53e2ad73102d 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -2275,8 +2275,10 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx) > > if (!is_software_event(event)) > cpc->active_oncpu--; > - if (event->attr.freq && event->attr.sample_freq) > + if (event->attr.freq && event->attr.sample_freq) { > ctx->nr_freq--; > + epc->nr_freq--; > + } > if (event->attr.exclusive || !cpc->active_oncpu) > cpc->exclusive = 0; > > @@ -2531,9 +2533,10 @@ event_sched_in(struct perf_event *event, struct perf_event_context *ctx) > > if (!is_software_event(event)) > cpc->active_oncpu++; > - if (event->attr.freq && event->attr.sample_freq) > + if (event->attr.freq && event->attr.sample_freq) { > ctx->nr_freq++; > - > + epc->nr_freq++; > + } > if (event->attr.exclusive) > cpc->exclusive = 1; > > @@ -4096,30 +4099,14 @@ static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bo > } > } > > -/* > - * combine freq adjustment with unthrottling to avoid two passes over the > - * events. At the same time, make sure, having freq events does not change > - * the rate of unthrottling as that would introduce bias. > - */ > -static void > -perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle) > +static void perf_adjust_freq_unthr_events(struct list_head *event_list) > { > struct perf_event *event; > struct hw_perf_event *hwc; > u64 now, period = TICK_NSEC; > s64 delta; > > - /* > - * only need to iterate over all events iff: > - * - context have events in frequency mode (needs freq adjust) > - * - there are events to unthrottle on this cpu > - */ > - if (!(ctx->nr_freq || unthrottle)) > - return; > - > - raw_spin_lock(&ctx->lock); > - > - list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { > + list_for_each_entry(event, event_list, active_list) { > if (event->state != PERF_EVENT_STATE_ACTIVE) > continue; > > @@ -4127,8 +4114,6 @@ perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle) > if (!event_filter_match(event)) > continue; > > - perf_pmu_disable(event->pmu); > - > hwc = &event->hw; > > if (hwc->interrupts == MAX_INTERRUPTS) { > @@ -4138,7 +4123,7 @@ perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle) > } > > if (!event->attr.freq || !event->attr.sample_freq) > - goto next; > + continue; > > /* > * stop the event and update event->count > @@ -4160,8 +4145,39 @@ perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle) > perf_adjust_period(event, period, delta, false); > > event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); > - next: > - perf_pmu_enable(event->pmu); > + } > +} > + > +/* > + * combine freq adjustment with unthrottling to avoid two passes over the > + * events. At the same time, make sure, having freq events does not change > + * the rate of unthrottling as that would introduce bias. > + */ > +static void > +perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle) > +{ > + struct perf_event_pmu_context *pmu_ctx; > + > + /* > + * only need to iterate over all events iff: > + * - context have events in frequency mode (needs freq adjust) > + * - there are events to unthrottle on this cpu > + */ > + if (!(ctx->nr_freq || unthrottle)) > + return; > + > + raw_spin_lock(&ctx->lock); > + > + list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { > + if (!(pmu_ctx->nr_freq || unthrottle)) > + continue; > + if (pmu_ctx->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) > + continue; > + > + perf_pmu_disable(pmu_ctx->pmu); > + perf_adjust_freq_unthr_events(&pmu_ctx->pinned_active); > + perf_adjust_freq_unthr_events(&pmu_ctx->flexible_active); > + perf_pmu_enable(pmu_ctx->pmu); > } > > raw_spin_unlock(&ctx->lock);