Sub topic 9
Why some profilers mislead: safepoint bias
This chapter assumes you know what a sampling profiler does and can read a stack trace. It explains why some samples point at the wrong code and how to avoid that.
What a safepoint is
A safepoint is a moment when the JVM has paused a thread at a known, safe spot. At that spot the JVM can read the thread's stack and object references accurately. Garbage collection, deoptimization and class redefinition all need threads stopped at safepoints.
The JIT compiler does not insert a safepoint check after every instruction. That would be slow. It places checks at method returns and at the ends of loop iterations, with exceptions. Counted loops over an int index often have no check at all, so the JIT can run them as tight code.
How the bias appears
Some profilers collect stack traces by asking the JVM for them, for example through the JVMTI function GetAllStackTraces. The JVM can only answer when each thread reaches a safepoint. The profiler therefore sees where threads were when they stopped, not where they were spending time.
Suppose a long counted loop has no safepoint check inside it. A sample requested during the loop waits until the loop ends. The sample then lands on the code after the loop. The loop is the real hot spot, but it never shows up in the profile.
The result is two kinds of error:
- Code that is hot but has no safepoint checks is under-reported.
- Code near a safepoint, such as a method return or a loop back-edge, is over-reported.
Profilers that avoid the bias
Java Flight Recorder does not wait for safepoints when it samples Java code. It suspends a thread at an arbitrary point and reconstructs the stack from the thread's state. Async-profiler uses a similar approach, sending an OS signal to the thread. Both rely on the HotSpot internal function AsyncGetCallTrace, or an equivalent, to walk the stack from a non-safepoint position.
These methods are not perfect. HotSpot records debug information only at certain points, so a stack reconstructed elsewhere may be slightly off. In practice the error is much smaller than safepoint bias.
Spotting a biased profile
Treat a profile with suspicion if you see these signs:
- Time concentrates in trivial methods, such as getters or loop-control code.
- A method you know is heavy barely appears.
- Two different profilers give very different top methods.
A useful check is to run a second profiler that does not use safepoints and compare the top ten methods. If the ranking changes a lot, trust the one without safepoint bias.
Exercise
Exercise
A profiler reports 40% of CPU time in a method that only returns a field, while a long for (int i = 0; i < n; i++) loop in another method does not appear. What is the likely cause, and what would you do?
Show answer
The profiler is probably safepoint biased. The counted loop has no safepoint check, so samples taken during it are recorded at the next safepoint, such as the return of the getter. Re-profile with a tool that samples without safepoints, such as JFR or async-profiler, and enable -XX:+DebugNonSafepoints for better line accuracy. Then compare the two results.