Projects / Profiler agent / Wrap up
Wrap up
You built a sampling profiler from the JVM's own parts: an agent that starts before main, a sampler thread, a way to keep whole stacks, a report with total and self time, three modes for deciding who counts, a flame graph, a way to attach to a running program, and a cross-check against JFR.
What this version leaves out
- It samples with
Thread.getAllStackTraces, which stops threads for a moment and sees only Java frames. - The report prints the top eight methods only, and the collapsed file is the only place to see everything.
mode=cpuuses a simple threshold, so a thread that uses a little CPU in many short bursts may be missed.- The flame graph is a static picture. There is no click to zoom and no search box.
- There is no allocation, lock or I/O profiling, only time.
- We ran everything on Linux with JDK 21. Agents and the Attach API are part of the JDK on other systems too, but we did not run the guide there.
Keep going
- Click to zoom. Add a little JavaScript to the SVG so that clicking a box makes it the new root.
- Per-thread graphs. Split the collapsed file by the thread name at the root and draw one graph per thread.
- Rewrite the bytecode. Use the
Instrumentationhandle we ignored to time every call of a chosen method. You will need a library such as ASM or Byte Buddy for the rewriting, and you will learn first-hand why instrumentation changes the very thing it measures. - Allocation profiling. Read JFR's allocation events from the recording and add a second report.
- Differences between two profiles. Compare two collapsed files and color the boxes by how much they grew.
What real profilers do differently
- Tools such as async-profiler avoid the safepoint problem by not relying on the stack walk that needs one.
- JFR is built into the JVM, records many event types at once with a low overhead, and writes them into a compact binary file.
- Researchers test profilers against programs with a known answer, just like our 60/30/10 workload, and the results are a good reason to keep the habit of a second opinion.
Check yourself
Answer each question in your head first, then open it to check.
In Phase 1, why did
Workload.warmshow only a small share even though it used 30% of the time?We counted only the innermost frame.warmspends its time insideStringBuildermethods, so the samples landed in those children andwarmitself was rarely on top. With whole stacks the same samples count toward the total ofwarm.A method has a total of 40% and a self of 2%. What does that tell you, and where do you look next?
The method itself does almost nothing, and the time is in what it calls. Look at its children in the flame graph, or at the methods right below it in the total column.Why can a thread blocked on a socket appear as
RUNNABLE, and how did we get around it?The JVM calls a threadRUNNABLEunless it is blocked on a Java lock or waiting in Java code, and a thread waiting inside a native call is neither. We read the thread's CPU time withThreadMXBeanand counted it only when the CPU time had moved since the previous sample.Your colleague says "the profile shows 2% in
parse, so it is not worth looking at". What would you ask before agreeing?How many samples was that, and for how long did we run? A few samples are not a measurement. Also, which mode was it: for a latency problem, a CPU profile can miss all the waiting.Teach it back: a teammate says flame graphs show what happened over time, with the left side being earlier. What do you answer?
No. The x axis of a flame graph has no time. Boxes are sorted alphabetically so that identical stacks merge, and the width of a box is only the share of samples that contained that stack. A graph that does put time on the x axis is called a flame chart.
Sources
- Package java.lang.instrument, the specification of
premain,agentmainand the manifest attributes. - Module jdk.attach, the Attach API.
- The jfr command, for printing and summarizing flight recordings.
- Package jdk.jfr.consumer, which includes an example that builds a histogram of method samples.
- The Flame Graph, the article by Brendan Gregg that explains how to read one.
- FlameGraph on GitHub, the tools behind the collapsed stack format.
- Profilers Are Lying Hobbitses, Nitsan Wakart's talk on safepoint bias in sampling profilers.
- Don't Trust Your Profiler, a 2023 study of how precise and accurate Java sampling profilers are.
- JEP draft: JFR Cooperative Sampling, which describes how JFR samples threads and how it deals with safepoint bias.