1 # JIT Profiling with perf
3 perf is a performance profiling tool available on Linux that is capable of measuring performance events such as cycles, instructions executed, cache misses, etc and providing assembly and source code annotation.
4 It is possible to collect performance profiles of the SpiderMonkey JIT using perf on Linux and also annotate the generated assembly with the IR opcodes that were used during compilation as shown below.
6 ![](img/annotation.png)
10 To enable JIT profiling with perf jitdump, you must build Firefox or the JS shell with the following flag:
13 ac_add_options --enable-perf
16 ## Environment Variables
18 Environment variables that must be defined for perf JIT profiling:
20 `PERF_SPEW_DIR`: Location of jitdump output files. Making this directory a tmpfs filesystem could help reduce overhead.\
21 `IONPERF`: Valid options include: `func`, `src`, `ir`, `ir-ops`.
23 `IONPERF=func` will disable all annotation and only function names will be available. It is the fastest option.\
24 `IONPERF=ir` will enable IR annotation.\
25 `IONPERF=ir-ops` will enable IR annotation with operand support. **Requires --enable-jitspew** and adds additional overhead to "ir".\
26 `IONPERF=src` will enable source code annotation **only if** perf can read the source file locally. Only really works well in the JS shell.
28 ## Profiling the JS shell
30 Profiling the JS shell requires the following commands but is very straight forward.
32 Begin by removing any pre-existing jitdump files:
34 `rm -rf output` or `rm -f jitted-*.so jit.data perf.data jit-*.dump jitdump-*.txt`
36 Next define environment variables:
39 export PERF_SPEW_DIR=output
42 Run your test case with perf attached:
44 perf record -g -k 1 /home/denis/src/mozilla-central/obj-js/dist/bin/js test.js
47 Inject the jitdump files into your perf.data file:
49 perf inject -j -i perf.data -o jit.data
54 perf report --no-children --call-graph=graph,0 -i jit.data
57 All of the above commands can be put into a single shell script.
59 ## Profiling the Browser
61 Profiling the browser is less straight forward than the shell, but the only main difference is that perf must attach to the content process while it is running.
63 Begin by removing any pre-existing jitdump files:
65 `rm -rf output` or `rm -f jitted-*.so jit.data perf.data jit-*.dump jitdump-*.txt`
67 Next define environment variables:
70 export PERF_SPEW_DIR=output
71 export MOZ_DISABLE_CONTENT_SANDBOX=1
74 Run the Firefox browser
76 ~/mozilla-central/obj-opt64/dist/bin/firefox -no-remote -profile ~/mozilla-central/obj-opt64/tmp/profile-default &
79 Navigate to the test case, but do not start it yet. Then hover over the tab to get the content process PID.
83 Attach perf to begin profiling:
85 perf record -g -k 1 -p <pid>
88 Close the browser when finished benchmarking.
90 Inject the jitdump files into your perf.data file:
92 perf inject -j -i perf.data -o jit.data
95 View the profile (--call-graph=graph,0 shows all call stacks instead of the default threshold of >= 0.5%):
97 perf report --no-children --call-graph=graph,0 -i jit.data
100 ## Additional Information
102 Some Linux distributions offer a "libc6-prof" package that includes frame pointers. This can help resolve symbols and call stacks that involve libc calls.
104 On Ubuntu, you can install this with:
106 sudo apt-get install libc6-prof
109 libc6-prof can be used with `LD_LIBRARY_PATH=/lib/libc6-prof/x86_64-linux-gnu`
111 It may also be useful to have access to kernel addresses during profiling. These can be exposed with:
113 sudo sh -c "echo 0 > /proc/sys/kernel/kptr_restrict"
116 The max stack depth is 127 by default. This is often too few. It can be increased with:
118 sudo sh -c "echo 4000 > /proc/sys/kernel/perf_event_max_stack"