From patchwork Fri Mar 7 20:22:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Barnett X-Patchwork-Id: 14007117 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 735EBC19F32 for ; Fri, 7 Mar 2025 20:27:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=vBIXxrcBrCU8gSPBHd/jAHltxSRSe7nuoZNbpUeVov8=; b=hlG0ldjfZmyGMGRmg7fhew7nhA thcxlTMM0WuFfNF1KWnZs5CE+DxzNjF8L7ndo2zlbjfLHhVMHVMqHgr6yyEYmVhukGaHbVcaLsF+d OvB3re3cSGCA9xKJfydjI0LSJ7bhIYbPxEJU+7+ewLAD5K0sqCOrHjRZMLy9WHhNASgNFU6uv2oon k/jXJh1a7KKdJn0bCDqg21XFeb6N4X3WUQhpCL/l3rqOzGti4O4I4Jgp4UKVc+UJOhLKrey3GHGAA WDa0Uzy1Q/k1RqFGZuvx6gAdK5mDDaTL89MoJ2+UIk7NXuhBKPeTJ8G46vBeJoAB2GpDizPQB3/Ra izgG+ZwA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeHv-0000000FUDK-0VnZ; Fri, 07 Mar 2025 20:27:19 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeEl-0000000FTl6-1uFC for linux-arm-kernel@lists.infradead.org; Fri, 07 Mar 2025 20:24:04 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6D6271477; Fri, 7 Mar 2025 12:24:15 -0800 (PST) Received: from e128066.arm.com (unknown [10.57.40.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2B5CE3F5A1; Fri, 7 Mar 2025 12:23:59 -0800 (PST) From: mark.barnett@arm.com To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, irogers@google.com Cc: ben.gainey@arm.com, deepak.surti@arm.com, ak@linux.intel.com, will@kernel.org, james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Mark Barnett Subject: [PATCH v3 1/5] perf: Record sample last_period before updating Date: Fri, 7 Mar 2025 20:22:43 +0000 Message-Id: <20250307202247.648633-2-mark.barnett@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250307202247.648633-1-mark.barnett@arm.com> References: <20250307202247.648633-1-mark.barnett@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250307_122403_594799_8C78C113 X-CRM114-Status: GOOD ( 15.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Mark Barnett This change alters the PowerPC and x86 driver implementations to record the last sample period before the event is updated for the next period. A common pattern in PMU driver implementations is to have a "*_event_set_period" function which takes care of updating the various period-related fields in a perf_event structure. In most cases, the drivers choose to call this function after initializing a sample data structure with perf_sample_data_init. The x86 and PowerPC drivers deviate from this, choosing to update the period before initializing the sample data. When using an event with an alternate sample period, this causes an incorrect period to be written to the sample data that gets reported to userspace. Link: https://lore.kernel.org/r/20240515193610.2350456-4-yabinc@google.com Signed-off-by: Mark Barnett --- arch/powerpc/perf/core-book3s.c | 3 ++- arch/powerpc/perf/core-fsl-emb.c | 3 ++- arch/x86/events/core.c | 4 +++- arch/x86/events/intel/core.c | 5 ++++- arch/x86/events/intel/knc.c | 4 +++- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/perf/core-book3s.c b/arch/powerpc/perf/core-book3s.c index 2b79171ee185..234803441caa 100644 --- a/arch/powerpc/perf/core-book3s.c +++ b/arch/powerpc/perf/core-book3s.c @@ -2231,6 +2231,7 @@ static void record_and_restart(struct perf_event *event, unsigned long val, struct pt_regs *regs) { u64 period = event->hw.sample_period; + const u64 last_period = event->hw.last_period; s64 prev, delta, left; int record = 0; @@ -2296,7 +2297,7 @@ static void record_and_restart(struct perf_event *event, unsigned long val, if (record) { struct perf_sample_data data; - perf_sample_data_init(&data, ~0ULL, event->hw.last_period); + perf_sample_data_init(&data, ~0ULL, last_period); if (event->attr.sample_type & PERF_SAMPLE_ADDR_TYPE) perf_get_data_addr(event, regs, &data.addr); diff --git a/arch/powerpc/perf/core-fsl-emb.c b/arch/powerpc/perf/core-fsl-emb.c index 1a53ab08447c..d2ffcc7021c5 100644 --- a/arch/powerpc/perf/core-fsl-emb.c +++ b/arch/powerpc/perf/core-fsl-emb.c @@ -590,6 +590,7 @@ static void record_and_restart(struct perf_event *event, unsigned long val, struct pt_regs *regs) { u64 period = event->hw.sample_period; + const u64 last_period = event->hw.last_period; s64 prev, delta, left; int record = 0; @@ -632,7 +633,7 @@ static void record_and_restart(struct perf_event *event, unsigned long val, if (record) { struct perf_sample_data data; - perf_sample_data_init(&data, 0, event->hw.last_period); + perf_sample_data_init(&data, 0, last_period); if (perf_event_overflow(event, &data, regs)) fsl_emb_pmu_stop(event, 0); diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 8f218ac0d445..a78a8f3244ef 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -1673,6 +1673,7 @@ int x86_pmu_handle_irq(struct pt_regs *regs) struct cpu_hw_events *cpuc; struct perf_event *event; int idx, handled = 0; + u64 last_period; u64 val; cpuc = this_cpu_ptr(&cpu_hw_events); @@ -1692,6 +1693,7 @@ int x86_pmu_handle_irq(struct pt_regs *regs) continue; event = cpuc->events[idx]; + last_period = event->hw.last_period; val = static_call(x86_pmu_update)(event); if (val & (1ULL << (x86_pmu.cntval_bits - 1))) @@ -1705,7 +1707,7 @@ int x86_pmu_handle_irq(struct pt_regs *regs) if (!static_call(x86_pmu_set_period)(event)) continue; - perf_sample_data_init(&data, 0, event->hw.last_period); + perf_sample_data_init(&data, 0, last_period); perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL); diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 7601196d1d18..add791c6e3d9 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3116,16 +3116,19 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { struct perf_event *event = cpuc->events[bit]; + u64 last_period; handled++; if (!test_bit(bit, cpuc->active_mask)) continue; + last_period = event->hw.last_period; + if (!intel_pmu_save_and_restart(event)) continue; - perf_sample_data_init(&data, 0, event->hw.last_period); + perf_sample_data_init(&data, 0, last_period); if (has_branch_stack(event)) intel_pmu_lbr_save_brstack(&data, cpuc, event); diff --git a/arch/x86/events/intel/knc.c b/arch/x86/events/intel/knc.c index 034a1f6a457c..3e8ec049b46d 100644 --- a/arch/x86/events/intel/knc.c +++ b/arch/x86/events/intel/knc.c @@ -241,16 +241,18 @@ static int knc_pmu_handle_irq(struct pt_regs *regs) for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { struct perf_event *event = cpuc->events[bit]; + u64 last_period; handled++; if (!test_bit(bit, cpuc->active_mask)) continue; + last_period = event->hw.last_period; if (!intel_pmu_save_and_restart(event)) continue; - perf_sample_data_init(&data, 0, event->hw.last_period); + perf_sample_data_init(&data, 0, last_period); if (perf_event_overflow(event, &data, regs)) x86_pmu_stop(event, 0); From patchwork Fri Mar 7 20:22:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Barnett X-Patchwork-Id: 14007118 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A80F6C28B23 for ; Fri, 7 Mar 2025 20:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=XlcdbkrabUG2lcUBU5BlLlpmLXWnOKhgRPLM1uz4Ib0=; b=nXgf05RwycXjZQsEObzQ6mBZrB 0fA9QdjX+k4M1Qfwt6hLBEyF8aFFTsFLsPy2e1MDzK2vJOgzwisQoMtmuusEhf4drgPmpp9a/+JGJ vcYqfHohH7BIIYbAxdxqyvFme3EIXC7aT8v42STWlNBEHSkv6aXYXrS2RvPfc4LSVg4s3GbLjmg7h 6b6EmA/6IkajVh+5bZtcBg7xQZ8OH84k4Ym36AsDje8/hXAyraFvKZEAIGLIPFf4HWzjaKV8voRnk FuWFxL0UP1ZeQDVkuaqFx9hxW2mkmVjGdiXYWGpnV1ri4IT+9ozLPJymb5PY+FVMpd3SHXYOSrDCd qQbENyZQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeJV-0000000FUNl-3Y3h; Fri, 07 Mar 2025 20:28:57 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeEu-0000000FTnK-02mF for linux-arm-kernel@lists.infradead.org; Fri, 07 Mar 2025 20:24:13 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E667716A3; Fri, 7 Mar 2025 12:24:23 -0800 (PST) Received: from e128066.arm.com (unknown [10.57.40.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id A99083F5A1; Fri, 7 Mar 2025 12:24:07 -0800 (PST) From: mark.barnett@arm.com To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, irogers@google.com Cc: ben.gainey@arm.com, deepak.surti@arm.com, ak@linux.intel.com, will@kernel.org, james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Mark Barnett Subject: [PATCH v3 2/5] perf: Allow periodic events to alternate between two sample periods Date: Fri, 7 Mar 2025 20:22:44 +0000 Message-Id: <20250307202247.648633-3-mark.barnett@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250307202247.648633-1-mark.barnett@arm.com> References: <20250307202247.648633-1-mark.barnett@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250307_122412_134598_D8D3F77B X-CRM114-Status: GOOD ( 21.23 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Ben Gainey This change modifies perf_event_attr to add a second, alternative sample period field, and modifies the core perf overflow handling such that when specified an event will alternate between two sample periods. Currently, perf does not provide a mechanism for decoupling the period over which counters are counted from the period between samples. This is problematic for building a tool to measure per-function metrics derived from a sampled counter group. Ideally such a tool wants a very small sample window in order to correctly attribute the metrics to a given function, but prefers a larger sample period that provides representative coverage without excessive probe effect, triggering throttling, or generating excessive amounts of data. By alternating between a long and short sample_period and subsequently discarding the long samples, tools may decouple the period between samples that the tool cares about from the window of time over which interesting counts are collected. It is expected that typically tools would use this feature with the cycles or instructions events as an approximation for time, but no restrictions are applied to which events this can be applied to. Signed-off-by: Ben Gainey Signed-off-by: Mark Barnett --- include/linux/perf_event.h | 5 +++++ include/uapi/linux/perf_event.h | 3 +++ kernel/events/core.c | 39 ++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 8333f132f4a9..99ba72c8fb6d 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -276,6 +276,11 @@ struct hw_perf_event { */ u64 freq_time_stamp; u64 freq_count_stamp; + + /* + * Indicates that the alternative sample period is used + */ + bool using_alt_sample_period; #endif }; diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 0524d541d4e3..499a8673df8e 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -379,6 +379,7 @@ enum perf_event_read_format { #define PERF_ATTR_SIZE_VER6 120 /* add: aux_sample_size */ #define PERF_ATTR_SIZE_VER7 128 /* add: sig_data */ #define PERF_ATTR_SIZE_VER8 136 /* add: config3 */ +#define PERF_ATTR_SIZE_VER9 144 /* add: alt_sample_period */ /* * Hardware event_id to monitor via a performance monitoring event: @@ -531,6 +532,8 @@ struct perf_event_attr { __u64 sig_data; __u64 config3; /* extension of config2 */ + + __u64 alt_sample_period; }; /* diff --git a/kernel/events/core.c b/kernel/events/core.c index bcb09e011e9e..7ec8ec6ba7ef 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -4178,6 +4178,8 @@ static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bo s64 period, sample_period; s64 delta; + WARN_ON_ONCE(hwc->using_alt_sample_period); + period = perf_calculate_period(event, nsec, count); delta = (s64)(period - hwc->sample_period); @@ -9894,6 +9896,7 @@ static int __perf_event_overflow(struct perf_event *event, int throttle, struct perf_sample_data *data, struct pt_regs *regs) { + struct hw_perf_event *hwc = &event->hw; int events = atomic_read(&event->event_limit); int ret = 0; @@ -9913,6 +9916,18 @@ static int __perf_event_overflow(struct perf_event *event, !bpf_overflow_handler(event, data, regs)) goto out; + /* + * Swap the sample period to the alternative period + */ + if (event->attr.alt_sample_period) { + bool using_alt = hwc->using_alt_sample_period; + u64 sample_period = (using_alt ? event->attr.sample_period + : event->attr.alt_sample_period); + + hwc->sample_period = sample_period; + hwc->using_alt_sample_period = !using_alt; + } + /* * XXX event_limit might not quite work as expected on inherited * events @@ -12335,9 +12350,19 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, if (attr->freq && attr->sample_freq) hwc->sample_period = 1; hwc->last_period = hwc->sample_period; - local64_set(&hwc->period_left, hwc->sample_period); + /* + * alt_sample_period cannot be used with freq + */ + if (attr->freq && attr->alt_sample_period) + goto err_ns; + + if (attr->alt_sample_period) { + hwc->sample_period = attr->alt_sample_period; + hwc->using_alt_sample_period = true; + } + /* * We do not support PERF_SAMPLE_READ on inherited events unless * PERF_SAMPLE_TID is also selected, which allows inherited events to @@ -12807,9 +12832,21 @@ SYSCALL_DEFINE5(perf_event_open, if (attr.freq) { if (attr.sample_freq > sysctl_perf_event_sample_rate) return -EINVAL; + if (attr.alt_sample_period) + return -EINVAL; } else { if (attr.sample_period & (1ULL << 63)) return -EINVAL; + if (attr.alt_sample_period) { + if (!attr.sample_period) + return -EINVAL; + if (attr.alt_sample_period & (1ULL << 63)) + return -EINVAL; + if (attr.alt_sample_period > attr.sample_period) + return -EINVAL; + if (attr.alt_sample_period == attr.sample_period) + attr.alt_sample_period = 0; + } } /* Only privileged users can get physical addresses */ From patchwork Fri Mar 7 20:22:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Barnett X-Patchwork-Id: 14007119 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 471DEC28B27 for ; Fri, 7 Mar 2025 20:30:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Flp3IMUSsMJZJ0dlK5G3dBfDptX1d3Zaq6rpN6QKmvc=; b=0SJmb+9rf6wsiR6aPc0f0hrWrT oE4fhIvMMMmMpQzoWXtNnk5UFd+Ms1dvtF3mWOZpXYP27JWAhn6xfzy//BiLE9oLWKUI/7QdxMhAt qyxh1imRFN2JUuGZ8dyJ1Nd4Nsn/cbuo8rWxMmCe5ohiIdbS4W+COXOGmIs+NbfaruHrbIiI4RWBM U6jvf3ij/z7T7d6tFHj0dooZT9bC8RCB6zP8WHPpTsHG3FKoCBkEQeyJowFyTMc5PCgqfyw2Mlr86 GdcOMPj0pZ0bJ5kevWJJm56UXvcY+3NDSMW/4RkstIsAkL/ad7N9oI/0Cz2DCTasFsvfgFKBbmBRq aLRL6ZoA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeL7-0000000FUYR-23yo; Fri, 07 Mar 2025 20:30:37 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeF3-0000000FTpZ-3HKJ for linux-arm-kernel@lists.infradead.org; Fri, 07 Mar 2025 20:24:23 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B20631A2D; Fri, 7 Mar 2025 12:24:31 -0800 (PST) Received: from e128066.arm.com (unknown [10.57.40.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 453603F5A1; Fri, 7 Mar 2025 12:24:15 -0800 (PST) From: mark.barnett@arm.com To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, irogers@google.com Cc: ben.gainey@arm.com, deepak.surti@arm.com, ak@linux.intel.com, will@kernel.org, james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Mark Barnett Subject: [PATCH v3 3/5] perf: Allow adding fixed random jitter to the alternate sampling period Date: Fri, 7 Mar 2025 20:22:45 +0000 Message-Id: <20250307202247.648633-4-mark.barnett@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250307202247.648633-1-mark.barnett@arm.com> References: <20250307202247.648633-1-mark.barnett@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250307_122421_858130_FC6C6FF8 X-CRM114-Status: GOOD ( 14.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Ben Gainey This change modifies the core perf overflow handler, adding some small random jitter to each sample period whenever an event switches between the two alternate sample periods. A new flag is added to perf_event_attr to opt into this behaviour. This change follows the discussion in [1], where it is recognized that it may be possible for certain patterns of execution to end up with biased results. [1] https://lore.kernel.org/linux-perf-users/Zc24eLqZycmIg3d2@tassilo/ Signed-off-by: Ben Gainey Signed-off-by: Mark Barnett --- include/uapi/linux/perf_event.h | 7 ++++++- kernel/events/core.c | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 499a8673df8e..c0076ce8f80a 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -461,7 +461,12 @@ struct perf_event_attr { inherit_thread : 1, /* children only inherit if cloned with CLONE_THREAD */ remove_on_exec : 1, /* event is removed from task on exec */ sigtrap : 1, /* send synchronous SIGTRAP on event */ - __reserved_1 : 26; + /* + * Add a limited amount of jitter on each alternate period, where + * the jitter is between [0, (2< #include #include +#include #include #include #include @@ -9922,7 +9923,10 @@ static int __perf_event_overflow(struct perf_event *event, if (event->attr.alt_sample_period) { bool using_alt = hwc->using_alt_sample_period; u64 sample_period = (using_alt ? event->attr.sample_period - : event->attr.alt_sample_period); + : event->attr.alt_sample_period) + + (event->attr.jitter_alt_period + ? get_random_u32_below(2 << event->attr.jitter_alt_period) + : 0); hwc->sample_period = sample_period; hwc->using_alt_sample_period = !using_alt; @@ -12849,6 +12853,9 @@ SYSCALL_DEFINE5(perf_event_open, } } + if (attr.jitter_alt_period && !attr.alt_sample_period) + return -EINVAL; + /* Only privileged users can get physical addresses */ if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) { err = perf_allow_kernel(&attr); From patchwork Fri Mar 7 20:22:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Barnett X-Patchwork-Id: 14007120 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 05768C19F32 for ; Fri, 7 Mar 2025 20:32:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=SH9MJgprxs3cl0psnq/WwzLvqyh6vz/UqBNIGhQZagg=; b=dGNQhGKVeYsCTGiumGfFlDUXvu qahf0EoHJHMULGZEI4k6vcnBahJTsM27LfDCsqzikKlSaae1XNlRrqdtyEzyKfpvRUtam+gl2q+d9 0od7euhxlAsNd3iYjw3hsH5C3fgHGcgNP4oabmy6QETtPA6UVc3hk0zgPQsHrtT1xYUbz60IqwneT dt5rEtWGXcaJIZb1DoRTRb53hK7CVqAp9J5wfOQMPPfB0MIOA6QWBIRg3VLyDGCuPZmxA7KXXXVmd 1IBhYQNMm6ciZLeE3YHYPBP2a/jwRbSdTxaEYXm6denkSXweJKJD1+DRrSE1PsrFT7CqTWOAITCb9 ljxEpUEA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeMh-0000000FUjq-2LWe; Fri, 07 Mar 2025 20:32:15 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeF8-0000000FTrY-3Aod for linux-arm-kernel@lists.infradead.org; Fri, 07 Mar 2025 20:24:27 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 76D8122EA; Fri, 7 Mar 2025 12:24:38 -0800 (PST) Received: from e128066.arm.com (unknown [10.57.40.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 7F51C3F5A1; Fri, 7 Mar 2025 12:24:22 -0800 (PST) From: mark.barnett@arm.com To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, irogers@google.com Cc: ben.gainey@arm.com, deepak.surti@arm.com, ak@linux.intel.com, will@kernel.org, james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Mark Barnett Subject: [PATCH v3 4/5] tools/perf: Modify event parser to support alt-period term Date: Fri, 7 Mar 2025 20:22:46 +0000 Message-Id: <20250307202247.648633-5-mark.barnett@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250307202247.648633-1-mark.barnett@arm.com> References: <20250307202247.648633-1-mark.barnett@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250307_122426_882438_EC78B57A X-CRM114-Status: GOOD ( 17.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Ben Gainey parse-events is modified, adding the "alt-period" term which can be used to specify the alternative sampling period. Signed-off-by: Ben Gainey Signed-off-by: Mark Barnett --- tools/include/uapi/linux/perf_event.h | 3 +++ tools/perf/tests/shell/attr/base-record | 3 ++- tools/perf/tests/shell/attr/base-record-spe | 1 + tools/perf/tests/shell/attr/base-stat | 3 ++- tools/perf/tests/shell/attr/system-wide-dummy | 3 ++- .../tests/shell/attr/test-record-alt-period-term | 12 ++++++++++++ tools/perf/tests/shell/attr/test-record-dummy-C0 | 3 ++- tools/perf/tests/shell/lib/attr.py | 1 + tools/perf/util/evsel.c | 1 + tools/perf/util/parse-events.c | 15 +++++++++++++++ tools/perf/util/parse-events.h | 3 ++- tools/perf/util/parse-events.l | 1 + tools/perf/util/perf_event_attr_fprintf.c | 1 + tools/perf/util/pmu.c | 1 + 14 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tools/perf/tests/shell/attr/test-record-alt-period-term diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h index 0524d541d4e3..499a8673df8e 100644 --- a/tools/include/uapi/linux/perf_event.h +++ b/tools/include/uapi/linux/perf_event.h @@ -379,6 +379,7 @@ enum perf_event_read_format { #define PERF_ATTR_SIZE_VER6 120 /* add: aux_sample_size */ #define PERF_ATTR_SIZE_VER7 128 /* add: sig_data */ #define PERF_ATTR_SIZE_VER8 136 /* add: config3 */ +#define PERF_ATTR_SIZE_VER9 144 /* add: alt_sample_period */ /* * Hardware event_id to monitor via a performance monitoring event: @@ -531,6 +532,8 @@ struct perf_event_attr { __u64 sig_data; __u64 config3; /* extension of config2 */ + + __u64 alt_sample_period; }; /* diff --git a/tools/perf/tests/shell/attr/base-record b/tools/perf/tests/shell/attr/base-record index b44e4e6e4443..28a7233f7bc1 100644 --- a/tools/perf/tests/shell/attr/base-record +++ b/tools/perf/tests/shell/attr/base-record @@ -5,7 +5,7 @@ group_fd=-1 flags=0|8 cpu=* type=0|1 -size=136 +size=144 config=0|1 sample_period=* sample_type=263 @@ -39,3 +39,4 @@ config2=0 branch_sample_type=0 sample_regs_user=0 sample_stack_user=0 +alt_sample_period=0 diff --git a/tools/perf/tests/shell/attr/base-record-spe b/tools/perf/tests/shell/attr/base-record-spe index 08fa96b59240..ad8eb72e655a 100644 --- a/tools/perf/tests/shell/attr/base-record-spe +++ b/tools/perf/tests/shell/attr/base-record-spe @@ -38,3 +38,4 @@ config2=* branch_sample_type=* sample_regs_user=* sample_stack_user=* +alt_sample_period=0 diff --git a/tools/perf/tests/shell/attr/base-stat b/tools/perf/tests/shell/attr/base-stat index fccd8ec4d1b0..2de92c5c300d 100644 --- a/tools/perf/tests/shell/attr/base-stat +++ b/tools/perf/tests/shell/attr/base-stat @@ -5,7 +5,7 @@ group_fd=-1 flags=0|8 cpu=* type=0 -size=136 +size=144 config=0 sample_period=0 sample_type=65536 @@ -39,3 +39,4 @@ config2=0 branch_sample_type=0 sample_regs_user=0 sample_stack_user=0 +alt_sample_period=0 diff --git a/tools/perf/tests/shell/attr/system-wide-dummy b/tools/perf/tests/shell/attr/system-wide-dummy index a1e1d6a263bf..c0a17bb3c022 100644 --- a/tools/perf/tests/shell/attr/system-wide-dummy +++ b/tools/perf/tests/shell/attr/system-wide-dummy @@ -7,7 +7,7 @@ cpu=* pid=-1 flags=8 type=1 -size=136 +size=144 config=9 sample_period=1 # PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | @@ -50,3 +50,4 @@ config2=0 branch_sample_type=0 sample_regs_user=0 sample_stack_user=0 +alt_sample_period=0 diff --git a/tools/perf/tests/shell/attr/test-record-alt-period-term b/tools/perf/tests/shell/attr/test-record-alt-period-term new file mode 100644 index 000000000000..fcdb790adcd3 --- /dev/null +++ b/tools/perf/tests/shell/attr/test-record-alt-period-term @@ -0,0 +1,12 @@ +[config] +command = record +args = --no-bpf-event -e cycles/period=3,alt-period=2/ -- kill >/dev/null 2>&1 +ret = 1 +kernel_since = 6.11 + +[event-10:base-record] +sample_period=3 +alt_sample_period=2 + +freq=0 +sample_type=7 diff --git a/tools/perf/tests/shell/attr/test-record-dummy-C0 b/tools/perf/tests/shell/attr/test-record-dummy-C0 index 91499405fff4..e6315918a95e 100644 --- a/tools/perf/tests/shell/attr/test-record-dummy-C0 +++ b/tools/perf/tests/shell/attr/test-record-dummy-C0 @@ -10,7 +10,7 @@ cpu=0 pid=-1 flags=8 type=1 -size=136 +size=144 config=9 sample_period=4000 # PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | @@ -53,3 +53,4 @@ config2=0 branch_sample_type=0 sample_regs_user=0 sample_stack_user=0 +alt_sample_period=0 diff --git a/tools/perf/tests/shell/lib/attr.py b/tools/perf/tests/shell/lib/attr.py index 3db9a7d78715..04e95f76005a 100644 --- a/tools/perf/tests/shell/lib/attr.py +++ b/tools/perf/tests/shell/lib/attr.py @@ -91,6 +91,7 @@ class Event(dict): 'branch_sample_type', 'sample_regs_user', 'sample_stack_user', + 'alt_sample_period', ] def add(self, data): diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 4a0ef095db92..0657e580ea13 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -180,6 +180,7 @@ static int store_event(struct perf_event_attr *attr, pid_t pid, struct perf_cpu WRITE_ASS(branch_sample_type, "llu"); WRITE_ASS(sample_regs_user, "llu"); WRITE_ASS(sample_stack_user, PRIu32); + WRITE_ASS(alt_sample_period, "llu"); fclose(file); return 0; diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 35e48fe56dfa..653d84080bc7 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -805,6 +805,7 @@ const char *parse_events__term_type_str(enum parse_events__term_type term_type) [PARSE_EVENTS__TERM_TYPE_RAW] = "raw", [PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE] = "legacy-cache", [PARSE_EVENTS__TERM_TYPE_HARDWARE] = "hardware", + [PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD] = "alt-period", }; if ((unsigned int)term_type >= __PARSE_EVENTS__TERM_TYPE_NR) return "unknown term"; @@ -833,6 +834,7 @@ config_term_avail(enum parse_events__term_type term_type, struct parse_events_er case PARSE_EVENTS__TERM_TYPE_NAME: case PARSE_EVENTS__TERM_TYPE_METRIC_ID: case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: + case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: case PARSE_EVENTS__TERM_TYPE_PERCORE: return true; case PARSE_EVENTS__TERM_TYPE_USER: @@ -981,6 +983,16 @@ do { \ return -EINVAL; } break; + case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: + CHECK_TYPE_VAL(NUM); + if (term->val.num == 0) { + parse_events_error__handle(err, term->err_val, + strdup("expected a non-zero value"), + NULL); + return -EINVAL; + } + attr->alt_sample_period = term->val.num; + break; case PARSE_EVENTS__TERM_TYPE_DRV_CFG: case PARSE_EVENTS__TERM_TYPE_USER: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: @@ -1108,6 +1120,7 @@ static int config_term_tracepoint(struct perf_event_attr *attr, case PARSE_EVENTS__TERM_TYPE_RAW: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: + case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: default: if (err) { parse_events_error__handle(err, term->err_term, @@ -1242,6 +1255,7 @@ do { \ case PARSE_EVENTS__TERM_TYPE_RAW: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: + case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: default: break; } @@ -1296,6 +1310,7 @@ static int get_config_chgs(struct perf_pmu *pmu, struct parse_events_terms *head case PARSE_EVENTS__TERM_TYPE_RAW: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: + case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: default: break; } diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index e176a34ab088..d00bb6c5d9ab 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -80,7 +80,8 @@ enum parse_events__term_type { PARSE_EVENTS__TERM_TYPE_RAW, PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE, PARSE_EVENTS__TERM_TYPE_HARDWARE, -#define __PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_HARDWARE + 1) + PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD, +#define __PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD + 1) }; struct parse_events_term { diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l index 7ed86e3e34e3..9c64434167b7 100644 --- a/tools/perf/util/parse-events.l +++ b/tools/perf/util/parse-events.l @@ -335,6 +335,7 @@ aux-output { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); } aux-action { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_ACTION); } aux-sample-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); } metric-id { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); } +alt-period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD); } cpu-cycles|cycles { return hw_term(yyscanner, PERF_COUNT_HW_CPU_CYCLES); } stalled-cycles-frontend|idle-cycles-frontend { return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); } stalled-cycles-backend|idle-cycles-backend { return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); } diff --git a/tools/perf/util/perf_event_attr_fprintf.c b/tools/perf/util/perf_event_attr_fprintf.c index c7f3543b9921..e823240b7dd8 100644 --- a/tools/perf/util/perf_event_attr_fprintf.c +++ b/tools/perf/util/perf_event_attr_fprintf.c @@ -334,6 +334,7 @@ int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr, PRINT_ATTRf(aux_start_paused, p_unsigned); PRINT_ATTRf(aux_pause, p_unsigned); PRINT_ATTRf(aux_resume, p_unsigned); + PRINT_ATTRf(alt_sample_period, p_unsigned); return ret; } diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 57450c73fb63..7576fe35a869 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1804,6 +1804,7 @@ int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_call "aux-output", "aux-action=(pause|resume|start-paused)", "aux-sample-size=number", + "alt-period=number", }; struct perf_pmu_format *format; int ret; From patchwork Fri Mar 7 20:22:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Barnett X-Patchwork-Id: 14007121 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 36416C19F32 for ; Fri, 7 Mar 2025 20:34:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Ewgzt3SkmZo7H0PuNRIxWwTKzQuiUxBFzmDNyKLJvVo=; b=lJnfXnePHMNGYNX6Rg/hSK2j1/ UIajnxBEvSY+ob4d4b+luYv9gxcaz6lkWHfBCqerDJyVmMqpAKGj/ob/Q6dU9qucYVEh0GS++4wD9 xZqnXV//xkoyvHW2D6RK0MlKn4gq9HRcPXrlPEpggGY4hP0F2DeMi9yWbFVGJMlQjGRE88AfKrrpP KL5PizLSecalXez5zkq1PKA541CLIErMYzSOnGrQHfo1DriXi/VE9u6O/iEQSCicZju8BU+3xj+wr 3T8mUGlqzYcMG7toYaiVADjq1bLQENYtuJ5oPn+5D+PxnPf47Cd8kd9TCccO7TFxAyF1urvGn7IFo i8hI9Y1g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeOI-0000000FUr8-0yQr; Fri, 07 Mar 2025 20:33:54 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tqeFG-0000000FTtt-03Ql for linux-arm-kernel@lists.infradead.org; Fri, 07 Mar 2025 20:24:35 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 186E322EE; Fri, 7 Mar 2025 12:24:46 -0800 (PST) Received: from e128066.arm.com (unknown [10.57.40.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id B4A5C3F5A1; Fri, 7 Mar 2025 12:24:29 -0800 (PST) From: mark.barnett@arm.com To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, irogers@google.com Cc: ben.gainey@arm.com, deepak.surti@arm.com, ak@linux.intel.com, will@kernel.org, james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Mark Barnett Subject: [PATCH v3 5/5] tools/perf: Modify event parser to support alt-period-jitter term Date: Fri, 7 Mar 2025 20:22:47 +0000 Message-Id: <20250307202247.648633-6-mark.barnett@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250307202247.648633-1-mark.barnett@arm.com> References: <20250307202247.648633-1-mark.barnett@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250307_122434_150271_54FDD0CA X-CRM114-Status: GOOD ( 17.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Ben Gainey parse-events is modified, adding the "alt-period-jitter" term which can be used to enable random jitter of the alternative sample period. Signed-off-by: Ben Gainey Signed-off-by: Mark Barnett --- tools/include/uapi/linux/perf_event.h | 7 ++++++- tools/perf/tests/shell/attr/base-record | 1 + tools/perf/tests/shell/attr/base-record-spe | 1 + tools/perf/tests/shell/attr/base-stat | 1 + tools/perf/tests/shell/attr/system-wide-dummy | 1 + .../shell/attr/test-record-alt-period-jitter-term | 13 +++++++++++++ tools/perf/tests/shell/attr/test-record-dummy-C0 | 1 + tools/perf/tests/shell/lib/attr.py | 1 + tools/perf/util/evsel.c | 1 + tools/perf/util/parse-events.c | 15 +++++++++++++++ tools/perf/util/parse-events.h | 3 ++- tools/perf/util/parse-events.l | 1 + tools/perf/util/pmu.c | 3 ++- 13 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tools/perf/tests/shell/attr/test-record-alt-period-jitter-term diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h index 499a8673df8e..c0076ce8f80a 100644 --- a/tools/include/uapi/linux/perf_event.h +++ b/tools/include/uapi/linux/perf_event.h @@ -461,7 +461,12 @@ struct perf_event_attr { inherit_thread : 1, /* children only inherit if cloned with CLONE_THREAD */ remove_on_exec : 1, /* event is removed from task on exec */ sigtrap : 1, /* send synchronous SIGTRAP on event */ - __reserved_1 : 26; + /* + * Add a limited amount of jitter on each alternate period, where + * the jitter is between [0, (2</dev/null 2>&1 +ret = 1 +kernel_since = 6.11 + +[event-10:base-record] +sample_period=3 +alt_sample_period=2 +jitter_alt_period=7 + +freq=0 +sample_type=7 diff --git a/tools/perf/tests/shell/attr/test-record-dummy-C0 b/tools/perf/tests/shell/attr/test-record-dummy-C0 index e6315918a95e..436534df0434 100644 --- a/tools/perf/tests/shell/attr/test-record-dummy-C0 +++ b/tools/perf/tests/shell/attr/test-record-dummy-C0 @@ -54,3 +54,4 @@ branch_sample_type=0 sample_regs_user=0 sample_stack_user=0 alt_sample_period=0 +jitter_alt_period=0 diff --git a/tools/perf/tests/shell/lib/attr.py b/tools/perf/tests/shell/lib/attr.py index 04e95f76005a..d15363e925fe 100644 --- a/tools/perf/tests/shell/lib/attr.py +++ b/tools/perf/tests/shell/lib/attr.py @@ -92,6 +92,7 @@ class Event(dict): 'sample_regs_user', 'sample_stack_user', 'alt_sample_period', + 'jitter_alt_period', ] def add(self, data): diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 0657e580ea13..2bb3e346f2b9 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -181,6 +181,7 @@ static int store_event(struct perf_event_attr *attr, pid_t pid, struct perf_cpu WRITE_ASS(sample_regs_user, "llu"); WRITE_ASS(sample_stack_user, PRIu32); WRITE_ASS(alt_sample_period, "llu"); + WRITE_ASS(jitter_alt_period, "d"); fclose(file); return 0; diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 653d84080bc7..2e0ecfb77af3 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -806,6 +806,7 @@ const char *parse_events__term_type_str(enum parse_events__term_type term_type) [PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE] = "legacy-cache", [PARSE_EVENTS__TERM_TYPE_HARDWARE] = "hardware", [PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD] = "alt-period", + [PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER] = "alt-period-jitter", }; if ((unsigned int)term_type >= __PARSE_EVENTS__TERM_TYPE_NR) return "unknown term"; @@ -835,6 +836,7 @@ config_term_avail(enum parse_events__term_type term_type, struct parse_events_er case PARSE_EVENTS__TERM_TYPE_METRIC_ID: case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: + case PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER: case PARSE_EVENTS__TERM_TYPE_PERCORE: return true; case PARSE_EVENTS__TERM_TYPE_USER: @@ -993,6 +995,16 @@ do { \ } attr->alt_sample_period = term->val.num; break; + case PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER: + CHECK_TYPE_VAL(NUM); + if ((unsigned int)term->val.num > 7) { + parse_events_error__handle(err, term->err_val, + strdup("expected a value between 0-7"), + NULL); + return -EINVAL; + } + attr->jitter_alt_period = (unsigned int)term->val.num; + break; case PARSE_EVENTS__TERM_TYPE_DRV_CFG: case PARSE_EVENTS__TERM_TYPE_USER: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: @@ -1121,6 +1133,7 @@ static int config_term_tracepoint(struct perf_event_attr *attr, case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: + case PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER: default: if (err) { parse_events_error__handle(err, term->err_term, @@ -1256,6 +1269,7 @@ do { \ case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: + case PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER: default: break; } @@ -1311,6 +1325,7 @@ static int get_config_chgs(struct perf_pmu *pmu, struct parse_events_terms *head case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: case PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD: + case PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER: default: break; } diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index d00bb6c5d9ab..dafd4b4d0f0e 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -81,7 +81,8 @@ enum parse_events__term_type { PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE, PARSE_EVENTS__TERM_TYPE_HARDWARE, PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD, -#define __PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD + 1) + PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER, +#define __PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER + 1) }; struct parse_events_term { diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l index 9c64434167b7..91d312a23769 100644 --- a/tools/perf/util/parse-events.l +++ b/tools/perf/util/parse-events.l @@ -336,6 +336,7 @@ aux-action { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_ACTION); } aux-sample-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); } metric-id { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); } alt-period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_ALT_SAMPLE_PERIOD); } +alt-period-jitter { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER); } cpu-cycles|cycles { return hw_term(yyscanner, PERF_COUNT_HW_CPU_CYCLES); } stalled-cycles-frontend|idle-cycles-frontend { return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); } stalled-cycles-backend|idle-cycles-backend { return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); } diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 7576fe35a869..c665dc35ad6b 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1429,7 +1429,7 @@ static int pmu_config_term(const struct perf_pmu *pmu, break; case PARSE_EVENTS__TERM_TYPE_USER: /* Not hardcoded. */ return -EINVAL; - case PARSE_EVENTS__TERM_TYPE_NAME ... PARSE_EVENTS__TERM_TYPE_HARDWARE: + case PARSE_EVENTS__TERM_TYPE_NAME ... PARSE_EVENTS__TERM_TYPE_ALT_PERIOD_JITTER: /* Skip non-config terms. */ break; default: @@ -1805,6 +1805,7 @@ int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_call "aux-action=(pause|resume|start-paused)", "aux-sample-size=number", "alt-period=number", + "alt-period-jitter=number", }; struct perf_pmu_format *format; int ret;