Performance Counters for Linux Wiki
This is the wiki page for the perfcounters subsystem in Linux.
Performance counters are special hardware registers available on most modern CPUs. These registers count the number of certain types of hw events: such as instructions executed, cache-misses suffered, or branches mispredicted - without slowing down the kernel or applications. These registers can also trigger interrupts when a threshold number of events have passed - and can thus be used to profile the code that runs on that CPU.
The Linux Performance Counter subsystem provides rich abstractions over these hardware capabilities. It provides per task, per CPU and per-workload counters, counter groups, and it provides sampling capabilities on top of those - and more.
It also provides abstraction for 'software events' - such as minor/major page faults, task migrations, task context-switches and tracepoints.
There is a new tool ('perf') that makes full use of this new kernel subsystem. It can be used to optimize, validate and measure applications, workloads or the full system.
'perf' is hosted in the upstream kernel repository and can be found under: tools/perf/
Once you have installed 'perf' on your system, the simplest way to start profiling an userspace program is to use the "perf record" and "perf report" command as follows:
$ perf record -f -- git gc Counting objects: 1283571, done. Compressing objects: 100% (206724/206724), done. Writing objects: 100% (1283571/1283571), done. Total 1283571 (delta 1070675), reused 1281443 (delta 1068566) [ perf record: Captured and wrote 31.054 MB perf.data (~1356768 samples) ] $ perf report --sort comm,dso,symbol | head -10 # Samples: 1355726 # # Overhead Command Shared Object Symbol # ........ ............... ....................................... ...... # 31.53% git /usr/bin/git [.] 0x0000000009804f 13.41% git-prune /usr/bin/git-prune [.] 0x000000000ad06d 10.05% git /lib/tls/i686/cmov/libc-2.8.90.so [.] _nl_make_l10nflist 5.36% git-prune /usr/lib/libz.so.22.214.171.124 [.] 0x00000000009d51 4.48% git /lib/tls/i686/cmov/libc-2.8.90.so [.] memcpy
For more examples of how 'perf' can be used see perf examples.
- Factorize the multidimensional sorting between perf report and annotate (will be used by perf trace)
- Implement a perf cmp (profile comparison between two perf.data)
- Implement a perf view (GUI)
- Enhance perf trace:
- Handle the cpu field
- Handle the timestamp
- Use the in-perf ip -> symbol resolving
- Use the in-perf pid -> cmdline resolving
- Implement multidimensional sorting by field name