This is achieved by opening a raw perf event for every cpu core type, quite similiarly to the perf userspace tool.
The values needed to open the proper raw events are gathered from /sys/devices/cpu*/{type,events/instructions,format/*}.
This solution should work as long as the sysfs interface doesn't drastically change while maintaining compatibility with older kernels, as the mentioned interface seems to be the same as in at least 2014, when there was a failed effort to document the interface as stable.
I tested on:
linux 6.8 with Intel 12700k (both hybrid and with e-cores disabled in bios settings)
the above, but with a perf event that has a more complex config
linux 6.6 with Intel core 2 duo T9600 (cpu from 2008)
linux 4.15 with some AMD EPYC processor (a vps)
linux 6.8 with AMD Ryzen 7 7735HS
Alternative approaches that I considered:
use libpfm4. This would probably need to be updated semi-often to support future cpus.
hardcode the config value for the perf event, as both Intel and AMD seem to use 0xc0 for counting instructions at the moment.
utilize a new config layout for PERF_TYPE_HARDWARE event type (see "INTEL HYBRID SUPPORT" in perf-stat(1)). This would require some separate logic for handling older kernels which don't support this.
I think that using raw perf events based on data from sysfs is more robust than the above methods.
Related issue: https://github.com/sio2project/sinol-make/issues/158.
This is achieved by opening a raw perf event for every cpu core type, quite similiarly to the perf userspace tool. The values needed to open the proper raw events are gathered from
/sys/devices/cpu*/{type,events/instructions,format/*}
. This solution should work as long as the sysfs interface doesn't drastically change while maintaining compatibility with older kernels, as the mentioned interface seems to be the same as in at least 2014, when there was a failed effort to document the interface as stable.I tested on:
Alternative approaches that I considered:
config
value for the perf event, as both Intel and AMD seem to use0xc0
for counting instructions at the moment.config
layout for PERF_TYPE_HARDWARE event type (see "INTEL HYBRID SUPPORT" inperf-stat(1)
). This would require some separate logic for handling older kernels which don't support this.I think that using raw perf events based on data from sysfs is more robust than the above methods.