[PATCH v5 20/21] tools/perf: Pass the Counter constraint values in the pmu events

Atish Patra atish.patra at linux.dev
Tue Apr 22 17:17:56 PDT 2025


On 3/27/25 12:36 PM, Atish Patra wrote:
> RISC-V doesn't have any standard event to counter mapping discovery
> mechanism in the ISA. The ISA defines 29 programmable counters and
> platforms can choose to implement any number of them and map any
> events to any counters. Thus, the perf tool need to inform the driver
> about the counter mapping of each events.
> 
> The current perf infrastructure only parses the 'Counter' constraints
> in metrics. This patch extends that to pass in the pmu events so that
> any driver can retrieve those values via perf attributes if defined
> accordingly.
> 

Hi Ian/Arnaldo/Namhyung,
Any thoughts on this patch ? Please let me know if there are any other 
better approaches to pass the counter constraints to the driver ?

The RISC-V pmu driver maps the attr.config2 with counterid_mask value
so that driver can parse the counter restrictions.


> Signed-off-by: Atish Patra <atishp at rivosinc.com>
> ---
>   tools/perf/pmu-events/jevents.py | 9 +++++++++
>   1 file changed, 9 insertions(+)
> 
> diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py
> index fdb7ddf093d2..f9f274678a32 100755
> --- a/tools/perf/pmu-events/jevents.py
> +++ b/tools/perf/pmu-events/jevents.py
> @@ -274,6 +274,11 @@ class JsonEvent:
>           return fixed[name.lower()]
>         return event
>   
> +    def counter_list_to_bitmask(counterlist):
> +      counter_ids = list(map(int, counterlist.split(',')))
> +      bitmask = sum(1 << pos for pos in counter_ids)
> +      return bitmask
> +
>       def unit_to_pmu(unit: str) -> Optional[str]:
>         """Convert a JSON Unit to Linux PMU name."""
>         if not unit or unit == "core":
> @@ -427,6 +432,10 @@ class JsonEvent:
>         else:
>           raise argparse.ArgumentTypeError('Cannot find arch std event:', arch_std)
>   
> +    if self.counters['list']:
> +      bitmask = counter_list_to_bitmask(self.counters['list'])
> +      event += f',counterid_mask={bitmask:#x}'
> +
>       self.event = real_event(self.name, event)
>   
>     def __repr__(self) -> str:
> 




More information about the kvm-riscv mailing list