Re: perf_event wakeup_events = 0

From: Theodore Dubois
Date: Sat Sep 07 2019 - 12:14:56 EST


>> The man page for perf_event_open(2) says that recent kernels treat a 0
>> value for wakeup_events the same as 1, which I believe means it will
>> notify after a single sample. However, strace on perf(1) shows that it
>> uses wakeup_events=0, and it's definitely not waking up on every
>> sample (it seems to be waking up every few seconds.)
>> tools/perf/design.txt says "Normally a notification is generated for
>> every page filled". Is the documentation wrong, or am I
>> misunderstanding something?
>
> wakeup_events, wakeup_watermark
> This union sets how many samples (wakeup_events) or bytes (wakeup_watermark) happen before an overflow
> notification happens. Which one is used is selected by the watermark bit flag.
>
> wakeup_events counts only PERF_RECORD_SAMPLE record types. To receive overflow notification for all
> PERF_RECORD types choose watermark and set wakeup_watermark to 1.
>
> Prior to Linux 3.0, setting wakeup_events to 0 resulted in no overflow notifications; more recent ker?
> nels treat 0 the same as 1.
>
> My reading of that is that in pre-3.0 kernels, you could choose to not get overflow
> notifications, and now you'll get them whether or not you wanted them.
>
> Under "overflow handling", we see:
>
> Overflows are generated only by sampling events (sample_period must have a nonzero value).
>
> So the reason strace says perf is only waking up every few seconds is probably
> because you either launched perf with options that only create trace events, or
> it takes several seconds for an overflow to happen on a sampling event. A lot
> of those fields are u64 counters, and won't overflow anytime soon. Even the
> u32 counters can take a few seconds to overflow....

I launched perf record with the default options. Hereâs one of the perf_event_open calls from strace:

08:57:37.083733 perf_event_open({type=PERF_TYPE_HARDWARE, size=PERF_ATTR_SIZE_VER5, config=PERF_COUNT_HW_CPU_CYCLES, sample_freq=4000, sample_type=PERF_SAMPLE_IP|PERF_SAMPLE_TID|PERF_SAMPLE_TIME|PERF_SAMPLE_PERIOD, read_format=PERF_FORMAT_ID, disabled=1, inherit=1, pinned=0, exclusive=0, exclusive_user=0, exclude_kernel=1, exclude_hv=0, exclude_idle=0, mmap=1, comm=1, freq=1, inherit_stat=0, enable_on_exec=1, task=1, watermark=0, precise_ip=3 /* must have 0 skid */, mmap_data=0, sample_id_all=1, exclude_host=0, exclude_guest=1, exclude_callchain_kernel=0, exclude_callchain_user=0, mmap2=1, comm_exec=1, use_clockid=0, context_switch=0, write_backward=0, namespaces=0, wakeup_events=0, config1=0, config2=0, sample_regs_user=0, sample_regs_intr=0, aux_watermark=0, sample_max_stack=0}, 134206, 18, -1, PERF_FLAG_FD_CLOEXEC) = 23 <0.000023>

sample_freq is 4000 (and freq is 1). Hereâs the man page on this field:

sample_period, sample_freq
A "sampling" event is one that generates an overflow notificaâ
tion every N events, where N is given by sample_period. A samâ
pling event has sample_period > 0. When an overflow occurs,
requested data is recorded in the mmap buffer. The sample_type
field controls what data is recorded on each overflow.

sample_freq can be used if you wish to use frequency rather than
period. In this case, you set the freq flag. The kernel will
adjust the sampling period to try and achieve the desired rate.
The rate of adjustment is a timer tick.

If Iâm reading this right, this is a sampling event which overflows 4000 times a second. But perf then does a poll call which wakes up on this FD with POLLIN after 1.637 seconds, instead of 0.00025 seconds.

The man page snippet you pasted seems to be a different definition of an overflow event, but that doesnât make sense either: the event generates a sample 4000 times a second, and 0 (supposedly the same as 1) would mean to wake up the FD after 1 sample, which would be 0.00025 seconds.

~Theodore