Path: blob/master/tools/perf/Documentation/examples.txt
26298 views
1------------------------------2****** perf by examples ******3------------------------------45[ From an e-mail by Ingo Molnar, https://lore.kernel.org/lkml/[email protected] ]678First, discovery/enumeration of available counters can be done via9'perf list':1011titan:~> perf list12[...]13kmem:kmalloc [Tracepoint event]14kmem:kmem_cache_alloc [Tracepoint event]15kmem:kmalloc_node [Tracepoint event]16kmem:kmem_cache_alloc_node [Tracepoint event]17kmem:kfree [Tracepoint event]18kmem:kmem_cache_free [Tracepoint event]19kmem:mm_page_free [Tracepoint event]20kmem:mm_page_free_batched [Tracepoint event]21kmem:mm_page_alloc [Tracepoint event]22kmem:mm_page_alloc_zone_locked [Tracepoint event]23kmem:mm_page_pcpu_drain [Tracepoint event]24kmem:mm_page_alloc_extfrag [Tracepoint event]2526Then any (or all) of the above event sources can be activated and27measured. For example the page alloc/free properties of a 'hackbench28run' are:2930titan:~> perf stat -e kmem:mm_page_pcpu_drain -e kmem:mm_page_alloc31-e kmem:mm_page_free_batched -e kmem:mm_page_free ./hackbench 1032Time: 0.5753334Performance counter stats for './hackbench 10':353613857 kmem:mm_page_pcpu_drain3727576 kmem:mm_page_alloc386025 kmem:mm_page_free_batched3920934 kmem:mm_page_free40410.613972165 seconds time elapsed4243You can observe the statistical properties as well, by using the44'repeat the workload N times' feature of perf stat:4546titan:~> perf stat --repeat 5 -e kmem:mm_page_pcpu_drain -e47kmem:mm_page_alloc -e kmem:mm_page_free_batched -e48kmem:mm_page_free ./hackbench 1049Time: 0.62750Time: 0.64451Time: 0.56452Time: 0.55953Time: 0.6265455Performance counter stats for './hackbench 10' (5 runs):565712920 kmem:mm_page_pcpu_drain ( +- 3.359% )5825035 kmem:mm_page_alloc ( +- 3.783% )596104 kmem:mm_page_free_batched ( +- 0.934% )6018376 kmem:mm_page_free ( +- 4.941% )61620.643954516 seconds time elapsed ( +- 2.363% )6364Furthermore, these tracepoints can be used to sample the workload as65well. For example the page allocations done by a 'git gc' can be66captured the following way:6768titan:~/git> perf record -e kmem:mm_page_alloc -c 1 ./git gc69Counting objects: 1148, done.70Delta compression using up to 2 threads.71Compressing objects: 100% (450/450), done.72Writing objects: 100% (1148/1148), done.73Total 1148 (delta 690), reused 1148 (delta 690)74[ perf record: Captured and wrote 0.267 MB perf.data (~11679 samples) ]7576To check which functions generated page allocations:7778titan:~/git> perf report79# Samples: 1064680#81# Overhead Command Shared Object82# ........ ............... ..........................83#8423.57% git-repack /lib64/libc-2.5.so8521.81% git /lib64/libc-2.5.so8614.59% git ./git8711.79% git-repack ./git887.12% git /lib64/ld-2.5.so893.16% git-repack /lib64/libpthread-2.5.so902.09% git-repack /bin/bash911.97% rm /lib64/libc-2.5.so921.39% mv /lib64/ld-2.5.so931.37% mv /lib64/libc-2.5.so941.12% git-repack /lib64/ld-2.5.so950.95% rm /lib64/ld-2.5.so960.90% git-update-serv /lib64/libc-2.5.so970.73% git-update-serv /lib64/ld-2.5.so980.68% perf /lib64/libpthread-2.5.so990.64% git-repack /usr/lib64/libz.so.1.2.3100101Or to see it on a more finegrained level:102103titan:~/git> perf report --sort comm,dso,symbol104# Samples: 10646105#106# Overhead Command Shared Object Symbol107# ........ ............... .......................... ......108#1099.35% git-repack ./git [.] insert_obj_hash1109.12% git ./git [.] insert_obj_hash1117.31% git /lib64/libc-2.5.so [.] memcpy1126.34% git-repack /lib64/libc-2.5.so [.] _int_malloc1136.24% git-repack /lib64/libc-2.5.so [.] memcpy1145.82% git-repack /lib64/libc-2.5.so [.] __GI___fork1155.47% git /lib64/libc-2.5.so [.] _int_malloc1162.99% git /lib64/libc-2.5.so [.] memset117118Furthermore, call-graph sampling can be done too, of page119allocations - to see precisely what kind of page allocations there120are:121122titan:~/git> perf record -g -e kmem:mm_page_alloc -c 1 ./git gc123Counting objects: 1148, done.124Delta compression using up to 2 threads.125Compressing objects: 100% (450/450), done.126Writing objects: 100% (1148/1148), done.127Total 1148 (delta 690), reused 1148 (delta 690)128[ perf record: Captured and wrote 0.963 MB perf.data (~42069 samples) ]129130titan:~/git> perf report -g131# Samples: 10686132#133# Overhead Command Shared Object134# ........ ............... ..........................135#13623.25% git-repack /lib64/libc-2.5.so137|138|--50.00%-- _int_free139|140|--37.50%-- __GI___fork141| make_child142|143|--12.50%-- ptmalloc_unlock_all2144| make_child145|146--6.25%-- __GI_strcpy14721.61% git /lib64/libc-2.5.so148|149|--30.00%-- __GI_read150| |151| --83.33%-- git_config_from_file152| git_config153| |154[...]155156Or you can observe the whole system's page allocations for 10157seconds:158159titan:~/git> perf stat -a -e kmem:mm_page_pcpu_drain -e160kmem:mm_page_alloc -e kmem:mm_page_free_batched -e161kmem:mm_page_free sleep 10162163Performance counter stats for 'sleep 10':164165171585 kmem:mm_page_pcpu_drain166322114 kmem:mm_page_alloc16773623 kmem:mm_page_free_batched168254115 kmem:mm_page_free16917010.000591410 seconds time elapsed171172Or observe how fluctuating the page allocations are, via statistical173analysis done over ten 1-second intervals:174175titan:~/git> perf stat --repeat 10 -a -e kmem:mm_page_pcpu_drain -e176kmem:mm_page_alloc -e kmem:mm_page_free_batched -e177kmem:mm_page_free sleep 1178179Performance counter stats for 'sleep 1' (10 runs):18018117254 kmem:mm_page_pcpu_drain ( +- 3.709% )18234394 kmem:mm_page_alloc ( +- 4.617% )1837509 kmem:mm_page_free_batched ( +- 4.820% )18425653 kmem:mm_page_free ( +- 3.672% )1851861.058135029 seconds time elapsed ( +- 3.089% )187188Or you can annotate the recorded 'git gc' run on a per symbol basis189and check which instructions/source-code generated page allocations:190191titan:~/git> perf annotate __GI___fork192------------------------------------------------193Percent | Source code & Disassembly of libc-2.5.so194------------------------------------------------195:196:197: Disassembly of section .plt:198: Disassembly of section .text:199:200: 00000031a2e95560 <__fork>:201[...]2020.00 : 31a2e95602: b8 38 00 00 00 mov $0x38,%eax2030.00 : 31a2e95607: 0f 05 syscall20483.42 : 31a2e95609: 48 3d 00 f0 ff ff cmp $0xfffffffffffff000,%rax2050.00 : 31a2e9560f: 0f 87 4d 01 00 00 ja 31a2e95762 <__fork+0x202>2060.00 : 31a2e95615: 85 c0 test %eax,%eax207208( this shows that 83.42% of __GI___fork's page allocations come from209the 0x38 system call it performs. )210211etc. etc. - a lot more is possible. I could list a dozen of212other different usecases straight away - neither of which is213possible via /proc/vmstat.214215/proc/vmstat is not in the same league really, in terms of216expressive power of system analysis and performance217analysis.218219All that the above results needed were those new tracepoints220in include/tracing/events/kmem.h.221222Ingo223224225226227