简体   繁体   中英

Using the perf events from perf list programatically

When I run perf list on my Linux system I get a long list of available perf events.

Is it possible to list and use these events programatically from another process, using perf_event_open(2) ? That is, how can I get this list from another process and determine the corresponding values to populate in perf_event_attr ?

I'm not looking for solutions that use another third-party listing of the events, eg,. libpfm4 or jevents. I know some events can be reconstructed from the files in /sys/devices/cpu/events/ (and similar files for other event types), but these are a small subset of the events that perf list shows.

There is no solution to get full list of raw events from kernel (with any syscall like perf_event_open(2)) without using third-party (or first party ) lists. Perf tool uses some basic events scanned from /sys/bus/event_source/devices/cpu/events and similar sysfs folders, but it has its own list of cpu model specific events: https://elixir.bootlin.com/linux/v5.5.19/source/tools/perf/pmu-events , and there is readme file which points that perf uses jevents (perf has 8 MB of x86 json event lists, at tools/perf/pmu-events/arch/x86 )

The contents of this directory allow users to specify PMU events in their CPUs by their symbolic names rather than raw event codes (see example below). The main program in this directory, is the 'jevents', which is built and executed BEFORE the perf binary itself is built. The 'jevents' program tries to locate and process JSON files in the directory tree tools/perf/pmu-events/arch/foo.

You can download perf sources from https://mirrors.edge.kernel.org/pub/linux/kernel/tools/perf/ and use some source code navigation tools to check cmd_list function builtin-list.c file (with some undocumented options). Also you can build perf tools from these sources and there will be compilation of jevents ( HOSTCC pmu-events/jevents.o , LINK pmu-events/jevents ) early in perf building.

Current cpu model is detected from table pmu_events_map (pmu-events/pmu-events.c) by perf_pmu__find_map (util/pmu.c) called from pmu_add_cpu_aliases, called from pmu_lookup , from perf_pmu__find, from perf_pmu__scan from print_pmu_events from cmd_list (handler of perf list builtin command).

As of 5.5 version of perf (from linux kernel 5.5 as perf is part of linux kernel), there is no raw dump of event list with description. There is undocumented option perf list --raw-dump which will print list of all events for every available monitoring unit, for example, pmu : perf list --raw-dump pmu |tr ' ' '\\n' . The output of this raw dump is unstable between perf versions.

Kernel part of perf_events subsystem has no full event lists inarch/x86/events or kernel/events folders, only mapping of standard perf events (listed in sysfs) like cycles or cpu/branch-misses/ to raw events of specific cpu model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM