Skip to contentSkip to conversation

perf_events: add sampling period randomization support (v2)

PATCH In review [email protected] 3 participants 7 messages Mar 3, 2010 – Mar 11, 2010 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf_events: add sampling period randomization support (v2)

This patch adds support for randomizing the sampling period. Randomization is very useful to mitigate the bias that exists with sampling. The random number generator does not need to be sophisticated. This patch uses the builtin random32() generator.

Randomization is activated by setting perf_event_attr.random_period_width to a non-zero value. It represents the width of the mask to apply to the random value, i.e, maximum range of variation. The random value is applied AROUND the period, i.e., period may be longer or shorter with a maximum variation of half the bit width. Thus, on average the sampling period remains equal to the initial period passed in perf_event_attr.sample_period.

Note that randomization is not available when a target interrupt rate (freq) is enabled.

The last used period can be collected using the PERF_SAMPLE_PERIOD flag in sample_type.

Randomization is implemented in generic code thus it applies to all PMU models and software events.

--

 include/linux/perf_event.h |    3 +++
 kernel/perf_event.c        |   44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
include/linux/perf_event.h +3 -0
index 04f06b4..9848e08 100644
@@ -214,6 +214,8 @@ struct perf_event_attr {
 	__u32			bp_type;
 	__u64			bp_addr;
 	__u64			bp_len;
+
+	__u8			random_period_width;
 };
 
 /*
@@ -877,6 +879,7 @@ extern int perf_swevent_get_recursion_context(void);
 extern void perf_swevent_put_recursion_context(int rctx);
 extern void perf_event_enable(struct perf_event *event);
 extern void perf_event_disable(struct perf_event *event);
+extern void perf_randomize_event_period(struct perf_event *event);
 #else
 static inline void
 perf_event_task_sched_in(struct task_struct *task)			{ }
kernel/perf_event.c +44 -0

74 lines not on this page · raw message

Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · On 02.03.10 16:21:45, [email protected] wrote:

This patch adds support for randomizing the sampling period. Randomization is very useful to mitigate the bias that exists with sampling. The random number generator does not need to be sophisticated. This patch uses the builtin random32() generator.

Randomization is activated by setting perf_event_attr.random_period_width to a non-zero value. It represents the width of the mask to apply to the random value, i.e, maximum range of variation. The random value is applied AROUND the period, i.e., period may be longer or shorter with a maximum variation of half the bit width. Thus, on average the sampling period remains equal to the initial period passed in perf_event_attr.sample_period.

Note that randomization is not available when a target interrupt rate (freq) is enabled.

The last used period can be collected using the PERF_SAMPLE_PERIOD flag in sample_type.

Randomization is implemented in generic code thus it applies to all PMU models and software events.

Quoted 6 lines

--

 include/linux/perf_event.h |    3 +++
 kernel/perf_event.c        |   44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
Signature · 3 lines
Advanced Micro Devices, Inc.
Operating System Research Center
email: [email protected]
#2 lore raw
In reply to [email protected]

Review

Quoted 7 lines · * [email protected] <[email protected]> wrote:

This patch adds support for randomizing the sampling period. Randomization is very useful to mitigate the bias that exists with sampling. The random number generator does not need to be sophisticated. This patch uses the builtin random32() generator.

(quoted diff) +2 -0
+	if (width > 63 || attr->freq)
+		return -EINVAL;

Why not for freq counters? Those are semi-randomized already, but it might make sense to make them 'more' randomized in special circumstances. That would also allow us to enable the randomization in perf top and perf record, by default.

Without that we'd have no immediate usecase and no way to ensure that this code works as intended.

Thanks,

Ingo

#3 lore raw
Quoted 8 lines · Ingo Molnar
Quoted 4 lines · * [email protected] <[email protected]> wrote:

This patch adds support for randomizing the sampling period.  Randomization is very useful to mitigate the bias that exists with sampling. The random number generator does not need to be sophisticated. This patch uses the builtin random32() generator.

Quoted 8 lines
Quoted 2 lines

+     if (width > 63 || attr->freq)
+             return -EINVAL;

Why not for freq counters? Those are semi-randomized already, but it might make sense to make them 'more' randomized in special circumstances. That would also allow us to enable the randomization in perf top and perf record, by default.

What's the goal of freq?
Achieve and maintain the target interrupt/rate. In doing so, it has to adjust the period (not randomly).

Randomization may prevent achieving the rate, or it may slow it down. What's the value add of that?

Quoted 3 lines

Without that we'd have no immediate usecase and no way to ensure that this code works as intended.

Why?
With perf you also have fixed sampling period (-c option), you simply need to express the fact you want it randomized.

Quoted 4 lines

Thanks,

       Ingo

Signature · 8 lines
Stephane Eranian  | EMEA Software Engineering
Google France | 38 avenue de l'Opéra | 75002 Paris
Tel : +33 (0) 1 42 68 53 00
This email may be confidential or privileged. If you received this
communication by mistake, please
don't forward it to anyone else, please erase all copies and
attachments, and please let me know that
it went to the wrong person. Thanks
#4 lore raw
Quoted 18 lines · Stephane Eranian
Quoted 8 lines · Ingo Molnar
Quoted 4 lines · * [email protected] <[email protected]> wrote:

This patch adds support for randomizing the sampling period. ??Randomization is very useful to mitigate the bias that exists with sampling. The random number generator does not need to be sophisticated. This patch uses the builtin random32() generator.

Quoted 8 lines
Quoted 2 lines

+ ?? ?? if (width > 63 || attr->freq)
+ ?? ?? ?? ?? ?? ?? return -EINVAL;

Why not for freq counters? Those are semi-randomized already, but it might make sense to make them 'more' randomized in special circumstances. That would also allow us to enable the randomization in perf top and perf record, by default.

Quoted 3 lines

What's the goal of freq?
Achieve and maintain the target interrupt/rate. In doing so, it has to adjust the period (not randomly).

No, the goal of auto-freq is to keep a steady average rate of sampling.

There is no requirement to keep it 'steady' - each sample comes with a specific weight.

Quoted 2 lines

Randomization may prevent achieving the rate, or it may slow it down. What's the value add of that?

Why do you assume that the two are incompatible? We can randomize auto-freq and still have a perfectly stable average rate.

We know how long each sample takes so the result is precise, via PERF_SAMPLE_PERIOD.

Quoted 7 lines
Quoted 2 lines

Without that we'd have no immediate usecase and no way to ensure that this code works as intended.

Why?

With perf you also have fixed sampling period (-c option), you simply need to express the fact you want it randomized.

-c is legacy in essence. The default is auto-freq and i doubt anyone uses -c anymore.

Why would they use it? Auto-freq is so much more convenient - it adapts to the workload and achieves a steady state of sampling, regardless of how frequent the hardware events are and regardless of how dynamic the workload itself is.

Ingo

#5 lore raw
Quoted 21 lines · Ingo Molnar
Quoted 18 lines · Stephane Eranian
Quoted 16 lines · Ingo Molnar
Quoted 7 lines · * [email protected] <[email protected]> wrote:

This patch adds support for randomizing the sampling period. ??Randomization is very useful to mitigate the bias that exists with sampling. The random number generator does not need to be sophisticated. This patch uses the builtin random32() generator.

+ ?? ?? if (width > 63 || attr->freq)
+ ?? ?? ?? ?? ?? ?? return -EINVAL;

Why not for freq counters? Those are semi-randomized already, but it might make sense to make them 'more' randomized in special circumstances. That would also allow us to enable the randomization in perf top and perf record, by default.

Quoted 6 lines
Quoted 3 lines

What's the goal of freq?
Achieve and maintain the target interrupt/rate. In doing so, it has to adjust the period (not randomly).

No, the goal of auto-freq is to keep a steady average rate of sampling.

rate of samples = rate of interrupts (if period < 32 bits on Intel).

Quoted 9 lines

There is no requirement to keep it 'steady' - each sample comes with a specific weight.

Quoted 2 lines

Randomization may prevent achieving the rate, or it may slow it down. What's the value add of that?

Why do you assume that the two are incompatible? We can randomize auto-freq and still have a perfectly stable average rate.

What would that buy you compared to what you already have?

Quoted 3 lines

We know how long each sample takes so the result is precise, via PERF_SAMPLE_PERIOD.

I understand that.

Quoted 3 lines

-c is legacy in essence. The default is auto-freq and i doubt anyone uses -c anymore.

The -c option may be more convenient when not counting events correlated with time.

#6 lore raw
Quoted 22 lines · Stephane Eranian
Quoted 21 lines · Ingo Molnar
Quoted 18 lines · Stephane Eranian
Quoted 16 lines · Ingo Molnar
Quoted 7 lines · * [email protected] <[email protected]> wrote:

This patch adds support for randomizing the sampling period. ??Randomization is very useful to mitigate the bias that exists with sampling. The random number generator does not need to be sophisticated. This patch uses the builtin random32() generator.

+ ?? ?? if (width > 63 || attr->freq)
+ ?? ?? ?? ?? ?? ?? return -EINVAL;

Why not for freq counters? Those are semi-randomized already, but it might make sense to make them 'more' randomized in special circumstances. That would also allow us to enable the randomization in perf top and perf record, by default.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

What's the goal of freq?
Achieve and maintain the target interrupt/rate. In doing so, it has to adjust the period (not randomly).

No, the goal of auto-freq is to keep a steady average rate of sampling.

rate of samples = rate of interrupts (if period < 32 bits on Intel).

What's your point? I corrected your statement which said that the goal of auto-freq was to maintain a target interrupt-rate and as such wouldnt be randomizable. So i said that auto-freq is slightly different from that: it provides a steady _average_ rate, and as such small amounts of randomization 'fuzz' could still be injected - the auto-freq system would auto-correct the effects of that.

Think of it as a dynamic steady-state equilibrium with noise injected. If the noise isnt too brutal and the system can adapt, the average sampling rate doesnt change.

Quoted 10 lines
Quoted 8 lines

There is no requirement to keep it 'steady' - each sample comes with a specific weight.

Quoted 2 lines

Randomization may prevent achieving the rate, or it may slow it down. What's the value add of that?

Why do you assume that the two are incompatible? We can randomize auto-freq and still have a perfectly stable average rate.

What would that buy you compared to what you already have?

The same goal as randomization in general: to decrease the chance for sampling artifacts that can occur due to the sampling frequency oscillating together with some internal workload parameter, skewing the sample.

Ingo