Flame Graphs

The GC log can report that executor 3 allocates 9.9 GB a second without saying which code does it, and the stage page stops at the task. A sampling profiler goes a level deeper. Many times a second it records the call stack of every running thread, then merges the stacks into a flame graph in which each frame's width is its share of the samples.

Attaching to an executor

async-profiler is the usual choice for executor JVMs. It samples without safepoint bias, the distortion that makes profilers built on the JVM's own stack API report the nearest safe point instead of the frame that was really on the CPU. You can load it as an agent when each executor JVM starts, or attach it to an executor that is already running.

spark = (
    SparkSession.builder
    .config(
        "spark.executor.extraJavaOptions",
        "-agentpath:/opt/async-profiler/lib/libasyncProfiler.so"
        "=start,event=cpu,file=/tmp/exec-%p.html",
    )
    .getOrCreate()
)

Reading by width

Each box is a stack frame. A frame's caller sits beneath it and the frames it calls sit on top, so the bottom row is where every thread starts and the top edge is where the CPU was when the sample fired. Sibling frames are merged and ordered by name, so horizontal position says nothing about when code ran. Width is the only measurement: a frame half as wide as the graph was on the stack in half the samples.

Hunt for wide frames along the top edge. A wide frame with little stacked above it spends its samples in its own code, and that self time is what an optimization removes. A wide frame covered by children is passing its time upward, so keep climbing until the width stops being covered.

A 60-second profile of a slow stage

An executor with 4 cores, sampled for 60 seconds at the default 10 ms interval, collects at most 24,000 CPU samples. This profile comes from a stage that pulls the domain out of each page view's referrer URL with regexp_extract:

FrameSits underSamplesWidth
Executor$TaskRunner.runthread root21,60090%
GeneratedIteratorForCodegenStage2.processNextTaskRunner.run15,84066%
java.util.regex.Matcher.findprocessNext9,36039%
VectorizedParquetRecordReader.nextBatchprocessNext1,6807%
LZ4BlockOutputStream.writeTaskRunner.run2,1609%
G1 GC worker threadstheir own roots1,9208%

The task runner covers 90% because nearly every sample came from a task thread, and processNext, the generated code for the whole stage, covers 66%. Climb further and most of that width belongs to Matcher.find, topped by nothing but regex internals. Matching the pattern against each referrer costs 39% of the executor's CPU, more than reading Parquet, compressing shuffle output and collecting garbage put together, which sum to 24%. That is the hot code. Rewriting the extraction with substring_index, which splits on a fixed delimiter without a pattern engine, or storing the domain once upstream, removes most of that width.

Do
  • Profile while the slow stage runs, using its start and end on the stage timeline
  • Profile the executor that runs the straggler task
  • Split threads with -t when wall mode mixes in idle pool threads
Avoid
  • Read left-to-right position as the order code ran in
  • Read a tall tower as slow code; height is only call depth
  • Trust a 2% frame from a 5-second profile

A 60-second cpu profile of a straggler shows Executor$TaskRunner.run at 92%. Above it, UnsafeExternalSorter.spill covers 41%, with LZ4BlockOutputStream.write at 30% on top of that, and processNext covers 35%. What does the graph say?

About this lesson

This section is part of the The Spark UI: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.