2023 talk about flamegraphs
[libguestfs-talks.git] / 2023-flamegraphs / NOTES
1 Flamegraphs
2 March 8th 2023
3
4
5 1000. Title page
6 ----------------------------------------------------------------------
7
8
9 2000. -fno-omit-frame-pointer
10 ----------------------------------------------------------------------
11
12 Fedora 38 compiles (almost) everything with frame pointers, making
13 total system performance analysis much easier.
14
15 For details of why this is better, see my blog:
16 https://rwmj.wordpress.com/2023/02/14/frame-pointers-vs-dwarf-my-verdict/
17
18 Unfortunately Fedora <= 37 and RHEL do not have frame pointers unless
19 you recompile your program and all library dependencies (including
20 glibc!)
21
22
23 2100. sudo perf record -a -g -- nbdkit -U - null 1G --run 'export uri; fio nbd.fio'
24 ----------------------------------------------------------------------
25
26 You can use perf to record the whole system while running a command.
27
28 -a => record the whole system
29
30 -g => use frame pointers to gather stack traces
31
32 On every CPU, 100s of times a second, an interrupt will fire and
33 the full stack trace is collected.  If it's running in the kernel,
34 it collects the kernel stack and the userspace stack.  If it's
35 running in userspace, only the userspace stack is collected.
36
37
38 2200/2250. flamegraph > analysis.svg
39 ----------------------------------------------------------------------
40
41 You can post-process the perf output to get a flamegraph, an
42 interactive SVG file.
43
44 I wrote the flamegraph shell script to paper over some
45 unnecessary complexity in the tools.
46
47
48 3000. << a flamegraph >>
49 ----------------------------------------------------------------------
50
51 Opens in a web browser.
52
53 Interactive, click in for more detail.
54
55 Searchable.
56
57
58 3100. Width is time
59 ----------------------------------------------------------------------
60
61 Shows the width as a % of total system (non-sleeping) time.
62
63
64
65 3200. Height is stack depth
66 ----------------------------------------------------------------------
67
68 Not usually interesting.
69
70
71 3300. Plateaus
72 ----------------------------------------------------------------------
73
74 Plateaus indicate functions consuming a lot of time.
75
76 Remember I said that every core fires hundreds of interrupts
77 and collects a stack trace?  The stack trace is shown upside down
78 in a flamegraph, with the inner stack frame at the top.
79
80 So a plateau indicates a function that was actually running
81 when the interrupt happened.
82
83 (Show osq_lock and others)
84
85 (Describe MSR problem in UKL)
86
87
88 3400. Left to right is NOT time
89 ----------------------------------------------------------------------
90
91 Stack frames are ordered alphabetically
92
93
94 3500. Wide function with wide function on top
95 ----------------------------------------------------------------------
96
97 Time consumed in the inner function, not the outer function.
98
99
100 3600. Kernel threads and other "disconnected" or "background" work
101 ----------------------------------------------------------------------
102
103 Noisy machine, running firefox.
104
105 Note kernel threads (kcryptd) taking longer than the task being measured.
106
107
108 3700. Sleeping / blocked time is not recorded
109 3750. Sleeping / blocked time is not recorded
110 ----------------------------------------------------------------------
111
112 Show sleeptest.c
113
114 Show unexpected graph.
115
116 Unless you take special steps.  The easiest is to use the perf
117 --off-cpu flag, but it is still not supported in the Fedora perf
118 builds.
119
120
121
122 4000. What kind of things can be revealed?
123 ----------------------------------------------------------------------
124
125  - Lock contention
126
127  - Inefficient algorithms like zlib inflate
128
129  - MSRs / slow CPU features
130
131  - Serialization through a CPU
132
133  - Copy to/from user
134
135  - Page faults
136
137  - Excessive mmap/munmap (from memory allocations)
138
139  - Opportunities to short-cut through the stack
140
141
142 5000. In summary
143 ----------------------------------------------------------------------
144
145  - Use Fedora >= 38
146
147  - perf record -a -g
148
149  - flamegraph script
150
151  - Width is time
152
153  - Look for plateaus
154
155  - Watch out for disconnected / background work
156
157  - perf --off-cpu (in future)
158
159  - Many fascinating, revealing insights
160
161 The backbone of performance work in UKL.  Students spent literally
162 months studying flamegraphs (and other perf-adjacent tools) to get
163 insights.