A flame graph is a picture of where a program spends its time. Each box is a method, and its width is the share of the samples that included it. The boxes above a method are the methods it called, so a wide box high in the picture is a place worth reading. (JFR) from Part 10 already collects the stack samples a flame graph is built from; it only lacks the picture. Neovim cannot draw one, but it can run the tools that do and open the result.
What a recording holds
JFR samples the stacks of running Java threads and stores each one as a jdk.ExecutionSample event. The hot-methods view from Part 10 counts only the method at the top of each stack. A flame graph keeps the whole stack, so it shows not just that a method is hot but which code called it. Our eight-second recording of the catalog app under load held 550 such events.
Three ways to see the picture
- JDK Mission has a Flame View for
.jfrfiles, described in its user guide. It is a desktop application, and we did not run it. - IntelliJ IDEA shows flame graphs in its profiler, as Part 10 noted. We did not run it either.
- The async-profiler converter is one 136 KB Java file that turns a
.jfrfile into a single HTML page. It runs from a Neovim command line, and it is the only route we tested.
Record the running app
Use the steps from Part 10, with the built-in profile settings. The recording needs requests to look at, because an idle app only records waiting threads:
PID=$(jcmd -l | awk '/ShopApp/{print $1}')
jcmd $PID JFR.start name=shop settings=profile filename=/tmp/shop.jfr
seq 1 400 | xargs -P8 -I{} curl -s -o /dev/null "localhost:8091/catalog?size=2"
jcmd $PID JFR.dump name=shop filename=/tmp/shop.jfr
jcmd $PID JFR.stop name=shop
The catalog app ran on port 8091 in our test. /tmp/shop.jfr is now a recording you can read with jfr summary.
Convert it to HTML
Download the converter from the async-profiler releases page and run it. Version 4.6 needs only a JDK:
curl -fLO https://github.com/async-profiler/async-profiler/releases/download/v4.6/jfr-converter.jar
java -jar jfr-converter.jar /tmp/shop.jfr /tmp/shop.html
Leave out the mode flags. On a recording made by the JDK itself, --cpu and --cpu-time produced an empty graph, and we did not find out why. Without a flag we got real stacks. --alloc produced an allocation graph instead, and -o collapsed writes the stacks as text, one per line with the sample count at the end, which you can open in Neovim and search with /.
From Neovim, the same command runs with :!java -jar ~/tools/jfr-converter.jar /tmp/shop.jfr /tmp/shop.html.
Cut away the waiting
The first graph is full of threads that wait. In ours, Tomcat’s acceptor and poller threads were the two widest stacks, 136 and 139 of 911 samples, each ending in a native wait. The filter -s runnable left them in place.
-I keeps only stacks that contain a frame matching a regular expression. The expression must match the whole frame name, so add .* on both sides:
java -jar jfr-converter.jar -I '.*springuniverse.*' /tmp/shop.jfr /tmp/shop.html
That kept 192 samples whose stacks pass through the companion code. -I 'springuniverse' without the dots matched nothing and gave an empty graph.
Open the page
:lua vim.ui.open("/tmp/shop.html") hands the file to the system opener; on Linux the command it started was xdg-open. Our test machine has no desktop session, so we rendered the file with headless Chrome into an image instead. The page showed a title, a toolbar of icons, and rows of boxes named after the frames. Below our info/mnafshin boxes sat Spring Data, Hibernate, the connection pool and the PostgreSQL driver, ending in socket reads. On a remote machine the file lives on the server, so copy it to your own computer first.
What this does not give you
There is no flame graph inside Neovim: the picture is a browser page. IntelliJ draws it beside your code and links a box to the source; here you read the method name and jump to it with gd or a symbol search. We did not run JDK Mission Control, IntelliJ’s profiler or Speedscope, so we cannot say how they compare. The converter is a separate download, not part of the JDK.
Recap
- A flame graph shows whole call stacks, with width equal to a method’s share of the samples; JFR already records them.
- Record with
jcmdagainst the running app while it handles requests, then convert the.jfrfile withjfr-converter.jar, which needs only a JDK. - Skip
--cpuon a JDK recording; filter with-I '.*package.*'to keep stacks through your own code. vim.ui.openopens the HTML page in a browser;-o collapsedgives text you can search in Neovim.- Neovim draws no flame graph itself, and we tested no desktop viewer.
Series: Neovim as a Java IDE. Previous: Practise the IDE keys with a game inside Neovim · Next: Observing a running Spring Boot app from Neovim
Sources
Official documentation and well-known tutorials read while writing this article. The wording here is our own.
Comments