Elvir Crncevic
Elvir Crncevic

Reputation: 485

How does JFR manage to record stack trace, given it's promises of low-overhead?

Going through https://www.oracle.com/technetwork/java/javaseproducts/mission-control/java-mission-control-wp-2008279.pdf, I ran into the following quote:

Most technologies used today to monitor, manage, and profile the Java runtime use fairly intrusive technologies, like byte code instrumentation and JVMTI.

which made me wonder about the way that JFR does stack trace sampling.

The closest thing that I could find to an answer on the web was this blog post: http://psy-lob-saw.blogspot.com/2016/06/the-pros-and-cons-of-agct.html, mentioning that profilers such as Honest profiler and async-profiler use the not-so-documented AsyncGetCallTrace, but the fact is that it does not mention the specific way in which JFR does stack trace sampling/recording.

Does anyone here have any insight into the JFR internals regarding this topic?

Upvotes: 4

Views: 1151

Answers (1)

Kire Haglin
Kire Haglin

Reputation: 7069

JFR maintains a thread that periodically wakes up, i.e. once every 10 ms, and suspends a small number of running Java threads by sending a signal. It then walks the stacks of the suspended threads to see what methods that were executing. It calculates a hash of the stack frames and then checks if that stack trace has been found before.

If that is the not case, it adds the the stack frames to a hash table and bumps a counter which becomes the id of the stack trace. It then emits the id as an Execution Sample event, which ends up in a buffer (lock free) that another thread periodically flushes to disk. If an id is new, it also writes down the full stack trace that corresponds to the id, so the stack trace can be resolved later by a parser.

If you want to dig deeper, you can have a look at the source code.

http://hg.openjdk.java.net/jdk/jdk/file/tip/src/hotspot/share/jfr/periodic/sampling

How threads are suspended depends on the platform. In the following file you can find the Linux implementation.

http://hg.openjdk.java.net/jdk/jdk/file/tip/src/hotspot/os/linux/os_linux.cpp

Upvotes: 9

Related Questions