linux/tools/perf
Ilya Leoshkevich 5e2ac8e857 perf bpf-filter: Enable events manually
On s390, and, in general, on all platforms where the respective event
supports auxiliary data gathering, the command:

   # ./perf record -u 0 -aB --synth=no -- ./perf test -w thloop
   [ perf record: Woken up 1 times to write data ]
   [ perf record: Captured and wrote 0.011 MB perf.data ]
   # ./perf report --stats | grep SAMPLE
   #

does not generate samples in the perf.data file. On x86 the command:

  # sudo perf record -e intel_pt// -u 0 ls

is broken too.

Looking at the sequence of calls in 'perf record' reveals this
behavior:

1. The event 'cycles' is created and enabled:

   record__open()
   +-> evlist__apply_filters()
       +-> perf_bpf_filter__prepare()
	   +-> bpf_program.attach_perf_event()
	       +-> bpf_program.attach_perf_event_opts()
	           +-> __GI___ioctl(..., PERF_EVENT_IOC_ENABLE, ...)

   The event 'cycles' is enabled and active now. However the event's
   ring-buffer to store the samples generated by hardware is not
   allocated yet.

2. The event's fd is mmap()ed to create the ring buffer:

   record__open()
   +-> record__mmap()
       +-> record__mmap_evlist()
	   +-> evlist__mmap_ex()
	       +-> perf_evlist__mmap_ops()
	           +-> mmap_per_cpu()
	               +-> mmap_per_evsel()
	                   +-> mmap__mmap()
	                       +-> perf_mmap__mmap()
	                           +-> mmap()

   This allocates the ring buffer for the event 'cycles'. With mmap()
   the kernel creates the ring buffer:

   perf_mmap(): kernel function to create the event's ring
   |            buffer to save the sampled data.
   |
   +-> ring_buffer_attach(): Allocates memory for ring buffer.
       |        The PMU has auxiliary data setup function. The
       |        has_aux(event) condition is true and the PMU's
       |        stop() is called to stop sampling. It is not
       |        restarted:
       |
       |        if (has_aux(event))
       |                perf_event_stop(event, 0);
       |
       +-> cpumsf_pmu_stop():

   Hardware sampling is stopped. No samples are generated and saved
   anymore.

3. After the event 'cycles' has been mapped, the event is enabled a
   second time in:

   __cmd_record()
   +-> evlist__enable()
       +-> __evlist__enable()
	   +-> evsel__enable_cpu()
	       +-> perf_evsel__enable_cpu()
	           +-> perf_evsel__run_ioctl()
	               +-> perf_evsel__ioctl()
	                   +-> __GI___ioctl(., PERF_EVENT_IOC_ENABLE, .)

   The second

      ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);

   is just a NOP in this case. The first invocation in (1.) sets the
   event::state to PERF_EVENT_STATE_ACTIVE. The kernel functions

   perf_ioctl()
   +-> _perf_ioctl()
       +-> _perf_event_enable()
           +-> __perf_event_enable()

   return immediately because event::state is already set to
   PERF_EVENT_STATE_ACTIVE.

This happens on s390, because the event 'cycles' offers the possibility
to save auxilary data. The PMU callbacks setup_aux() and free_aux() are
defined. Without both callback functions, cpumsf_pmu_stop() is not
invoked and sampling continues.

To remedy this, remove the first invocation of

   ioctl(..., PERF_EVENT_IOC_ENABLE, ...).

in step (1.) Create the event in step (1.) and enable it in step (3.)
after the ring buffer has been mapped.

Output after:

 # ./perf record -aB --synth=no -u 0 -- ./perf test -w thloop 2
 [ perf record: Woken up 3 times to write data ]
 [ perf record: Captured and wrote 0.876 MB perf.data ]
 # ./perf  report --stats | grep SAMPLE
              SAMPLE events:      16200  (99.5%)
              SAMPLE events:      16200
 #

The software event succeeded both before and after the patch:

 # ./perf record -e cpu-clock -aB --synth=no -u 0 -- \
					  ./perf test -w thloop 2
 [ perf record: Woken up 7 times to write data ]
 [ perf record: Captured and wrote 2.870 MB perf.data ]
 # ./perf  report --stats | grep SAMPLE
              SAMPLE events:      53506  (99.8%)
              SAMPLE events:      53506
 #

