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 <mark.barnett@arm.com>
Subject: [PATCH v4 3/5] perf: Allow adding fixed random jitter to the sampling period
Date: Tue, 8 Apr 2025 18:15:28 +0100 [thread overview]
Message-ID: <20250408171530.140858-4-mark.barnett@arm.com> (raw)
In-Reply-To: <20250408171530.140858-1-mark.barnett@arm.com>
From: Ben Gainey <ben.gainey@arm.com>
This change modifies the core perf overflow handler, adding some small
random jitter to each sample period when the high-frequency sample
period is in use. 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 <ben.gainey@arm.com>
Signed-off-by: Mark Barnett <mark.barnett@arm.com>
---
include/linux/perf_event.h | 1 +
kernel/events/core.c | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index be006965054e..78a6fd14b412 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -233,6 +233,7 @@ struct hw_perf_event {
#define PERF_SPS_HF_ON 0x00000001
#define PERF_SPS_HF_SAMPLE 0x00000002
+#define PERF_SPS_HF_RAND 0x00000004
u32 sample_period_state;
/*
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 5752ac7408b1..bc6991a33048 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -56,6 +56,7 @@
#include <linux/buildid.h>
#include <linux/task_work.h>
#include <linux/percpu-rwsem.h>
+#include <linux/prandom.h>
#include "internal.h"
@@ -472,6 +473,8 @@ static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
static int perf_sample_allowed_ns __read_mostly =
DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
+static DEFINE_PER_CPU(struct rnd_state, sample_period_jitter_rnd);
+
static void update_perf_cpu_limits(void)
{
u64 tmp = perf_sample_period_ns;
@@ -10224,6 +10227,19 @@ static int __perf_event_overflow(struct perf_event *event,
*
* By ignoring the HF samples, we measure the actual period.
*/
+
+ /*
+ * Apply optional jitter to the overall sample period
+ */
+ if (hwc->sample_period_state & PERF_SPS_HF_RAND
+ && !(hwc->sample_period_state & PERF_SPS_HF_SAMPLE)) {
+ struct rnd_state *state = &get_cpu_var(sample_period_jitter_rnd);
+ u64 rand_period = 1 << event->attr.hf_sample_rand;
+
+ sample_period -= rand_period / 2;
+ sample_period += prandom_u32_state(state) & (rand_period - 1);
+ }
+
if (hwc->sample_period_state & PERF_SPS_HF_ON) {
u64 hf_sample_period = event->attr.hf_sample_period;
@@ -12756,6 +12772,14 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
if (attr->hf_sample_period)
hwc->sample_period_state |= PERF_SPS_HF_ON;
+ if (attr->hf_sample_rand) {
+ /* high-frequency jitter is only valid with a high-freq period */
+ if (!attr->hf_sample_period)
+ return ERR_PTR(-EINVAL);
+
+ hwc->sample_period_state |= PERF_SPS_HF_RAND;
+ }
+
/*
* Disallow uncore-task events. Similarly, disallow uncore-cgroup
* events (they don't make sense as the cgroup will be different
@@ -14367,6 +14391,7 @@ static void __init perf_event_init_all_cpus(void)
zalloc_cpumask_var(&perf_online_pkg_mask, GFP_KERNEL);
zalloc_cpumask_var(&perf_online_sys_mask, GFP_KERNEL);
+ prandom_seed_full_state(&sample_period_jitter_rnd);
for_each_possible_cpu(cpu) {
swhash = &per_cpu(swevent_htable, cpu);
@@ -14384,6 +14409,7 @@ static void __init perf_event_init_all_cpus(void)
cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask);
cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default);
cpuctx->heap = cpuctx->heap_default;
+
}
}
--
2.43.0
next prev parent reply other threads:[~2025-04-08 17:16 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-08 17:15 [PATCH v4 0/5] A mechanism for efficient support for per-function metrics mark.barnett
2025-04-08 17:15 ` [PATCH v4 1/5] perf: Record sample last_period before updating mark.barnett
2025-04-09 11:39 ` Ingo Molnar
2025-04-09 11:43 ` [tip: perf/core] perf/arch: Record sample last_period before updating on the x86 and PowerPC platforms tip-bot2 for Mark Barnett
2025-04-09 11:53 ` tip-bot2 for Mark Barnett
2025-04-08 17:15 ` [PATCH v4 2/5] perf: Allow periodic events to alternate between two sample periods mark.barnett
2025-04-08 17:15 ` mark.barnett [this message]
2025-04-09 10:54 ` [PATCH v4 3/5] perf: Allow adding fixed random jitter to the sampling period Ingo Molnar
2025-04-09 14:24 ` Peter Zijlstra
2025-04-08 17:15 ` [PATCH v4 4/5] tools/perf: Modify event parser to support hf-period term mark.barnett
2025-04-08 17:15 ` [PATCH v4 5/5] tools/perf: Modify event parser to support hf-rand term mark.barnett
2025-04-09 11:38 ` [PATCH v4 0/5] A mechanism for efficient support for per-function metrics Ingo Molnar
2025-04-11 11:07 ` Mark Barnett
2025-04-11 17:34 ` Ian Rogers
2025-04-12 20:42 ` Ingo Molnar
2025-04-12 20:38 ` Ingo Molnar
2025-04-09 14:29 ` Peter Zijlstra
2025-04-11 9:44 ` Mark Barnett
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250408171530.140858-4-mark.barnett@arm.com \
--to=mark.barnett@arm.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=ben.gainey@arm.com \
--cc=deepak.surti@arm.com \
--cc=irogers@google.com \
--cc=james.clark@arm.com \
--cc=jolsa@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®