Performance counters over Linux

Hello,

I would like to ask how to access performance counters from userland Linux processes (since M mode privileges are required).

If I have to build a custom kernel in which I set the mcounteren and scounteren registers appropriately, could someone help me on how to achieve it? It is pretty unclear to me which files to modify and mainly how to build a custom kernel (most solutions just result in build errors).

Thanks in advance.

It has been a few years since I looked at the perf counters, but originally linux refused to support them because there was no overflow support, which is necessary to get meaningful results over non trivial time periods. A patch set was written at WD as a proof of concept but not upstreamed. Meanwhile, a new ISA extension was created to add the missing overflow support, and that has since been reviewed and approved. But neither Unleashed nor Unmatched has the new counter overflow extension. I don’t know if the new Pro P550 does, I haven’t seen one yet. It might not as the new ISA extension is still very new, ratified Mar 28 2024 apparently.

To access the perf counters, you need patches for opensbi (which runs in M mode) to make the perf counters available to the linux kernel (which runs in S mode). Then you need linux kernel patches to make them available to user mode. It has been a few years since I last looked so there might already be patches but these patches likely won’t work on Unleashed/Unmatched because of the missing overflow support.

The missing ISA support is
https://github.com/riscv/riscv-smcdeleg-ssccfg/releases

Oh ok ok I see. Thank you very much.