blob: 694e4e951055ca78addd6f596097afc2cfac0ca9 [file] [log] [blame] [view]
andybons3322f762015-08-24 21:37:091# Linux Profiling
2
qyearsleyc0dc6f42016-12-02 22:13:393How to profile Chromium on Linux.
andybons3322f762015-08-24 21:37:094
andybonsad92aa32015-08-31 02:27:445See
6[Profiling Chromium and WebKit](https://sites.google.com/a/chromium.org/dev/developers/profiling-chromium-and-webkit)
7for alternative discussion.
andybons3322f762015-08-24 21:37:098
9## CPU Profiling
10
11gprof: reported not to work (taking an hour to load on our large binary).
12
andybonsad92aa32015-08-31 02:27:4413oprofile: Dean uses it, says it's good. (As of 9/16/9 oprofile only supports
14timers on the new Z600 boxes, which doesn't give good granularity for profiling
15startup).
andybons3322f762015-08-24 21:37:0916
17TODO(willchan): Talk more about oprofile, gprof, etc.
18
andybonsad92aa32015-08-31 02:27:4419Also see
20https://sites.google.com/a/chromium.org/dev/developers/profiling-chromium-and-webkit
andybons3322f762015-08-24 21:37:0921
22### perf
23
andybonsad92aa32015-08-31 02:27:4424`perf` is the successor to `oprofile`. It's maintained in the kernel tree, it's
25available on Ubuntu in the package `linux-tools`.
andybons3322f762015-08-24 21:37:0926
27To capture data, you use `perf record`. Some examples:
andybonsad92aa32015-08-31 02:27:4428
29```shell
30# captures the full execution of the program
31perf record -f -g out/Release/chrome
32# captures a particular pid, you can start at the right time, and stop with
33# ctrl-C
34perf record -f -g -p 1234
35perf record -f -g -a # captures the whole system
andybons3322f762015-08-24 21:37:0936```
37
andybonsad92aa32015-08-31 02:27:4438Some versions of the perf command can be confused by process renames. Affected
39versions will be unable to resolve Chromium's symbols if it was started through
40perf, as in the first example above. It should work correctly if you attach to
41an existing Chromium process as shown in the second example. (This is known to
42be broken as late as 3.2.5 and fixed as early as 3.11.rc3.g36f571. The actual
43affected range is likely much smaller. You can download and build your own perf
44from source.)
andybons3322f762015-08-24 21:37:0945
andybonsad92aa32015-08-31 02:27:4446The last one is useful on limited systems with few cores and low memory
47bandwidth, where the CPU cycles are shared between several processes (e.g.
48chrome browser, renderer, plugin, X, pulseaudio, etc.)
andybons3322f762015-08-24 21:37:0949
50To look at the data, you use:
andybonsad92aa32015-08-31 02:27:4451
52 perf report
andybons3322f762015-08-24 21:37:0953
54This will use the previously captured data (`perf.data`).
55
56### google-perftools
57
andybonsad92aa32015-08-31 02:27:4458google-perftools code is enabled when the `use_allocator` variable in gyp is set
59to `tcmalloc` (currently the default). That will build the tcmalloc library,
60including the cpu profiling and heap profiling code into Chromium. In order to
61get stacktraces in release builds on 64 bit, you will need to build with some
62extra flags enabled by setting `profiling=1` in gyp.
andybons3322f762015-08-24 21:37:0963
andybonsad92aa32015-08-31 02:27:4464If the stack traces in your profiles are incomplete, this may be due to missing
65frame pointers in some of the libraries. A workaround is to use the
66`linux_keep_shadow_stacks=1` gyp option. This will keep a shadow stack using the
67`-finstrument-functions` option of gcc and consult the stack when unwinding.
andybons3322f762015-08-24 21:37:0968
andybonsad92aa32015-08-31 02:27:4469In order to enable cpu profiling, run Chromium with the environment variable
70`CPUPROFILE` set to a filename. For example:
andybons3322f762015-08-24 21:37:0971
andybonsad92aa32015-08-31 02:27:4472 CPUPROFILE=/tmp/cpuprofile out/Release/chrome
andybons3322f762015-08-24 21:37:0973
andybonsad92aa32015-08-31 02:27:4474After the program exits successfully, the cpu profile will be available at the
75filename specified in the CPUPROFILE environment variable. You can then analyze
76it using the pprof script (distributed with google-perftools, installed by
77default on Googler Linux workstations). For example:
andybons3322f762015-08-24 21:37:0978
andybonsad92aa32015-08-31 02:27:4479 pprof --gv out/Release/chrome /tmp/cpuprofile
andybons3322f762015-08-24 21:37:0980
andybonsad92aa32015-08-31 02:27:4481This will generate a visual representation of the cpu profile as a postscript
82file and load it up using `gv`. For more powerful commands, please refer to the
83pprof help output and the google-perftools documentation.
andybons3322f762015-08-24 21:37:0984
andybonsad92aa32015-08-31 02:27:4485Note that due to the current design of google-perftools' profiling tools, it is
86only possible to profile the browser process. You can also profile and pass the
87`--single-process` flag for a rough idea of what the render process looks like,
88but keep in mind that you'll be seeing a mixed browser/renderer codepath that is
89not used in production.
andybons3322f762015-08-24 21:37:0990
andybonsad92aa32015-08-31 02:27:4491For further information, please refer to
92http://google-perftools.googlecode.com/svn/trunk/doc/cpuprofile.html.
andybons3322f762015-08-24 21:37:0993
94## Heap Profiling
95
96### google-perftools
97
98#### Turning on heap profiles
andybons3322f762015-08-24 21:37:0999
andybonsad92aa32015-08-31 02:27:44100Follow the instructions for enabling profiling as described above in the
101google-perftools section under CPU Profiling.
andybons3322f762015-08-24 21:37:09102
andybonsad92aa32015-08-31 02:27:44103To turn on the heap profiler on a Chromium build with tcmalloc, use the
104`HEAPPROFILE` environment variable to specify a filename for the heap profile.
105For example:
andybons3322f762015-08-24 21:37:09106
andybonsad92aa32015-08-31 02:27:44107 HEAPPROFILE=/tmp/heapprofile out/Release/chrome
andybons3322f762015-08-24 21:37:09108
andybonsad92aa32015-08-31 02:27:44109After the program exits successfully, the heap profile will be available at the
110filename specified in the `HEAPPROFILE` environment variable.
111
112Some tests fork short-living processes which have a small memory footprint. To
113catch those, use the `HEAP_PROFILE_ALLOCATION_INTERVAL` environment variable.
andybons3322f762015-08-24 21:37:09114
115#### Dumping a profile of a running process
116
117To programmatically generate a heap profile before exit, use code like:
andybonsad92aa32015-08-31 02:27:44118
119 #include "third_party/tcmalloc/chromium/src/google/heap-profiler.h"
120
121 // "foobar" will be included in the message printed to the console
122 HeapProfilerDump("foobar");
123
andybons3322f762015-08-24 21:37:09124For example, you might hook that up to some action in the UI.
125
126Or you can use gdb to attach at any point:
127
andybonsad92aa32015-08-31 02:27:441281. Attach gdb to the process: `$ gdb -p 12345`
1291. Cause it to dump a profile: `(gdb) p HeapProfilerDump("foobar")`
1301. The filename will be printed on the console you started Chrome from; e.g.
131 "`Dumping heap profile to heap.0001.heap (foobar)`"
andybons3322f762015-08-24 21:37:09132
133#### Analyzing dumps
134
andybonsad92aa32015-08-31 02:27:44135You can then analyze dumps using the `pprof` script (distributed with
136google-perftools, installed by default on Googler Linux workstations; on Ubuntu
137it is called `google-pprof`). For example:
andybons3322f762015-08-24 21:37:09138
andybonsad92aa32015-08-31 02:27:44139 pprof --gv out/Release/chrome /tmp/heapprofile
andybons3322f762015-08-24 21:37:09140
andybonsad92aa32015-08-31 02:27:44141This will generate a visual representation of the heap profile as a postscript
142file and load it up using `gv`. For more powerful commands, please refer to the
143pprof help output and the google-perftools documentation.
andybons3322f762015-08-24 21:37:09144
andybonsad92aa32015-08-31 02:27:44145(pprof is slow. Googlers can try the not-open-source cpprof; Evan wrote an open
146source alternative [available on github](https://github.com/martine/hp).)
andybons3322f762015-08-24 21:37:09147
148#### Sandbox
149
andybonsad92aa32015-08-31 02:27:44150Sandboxed renderer subprocesses will fail to write out heap profiling dumps. To
151work around this, turn off the sandbox (via `export CHROME_DEVEL_SANDBOX=`).
andybons3322f762015-08-24 21:37:09152
153#### Troubleshooting
154
andybonsad92aa32015-08-31 02:27:44155* "Hooked allocator frame not found": build with `-Dcomponent=static_library`.
156 `tcmalloc` gets confused when the allocator routines are in a different
157 `.so` than the rest of the code.
andybons3322f762015-08-24 21:37:09158
159#### More reading
160
andybonsad92aa32015-08-31 02:27:44161For further information, please refer to
162http://google-perftools.googlecode.com/svn/trunk/doc/heapprofile.html.
andybons3322f762015-08-24 21:37:09163
andybons3322f762015-08-24 21:37:09164## Paint profiling
165
andybonsad92aa32015-08-31 02:27:44166You can use Xephyr to profile how chrome repaints the screen. Xephyr is a
167virtual X server like Xnest with debugging options which draws red rectangles to
168where applications are drawing before drawing the actual information.
andybons3322f762015-08-24 21:37:09169
andybonsad92aa32015-08-31 02:27:44170 export XEPHYR_PAUSE=10000
171 Xephyr :1 -ac -screen 800x600 &
172 DISPLAY=:1 out/Debug/chrome
andybons3322f762015-08-24 21:37:09173
andybonsad92aa32015-08-31 02:27:44174When ready to start debugging issue the following command, which will tell
175Xephyr to start drawing red rectangles:
andybons3322f762015-08-24 21:37:09176
andybonsad92aa32015-08-31 02:27:44177 kill -USR1 `pidof Xephyr`
andybons3322f762015-08-24 21:37:09178
andybonsad92aa32015-08-31 02:27:44179For further information, please refer to
180http://cgit.freedesktop.org/xorg/xserver/tree/hw/kdrive/ephyr/README.