Согласно man 2 perf_event_open , в разделе «Обработка переполнения» существует режим perf_event_attr.watermark=0; perf_event_attr.wakeup_events=1
, который может генерировать события в fd или обработчике сигнала вызова для каждой выборки, добавленной в буфер rind perf_events (в режиме выборки как в perf record
)
watermark If set ... Otherwise, overflow notifications happen
after wakeup_events samples.
wakeup_events
This union sets how many samples (wakeup_events) happen
before an overflow notification happens.
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.
Overflow handling
Events can be set to notify when a threshold is crossed, indicating
an overflow. Overflow conditions can be captured by monitoring the
event file descriptor with poll(2), select(2), or epoll(7). Alterna‐
tively, the overflow events can be captured via sa signal handler, by
enabling I/O signaling on the file descriptor; see the discussion of
the F_SETOWN and F_SETSIG operations in fcntl(2).
Overflows are generated only by sampling events (sample_period must
have a nonzero value).
There are two ways to generate overflow notifications.
The first is to set a wakeup_events or wakeup_watermark value that
will trigger if a certain number of samples or bytes have been writ‐
ten to the mmap ring buffer. In this case, POLL_IN is indicated.
The other way is by use of the PERF_EVENT_IOC_REFRESH ioctl. This
ioctl adds to a counter that decrements each time the event over‐
flows. When nonzero, POLL_IN is indicated, but once the counter
reaches 0 POLL_HUP is indicated and the underlying event is disabled.
Существует аналогичный, но не точный метод ограничения размера кольцевого буфера, например, с параметром -m 1
для записи perf (инструмент будет сигнализирован для кольца, заполненного примерно на каждые 100 образцов, с опросом на fd). Это также полезно для просмотра фактических аргументов perf_event_open.
$ strace -tttT -v perf record -m 1 -e cycles:u -F 20000 python -c 'print(1)' 2>&1 |less
Проверьте также примеры https://mirrors.edge.kernel.org/pub/linux/kernel/tools/perf/ - любой недавний perf tar.gz, затем tests / bp_signal. c или tests / bp_signal_overflow. c:
// SPDX-License-Identifier: GPL-2.0
...
pe.sample_period = THRESHOLD;
pe.sample_type = PERF_SAMPLE_IP;
pe.wakeup_events = 1;
...
ioctl(fd, PERF_EVENT_IOC_RESET, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
for (i = 0; i < EXECUTIONS; i++)
test_function();
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);