Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf python: Add counting.py as example for counting perf events

Add counting.py - a python version of counting.c to demonstrate
measuring and reading of counts for given perf events.

Committer testing:

Build perf and make the generated python binding somewhere you can point
to to avoid using the one in the distro python3-perf (fedora, may be
different in other distros):

$ make -k O=/tmp/build/$(basename $PWD)/ -C tools/perf install-bin

Copy /tmp/build/perf-tools-next/python/perf.cpython-313-x86_64-linux-gnu.so to
somewhere outside this toolbox container and then use it with root:

# export PYTHONPATH=/root/python/
# ls -la /root/python/
total 10640
drwxr-xr-x. 1 root root 72 May 21 11:40 .
dr-xr-x---. 1 root root 574 May 21 11:40 ..
-rwxr-xr-x. 1 acme acme 10894360 May 21 11:40 perf.cpython-313-x86_64-linux-gnu.so
# tools/perf/python/counting.py | head -5
For evsel(software/cpu-clock/) val: 2930946 enable: 2932479 run: 2932479
For evsel(software/cpu-clock/) val: 2924975 enable: 2926267 run: 2926267
For evsel(software/cpu-clock/) val: 2921017 enable: 2922430 run: 2922430
For evsel(software/cpu-clock/) val: 2914966 enable: 2916549 run: 2916549
For evsel(software/cpu-clock/) val: 2910027 enable: 2911589 run: 2911589
#

Signed-off-by: Gautam Menghani <gautam@linux.ibm.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
[ make the API take a CPU and thread then compute from these the appropriate indices. ]
Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/linux-perf-users/CAP-5=fWb-=hCYmpg7U5N9C94EucQGTOS7YwR2-fo4ptOexzxyg@mail.gmail.com/
Link: https://lore.kernel.org/r/20250519195148.1708988-8-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Gautam Menghani and committed by
Arnaldo Carvalho de Melo
59df607b aa684837

+36
+36
tools/perf/python/counting.py
··· 1 + #!/usr/bin/env python3 2 + # SPDX-License-Identifier: GPL-2.0 3 + # -*- python -*- 4 + # -*- coding: utf-8 -*- 5 + 6 + import argparse 7 + import perf 8 + 9 + def main(event: str): 10 + evlist = perf.parse_events(event) 11 + 12 + for evsel in evlist: 13 + evsel.read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING 14 + 15 + evlist.open() 16 + evlist.enable() 17 + 18 + count = 100000 19 + while count > 0: 20 + count -= 1 21 + 22 + evlist.disable() 23 + 24 + for evsel in evlist: 25 + for cpu in evsel.cpus(): 26 + for thread in evsel.threads(): 27 + counts = evsel.read(cpu, thread) 28 + print(f"For {evsel} val: {counts.val} enable: {counts.ena} run: {counts.run}") 29 + 30 + evlist.close() 31 + 32 + if __name__ == '__main__': 33 + ap = argparse.ArgumentParser() 34 + ap.add_argument('-e', '--event', help="Events to open", default="cpu-clock,task-clock") 35 + args = ap.parse_args() 36 + main(args.event)