[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAP-5=fWb-=hCYmpg7U5N9C94EucQGTOS7YwR2-fo4ptOexzxyg@mail.gmail.com>
Date: Mon, 12 May 2025 10:23:39 -0700
From: Ian Rogers <irogers@...gle.com>
To: Gautam Menghani <gautam@...ux.ibm.com>, namhyung@...nel.org, acme@...nel.org
Cc: peterz@...radead.org, mingo@...hat.com, mark.rutland@....com,
alexander.shishkin@...ux.intel.com, jolsa@...nel.org, adrian.hunter@...el.com,
kan.liang@...ux.intel.com, linux-perf-users@...r.kernel.org,
linux-kernel@...r.kernel.org, maddy@...ux.ibm.com
Subject: Re: [PATCH v2 4/4] perf python: Add counting.py as example for
counting perf events
On Sun, May 11, 2025 at 10:58 PM Gautam Menghani <gautam@...ux.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@...ux.ibm.com>
> ---
> v1 -> v2:
> 1. Use existing iteration support instead of next
> 2. Read the counters on all cpus
> 3. Use existing helper functions
>
> tools/perf/python/counting.py | 34 ++++++++++++++++++++++++++++++++++
> 1 file changed, 34 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..e535e3ae8bdf
> --- /dev/null
> +++ b/tools/perf/python/counting.py
> @@ -0,0 +1,34 @@
> +#!/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.parse_events("cpu-clock,task-clock", cpus, thread_map)
Thanks Gautam! I think this is really good. Perhaps the events could
be a command line option, but I can see why you want to keep this
similar to counting.c.
> +
> + for ev in evlist:
> + ev.read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING
> +
> + evlist.open()
> + evlist.enable()
> +
> + count = 100000
> + while count > 0:
> + count -= 1
> +
> + evlist.disable()
> +
> + for evsel in evlist:
> + for cpu in cpus:
> + for thread in range(len(thread_map)):
I kind of wish, for the reason of being intention revealing, this could just be:
for thread in thread_map:
I can see the problem though, the counts lack the thread_map and the
thread_map is needed to turn a thread back into an index. Perhaps when
the python counts is created we hold onto the evsel so that this is
possible. I also suspect that in the code:
for cpu in cpus:
The CPU number is being used rather than its index, which is a similar
story/problem.
Arnaldo, could you give some input on what to do wrt indices, threads
and CPUs at the API level? Perhaps we need a refactor and objects for
perf CPU and perf thread, similar to the use of struct perf_cpu in the
C code. The original API all pre-dates that change. The issue is that
changing the API could break existing scripts and we can only fix
those that ship with perf.
Thanks,
Ian
> + counts = evsel.read(cpu, thread)
> + print(f"For {evsel} val: {counts.val} enable: {counts.ena} run: {counts.run}")
> +
> + evlist.close()
> +
> +if __name__ == '__main__':
> + main()
> --
> 2.49.0
>
Powered by blists - more mailing lists