Flame graphs & scala

I was profiling a scala application not so long ago and came across this interesting flamegraphs. This post will guide you through setting up the environment and exploring the flamegraphs of your scala application.

Environment Setup

  • Java >= 8
  • Install linux perf (yum / apt-get install perf)
  • Perf-map-agent: A java agent to generate method mappings to use with the linux perf tool
  • FlameGraph: Stack trace visualizer.
  • export JAVA_HOME= Set your Java Home.

Starting the application

The JVM flag -XX:+PreserveFramePointer is available since Java 8, we need to use this flag while starting the scala application.

java -XX:+PreserveFramePointer -jar my-application.jar

Lets say the process started and the process id is 7706. You will see 7706 as the PID throughout the post and in the screenshots.

Run the perf-map-agent/bin/ <PID> to create /tmp/perf-<pid>.map files for JIT-compiled methods to use with the perf tools.

Capturing the performance events

Start the perf command to capture 60 seconds of 99 Hertz stack samples for the PID 7706, execute:

perf record -F 99 -p 7706 -g -- sleep 60

Generating the flame graph

Execute the perf script, stackcollapse, flamegraph scripts from Netflix to generate the flamegraph.svg file.

perf script | | --color=java --hash > flamegraph.svg

Reading the flame graph

Taken from this blog-post.

Now, lets see how does the flamegraph.svg look, you can open it in the browser and hover over each blocks to zoom in.

Flame graph color schemes

There are different color blocks in the flame graph listed above, lets see what each color represents here

Happy flame graphing! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *