Fire & Fury:
Java Flight Recorder &
Flamegraphs
©AmadeusITGroupanditsaffiliatesandsubsidiaries
_Java Flight Recorder
_Java Mission Control & jcmd
_Flamegraphs
_Flamegrapher
2
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Agenda
A little bit of history
©AmadeusITGroupanditsaffiliatesandsubsidiaries
4
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Have you heard about
Java Flight Recorder
©AmadeusITGroupanditsaffiliatesandsubsidiaries
_An “airplane flight recorder” for JVM
_Recording telemetry information while the JVM is running
_Has very low overhead (< 1% for the long running application)
_Bundled with JVM
_Extensible
_CPU profiling outside of safepoints
6
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder
_CPU & thread stack sampling
_Garbage collection information
_Exceptions created
_File and socket operations
_Locks that were held for longer than 10ms
_Allocations on Thread Local Allocation Buffer (TLAB) and outside TLABs.
7
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder
(A subset of) collected events
_Use cases
• Profiling of running application
• Debugging or investigating issues
• Black box analysis & anomaly detection
_Use
• Can be started at launch
• Or dynamically for the running project
8
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder
Use Cases
Java Mission Control
©AmadeusITGroupanditsaffiliatesandsubsidiaries
_Tool for managing, monitoring, profiling JVM processes
• $JAVA_HOME/bin/jmc
• JMX Console
• Java Flight Recorder
_Online from running program or offline from JFR dump
10
©AmadeusITGroupanditsaffiliatesandsubsidiaries
What is Mission Control
jcmd
©AmadeusITGroupanditsaffiliatesandsubsidiaries
_Tool to send diagnostic commands to JVM
_Replaces: jheap, jmap, jps, jstack, jstat
_Usage:
• jcmd <process id|main class> <command>
• jcmd
• jcmd my.MainClass help
• jcmd my.MainClass VM.command_line
• jcmd 3871 Thread.print
18
©AmadeusITGroupanditsaffiliatesandsubsidiaries
jcmd
jcmd
_jcmd VM.flags
_jcmd VM.command_line
19
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Some examples
20
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Oracle JDK and Open JDK, 8 vs 10
jcmd help
The following commands are available:
JFR.configure
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.log
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.status
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
Compiler.directives_clear
Compiler.directives_remove
Compiler.directives_add
Compiler.directives_print
VM.print_touched_methods
Compiler.codecache
Compiler.codelist
Compiler.queue
VM.classloader_stats
Thread.print
JVMTI.data_dump
JVMTI.agent_load
VM.stringtable
VM.symboltable
VM.class_hierarchy
VM.systemdictionary
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.finalizer_info
GC.heap_info
GC.run_finalization
GC.run
VM.info
VM.uptime
VM.dynlibs
VM.set_flag
VM.flags
VM.system_properties
VM.command_line
VM.version
help
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help
JDK 8
JDK 10
The following commands are available:
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help
Open JDK
Flamegraphs 🔥
©AmadeusITGroupanditsaffiliatesandsubsidiaries
_Data visualization developed by Brendan Gregg
_Trying to make sense of the output provided by Linux perf
_Allows you to:
• Identify most frequent code-paths easily
• Once you understand how it works :)
22
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Flamegraphs?
Stack
depth
Alphabet
SAMPLING
Main
Credits: https://speakerdeck.com/mrfoto/what-are-flame-graphs-and-how-to-read-them-rubyconfby-2017
SAMPLING
Main
Parse HTTP headers
SAMPLING
Main
Parse HTTP headers
Parse HTTP parameters
SAMPLING
Main
Call service
SAMPLING
Main
Call service
Call ORM
SAMPLING
Main
Call service
Call ORM
Call database driver
SAMPLING
Main
Call service
SAMPLING
Main
Call service
SAMPLING
Main
Build HTTP response body
SAMPLING
Main
Add HTTP response headers
SAMPLING
Main
GROUPING
GROUPING
GROUPING
Header
Driver
Body Header
Main
Request Service
ORM
Stack
depth
Alphabet
Credits: Brendan Gregg
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
g ( )
h ( )
i ( )
Credits: Brendan Gregg
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
g ( )
h ( )
i ( )
Top down shows the stack
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
g ( )
h ( )
i ( )
What’s running on CPU
Credits: Brendan Gregg
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
g ( )
h ( )
i ( )
How much (width)
Credits: Brendan Gregg
Credits: Brendan Gregg
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
g ( )
h ( )
i ( )
Widths are proportional to presence in samples
_Flamegraphs are great!
_We need a way to get them out of JFR recordings
47
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
48
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
49
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
50
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
As off today, JFR is a commercial feature of Oracle JDK
Non-production usage is free
51
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
Start a new record
Using the profiling settings
52
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
Dump the recording Specify location
53
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
Dumping a recording doesn’t stop it
We need to explicitly stop it.
54
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
55
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
By default, creates a CPU flamegraph.
Needs to have Brendan Gregg’s repository cloned locally for
SVG generation
56
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Java Flight Recorder & Flamegraphs
Flamegrapher 🔥
©AmadeusITGroupanditsaffiliatesandsubsidiaries
_ To generate a flamegraph:
• Run JCMD
• Start a recording
• Dump the record
• Stop the recording
• Run tool to generate SVG
• Open in the your favorite browser
58
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Why Flamegrapher?
59
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Wouldn’t it be nicer to simply
open the browser?
60
©AmadeusITGroupanditsaffiliatesandsubsidiaries
github.com/flamegrapher
61
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Demo time
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
g ( )
h ( )
i ( )
For how long threads have been held waiting to acquire this lock?
Locks flamegraphs
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
g ( )
h ( )
i ( )
Width is time in milliseconds
Locks flamegraphs
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
g ( )
h ( )
i ( )
Stack trace leads to an exception (or error) being created
Exceptions flamegraphs
g ( )
a ( )
b ( )
c ( )
d ( )
e ( ) f ( )
h ( )
i ( )
Width is the number of instances of that exception (or error),
that have been created
Exceptions flamegraphs
67
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Demo time
68
©AmadeusITGroupanditsaffiliatesandsubsidiaries
jcmd
Mission Control
Flamegraph
JFR
Not only java
©AmadeusITGroupanditsaffiliatesandsubsidiaries
_https://github.com/google/pprof (golang)
_https://github.com/evanhempel/python-flamegraph (python)
_https://nodejs.org/en/blog/uncategorized/profiling-node-js/ (nodejs)
_https://github.com/jstepien/flames/ (clojure)
70
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Other languages
Questions?
@NenadBo
@lgomes
github.com/flamegrapher
Thank you!
©AmadeusITGroupanditsaffiliatesandsubsidiaries
You can follow us on:
AmadeusITgroup
amadeus.com
amadeus.com/blog
_https://www.flickr.com/photos/lsfbs/34770141071/
_https://www.flickr.com/photos/genista/20091294
_https://www.flickr.com/photos/neilmoralee/8457411309
_https://www.flickr.com/photos/andrewmalone/5162620357/
_https://www.flickr.com/photos/richardthomas78/59947625
74
©AmadeusITGroupanditsaffiliatesandsubsidiaries
Photo credits

Java Flight Recorder & Flamegraphs

Editor's Notes

  • #28 Sampling is like taking a snapshot of what’s on CPU at a given moment; Imagine we have a program running and it’s like an HTTP-based server.
  • #29 - It receives a request and starts by parsing its headers
  • #30 - Next time we take a snapshot it’s parsing HTTP parameters
  • #31 - On the next snapshot it’s calling a certain service
  • #32 - New snapshot and the service is calling the ORM layer
  • #33 - We sample again the the ORM layer is calling the code of a database driver
  • #34 New snapshot and it’s the service code that is executing
  • #35 Again, service code
  • #36 We sample again and it’s building an HTTP response body
  • #37 New sample and it’s calling a method to add http headers to the response
  • #38 We’re finally back to main, probably waiting for new requests to be processed
  • #39 Now imagine grouping all the parts that are common to the different samples
  • #41 And if we look at the them grouped we can have an idea of the where we’re spending time in our program.
  • #42 Finally, that flamegraph probably starts making more sense
  • #51 http://openjdk.java.net/jeps/328
  • #74 Backcover option 2