Skip to content

Commit 268332c

Browse files
committed
[Inside Rust] Intro to rustc's self-profile feature
1 parent 01d4c93 commit 268332c

File tree

7 files changed

+672
-0
lines changed

7 files changed

+672
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
---
2+
layout: post
3+
title: "Intro to rustc's self profiler"
4+
author: Wesley Wiser
5+
description: "Learn how to use the -Zself-profile rustc flag"
6+
team: the self-profile working group <https://rust-lang.github.io/compiler-team/working-groups/self-profile/>
7+
---
8+
9+
Over the last year, the [Self-Profile Working Group] has been building tools to profile `rustc` because we often hear requests to know where compilation time is being spent.
10+
This is useful when optimizing the compiler, one of the Compiler Team's ongoing efforts to improve compile times, but it's also useful to users who want to refactor their crate so that it will compile faster.
11+
We've been working on a new feature that will help with that and this blog post gives a preview of how it works.
12+
Be warned, though: it is still experimental and we expect the interface to change over time.
13+
The Rust Unstable Book has [documentation for this feature] and we'll keep that up to date so you can always find the latest instructions there.
14+
15+
In this post, we'll look at the tools currently available and use them to profile `rustc` while it compiles an example crate.
16+
17+
## Setup
18+
19+
First, we'll download and build the `measureme` repository which provides tools to analyze self-profile trace data.
20+
21+
```sh
22+
$ git clone https://github.com/rust-lang/measureme.git
23+
$ cd measureme
24+
$ cargo build --release --all
25+
```
26+
27+
Now that we have our tools, let's download an example crate to profile its build.
28+
29+
```sh
30+
$ cd ..
31+
$ git clone https://github.com/rust-lang/regex.git
32+
$ cd regex
33+
```
34+
35+
We'll need to use a recent nightly compiler to get access to unstable `-Z` flags.
36+
37+
```sh
38+
$ rustup override set nightly
39+
```
40+
41+
If you haven't installed a nightly compiler before, this will download the nightly compiler for you.
42+
If you have, then update it to make sure you're on a recent version.
43+
44+
```sh
45+
$ rustup update nightly
46+
```
47+
48+
## Profiling the compiler
49+
50+
Now we can build it and tell `rustc` to profile the build of the `regex` crate.
51+
This will cause three new files to be created in the working directory which contain the profling data.
52+
53+
```sh
54+
$ cargo rustc -- -Zself-profile
55+
$ ls
56+
CHANGELOG.md LICENSE-APACHE UNICODE.md regex-17088.string_data regex-syntax target
57+
Cargo.lock LICENSE-MIT bench regex-17088.string_index rustfmt.toml test
58+
Cargo.toml PERFORMANCE.md examples regex-capi scripts tests
59+
HACKING.md README.md regex-17088.events regex-debug src
60+
```
61+
62+
The new files follow the format `{crate name}-{rustc process id}.{events,string_data,string_index}`.
63+
64+
We'll use each of the three main tools to analyze the profiling data:
65+
66+
### `summarize`
67+
68+
As its name suggests, this tool summarizes the data found in the trace files.
69+
Additionally, `summarize` can also show a "diff" between two trace files but we won't be using this mode.
70+
71+
Let's run the tool, passing just the file name (but not the extension) for the trace:
72+
73+
```sh
74+
$ ../measureme/target/release/summarize summarize regex-17088
75+
+-----------------------------------------------+-----------+-----------------+----------+------------+
76+
| Item | Self time | % of total time | Time | Item count |
77+
+-----------------------------------------------+-----------+-----------------+----------+------------+
78+
| LLVM_module_codegen_emit_obj | 4.89s | 42.752 | 4.89s | 159 |
79+
+-----------------------------------------------+-----------+-----------------+----------+------------+
80+
| codegen_module | 1.25s | 10.967 | 1.37s | 159 |
81+
+-----------------------------------------------+-----------+-----------------+----------+------------+
82+
| LLVM_module_optimize_module_passes | 1.15s | 10.022 | 1.15s | 159 |
83+
+-----------------------------------------------+-----------+-----------------+----------+------------+
84+
| LLVM_module_codegen_make_bitcode | 786.56ms | 6.875 | 960.73ms | 159 |
85+
+-----------------------------------------------+-----------+-----------------+----------+------------+
86+
| typeck_tables_of | 565.18ms | 4.940 | 689.39ms | 848 |
87+
+-----------------------------------------------+-----------+-----------------+----------+------------+
88+
| LLVM_module_codegen | 408.01ms | 3.566 | 6.26s | 159 |
89+
+-----------------------------------------------+-----------+-----------------+----------+------------+
90+
| mir_borrowck | 224.03ms | 1.958 | 543.33ms | 848 |
91+
+-----------------------------------------------+-----------+-----------------+----------+------------+
92+
| LLVM_module_codegen_emit_compressed_bitcode | 174.17ms | 1.522 | 174.17ms | 159 |
93+
+-----------------------------------------------+-----------+-----------------+----------+------------+
94+
| optimized_mir | 157.91ms | 1.380 | 205.29ms | 1996 |
95+
+-----------------------------------------------+-----------+-----------------+----------+------------+
96+
| evaluate_obligation | 146.50ms | 1.281 | 184.17ms | 8304 |
97+
+-----------------------------------------------+-----------+-----------------+----------+------------+
98+
| codegen_crate | 139.48ms | 1.219 | 1.58s | 1 |
99+
+-----------------------------------------------+-----------+-----------------+----------+------------+
100+
| mir_built | 123.88ms | 1.083 | 168.01ms | 848 |
101+
+-----------------------------------------------+-----------+-----------------+----------+------------+
102+
| metadata_decode_entry | 88.36ms | 0.772 | 117.77ms | 55642 |
103+
+-----------------------------------------------+-----------+-----------------+----------+------------+
104+
| incr_comp_copy_cgu_workproducts | 64.21ms | 0.561 | 64.21ms | 1 |
105+
+-----------------------------------------------+-----------+-----------------+----------+------------+
106+
| monomorphization_collector_graph_walk | 54.11ms | 0.473 | 344.00ms | 1 |
107+
+-----------------------------------------------+-----------+-----------------+----------+------------+
108+
| link_rlib | 43.21ms | 0.378 | 43.21ms | 1 |
109+
+-----------------------------------------------+-----------+-----------------+----------+------------+
110+
| check_impl_item_well_formed | 41.36ms | 0.362 | 77.14ms | 736 |
111+
+-----------------------------------------------+-----------+-----------------+----------+------------+
112+
| codegen_fulfill_obligation | 40.36ms | 0.353 | 51.56ms | 1759 |
113+
+-----------------------------------------------+-----------+-----------------+----------+------------+
114+
| expand_crate | 37.24ms | 0.326 | 48.52ms | 1 |
115+
+-----------------------------------------------+-----------+-----------------+----------+------------+
116+
| symbol_name | 36.31ms | 0.317 | 39.06ms | 5513 |
117+
+-----------------------------------------------+-----------+-----------------+----------+------------+
118+
| free_global_ctxt | 34.34ms | 0.300 | 34.34ms | 1 |
119+
+-----------------------------------------------+-----------+-----------------+----------+------------+
120+
...
121+
Total cpu time: 11.440758871s
122+
```
123+
124+
The output is sorted by the self time (time spent in the query or activity but not other queries or activities called by itself).
125+
As you can see, most of the compilation time is spent in LLVM generating the binary code for the executable.
126+
127+
### `flamegraph`
128+
129+
As you may have guessed, `flamegraph` will produce a [flame graph] of the profiling data.
130+
To run the tool, we'll pass just the filename without a file extension like we did for `summarize`:
131+
132+
```sh
133+
$ ../measureme/target/release/flamegraph regex-17088
134+
```
135+
136+
This will create a file called `rustc.svg` in the working directory:
137+
138+
[![Image of flamegraph output][flame graph img]][flame graph img]
139+
140+
[Click here] to try the interactive svg.
141+
142+
### `crox`
143+
144+
This tool processes self-profiling data into the JSON format that the Chromium profiler understands.
145+
You can use it to create a graphical timeline showing exactly when various traced events occurred.
146+
147+
In this section, we'll cover a few different modes `crox` can run in such as profiling an entire crate compilation including dependencies and filtering out small events.
148+
Let's get started with the basics!
149+
150+
#### Basic usage
151+
152+
To run the tool, we'll just pass the filename without a file extension like we've done before:
153+
154+
```sh
155+
$ ../measureme/target/release/crox regex-17088
156+
```
157+
158+
This creates a file called `chrome_profiler.json` in the working directory.
159+
To open it, we'll use the regular Chromium performance tools you might already be familiar with:
160+
161+
1. Open Chrome
162+
2. Open the Developer Tools console by pressing `Ctrl` + `Shift` + `i` (Windows/Linux) or `Cmd` + `Option` + `i` (macOS)
163+
3. Click the Performance tab at the top of the console.
164+
4. Click the "Load profile" button which looks like an arrow pointing up.
165+
5. Select the `chrome_profiler.json` file we created.
166+
167+
You should now see something similar to this:
168+
169+
[![Image of chrome profiler][chrome profiler img1]][chrome profiler img1]
170+
171+
You can use the scroll wheel on a mouse or the appropriate gesture on a touchpad to zoom in or out of the timeline.
172+
173+
#### Filtering short events
174+
175+
If the `chrome_profiler.json` file gets too large, the normal Chromium performance tools have issues opening the file.
176+
One easy way to deal with this is to tell `crox` to remove events shorter than a chosen duration:
177+
178+
```sh
179+
$ ../measureme/target/release/crox --minumum-duration 2 regex-17088
180+
```
181+
182+
Filtering out events less than 2 microseconds shrinks our `chrome_profiler.js` file from 27mb to 11mb.
183+
184+
#### Capturing event arguments
185+
186+
The self-profiler can be configured to record event arguments during compilation.
187+
For example, queries will include their query key.
188+
This functionality is turned off by default because it increases the self-profiler overhead.
189+
190+
To turn this feature on, we'll need to record a new compilation, passing an additional argument to `rustc`:
191+
192+
```sh
193+
$ cargo clean
194+
$ cargo rustc -- -Zself-profile -Zself-profile-events=default,args
195+
```
196+
197+
And then process the new output files:
198+
199+
```sh
200+
$ ../measureme/target/release/crox regex-23649
201+
```
202+
203+
Now in the Chromium profiler, if you click on a node, you can see additional data about many of the events at the bottom of the screen:
204+
205+
[![Image of Chrome profiler details][chrome profiler img2]][chrome profiler img2]
206+
207+
Which shows this `optimized_mir` query was processing the `regex::compile::{{impl}}::new` function body.
208+
209+
#### Profiling an entire crate graph
210+
211+
By using the `RUSTFLAGS` environment variable, we can profile every `rustc` invocation, not just the final crate's.
212+
`crox` can then combine all of the profiles together into one output file.
213+
Since this will create a lot of files, we'll first create a folder to put all the traces in.
214+
215+
```sh
216+
$ rm regex-17088.* regex-23649.* # clean up the old trace files since we're done with them
217+
$ mkdir profiles
218+
$ cargo clean
219+
$ RUSTFLAGS="-Zself-profile=./profiles -Zself-profile-events=default,args" cargo build
220+
```
221+
222+
This creates quite a few trace files in the working directory.
223+
Now, we'll tell `crox` to combine all of the trace files in the current directory together:
224+
225+
```sh
226+
$ ../measureme/target/release/crox --dir .
227+
```
228+
229+
Opening this file shows all of the crates compiled:
230+
231+
[![Image of Chrome profiler with all crates][chrome profiler img3]][chrome profiler img3]
232+
233+
Clicking on a crate will expand it to show the threads and event data inside it:
234+
235+
[![Image of Chrome profiler with a crate expanded][chrome profiler img4]][chrome profiler img4]
236+
237+
Thanks for reading!
238+
239+
We've been using these tools extensively ourselves over the last few months and they've helped us tremendously in understanding where the compiler spends its time.
240+
In the future we'll be adding more features and we'll work on making the tooling easier to use.
241+
If you have questions or would like to get involved with the Self-Profile Working Group, please check out the [measureme repository] or stop by our [Zulip stream].
242+
243+
[chrome profiler img1]: /images/inside-rust/2020-02-18-intro-rustc-self-profile/chrome_profiler1.png
244+
[chrome profiler img2]: /images/inside-rust/2020-02-18-intro-rustc-self-profile/chrome_profiler2.png
245+
[chrome profiler img3]: /images/inside-rust/2020-02-18-intro-rustc-self-profile/chrome_profiler3.png
246+
[chrome profiler img4]: /images/inside-rust/2020-02-18-intro-rustc-self-profile/chrome_profiler4.png
247+
[Click here]: /images/inside-rust/2020-02-18-intro-rustc-self-profile/rustc.svg
248+
[documentation for this feature]: https://doc.rust-lang.org/unstable-book/compiler-flags/self-profile.html
249+
[flame graph]: http://www.brendangregg.com/flamegraphs.html
250+
[flame graph img]: /images/inside-rust/2020-02-18-intro-rustc-self-profile/flamegraph_image.png
251+
[measureme repository]: https://github.com/rust-lang/measureme
252+
[Self-Profile Working Group]: https://rust-lang.github.io/compiler-team/working-groups/self-profile/
253+
[Zulip stream]: https://rust-lang.zulipchat.com/#narrow/stream/187831-t-compiler.2Fwg-self-profile
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)