Add counting.py - a python version of counting.c to demonstrate
measuring and reading of counts for given perf events.
Signed-off-by: Gautam Menghani <gautam@linux.ibm.com>
---
tools/perf/python/counting.py | 41 +++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100755 tools/perf/python/counting.py
diff --git a/tools/perf/python/counting.py b/tools/perf/python/counting.py
new file mode 100755
index 000000000000..0c58907bd8bf
--- /dev/null
+++ b/tools/perf/python/counting.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+# -*- python -*-
+# -*- coding: utf-8 -*-
+
+import perf
+
+def main():
+ cpus = perf.cpu_map()
+ thread_map = perf.thread_map(-1)
+ evlist = perf.evlist(cpus, thread_map)
+
+ evsel1 = perf.evsel(type = perf.TYPE_SOFTWARE,
+ config = perf.COUNT_SW_CPU_CLOCK,
+ read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
+ disabled=1)
+ evlist.add(evsel1)
+
+ evsel2 = perf.evsel(type = perf.TYPE_SOFTWARE,
+ config = perf.COUNT_SW_TASK_CLOCK,
+ read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
+ disabled=1)
+ evlist.add(evsel2)
+
+ evlist.open()
+ evlist.enable()
+
+ count = 100000
+ while count > 0:
+ count -= 1
+
+ evlist.disable()
+ evsel = evlist.next(None)
+ while evsel != None:
+ counts = evsel.read(0, 0)
+ print(counts.val, counts.ena, counts.run)
+ evsel = evlist.next(evsel)
+ evlist.close()
+
+if __name__ == '__main__':
+ main()
--
2.49.0
On Thu, May 1, 2025 at 2:37 AM Gautam Menghani <gautam@linux.ibm.com> wrote:
>
> Add counting.py - a python version of counting.c to demonstrate
> measuring and reading of counts for given perf events.
>
> Signed-off-by: Gautam Menghani <gautam@linux.ibm.com>
> ---
> tools/perf/python/counting.py | 41 +++++++++++++++++++++++++++++++++++
> 1 file changed, 41 insertions(+)
> create mode 100755 tools/perf/python/counting.py
>
> diff --git a/tools/perf/python/counting.py b/tools/perf/python/counting.py
> new file mode 100755
> index 000000000000..0c58907bd8bf
> --- /dev/null
> +++ b/tools/perf/python/counting.py
> @@ -0,0 +1,41 @@
> +#!/usr/bin/env python3
> +# SPDX-License-Identifier: GPL-2.0
> +# -*- python -*-
> +# -*- coding: utf-8 -*-
> +
> +import perf
> +
> +def main():
> + cpus = perf.cpu_map()
> + thread_map = perf.thread_map(-1)
> + evlist = perf.evlist(cpus, thread_map)
> +
> + evsel1 = perf.evsel(type = perf.TYPE_SOFTWARE,
> + config = perf.COUNT_SW_CPU_CLOCK,
> + read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
> + disabled=1)
> + evlist.add(evsel1)
> +
> + evsel2 = perf.evsel(type = perf.TYPE_SOFTWARE,
> + config = perf.COUNT_SW_TASK_CLOCK,
> + read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
> + disabled=1)
> + evlist.add(evsel2)
Nice example! Would this be better as:
```
cpus = perf.cpu_map()
threads = perf.thread_map(-1)
evlist = perf.parse_events("cpu-clock,task-clock", cpus, threads)
```
If you run `perf stat -vv -e 'cpu-clock,task-clock' .. ` you can
double check the perf event attribute bits. For example in
tracepoint.py we remove the SAMPLE_IP:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/python/tracepoint.py?h=perf-tools-next#n27
> +
> + evlist.open()
> + evlist.enable()
> +
> + count = 100000
> + while count > 0:
> + count -= 1
> +
> + evlist.disable()
> + evsel = evlist.next(None)
> + while evsel != None:
> + counts = evsel.read(0, 0)
Rather than just reading on the first CPU and thread, perhaps change
to iterate over the cpus and threads? Something like:
```
for evsel in evlist:
for cpu in cpus:
for thread in threads:
counts = evsel.read(cpu, thread)
print(f"For {evsel} read val={counts.val}
enable={counts.ena} run ={counts.run}")
```
Thanks,
Ian
> + print(counts.val, counts.ena, counts.run)
> + evsel = evlist.next(evsel)
> + evlist.close()
> +
> +if __name__ == '__main__':
> + main()
> --
> 2.49.0
>
On Thu, May 01, 2025 at 09:04:43AM -0700, Ian Rogers wrote:
> On Thu, May 1, 2025 at 2:37 AM Gautam Menghani <gautam@linux.ibm.com> wrote:
> >
> > Add counting.py - a python version of counting.c to demonstrate
> > measuring and reading of counts for given perf events.
> >
> > Signed-off-by: Gautam Menghani <gautam@linux.ibm.com>
> > ---
> > tools/perf/python/counting.py | 41 +++++++++++++++++++++++++++++++++++
> > 1 file changed, 41 insertions(+)
> > create mode 100755 tools/perf/python/counting.py
> >
> > diff --git a/tools/perf/python/counting.py b/tools/perf/python/counting.py
> > new file mode 100755
> > index 000000000000..0c58907bd8bf
> > --- /dev/null
> > +++ b/tools/perf/python/counting.py
> > @@ -0,0 +1,41 @@
> > +#!/usr/bin/env python3
> > +# SPDX-License-Identifier: GPL-2.0
> > +# -*- python -*-
> > +# -*- coding: utf-8 -*-
> > +
> > +import perf
> > +
> > +def main():
> > + cpus = perf.cpu_map()
> > + thread_map = perf.thread_map(-1)
> > + evlist = perf.evlist(cpus, thread_map)
> > +
> > + evsel1 = perf.evsel(type = perf.TYPE_SOFTWARE,
> > + config = perf.COUNT_SW_CPU_CLOCK,
> > + read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
> > + disabled=1)
> > + evlist.add(evsel1)
> > +
> > + evsel2 = perf.evsel(type = perf.TYPE_SOFTWARE,
> > + config = perf.COUNT_SW_TASK_CLOCK,
> > + read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
> > + disabled=1)
> > + evlist.add(evsel2)
>
> Nice example! Would this be better as:
> ```
> cpus = perf.cpu_map()
> threads = perf.thread_map(-1)
> evlist = perf.parse_events("cpu-clock,task-clock", cpus, threads)
> ```
> If you run `perf stat -vv -e 'cpu-clock,task-clock' .. ` you can
> double check the perf event attribute bits. For example in
> tracepoint.py we remove the SAMPLE_IP:
> https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/python/tracepoint.py?h=perf-tools-next#n27
Yes this is cleaner, will fix in v2.
>
> > +
> > + evlist.open()
> > + evlist.enable()
> > +
> > + count = 100000
> > + while count > 0:
> > + count -= 1
> > +
> > + evlist.disable()
> > + evsel = evlist.next(None)
> > + while evsel != None:
> > + counts = evsel.read(0, 0)
>
> Rather than just reading on the first CPU and thread, perhaps change
> to iterate over the cpus and threads? Something like:
> ```
> for evsel in evlist:
> for cpu in cpus:
> for thread in threads:
> counts = evsel.read(cpu, thread)
> print(f"For {evsel} read val={counts.val}
> enable={counts.ena} run ={counts.run}")
> ```
Ack, will do
Thanks,
Gautam
On Thu, May 1, 2025 at 9:04 AM Ian Rogers <irogers@google.com> wrote:
>
> On Thu, May 1, 2025 at 2:37 AM Gautam Menghani <gautam@linux.ibm.com> wrote:
> >
> > Add counting.py - a python version of counting.c to demonstrate
> > measuring and reading of counts for given perf events.
> >
> > Signed-off-by: Gautam Menghani <gautam@linux.ibm.com>
> > ---
> > tools/perf/python/counting.py | 41 +++++++++++++++++++++++++++++++++++
> > 1 file changed, 41 insertions(+)
> > create mode 100755 tools/perf/python/counting.py
> >
> > diff --git a/tools/perf/python/counting.py b/tools/perf/python/counting.py
> > new file mode 100755
> > index 000000000000..0c58907bd8bf
> > --- /dev/null
> > +++ b/tools/perf/python/counting.py
> > @@ -0,0 +1,41 @@
> > +#!/usr/bin/env python3
> > +# SPDX-License-Identifier: GPL-2.0
> > +# -*- python -*-
> > +# -*- coding: utf-8 -*-
> > +
> > +import perf
> > +
> > +def main():
> > + cpus = perf.cpu_map()
> > + thread_map = perf.thread_map(-1)
> > + evlist = perf.evlist(cpus, thread_map)
> > +
> > + evsel1 = perf.evsel(type = perf.TYPE_SOFTWARE,
> > + config = perf.COUNT_SW_CPU_CLOCK,
> > + read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
> > + disabled=1)
> > + evlist.add(evsel1)
> > +
> > + evsel2 = perf.evsel(type = perf.TYPE_SOFTWARE,
> > + config = perf.COUNT_SW_TASK_CLOCK,
> > + read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
> > + disabled=1)
> > + evlist.add(evsel2)
>
> Nice example! Would this be better as:
> ```
> cpus = perf.cpu_map()
> threads = perf.thread_map(-1)
> evlist = perf.parse_events("cpu-clock,task-clock", cpus, threads)
> ```
> If you run `perf stat -vv -e 'cpu-clock,task-clock' .. ` you can
> double check the perf event attribute bits. For example in
> tracepoint.py we remove the SAMPLE_IP:
> https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/python/tracepoint.py?h=perf-tools-next#n27
>
> > +
> > + evlist.open()
> > + evlist.enable()
> > +
> > + count = 100000
> > + while count > 0:
> > + count -= 1
> > +
> > + evlist.disable()
> > + evsel = evlist.next(None)
> > + while evsel != None:
> > + counts = evsel.read(0, 0)
>
> Rather than just reading on the first CPU and thread, perhaps change
> to iterate over the cpus and threads? Something like:
> ```
> for evsel in evlist:
> for cpu in cpus:
> for thread in threads:
> counts = evsel.read(cpu, thread)
> print(f"For {evsel} read val={counts.val}
> enable={counts.ena} run ={counts.run}")
> ```
Oh note, it is easy to confuse a thread or a CPU with the index into
the thread_map or cpu_map. For CPUs the two values of CPU and index
are often the same making it very easy to write code that works only
on CPU events (not events on things like memory controllers that
typically list a CPU per-socket). We added a struct perf_cpu to try to
avoid this kind of issue in the C code but in the python code
everything is back to being ints :-(
Thanks,
Ian
> Thanks,
> Ian
>
> > + print(counts.val, counts.ena, counts.run)
> > + evsel = evlist.next(evsel)
> > + evlist.close()
> > +
> > +if __name__ == '__main__':
> > + main()
> > --
> > 2.49.0
> >
© 2016 - 2026 Red Hat, Inc.