1*4882a593Smuzhiyun 2*4882a593Smuzhiyun ------------------------------ 3*4882a593Smuzhiyun ****** perf by examples ****** 4*4882a593Smuzhiyun ------------------------------ 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun[ From an e-mail by Ingo Molnar, http://lkml.org/lkml/2009/8/4/346 ] 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunFirst, discovery/enumeration of available counters can be done via 10*4882a593Smuzhiyun'perf list': 11*4882a593Smuzhiyun 12*4882a593Smuzhiyuntitan:~> perf list 13*4882a593Smuzhiyun [...] 14*4882a593Smuzhiyun kmem:kmalloc [Tracepoint event] 15*4882a593Smuzhiyun kmem:kmem_cache_alloc [Tracepoint event] 16*4882a593Smuzhiyun kmem:kmalloc_node [Tracepoint event] 17*4882a593Smuzhiyun kmem:kmem_cache_alloc_node [Tracepoint event] 18*4882a593Smuzhiyun kmem:kfree [Tracepoint event] 19*4882a593Smuzhiyun kmem:kmem_cache_free [Tracepoint event] 20*4882a593Smuzhiyun kmem:mm_page_free [Tracepoint event] 21*4882a593Smuzhiyun kmem:mm_page_free_batched [Tracepoint event] 22*4882a593Smuzhiyun kmem:mm_page_alloc [Tracepoint event] 23*4882a593Smuzhiyun kmem:mm_page_alloc_zone_locked [Tracepoint event] 24*4882a593Smuzhiyun kmem:mm_page_pcpu_drain [Tracepoint event] 25*4882a593Smuzhiyun kmem:mm_page_alloc_extfrag [Tracepoint event] 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunThen any (or all) of the above event sources can be activated and 28*4882a593Smuzhiyunmeasured. For example the page alloc/free properties of a 'hackbench 29*4882a593Smuzhiyunrun' are: 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun titan:~> perf stat -e kmem:mm_page_pcpu_drain -e kmem:mm_page_alloc 32*4882a593Smuzhiyun -e kmem:mm_page_free_batched -e kmem:mm_page_free ./hackbench 10 33*4882a593Smuzhiyun Time: 0.575 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun Performance counter stats for './hackbench 10': 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun 13857 kmem:mm_page_pcpu_drain 38*4882a593Smuzhiyun 27576 kmem:mm_page_alloc 39*4882a593Smuzhiyun 6025 kmem:mm_page_free_batched 40*4882a593Smuzhiyun 20934 kmem:mm_page_free 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun 0.613972165 seconds time elapsed 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunYou can observe the statistical properties as well, by using the 45*4882a593Smuzhiyun'repeat the workload N times' feature of perf stat: 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun titan:~> perf stat --repeat 5 -e kmem:mm_page_pcpu_drain -e 48*4882a593Smuzhiyun kmem:mm_page_alloc -e kmem:mm_page_free_batched -e 49*4882a593Smuzhiyun kmem:mm_page_free ./hackbench 10 50*4882a593Smuzhiyun Time: 0.627 51*4882a593Smuzhiyun Time: 0.644 52*4882a593Smuzhiyun Time: 0.564 53*4882a593Smuzhiyun Time: 0.559 54*4882a593Smuzhiyun Time: 0.626 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun Performance counter stats for './hackbench 10' (5 runs): 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun 12920 kmem:mm_page_pcpu_drain ( +- 3.359% ) 59*4882a593Smuzhiyun 25035 kmem:mm_page_alloc ( +- 3.783% ) 60*4882a593Smuzhiyun 6104 kmem:mm_page_free_batched ( +- 0.934% ) 61*4882a593Smuzhiyun 18376 kmem:mm_page_free ( +- 4.941% ) 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun 0.643954516 seconds time elapsed ( +- 2.363% ) 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunFurthermore, these tracepoints can be used to sample the workload as 66*4882a593Smuzhiyunwell. For example the page allocations done by a 'git gc' can be 67*4882a593Smuzhiyuncaptured the following way: 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun titan:~/git> perf record -e kmem:mm_page_alloc -c 1 ./git gc 70*4882a593Smuzhiyun Counting objects: 1148, done. 71*4882a593Smuzhiyun Delta compression using up to 2 threads. 72*4882a593Smuzhiyun Compressing objects: 100% (450/450), done. 73*4882a593Smuzhiyun Writing objects: 100% (1148/1148), done. 74*4882a593Smuzhiyun Total 1148 (delta 690), reused 1148 (delta 690) 75*4882a593Smuzhiyun [ perf record: Captured and wrote 0.267 MB perf.data (~11679 samples) ] 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunTo check which functions generated page allocations: 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun titan:~/git> perf report 80*4882a593Smuzhiyun # Samples: 10646 81*4882a593Smuzhiyun # 82*4882a593Smuzhiyun # Overhead Command Shared Object 83*4882a593Smuzhiyun # ........ ............... .......................... 84*4882a593Smuzhiyun # 85*4882a593Smuzhiyun 23.57% git-repack /lib64/libc-2.5.so 86*4882a593Smuzhiyun 21.81% git /lib64/libc-2.5.so 87*4882a593Smuzhiyun 14.59% git ./git 88*4882a593Smuzhiyun 11.79% git-repack ./git 89*4882a593Smuzhiyun 7.12% git /lib64/ld-2.5.so 90*4882a593Smuzhiyun 3.16% git-repack /lib64/libpthread-2.5.so 91*4882a593Smuzhiyun 2.09% git-repack /bin/bash 92*4882a593Smuzhiyun 1.97% rm /lib64/libc-2.5.so 93*4882a593Smuzhiyun 1.39% mv /lib64/ld-2.5.so 94*4882a593Smuzhiyun 1.37% mv /lib64/libc-2.5.so 95*4882a593Smuzhiyun 1.12% git-repack /lib64/ld-2.5.so 96*4882a593Smuzhiyun 0.95% rm /lib64/ld-2.5.so 97*4882a593Smuzhiyun 0.90% git-update-serv /lib64/libc-2.5.so 98*4882a593Smuzhiyun 0.73% git-update-serv /lib64/ld-2.5.so 99*4882a593Smuzhiyun 0.68% perf /lib64/libpthread-2.5.so 100*4882a593Smuzhiyun 0.64% git-repack /usr/lib64/libz.so.1.2.3 101*4882a593Smuzhiyun 102*4882a593SmuzhiyunOr to see it on a more finegrained level: 103*4882a593Smuzhiyun 104*4882a593Smuzhiyuntitan:~/git> perf report --sort comm,dso,symbol 105*4882a593Smuzhiyun# Samples: 10646 106*4882a593Smuzhiyun# 107*4882a593Smuzhiyun# Overhead Command Shared Object Symbol 108*4882a593Smuzhiyun# ........ ............... .......................... ...... 109*4882a593Smuzhiyun# 110*4882a593Smuzhiyun 9.35% git-repack ./git [.] insert_obj_hash 111*4882a593Smuzhiyun 9.12% git ./git [.] insert_obj_hash 112*4882a593Smuzhiyun 7.31% git /lib64/libc-2.5.so [.] memcpy 113*4882a593Smuzhiyun 6.34% git-repack /lib64/libc-2.5.so [.] _int_malloc 114*4882a593Smuzhiyun 6.24% git-repack /lib64/libc-2.5.so [.] memcpy 115*4882a593Smuzhiyun 5.82% git-repack /lib64/libc-2.5.so [.] __GI___fork 116*4882a593Smuzhiyun 5.47% git /lib64/libc-2.5.so [.] _int_malloc 117*4882a593Smuzhiyun 2.99% git /lib64/libc-2.5.so [.] memset 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunFurthermore, call-graph sampling can be done too, of page 120*4882a593Smuzhiyunallocations - to see precisely what kind of page allocations there 121*4882a593Smuzhiyunare: 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun titan:~/git> perf record -g -e kmem:mm_page_alloc -c 1 ./git gc 124*4882a593Smuzhiyun Counting objects: 1148, done. 125*4882a593Smuzhiyun Delta compression using up to 2 threads. 126*4882a593Smuzhiyun Compressing objects: 100% (450/450), done. 127*4882a593Smuzhiyun Writing objects: 100% (1148/1148), done. 128*4882a593Smuzhiyun Total 1148 (delta 690), reused 1148 (delta 690) 129*4882a593Smuzhiyun [ perf record: Captured and wrote 0.963 MB perf.data (~42069 samples) ] 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun titan:~/git> perf report -g 132*4882a593Smuzhiyun # Samples: 10686 133*4882a593Smuzhiyun # 134*4882a593Smuzhiyun # Overhead Command Shared Object 135*4882a593Smuzhiyun # ........ ............... .......................... 136*4882a593Smuzhiyun # 137*4882a593Smuzhiyun 23.25% git-repack /lib64/libc-2.5.so 138*4882a593Smuzhiyun | 139*4882a593Smuzhiyun |--50.00%-- _int_free 140*4882a593Smuzhiyun | 141*4882a593Smuzhiyun |--37.50%-- __GI___fork 142*4882a593Smuzhiyun | make_child 143*4882a593Smuzhiyun | 144*4882a593Smuzhiyun |--12.50%-- ptmalloc_unlock_all2 145*4882a593Smuzhiyun | make_child 146*4882a593Smuzhiyun | 147*4882a593Smuzhiyun --6.25%-- __GI_strcpy 148*4882a593Smuzhiyun 21.61% git /lib64/libc-2.5.so 149*4882a593Smuzhiyun | 150*4882a593Smuzhiyun |--30.00%-- __GI_read 151*4882a593Smuzhiyun | | 152*4882a593Smuzhiyun | --83.33%-- git_config_from_file 153*4882a593Smuzhiyun | git_config 154*4882a593Smuzhiyun | | 155*4882a593Smuzhiyun [...] 156*4882a593Smuzhiyun 157*4882a593SmuzhiyunOr you can observe the whole system's page allocations for 10 158*4882a593Smuzhiyunseconds: 159*4882a593Smuzhiyun 160*4882a593Smuzhiyuntitan:~/git> perf stat -a -e kmem:mm_page_pcpu_drain -e 161*4882a593Smuzhiyunkmem:mm_page_alloc -e kmem:mm_page_free_batched -e 162*4882a593Smuzhiyunkmem:mm_page_free sleep 10 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun Performance counter stats for 'sleep 10': 165*4882a593Smuzhiyun 166*4882a593Smuzhiyun 171585 kmem:mm_page_pcpu_drain 167*4882a593Smuzhiyun 322114 kmem:mm_page_alloc 168*4882a593Smuzhiyun 73623 kmem:mm_page_free_batched 169*4882a593Smuzhiyun 254115 kmem:mm_page_free 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun 10.000591410 seconds time elapsed 172*4882a593Smuzhiyun 173*4882a593SmuzhiyunOr observe how fluctuating the page allocations are, via statistical 174*4882a593Smuzhiyunanalysis done over ten 1-second intervals: 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun titan:~/git> perf stat --repeat 10 -a -e kmem:mm_page_pcpu_drain -e 177*4882a593Smuzhiyun kmem:mm_page_alloc -e kmem:mm_page_free_batched -e 178*4882a593Smuzhiyun kmem:mm_page_free sleep 1 179*4882a593Smuzhiyun 180*4882a593Smuzhiyun Performance counter stats for 'sleep 1' (10 runs): 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun 17254 kmem:mm_page_pcpu_drain ( +- 3.709% ) 183*4882a593Smuzhiyun 34394 kmem:mm_page_alloc ( +- 4.617% ) 184*4882a593Smuzhiyun 7509 kmem:mm_page_free_batched ( +- 4.820% ) 185*4882a593Smuzhiyun 25653 kmem:mm_page_free ( +- 3.672% ) 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun 1.058135029 seconds time elapsed ( +- 3.089% ) 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunOr you can annotate the recorded 'git gc' run on a per symbol basis 190*4882a593Smuzhiyunand check which instructions/source-code generated page allocations: 191*4882a593Smuzhiyun 192*4882a593Smuzhiyun titan:~/git> perf annotate __GI___fork 193*4882a593Smuzhiyun ------------------------------------------------ 194*4882a593Smuzhiyun Percent | Source code & Disassembly of libc-2.5.so 195*4882a593Smuzhiyun ------------------------------------------------ 196*4882a593Smuzhiyun : 197*4882a593Smuzhiyun : 198*4882a593Smuzhiyun : Disassembly of section .plt: 199*4882a593Smuzhiyun : Disassembly of section .text: 200*4882a593Smuzhiyun : 201*4882a593Smuzhiyun : 00000031a2e95560 <__fork>: 202*4882a593Smuzhiyun [...] 203*4882a593Smuzhiyun 0.00 : 31a2e95602: b8 38 00 00 00 mov $0x38,%eax 204*4882a593Smuzhiyun 0.00 : 31a2e95607: 0f 05 syscall 205*4882a593Smuzhiyun 83.42 : 31a2e95609: 48 3d 00 f0 ff ff cmp $0xfffffffffffff000,%rax 206*4882a593Smuzhiyun 0.00 : 31a2e9560f: 0f 87 4d 01 00 00 ja 31a2e95762 <__fork+0x202> 207*4882a593Smuzhiyun 0.00 : 31a2e95615: 85 c0 test %eax,%eax 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun( this shows that 83.42% of __GI___fork's page allocations come from 210*4882a593Smuzhiyun the 0x38 system call it performs. ) 211*4882a593Smuzhiyun 212*4882a593Smuzhiyunetc. etc. - a lot more is possible. I could list a dozen of 213*4882a593Smuzhiyunother different usecases straight away - neither of which is 214*4882a593Smuzhiyunpossible via /proc/vmstat. 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun/proc/vmstat is not in the same league really, in terms of 217*4882a593Smuzhiyunexpressive power of system analysis and performance 218*4882a593Smuzhiyunanalysis. 219*4882a593Smuzhiyun 220*4882a593SmuzhiyunAll that the above results needed were those new tracepoints 221*4882a593Smuzhiyunin include/tracing/events/kmem.h. 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun Ingo 224*4882a593Smuzhiyun 225*4882a593Smuzhiyun 226