knaxuslearn · build · ship

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=cpu uses 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 Instrumentation handle 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.

  1. In Phase 1, why did Workload.warm show only a small share even though it used 30% of the time?
    We counted only the innermost frame. warm spends its time inside StringBuilder methods, so the samples landed in those children and warm itself was rarely on top. With whole stacks the same samples count toward the total of warm.
  2. 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.
  3. Why can a thread blocked on a socket appear as RUNNABLE, and how did we get around it?
    The JVM calls a thread RUNNABLE unless 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 with ThreadMXBean and counted it only when the CPU time had moved since the previous sample.
  4. 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.
  5. 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