BeeOnRope
BeeOnRope

Reputation: 64915

Can I get a userspace interrupt or other indication on PMC overflow

Can I configure perf so that when a PMC overflows, I get a userspace interrupt or other notification?

That is, usually when the PMC overflows the kernel handles updating the counter, taking a sample, or other bookkeeping that needs to be done, but I'm interesting in receiving these in userspace and taking some action.

Upvotes: 4

Views: 243

Answers (1)

osgx
osgx

Reputation: 94225

According to man 2 perf_event_open, section "Overflow handling" there is perf_event_attr.watermark=0; perf_event_attr.wakeup_events=1 mode, which may generate events on fd or call signal handler for every sample added to rind buffer of perf_events (in sampling mode like in 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.

There is similar but not exact method of limiting size of ring buffer, for example with -m 1 option of perf record (tool will be signaled for ring filled for around every 100 samples, with poll on fd). This is also useful to see actual perf_event_open arguments.

 $ strace -tttT -v perf record -m 1 -e cycles:u -F 20000 python -c 'print(1)' 2>&1 |less

Check also examples https://mirrors.edge.kernel.org/pub/linux/kernel/tools/perf/ - any recent perf tar.gz, then tests/bp_signal.c or 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);

Upvotes: 3

Related Questions