3 How to profile chromium on Linux.
5 See [Profiling Chromium and WebKit](https://sites.google.com/a/chromium.org/dev/developers/profiling-chromium-and-webkit) for alternative discussion.
9 gprof: reported not to work (taking an hour to load on our large binary).
11 oprofile: Dean uses it, says it's good. (As of 9/16/9 oprofile only supports timers on the new Z600 boxes, which doesn't give good granularity for profiling startup).
13 TODO(willchan): Talk more about oprofile, gprof, etc.
15 Also see https://sites.google.com/a/chromium.org/dev/developers/profiling-chromium-and-webkit
19 `perf` is the successor to `oprofile`. It's maintained in the kernel tree, it's available on Ubuntu in the package `linux-tools`.
21 To capture data, you use `perf record`. Some examples:
23 $ perf record -f -g out/Release/chrome # captures the full execution of the program
24 $ perf record -f -g -p 1234 # captures a particular pid, you can start at the right time, and stop with ctrl-C
25 $ perf record -f -g -a # captures the whole system
28 Some versions of the perf command can be confused by process renames. Affected versions will be unable to resolve Chromium's symbols if it was started through perf, as in the first example above. It should work correctly if you attach to an existing Chromium process as shown in the second example. (This is known to be broken as late as 3.2.5 and fixed as early as 3.11.rc3.g36f571. The actual affected range is likely much smaller. You can download and build your own perf from source.)
30 The last one is useful on limited systems with few cores and low memory bandwidth, where the CPU cycles are shared between several processes (e.g. chrome browser, renderer, plugin, X, pulseaudio, etc.)
32 To look at the data, you use:
37 This will use the previously captured data (`perf.data`).
41 google-perftools code is enabled when the `use_allocator` variable in gyp is set to `tcmalloc` (currently the default). That will build the tcmalloc library, including the cpu profiling and heap profiling code into Chromium. In order to get stacktraces in release builds on 64 bit, you will need to build with some extra flags enabled by setting `profiling=1` in gyp.
43 If the stack traces in your profiles are incomplete, this may be due to missing frame pointers in some of the libraries. A workaround is to use the `linux_keep_shadow_stacks=1` gyp option. This will keep a shadow stack using the -finstrument-functions option of gcc and consult the stack when unwinding.
45 In order to enable cpu profiling, run Chromium with the environment variable CPUPROFILE set to a filename. For example:
48 $ CPUPROFILE=/tmp/cpuprofile out/Release/chrome
51 After the program exits successfully, the cpu profile will be available at the filename specified in the CPUPROFILE environment variable. You can then analyze it using the pprof script (distributed with google-perftools, installed by default on Googler Linux workstations). For example:
54 $ pprof --gv out/Release/chrome /tmp/cpuprofile
57 This will generate a visual representation of the cpu profile as a postscript file and load it up using `gv`. For more powerful commands, please refer to the pprof help output and the google-perftools documentation.
59 Note that due to the current design of google-perftools' profiling tools, it is only possible to profile the browser process. You can also profile and pass the --single-process flag for a rough idea of what the render process looks like, but keep in mind that you'll be seeing a mixed browser/renderer codepath that is not used in production.
61 For further information, please refer to http://google-perftools.googlecode.com/svn/trunk/doc/cpuprofile.html.
67 #### Turning on heap profiles
68 Follow the instructions for enabling profiling as described above in the google-perftools section under Cpu Profiling.
70 To turn on the heap profiler on a Chromium build with tcmalloc, use the HEAPPROFILE environment variable to specify a filename for the heap profile. For example:
73 $ HEAPPROFILE=/tmp/heapprofile out/Release/chrome
76 After the program exits successfully, the heap profile will be available at the filename specified in the `HEAPPROFILE` environment variable.
78 Some tests fork short-living processes which have a small memory footprint. To catch those, use the `HEAP_PROFILE_ALLOCATION_INTERVAL` environment variable.
80 #### Dumping a profile of a running process
82 To programmatically generate a heap profile before exit, use code like:
84 #include "third_party/tcmalloc/chromium/src/google/heap-profiler.h"
86 HeapProfilerDump("foobar"); // "foobar" will be included in the message printed to the console
88 For example, you might hook that up to some action in the UI.
90 Or you can use gdb to attach at any point:
92 1. Attach gdb to the process: `$ gdb -p 12345`
93 1. Cause it to dump a profile: `(gdb) p HeapProfilerDump("foobar")`
94 1. The filename will be printed on the console you started Chrome from; e.g. "`Dumping heap profile to heap.0001.heap (foobar)`"
99 You can then analyze dumps using the `pprof` script (distributed with google-perftools, installed by default on Googler Linux workstations; on Ubuntu it is called `google-pprof`). For example:
102 $ pprof --gv out/Release/chrome /tmp/heapprofile
105 This will generate a visual representation of the heap profile as a postscript file and load it up using `gv`. For more powerful commands, please refer to the pprof help output and the google-perftools documentation.
107 (pprof is slow. Googlers can try the not-open-source cpprof; Evan wrote an open source alternative [available on github](https://github.com/martine/hp).)
111 Sandboxed renderer subprocesses will fail to write out heap profiling dumps. To work around this, turn off the sandbox (via `export CHROME_DEVEL_SANDBOX=`).
115 * "Hooked allocator frame not found": build with `-Dcomponent=static_library`. tcmalloc gets confused when the allocator routines are in a different `.so` than the rest of the code.
119 For further information, please refer to http://google-perftools.googlecode.com/svn/trunk/doc/heapprofile.html.
122 [Massif](http://valgrind.org/docs/manual/mc-manual.html) is a [Valgrind](http://www.chromium.org/developers/how-tos/using-valgrind)-based heap profiler.
123 It is much slower than the heap profiler from google-perftools, but it may have some advantages. (In particular, it handles the multi-process executables well).
125 First, you will need to build massif from valgrind-variant project yourself, it's [easy](http://code.google.com/p/valgrind-variant/wiki/HowTo).
127 Then, make sure your chromium is built using the [valgrind instructions](http://www.chromium.org/developers/how-tos/using-valgrind).
128 Now, you can run massif like this:
131 % path-to-valgrind-variant/valgrind/inst/bin/valgrind --fullpath-after=/chromium/src/ \
132 --trace-children-skip=*npviewer*,/bin/uname,/bin/sh,/usr/bin/which,/bin/ps,/bin/grep,/usr/bin/linux32 --trace-children=yes --tool=massif \
133 out/Release/chrome --noerrdialogs --disable-hang-monitor --other-chrome-flags
136 The result will be stored in massif.out.PID files, which you can post-process with [ms\_print](http://valgrind.org/docs/manual/mc-manual.html).
138 TODO(kcc) sometimes when closing a tab the main process kills the tab process before massif completes writing it's log file. Need a flag that tells the main process to wait longer.
142 You can use Xephyr to profile how chrome repaints the screen. Xephyr is a virtual X server like Xnest with debugging options which draws red rectangles to where applications are drawing before drawing the actual information.
145 $ export XEPHYR_PAUSE=10000
146 $ Xephyr :1 -ac -screen 800x600 &
147 $ DISPLAY=:1 out/Debug/chrome
150 When ready to start debugging issue the following command, which will tell Xephyr to start drawing red rectangles:
153 $ kill -USR1 `pidof Xephyr`
156 For further information, please refer to http://cgit.freedesktop.org/xorg/xserver/tree/hw/kdrive/ephyr/README.