# Profiling rustc with perf

**URL:** <https://internals.rust-lang.org/t/profiling-rustc-with-perf/6773>\
**Category:** compiler\
**Created:** [February 16, 2018, 5:25pm UTC](https://internals.rust-lang.org/t/profiling-rustc-with-perf/6773 "2018-02-16T17:25:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 16, 2018, 5:25pm UTC](https://internals.rust-lang.org/t/profiling-rustc-with-perf/6773/1 "2018-02-16T17:25:20Z")

</div>

So I have this tool `perf-focus` that I use to analyze perf results. It’s kind of handy, though probably it duplicates a bunch of other, better tools that I’m not aware of. But I just added a feature that probably _does_ make it of interest to everyone: it now has the ability to profile rustc queries, letting us unintrusively gather statistical data about where rustc is spending its time.

To use it, you would first want to install the tool:

```bash
> cargo install perf-focus

```

You also want to configure your `config.toml` to include `debuginfo-lines` at least, I think, since that will give us the frame pointers that `perf` needs. Then you want to gather some data:

```bash
> perf record -F 99 -g rustc ...

```

Finally, you can analyze the data. The [README] for perf-focus describes its various options, but a command like this is probably what you want:

```bash
> perf focus '{main}' --rustc-query --tree-callees

```

For me, this gives a [lot of output](https://gist.github.com/a104bda28f02d092803bc6d31883234b), so you may want to filter it down to interesting stuff, like anything that takes at least 5% of the total time:

```bash
> perf focus '{main}' --rustc-query --tree-callees --tree-min-percent 5
Matcher : {main}
Matches : 2690
Not Matches: 0
Percentage : 100%

Tree
| matched `{main}` (100% total, 25% self)
: | mir_borrowck<'tcx> (35% total, 35% self)
: | typeck_item_bodies<'tcx> (11% total, 0% self)
: : | typeck_tables_of<'tcx> (11% total, 11% self)
: | compile_codegen_unit<'tcx> (9% total, 8% self)
: | borrowck<'tcx> (6% total, 0% self)
: : | mir_validated<'tcx> (5% total, 0% self)

```

Note here that the “self” measurements are most meaningful – i.e., the `mir_borrowck` accounts for 35% of this run. The `typeck_item_bodies` query, otoh, invoked queries that accounted for 11%, though it itself only accounted for 0%.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 16, 2018, 5:25pm UTC](https://internals.rust-lang.org/t/profiling-rustc-with-perf/6773/2 "2018-02-16T17:25:31Z")

</div>

cc @michaelwoerister

---

<div class="post-metadata">

**Author:** ![michaelwoerister](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/michaelwoerister/32/3557_2.png) [@michaelwoerister](https://internals.rust-lang.org/u/michaelwoerister)\
**Post date:** [February 19, 2018, 10:27am UTC](https://internals.rust-lang.org/t/profiling-rustc-with-perf/6773/3 "2018-02-19T10:27:32Z")

</div>

Awesome! I’ve used `perf-focus` a little in the past, but for a while it would only compile with a rustc from Aug 2016 🙂

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 19, 2018, 4:57pm UTC](https://internals.rust-lang.org/t/profiling-rustc-with-perf/6773/4 "2018-02-19T16:57:39Z")

</div>

It compiles with stable now =)

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/profiling-rustc-with-perf/6773/5 "2019-03-25T08:29:40Z")

</div>

Closed on March 25, 2019, 8:29am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
