Blame view

Documentation/trace/tracepoint-analysis.txt 12 KB
bb7222208   Mel Gorman   tracing, document...
1
2
3
4
5
6
7
8
9
10
11
  		Notes on Analysing Behaviour Using Events and Tracepoints
  
  			Documentation written by Mel Gorman
  		PCL information heavily based on email from Ingo Molnar
  
  1. Introduction
  ===============
  
  Tracepoints (see Documentation/trace/tracepoints.txt) can be used without
  creating custom kernel modules to register probe functions using the event
  tracing infrastructure.
b41df645c   Randy Dunlap   Documentation: Up...
12
13
  Simplistically, tracepoints represent important events that can be
  taken in conjunction with other tracepoints to build a "Big Picture" of
bb7222208   Mel Gorman   tracing, document...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  what is going on within the system. There are a large number of methods for
  gathering and interpreting these events. Lacking any current Best Practises,
  this document describes some of the methods that can be used.
  
  This document assumes that debugfs is mounted on /sys/kernel/debug and that
  the appropriate tracing options have been configured into the kernel. It is
  assumed that the PCL tool tools/perf has been installed and is in your path.
  
  2. Listing Available Events
  ===========================
  
  2.1 Standard Utilities
  ----------------------
  
  All possible events are visible from /sys/kernel/debug/tracing/events. Simply
  calling
  
    $ find /sys/kernel/debug/tracing/events -type d
  
  will give a fair indication of the number of events available.
b41df645c   Randy Dunlap   Documentation: Up...
34
  2.2 PCL (Performance Counters for Linux)
bb7222208   Mel Gorman   tracing, document...
35
  -------
b41df645c   Randy Dunlap   Documentation: Up...
36
  Discovery and enumeration of all counters and events, including tracepoints,
bb7222208   Mel Gorman   tracing, document...
37
  are available with the perf tool. Getting a list of available events is a
b41df645c   Randy Dunlap   Documentation: Up...
38
  simple case of:
bb7222208   Mel Gorman   tracing, document...
39
40
41
42
43
44
45
46
  
    $ perf list 2>&1 | grep Tracepoint
    ext4:ext4_free_inode                     [Tracepoint event]
    ext4:ext4_request_inode                  [Tracepoint event]
    ext4:ext4_allocate_inode                 [Tracepoint event]
    ext4:ext4_write_begin                    [Tracepoint event]
    ext4:ext4_ordered_write_end              [Tracepoint event]
    [ .... remaining output snipped .... ]
b41df645c   Randy Dunlap   Documentation: Up...
47
  3. Enabling Events
bb7222208   Mel Gorman   tracing, document...
48
  ==================
b41df645c   Randy Dunlap   Documentation: Up...
49
  3.1 System-Wide Event Enabling
bb7222208   Mel Gorman   tracing, document...
50
51
52
53
  ------------------------------
  
  See Documentation/trace/events.txt for a proper description on how events
  can be enabled system-wide. A short example of enabling all events related
b41df645c   Randy Dunlap   Documentation: Up...
54
  to page allocation would look something like:
bb7222208   Mel Gorman   tracing, document...
55
56
  
    $ for i in `find /sys/kernel/debug/tracing/events -name "enable" | grep mm_`; do echo 1 > $i; done
b41df645c   Randy Dunlap   Documentation: Up...
57
  3.2 System-Wide Event Enabling with SystemTap
