tracepoint-analysis.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. Notes on Analysing Behaviour Using Events and Tracepoints
  2. Documentation written by Mel Gorman
  3. PCL information heavily based on email from Ingo Molnar
  4. 1. Introduction
  5. ===============
  6. Tracepoints (see Documentation/trace/tracepoints.txt) can be used without
  7. creating custom kernel modules to register probe functions using the event
  8. tracing infrastructure.
  9. Simplistically, tracepoints represent important events that can be
  10. taken in conjunction with other tracepoints to build a "Big Picture" of
  11. what is going on within the system. There are a large number of methods for
  12. gathering and interpreting these events. Lacking any current Best Practises,
  13. this document describes some of the methods that can be used.
  14. This document assumes that debugfs is mounted on /sys/kernel/debug and that
  15. the appropriate tracing options have been configured into the kernel. It is
  16. assumed that the PCL tool tools/perf has been installed and is in your path.
  17. 2. Listing Available Events
  18. ===========================
  19. 2.1 Standard Utilities
  20. ----------------------
  21. All possible events are visible from /sys/kernel/debug/tracing/events. Simply
  22. calling
  23. $ find /sys/kernel/debug/tracing/events -type d
  24. will give a fair indication of the number of events available.
  25. 2.2 PCL (Performance Counters for Linux)
  26. -------
  27. Discovery and enumeration of all counters and events, including tracepoints,
  28. are available with the perf tool. Getting a list of available events is a
  29. simple case of:
  30. $ perf list 2>&1 | grep Tracepoint
  31. ext4:ext4_free_inode [Tracepoint event]
  32. ext4:ext4_request_inode [Tracepoint event]
  33. ext4:ext4_allocate_inode [Tracepoint event]
  34. ext4:ext4_write_begin [Tracepoint event]
  35. ext4:ext4_ordered_write_end [Tracepoint event]
  36. [ .... remaining output snipped .... ]
  37. 3. Enabling Events
  38. ==================
  39. 3.1 System-Wide Event Enabling
  40. ------------------------------
  41. See Documentation/trace/events.txt for a proper description on how events
  42. can be enabled system-wide. A short example of enabling all events related
  43. to page allocation would look something like:
  44. $ for i in `find /sys/kernel/debug/tracing/events -name "enable" | grep mm_`; do echo 1 > $i; done
  45. 3.2 System-Wide Event Enabling with SystemTap
  46. ---------------------------------------------
  47. In SystemTap, tracepoints are accessible using the kernel.trace() function
  48. call. The following is an example that reports every 5 seconds what processes
  49. were allocating the pages.
  50. global page_allocs
  51. probe kernel.trace("mm_page_alloc") {
  52. page_allocs[execname()]++
  53. }
  54. function print_count() {
  55. printf ("%-25s %-s\n", "#Pages Allocated", "Process Name")
  56. foreach (proc in page_allocs-)
  57. printf("%-25d %s\n", page_allocs[proc], proc)
  58. printf ("\n")
  59. delete page_allocs
  60. }
  61. probe timer.s(5) {
  62. print_count()
  63. }
  64. 3.3 System-Wide Event Enabling with PCL
  65. ---------------------------------------
  66. By specifying the -a switch and analysing sleep, the system-wide events
  67. for a duration of time can be examined.
  68. $ perf stat -a \
  69. -e kmem:mm_page_alloc -e kmem:mm_page_free \
  70. -e kmem:mm_page_free_batched \
  71. sleep 10
  72. Performance counter stats for 'sleep 10':
  73. 9630 kmem:mm_page_alloc
  74. 2143 kmem:mm_page_free
  75. 7424 kmem:mm_page_free_batched
  76. 10.002577764 seconds time elapsed
  77. Similarly, one could execute a shell and exit it as desired to get a report
  78. at that point.
  79. 3.4 Local Event Enabling
  80. ------------------------
  81. Documentation/trace/ftrace.txt describes how to enable events on a per-thread
  82. basis using set_ftrace_pid.
  83. 3.5 Local Event Enablement with PCL
  84. -----------------------------------
  85. Events can be activated and tracked for the duration of a process on a local
  86. basis using PCL such as follows.
  87. $ perf stat -e kmem:mm_page_alloc -e kmem:mm_page_free \
  88. -e kmem:mm_page_free_batched ./hackbench 10
  89. Time: 0.909
  90. Performance counter stats for './hackbench 10':
  91. 17803 kmem:mm_page_alloc
  92. 12398 kmem:mm_page_free
  93. 4827 kmem:mm_page_free_batched
  94. 0.973913387 seconds time elapsed
  95. 4. Event Filtering
  96. ==================
  97. Documentation/trace/ftrace.txt covers in-depth how to filter events in
  98. ftrace. Obviously using grep and awk of trace_pipe is an option as well
  99. as any script reading trace_pipe.
  100. 5. Analysing Event Variances with PCL
  101. =====================================
  102. Any workload can exhibit variances between runs and it can be important
  103. to know what the standard deviation is. By and large, this is left to the
  104. performance analyst to do it by hand. In the event that the discrete event
  105. occurrences are useful to the performance analyst, then perf can be used.
  106. $ perf stat --repeat 5 -e kmem:mm_page_alloc -e kmem:mm_page_free
  107. -e kmem:mm_page_free_batched ./hackbench 10
  108. Time: 0.890
  109. Time: 0.895
  110. Time: 0.915
  111. Time: 1.001
  112. Time: 0.899
  113. Performance counter stats for './hackbench 10' (5 runs):
  114. 16630 kmem:mm_page_alloc ( +- 3.542% )
  115. 11486 kmem:mm_page_free ( +- 4.771% )
  116. 4730 kmem:mm_page_free_batched ( +- 2.325% )
  117. 0.982653002 seconds time elapsed ( +- 1.448% )
  118. In the event that some higher-level event is required that depends on some
  119. aggregation of discrete events, then a script would need to be developed.
  120. Using --repeat, it is also possible to view how events are fluctuating over
  121. time on a system-wide basis using -a and sleep.
  122. $ perf stat -e kmem:mm_page_alloc -e kmem:mm_page_free \
  123. -e kmem:mm_page_free_batched \
  124. -a --repeat 10 \
  125. sleep 1
  126. Performance counter stats for 'sleep 1' (10 runs):
  127. 1066 kmem:mm_page_alloc ( +- 26.148% )
  128. 182 kmem:mm_page_free ( +- 5.464% )
  129. 890 kmem:mm_page_free_batched ( +- 30.079% )
  130. 1.002251757 seconds time elapsed ( +- 0.005% )
  131. 6. Higher-Level Analysis with Helper Scripts
  132. ============================================
  133. When events are enabled the events that are triggering can be read from
  134. /sys/kernel/debug/tracing/trace_pipe in human-readable format although binary
  135. options exist as well. By post-processing the output, further information can
  136. be gathered on-line as appropriate. Examples of post-processing might include
  137. o Reading information from /proc for the PID that triggered the event
  138. o Deriving a higher-level event from a series of lower-level events.
  139. o Calculating latencies between two events
  140. Documentation/trace/postprocess/trace-pagealloc-postprocess.pl is an example
  141. script that can read trace_pipe from STDIN or a copy of a trace. When used
  142. on-line, it can be interrupted once to generate a report without exiting
  143. and twice to exit.
  144. Simplistically, the script just reads STDIN and counts up events but it
  145. also can do more such as
  146. o Derive high-level events from many low-level events. If a number of pages
  147. are freed to the main allocator from the per-CPU lists, it recognises
  148. that as one per-CPU drain even though there is no specific tracepoint
  149. for that event
  150. o It can aggregate based on PID or individual process number
  151. o In the event memory is getting externally fragmented, it reports
  152. on whether the fragmentation event was severe or moderate.
  153. o When receiving an event about a PID, it can record who the parent was so
  154. that if large numbers of events are coming from very short-lived
  155. processes, the parent process responsible for creating all the helpers
  156. can be identified
  157. 7. Lower-Level Analysis with PCL
  158. ================================
  159. There may also be a requirement to identify what functions within a program
  160. were generating events within the kernel. To begin this sort of analysis, the
  161. data must be recorded. At the time of writing, this required root:
  162. $ perf record -c 1 \
  163. -e kmem:mm_page_alloc -e kmem:mm_page_free \
  164. -e kmem:mm_page_free_batched \
  165. ./hackbench 10
  166. Time: 0.894
  167. [ perf record: Captured and wrote 0.733 MB perf.data (~32010 samples) ]
  168. Note the use of '-c 1' to set the event period to sample. The default sample
  169. period is quite high to minimise overhead but the information collected can be
  170. very coarse as a result.
  171. This record outputted a file called perf.data which can be analysed using
  172. perf report.
  173. $ perf report
  174. # Samples: 30922
  175. #
  176. # Overhead Command Shared Object
  177. # ........ ......... ................................
  178. #
  179. 87.27% hackbench [vdso]
  180. 6.85% hackbench /lib/i686/cmov/libc-2.9.so
  181. 2.62% hackbench /lib/ld-2.9.so
  182. 1.52% perf [vdso]
  183. 1.22% hackbench ./hackbench
  184. 0.48% hackbench [kernel]
  185. 0.02% perf /lib/i686/cmov/libc-2.9.so
  186. 0.01% perf /usr/bin/perf
  187. 0.01% perf /lib/ld-2.9.so
  188. 0.00% hackbench /lib/i686/cmov/libpthread-2.9.so
  189. #
  190. # (For more details, try: perf report --sort comm,dso,symbol)
  191. #
  192. According to this, the vast majority of events triggered on events
  193. within the VDSO. With simple binaries, this will often be the case so let's
  194. take a slightly different example. In the course of writing this, it was
  195. noticed that X was generating an insane amount of page allocations so let's look
  196. at it:
  197. $ perf record -c 1 -f \
  198. -e kmem:mm_page_alloc -e kmem:mm_page_free \
  199. -e kmem:mm_page_free_batched \
  200. -p `pidof X`
  201. This was interrupted after a few seconds and
  202. $ perf report
  203. # Samples: 27666
  204. #
  205. # Overhead Command Shared Object
  206. # ........ ....... .......................................
  207. #
  208. 51.95% Xorg [vdso]
  209. 47.95% Xorg /opt/gfx-test/lib/libpixman-1.so.0.13.1
  210. 0.09% Xorg /lib/i686/cmov/libc-2.9.so
  211. 0.01% Xorg [kernel]
  212. #
  213. # (For more details, try: perf report --sort comm,dso,symbol)
  214. #
  215. So, almost half of the events are occurring in a library. To get an idea which
  216. symbol:
  217. $ perf report --sort comm,dso,symbol
  218. # Samples: 27666
  219. #
  220. # Overhead Command Shared Object Symbol
  221. # ........ ....... ....................................... ......
  222. #
  223. 51.95% Xorg [vdso] [.] 0x000000ffffe424
  224. 47.93% Xorg /opt/gfx-test/lib/libpixman-1.so.0.13.1 [.] pixmanFillsse2
  225. 0.09% Xorg /lib/i686/cmov/libc-2.9.so [.] _int_malloc
  226. 0.01% Xorg /opt/gfx-test/lib/libpixman-1.so.0.13.1 [.] pixman_region32_copy_f
  227. 0.01% Xorg [kernel] [k] read_hpet
  228. 0.01% Xorg /opt/gfx-test/lib/libpixman-1.so.0.13.1 [.] get_fast_path
  229. 0.00% Xorg [kernel] [k] ftrace_trace_userstack
  230. To see where within the function pixmanFillsse2 things are going wrong:
  231. $ perf annotate pixmanFillsse2
  232. [ ... ]
  233. 0.00 : 34eeb: 0f 18 08 prefetcht0 (%eax)
  234. : }
  235. :
  236. : extern __inline void __attribute__((__gnu_inline__, __always_inline__, _
  237. : _mm_store_si128 (__m128i *__P, __m128i __B) : {
  238. : *__P = __B;
  239. 12.40 : 34eee: 66 0f 7f 80 40 ff ff movdqa %xmm0,-0xc0(%eax)
  240. 0.00 : 34ef5: ff
  241. 12.40 : 34ef6: 66 0f 7f 80 50 ff ff movdqa %xmm0,-0xb0(%eax)
  242. 0.00 : 34efd: ff
  243. 12.39 : 34efe: 66 0f 7f 80 60 ff ff movdqa %xmm0,-0xa0(%eax)
  244. 0.00 : 34f05: ff
  245. 12.67 : 34f06: 66 0f 7f 80 70 ff ff movdqa %xmm0,-0x90(%eax)
  246. 0.00 : 34f0d: ff
  247. 12.58 : 34f0e: 66 0f 7f 40 80 movdqa %xmm0,-0x80(%eax)
  248. 12.31 : 34f13: 66 0f 7f 40 90 movdqa %xmm0,-0x70(%eax)
  249. 12.40 : 34f18: 66 0f 7f 40 a0 movdqa %xmm0,-0x60(%eax)
  250. 12.31 : 34f1d: 66 0f 7f 40 b0 movdqa %xmm0,-0x50(%eax)
  251. At a glance, it looks like the time is being spent copying pixmaps to
  252. the card. Further investigation would be needed to determine why pixmaps
  253. are being copied around so much but a starting point would be to take an
  254. ancient build of libpixmap out of the library path where it was totally
  255. forgotten about from months ago!