Re: [PATCH v9 0/4] perf: Support PERF_SAMPLE_READ with inherit

From: Peter Zijlstra
Date: Tue Jul 30 2024 - 08:25:44 EST


On Tue, Jul 30, 2024 at 09:44:13AM +0100, Ben Gainey wrote:
> This change allows events to use PERF_SAMPLE READ with inherit so long
> as PERF_SAMPLE_TID is also set.
>
> Currently it is not possible to use PERF_SAMPLE_READ with inherit. This
> restriction assumes the user is interested in collecting aggregate
> statistics as per `perf stat`. It prevents a user from collecting
> per-thread samples using counter groups from a multi-threaded or
> multi-process application, as with `perf record -e '{....}:S'`. Instead
> users must use system-wide mode, or forgo the ability to sample counter
> groups, or profile a single thread. System-wide mode is often
> problematic as it requires specific permissions (no CAP_PERFMON / root
> access), or may lead to capture of significant amounts of extra data
> from other processes running on the system.
>
> This patch changes `perf_event_alloc` relaxing the restriction against
> combining `inherit` with `PERF_SAMPLE_READ` so that the combination
> will be allowed so long as `PERF_SAMPLE_TID` is enabled. It modifies
> sampling so that only the count associated with the active thread is
> recorded into the buffer. It modifies the context switch handling so
> that perf contexts are always switched out if they have this kind of
> event so that the correct per-thread state is maintained. Finally, the
> tools are updated to allow perf record to specify this combination and
> to correctly decode the sample data.
>
> In this configuration sample values, as may appear in the read_format
> field of a PERF_RECORD_SAMPLE, are no longer global counters. Instead
> the value reports the per-thread value for the active thread.
> Tools that expect the global total, for example when calculate a delta
> between samples, would need updating to take this into account when
> opting into this new behaviour. Previously valid event configurations
> (system-wide, no-inherit and so on) are unaffected.
>

Thanks, I've picked up the kernel patches, and provided the robot
doesn't hate on them, they will appear in tip/perf/core soonish.