Video in progress
CPU is 90% busy and the cause is unknown. How do you take a flame graph with async-profiler and how do you read it?
I attach async-profiler to the running process, collect CPU samples for 30-60 seconds and get an HTML flame graph. In it, width is the share of CPU time and height is call depth. Wide "plateaus" at the top are the hot spots.
📖 Answer
async-profiler works without safepoint bias. That means it shows where the CPU really is, not just the places where the JVM is able to stop. It sees native and kernel frames too.
./asprof -d 60 -e cpu -f /tmp/cpu.html $PID
# by allocation:
./asprof -d 60 -e alloc -f /tmp/alloc.html $PID
# by lock wait:
./asprof -d 60 -e lock -f /tmp/lock.html $PID
Reading a flame graph:
- at the bottom is
mainor the thread's entry point, and the called methods go upward; - the X axis is not time! Frames are sorted alphabetically, and only the width means anything;
- a wide block with a flat top means the method itself is eating the CPU (self time); a wide block with narrow children on top means the time is spread inside.
Findings that are common in practice: Pattern.compile called on every request, creating Jackson's ObjectMapper over and over, String.format in logging, a bad hashCode for a HashMap. Another one: if G1 or GC Thread takes up a large part of the flame graph, the problem is not in the code but in allocation.
In a container perf_events may be blocked. In that case use the -e itimer mode.
Sign up to mark studied questions and save your progress.