tracepoint-analysis.rst 12 KB

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