Fixes: b4c658d4d6 ("perf target: Remove uid from target")
Suggested-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Thomas Richter <tmricht@linux.ibm.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Co-developed-by: Thomas Richter <tmricht@linux.ibm.com>
Signed-off-by: Thomas Richter <tmricht@linux.ibm.com>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Link: https://lore.kernel.org/r/20250806162417.19666-3-iii@linux.ibm.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2025-08-07 09:03:44 -07:00
..
arch [GIT PULL] perf tools changes for v6.17 2025-08-01 16:55:47 -07:00
bench [GIT PULL] perf tools changes for v6.17 2025-08-01 16:55:47 -07:00
check-header_ignore_hunks/lib
dlfilters
Documentation perf record: Make --buildid-mmap the default 2025-07-25 10:37:56 -07:00
include/perf perf dso: Move build_id to dso_id 2025-07-25 10:37:56 -07:00
jvmti perf: Fix libjvmti.c sign compare error 2025-06-09 22:12:08 -07:00
pmu-events perf jevents: Add common software event json 2025-07-26 16:31:43 -07:00
python perf python: Add counting.py as example for counting perf events 2025-05-22 22:24:58 -03:00
scripts perf flamegraph: Fix minor pylint/type hint issues 2025-07-16 10:43:27 -07:00
tests perf test: Ensure lock contention using pipe mode 2025-07-30 13:38:53 -07:00
trace [GIT PULL] perf tools changes for v6.17 2025-08-01 16:55:47 -07:00
ui perf evlist: Change env variable to session 2025-07-25 10:37:56 -07:00
util perf bpf-filter: Enable events manually 2025-08-07 09:03:44 -07:00
.gitignore perf tools: Remove libtraceevent in .gitignore 2025-07-26 15:38:10 -07:00
Build perf build: Specify shellcheck should use bash 2025-06-30 09:43:06 -07:00
builtin-annotate.c perf sort: Use perf_env to set arch sort keys and header 2025-07-25 10:37:58 -07:00
builtin-bench.c
builtin-buildid-cache.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-buildid-list.c perf machine: Explicitly pass in host perf_env 2025-07-25 10:37:57 -07:00
builtin-c2c.c perf sort: Use perf_env to set arch sort keys and header 2025-07-25 10:37:58 -07:00
builtin-check.c tools: Remove libcrypto dependency 2025-06-26 10:51:41 -07:00
builtin-config.c perf config: Add a function to set one variable in .perfconfig 2025-01-14 15:05:56 -03:00
builtin-daemon.c perf daemon: Fix the build on more 32-bit architectures 2024-08-19 21:44:30 -03:00
builtin-data.c
builtin-diff.c perf sort: Use perf_env to set arch sort keys and header 2025-07-25 10:37:58 -07:00
builtin-evlist.c
builtin-ftrace.c perf: ftrace: add graph tracer options args/retval/retval-hex/retaddr 2025-07-22 17:47:22 -07:00
builtin-help.c perf tools: Remove dependency on libaudit 2025-01-10 10:59:42 -03:00
builtin-inject.c perf session: Add host_env argument to perf_session__new 2025-07-25 10:37:57 -07:00
builtin-kallsyms.c perf machine: Explicitly pass in host perf_env 2025-07-25 10:37:57 -07:00
builtin-kmem.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-kvm.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-kwork.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-list.c perf list: Skip ABI PMUs when printing pmu values 2025-07-26 16:31:43 -07:00
builtin-lock.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-mem.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-probe.c
builtin-record.c perf auxtrace: Pass perf_env from session through to mmap read 2025-07-25 10:37:58 -07:00
builtin-report.c perf sort: Use perf_env to set arch sort keys and header 2025-07-25 10:37:58 -07:00
builtin-sched.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-script.c perf sample: Remove arch notion of sample parsing 2025-07-25 10:37:58 -07:00
builtin-stat.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-timechart.c perf session: Add accessor for session->header.env 2025-07-25 10:37:56 -07:00
builtin-top.c perf sort: Use perf_env to set arch sort keys and header 2025-07-25 10:37:58 -07:00
builtin-trace.c perf trace: Avoid global perf_env with evsel__env 2025-07-25 10:37:58 -07:00
builtin-version.c perf check: Share the feature status printing routine with 'perf version' 2025-04-10 10:44:04 -03:00
builtin.h perf check: Allow showing a tip for opt-in features not built into perf 2025-04-10 10:44:42 -03:00
check-headers.sh perf tools: Remove excess variable declarations 2025-06-24 11:06:02 -07:00
command-list.txt perf help: Use HAVE_LIBTRACEEVENT to filter out unsupported commands 2023-01-02 11:51:53 -03:00
CREDITS
design.txt
Makefile
Makefile.config tools: Remove libcrypto dependency 2025-06-26 10:51:41 -07:00
Makefile.perf perf build: Always disable stack protection for BPF skeleton objects 2025-07-20 20:49:35 -07:00
MANIFEST perf tools: Fix arm64 source package build 2025-05-13 17:26:35 -03:00
perf-archive.sh tools/perf: Add --exclude-buildids option to perf archive command 2025-06-26 15:40:19 -07:00
perf-completion.sh
perf-iostat.sh
perf-read-vdso.c
perf-sys.h
perf.c perf env: Remove global perf_env 2025-07-25 10:37:58 -07:00
perf.h