Slides used for an internal training. This explains how to generate Flame Graphs using Java Flight Recorder dumps. There is also an example to use Linux "perf_events" to generate a Java Mixed-Mode Flame Graph.
2. Flame Graphs
o Flame graphs are a visualization of profiled
software, allowing the most frequent code-paths
to be identified quickly and accurately.
o Flame Graphs can be generated using
https://github.com/brendangregg/FlameGraph
o This creates an interactive SVG
http://www.brendangregg.com/flamegraphs.html
3. Types of Flame Graphs
o CPU
o Memory
o Off-CPU
o Hot/Cold
o Differential
4. Flame Graph: Definition
o The x-axis shows the stack profile population, sorted alphabetically
o The y-axis shows stack depth
o The top edge shows what is on-CPU, and beneath it is its ancestry
o Each rectangle represents a stack frame.
o Box width is proportional to the total time a function was profiled directly or
its children were profiled
5. Flame Graphs with Java Flight Recordings
o We can generate CPU Flame Graphs from a
Java Flight Recording
o Program is available at GitHub:
https://github.com/chrishantha/jfr-flame-graph
o The program uses the (unsupported) JMC
Parser
6. Generating a Flame Graph using JFR dump
o JFR has Method Profiling Samples
o You can view those in “Hot Methods” and “Call Tree”
tabs
o A Flame Graph can be generated using these
Method Profilings Samples
7. Profiling a Sample Program
o Get Sample “highcpu” program from
https://github.com/chrishantha/sample-jav
a-programs
o Checkout v0.0.1 tag and build
o Get a Profiling Recording
o java -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+UnlockCommercialFeatures
-XX:+FlightRecorder
-XX:StartFlightRecording=delay=20s,duration=1m,name=Profiling,filename=highcpu_profiling.jfr,settings=
profile -jar target/highcpu-0.0.1.jar
9. Java Mixed-Mode Flame Graphs
o With Java Profilers, we can get information
about Java process only.
o However with Java Mixed-Mode Flame Graphs,
we can see how much CPU time is spent in
Java methods, system libraries and the kernel.
o Mixed-mode means that the Flame Graph
shows profile information from both system
code paths and Java code paths.
11. The Problem with Java and Perf
o perf needs the Java symbol table
o JVM doesn’t preserve frame pointers by default
o Run sample program
o java -jar target/highcpu-0.0.1.jar --exit-timeout 600
o Run perf record
o sudo perf record -F 99 -g -p `pgrep -f highcpu`
o Display trace output
o sudo perf script
12. Preserving Frame Pointers in JVM
o Run java program with the JVM flag
"-XX:+PreserveFramePointer"
o java -XX:+PreserveFramePointer -jar
target/highcpu-0.0.1.jar --exit-timeout 600
o This flag is working only on JDK 8 update 60
and above.
13. How to generate Java symbol table
o Use a java agent to generate method mappings
to use with the linux `perf` tool
o Clone & Build
https://github.com/jrudolph/perf-map-agent
o Create symbol map
o ./create-java-perf-map.sh `pgrep -f highcpu`
14. Generate Java Mixed Mode Flame Graph
o Run perf
o sudo perf record -F 99 -g -p `pgrep -f highcpu` --
sleep 60
o Create symbol map
o Generate Flame Graph
o sudo perf script > out.stacks
o $FLAMEGRAPH_DIR/stackcollapse-perf.pl
out.stacks | $FLAMEGRAPH_DIR/flamegraph.pl
--color=java --hash --width 1680 >
java-mixed-mode.svg
15. Java Mixed-Mode Flame Graphs
o Helps to understand
Java CPU Usage
o With Flame Graphs, we
can see both java and
system profiles
o Can profile GC as well