bb7222208   Mel Gorman   tracing, document...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  ---------------------------------------------
  
  In SystemTap, tracepoints are accessible using the kernel.trace() function
  call. The following is an example that reports every 5 seconds what processes
  were allocating the pages.
  
    global page_allocs
  
    probe kernel.trace("mm_page_alloc") {
    	page_allocs[execname()]++
    }
  
    function print_count() {
    	printf ("%-25s %-s
  ", "#Pages Allocated", "Process Name")
    	foreach (proc in page_allocs-)
    		printf("%-25d %s
  ", page_allocs[proc], proc)
    	printf ("
  ")
    	delete page_allocs
    }
  
    probe timer.s(5) {
            print_count()
    }
b41df645c   Randy Dunlap   Documentation: Up...
84
  3.3 System-Wide Event Enabling with PCL
bb7222208   Mel Gorman   tracing, document...
85
86
87
88
89
90
  ---------------------------------------
  
  By specifying the -a switch and analysing sleep, the system-wide events
  for a duration of time can be examined.
  
   $ perf stat -a \
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
91
92
  	-e kmem:mm_page_alloc -e kmem:mm_page_free \
  	-e kmem:mm_page_free_batched \
bb7222208   Mel Gorman   tracing, document...
93
94
95
96
  	sleep 10
   Performance counter stats for 'sleep 10':
  
             9630  kmem:mm_page_alloc
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
97
98
             2143  kmem:mm_page_free
             7424  kmem:mm_page_free_batched
bb7222208   Mel Gorman   tracing, document...
99
100
101
102
103
  
     10.002577764  seconds time elapsed
  
  Similarly, one could execute a shell and exit it as desired to get a report
  at that point.
b41df645c   Randy Dunlap   Documentation: Up...
104
  3.4 Local Event Enabling
bb7222208   Mel Gorman   tracing, document...
105
106
107
108
  ------------------------
  
  Documentation/trace/ftrace.txt describes how to enable events on a per-thread
  basis using set_ftrace_pid.
b41df645c   Randy Dunlap   Documentation: Up...
109
  3.5 Local Event Enablement with PCL
bb7222208   Mel Gorman   tracing, document...
110
  -----------------------------------
b41df645c   Randy Dunlap   Documentation: Up...
111
  Events can be activated and tracked for the duration of a process on a local
bb7222208   Mel Gorman   tracing, document...
112
  basis using PCL such as follows.
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
113
114
    $ perf stat -e kmem:mm_page_alloc -e kmem:mm_page_free \
  		 -e kmem:mm_page_free_batched ./hackbench 10
bb7222208   Mel Gorman   tracing, document...
115
116
117
118
119
    Time: 0.909
  
      Performance counter stats for './hackbench 10':
  
            17803  kmem:mm_page_alloc
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
120
121
            12398  kmem:mm_page_free
             4827  kmem:mm_page_free_batched
bb7222208   Mel Gorman   tracing, document...
122
123
  
      0.973913387  seconds time elapsed
b41df645c   Randy Dunlap   Documentation: Up...
124
  4. Event Filtering
bb7222208   Mel Gorman   tracing, document...
125
126
127
128
129
  ==================
  
  Documentation/trace/ftrace.txt covers in-depth how to filter events in
  ftrace.  Obviously using grep and awk of trace_pipe is an option as well
  as any script reading trace_pipe.
b41df645c   Randy Dunlap   Documentation: Up...
130
  5. Analysing Event Variances with PCL
bb7222208   Mel Gorman   tracing, document...
131
132
133
  =====================================
  
  Any workload can exhibit variances between runs and it can be important
b41df645c   Randy Dunlap   Documentation: Up...
134
  to know what the standard deviation is. By and large, this is left to the
bb7222208   Mel Gorman   tracing, document...
135
136
  performance analyst to do it by hand. In the event that the discrete event
  occurrences are useful to the performance analyst, then perf can be used.
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
137
138
    $ perf stat --repeat 5 -e kmem:mm_page_alloc -e kmem:mm_page_free
  			-e kmem:mm_page_free_batched ./hackbench 10
bb7222208   Mel Gorman   tracing, document...
139
140
141
142
143
144
145
146
147
    Time: 0.890
    Time: 0.895
    Time: 0.915
    Time: 1.001
    Time: 0.899
  
     Performance counter stats for './hackbench 10' (5 runs):
  
            16630  kmem:mm_page_alloc         ( +-   3.542% )
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
148
149
            11486  kmem:mm_page_free	    ( +-   4.771% )
             4730  kmem:mm_page_free_batched  ( +-   2.325% )
bb7222208   Mel Gorman   tracing, document...
150
151
152
153
154
155
156
  
      0.982653002  seconds time elapsed   ( +-   1.448% )
  
  In the event that some higher-level event is required that depends on some
  aggregation of discrete events, then a script would need to be developed.
  
  Using --repeat, it is also possible to view how events are fluctuating over
b41df645c   Randy Dunlap   Documentation: Up...
157
  time on a system-wide basis using -a and sleep.
bb7222208   Mel Gorman   tracing, document...
158

90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
159
160
    $ perf stat -e kmem:mm_page_alloc -e kmem:mm_page_free \
  		-e kmem:mm_page_free_batched \
bb7222208   Mel Gorman   tracing, document...
161
162
163
164
165
  		-a --repeat 10 \
  		sleep 1
    Performance counter stats for 'sleep 1' (10 runs):
  
             1066  kmem:mm_page_alloc         ( +-  26.148% )
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
166
167
              182  kmem:mm_page_free          ( +-   5.464% )
              890  kmem:mm_page_free_batched  ( +-  30.079% )
bb7222208   Mel Gorman   tracing, document...
168
169
  
      1.002251757  seconds time elapsed   ( +-   0.005% )
b41df645c   Randy Dunlap   Documentation: Up...
170
  6. Higher-Level Analysis with Helper Scripts
bb7222208   Mel Gorman   tracing, document...
171
172
173
174
175
176
177
178
179
  ============================================
  
  When events are enabled the events that are triggering can be read from
  /sys/kernel/debug/tracing/trace_pipe in human-readable format although binary
  options exist as well. By post-processing the output, further information can
  be gathered on-line as appropriate. Examples of post-processing might include
  
    o Reading information from /proc for the PID that triggered the event
    o Deriving a higher-level event from a series of lower-level events.
b41df645c   Randy Dunlap   Documentation: Up...
180
    o Calculating latencies between two events
bb7222208   Mel Gorman   tracing, document...
181
182
183
  
  Documentation/trace/postprocess/trace-pagealloc-postprocess.pl is an example
  script that can read trace_pipe from STDIN or a copy of a trace. When used
b41df645c   Randy Dunlap   Documentation: Up...
184
  on-line, it can be interrupted once to generate a report without exiting
bb7222208   Mel Gorman   tracing, document...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  and twice to exit.
  
  Simplistically, the script just reads STDIN and counts up events but it
  also can do more such as
  
    o Derive high-level events from many low-level events. If a number of pages
      are freed to the main allocator from the per-CPU lists, it recognises
      that as one per-CPU drain even though there is no specific tracepoint
      for that event
    o It can aggregate based on PID or individual process number
    o In the event memory is getting externally fragmented, it reports
      on whether the fragmentation event was severe or moderate.
    o When receiving an event about a PID, it can record who the parent was so
      that if large numbers of events are coming from very short-lived
      processes, the parent process responsible for creating all the helpers
      can be identified
b41df645c   Randy Dunlap   Documentation: Up...
201
  7. Lower-Level Analysis with PCL
bb7222208   Mel Gorman   tracing, document...
202
  ================================
b41df645c   Randy Dunlap   Documentation: Up...
203
  There may also be a requirement to identify what functions within a program
bb7222208   Mel Gorman   tracing, document...
204
  were generating events within the kernel. To begin this sort of analysis, the
b41df645c   Randy Dunlap   Documentation: Up...
205
  data must be recorded. At the time of writing, this required root:
bb7222208   Mel Gorman   tracing, document...
206
207
  
    $ perf record -c 1 \
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
208
209
  	-e kmem:mm_page_alloc -e kmem:mm_page_free \
  	-e kmem:mm_page_free_batched \
bb7222208   Mel Gorman   tracing, document...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  	./hackbench 10
    Time: 0.894
    [ perf record: Captured and wrote 0.733 MB perf.data (~32010 samples) ]
  
  Note the use of '-c 1' to set the event period to sample. The default sample
  period is quite high to minimise overhead but the information collected can be
  very coarse as a result.
  
  This record outputted a file called perf.data which can be analysed using
  perf report.
  
    $ perf report
    # Samples: 30922
    #
    # Overhead    Command                     Shared Object
    # ........  .........  ................................
    #
        87.27%  hackbench  [vdso]
         6.85%  hackbench  /lib/i686/cmov/libc-2.9.so
         2.62%  hackbench  /lib/ld-2.9.so
         1.52%       perf  [vdso]
         1.22%  hackbench  ./hackbench
         0.48%  hackbench  [kernel]
         0.02%       perf  /lib/i686/cmov/libc-2.9.so
         0.01%       perf  /usr/bin/perf
         0.01%       perf  /lib/ld-2.9.so
         0.00%  hackbench  /lib/i686/cmov/libpthread-2.9.so
    #
    # (For more details, try: perf report --sort comm,dso,symbol)
    #
b41df645c   Randy Dunlap   Documentation: Up...
240
241
  According to this, the vast majority of events triggered on events
  within the VDSO. With simple binaries, this will often be the case so let's
bb7222208   Mel Gorman   tracing, document...
242
  take a slightly different example. In the course of writing this, it was
b41df645c   Randy Dunlap   Documentation: Up...
243
244
  noticed that X was generating an insane amount of page allocations so let's look
  at it:
bb7222208   Mel Gorman   tracing, document...
245
246
  
    $ perf record -c 1 -f \
90a5d5af7   Konstantin Khlebnikov   mm-tracepoint: fi...
247
248
  		-e kmem:mm_page_alloc -e kmem:mm_page_free \
  		-e kmem:mm_page_free_batched \
bb7222208   Mel Gorman   tracing, document...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  		-p `pidof X`
  
  This was interrupted after a few seconds and
  
    $ perf report
    # Samples: 27666
    #
    # Overhead  Command                            Shared Object
    # ........  .......  .......................................
    #
        51.95%     Xorg  [vdso]
        47.95%     Xorg  /opt/gfx-test/lib/libpixman-1.so.0.13.1
         0.09%     Xorg  /lib/i686/cmov/libc-2.9.so
         0.01%     Xorg  [kernel]
    #
    # (For more details, try: perf report --sort comm,dso,symbol)
    #
b41df645c   Randy Dunlap   Documentation: Up...
266
267
  So, almost half of the events are occurring in a library. To get an idea which
  symbol:
bb7222208   Mel Gorman   tracing, document...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  
    $ perf report --sort comm,dso,symbol
    # Samples: 27666
    #
    # Overhead  Command                            Shared Object  Symbol
    # ........  .......  .......................................  ......
    #
        51.95%     Xorg  [vdso]                                   [.] 0x000000ffffe424
        47.93%     Xorg  /opt/gfx-test/lib/libpixman-1.so.0.13.1  [.] pixmanFillsse2
         0.09%     Xorg  /lib/i686/cmov/libc-2.9.so               [.] _int_malloc
         0.01%     Xorg  /opt/gfx-test/lib/libpixman-1.so.0.13.1  [.] pixman_region32_copy_f
         0.01%     Xorg  [kernel]                                 [k] read_hpet
         0.01%     Xorg  /opt/gfx-test/lib/libpixman-1.so.0.13.1  [.] get_fast_path
         0.00%     Xorg  [kernel]                                 [k] ftrace_trace_userstack
b41df645c   Randy Dunlap   Documentation: Up...
282
  To see where within the function pixmanFillsse2 things are going wrong:
bb7222208   Mel Gorman   tracing, document...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  
    $ perf annotate pixmanFillsse2
    [ ... ]
      0.00 :         34eeb:       0f 18 08                prefetcht0 (%eax)
           :      }
           :
           :      extern __inline void __attribute__((__gnu_inline__, __always_inline__, _
           :      _mm_store_si128 (__m128i *__P, __m128i __B) :      {
           :        *__P = __B;
     12.40 :         34eee:       66 0f 7f 80 40 ff ff    movdqa %xmm0,-0xc0(%eax)
      0.00 :         34ef5:       ff
     12.40 :         34ef6:       66 0f 7f 80 50 ff ff    movdqa %xmm0,-0xb0(%eax)
      0.00 :         34efd:       ff
     12.39 :         34efe:       66 0f 7f 80 60 ff ff    movdqa %xmm0,-0xa0(%eax)
      0.00 :         34f05:       ff
     12.67 :         34f06:       66 0f 7f 80 70 ff ff    movdqa %xmm0,-0x90(%eax)
      0.00 :         34f0d:       ff
     12.58 :         34f0e:       66 0f 7f 40 80          movdqa %xmm0,-0x80(%eax)
     12.31 :         34f13:       66 0f 7f 40 90          movdqa %xmm0,-0x70(%eax)
     12.40 :         34f18:       66 0f 7f 40 a0          movdqa %xmm0,-0x60(%eax)
     12.31 :         34f1d:       66 0f 7f 40 b0          movdqa %xmm0,-0x50(%eax)
  
  At a glance, it looks like the time is being spent copying pixmaps to
  the card.  Further investigation would be needed to determine why pixmaps
  are being copied around so much but a starting point would be to take an
  ancient build of libpixmap out of the library path where it was totally
  forgotten about from months ago!