How to specify monitoring events

The power of perf embodies it can monitor various of events (hardware, software, etc). Currently, 4 subcommands support -e option which can specify events: perf record, perf stat, perf top and perf trace. Use perf list to show available events:

  1. # perf list
  2. List of pre-defined events (to be used in -e):
  3. branch-instructions OR branches [Hardware event]
  4. branch-misses [Hardware event]
  5. bus-cycles [Hardware event]
  6. cache-misses [Hardware event]
  7. cache-references [Hardware event]
  8. cpu-cycles OR cycles [Hardware event]
  9. instructions [Hardware event]
  10. ref-cycles [Hardware event]
  11. alignment-faults [Software event]
  12. bpf-output [Software event]
  13. context-switches OR cs [Software event]
  14. cpu-clock [Software event]
  15. ......

Perf also provides plentiful formats of specifying interested events:

(1) Standard event name:
This is the most straightforward method: just use event name showed in “perf list“. E.g.:”perf stat -e cache-misses“.

(2) Raw PMU event:
PMU, abbreviated for “Performance Monitoring Unit”, are hardware counters which record CPU utilities. PMU events are defined in /sys/bus/event_source/devices/<pmu>/events/ directory. Either Use “pmu/param1=0xxx,param2/“ or “rNNNN“ (r denotes raw, and N is a hex digit. It is composed of “event + mask”). Still use “cache-misses” as an example:

  1. # cat /sys/bus/event_source/devices/cpu/events/cache-misses
  2. event=0x2e,umask=0x41

So both following instructions monitor “cache-misses” event:

  1. # perf stat -e cpu/event=0x2e,umask=0x41/
  2. # perf stat -e r412e

(3) Hardware breakpoint:
The format is “mem:addr[/len][:access]“, and access can be r (read), w (write) and x (execute). E.g., profile write accesses in [0x1000~1008):

  1. # perf stat -e mem:0x1000/8:w

(4) Event group. E.g,:

  1. # perf stat -e "{LLC-load-misses,cache-misses}"