Skip to content

Commit

Permalink
[GR-19691] Update documentation about flamegraphs
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/3447
  • Loading branch information
eregon committed Jul 28, 2022
2 parents c37d6ae + 02835dd commit af1b318
Showing 1 changed file with 8 additions and 51 deletions.
59 changes: 8 additions & 51 deletions doc/contributor/profiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,22 @@ the most useful information from the tools, assuming basic knowledge of their us

### Creating a Flame Graph

#### The Easy Way

The easiest way to generate a flame graph from a profile is to accept the default profiling
options configured in _jt_. In this case, simply run `jt profile` like you would `jt ruby`
and your application or inline script will be profiled and an SVG written to your
TruffleRuby source directory, named _profiles/flamegraph\_\<timestamp\>.svg_.

To make things even easier, `jt profile` will automatically open up the flame graph for
you in whatever application you have configured as your default for SVG files. This is
handled by the `open` command on macOS and the `xdg-open` command on Linux.

To illustrate, here's a command for profiling and inline script.

```bash
jt profile -e "x = 'abc'; 100_000.times { x.upcase }"
```

Here's a command for profiling the `gem list` command:

Use the [CPUSampler](https://www.graalvm.org/dev/tools/profiling/) ([blog post](https://medium.com/graalvm/where-has-all-my-run-time-gone-245f0ccde853)) in flamegraph mode, like this:
```bash
jt profile -S gem list
ruby --cpusampler=flamegraph ...
```

#### The Less Easy Way
This will create a `flamegraph.svg`, which you can open in a web browser
(Chromium-based browsers seem faster for rendering the flamegraph).

The histogram output from the Truffle profiler can be quite large, making it difficult to
analyze. Additionally, as a flat format it isn't possible to analyze a call graph as that
information simply isn't encoded in the output. A flame graph shows the entire call graph
and its structure makes it considerably simpler to see where the application time is being
spent.

Creating the flame graph is a multi-stage process. First, we need to profile the application
with the JSON formatter:

There is also `jt profile` which does the same but uses a unique filename, prints the wall-clock time and open the SVG for you:
Here are some examples:
```bash
jt ruby --cpusampler --cpusampler.Output=json -e 'p :hello' > simple-app.json
```

The JSON profiler formatter encodes call graph information that isn't available in the
histogram format. To make a flame graph out of this output, however, we need to transform
it into a format that folds the call stack samples into single lines. This can be done
using [stackcollapse-graalvm.rb](https://github.com/eregon/FlameGraph/blob/graalvm/stackcollapse-graalvm.rb)
from Benoit's fork of FlameGraph.

If you haven't yet, you should clone Benoit's [fork of FlameGraph](https://github.com/eregon/FlameGraph/tree/graalvm)
into TruffleRuby's parent directory. Now you can run the script to transform the output and
pipe it into the script that will generate the SVG data:

```bash
../FlameGraph/stackcollapse-graalvm.rb simple-app.json | ../FlameGraph/flamegraph.pl > simple-app.svg
jt profile -e "x = 'abc'; 100_000.times { x.upcase }"
jt profile -S gem list
```

At this point, you should open the SVG file in a Chromium-based web browser. Your system
might have a different image manipulation application configured as the default application
for SVG files. While loading the file in such an application make render a graph, it likely
will not handle the interactive components of the flame graph. Firefox may work as well,
but Chromium-based browsers seem to have better support and performance for the flame graph
files as of this writing (Dec. 2018).

### Require Profiling
The `--metrics-profile-require` option can be used to profile the time used for searching, parsing, translating and loading files during require.

Expand Down

0 comments on commit af1b318

Please sign in to comment.