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.
- To a running executor: on the worker, jps -l lists the executor as the process whose main class ends in CoarseGrainedExecutorBackend. Then asprof -d 60 -e cpu -f /tmp/exec7.html 48211 samples process 48211 for 60 seconds while the slow stage runs.
- From launch: load the profiler as an agent through spark.executor.extraJavaOptions, and every executor writes its own profile when it exits.
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:
| Frame | Sits under | Samples | Width |
|---|---|---|---|
| Executor$TaskRunner.run | thread root | 21,600 | 90% |
| GeneratedIteratorForCodegenStage2.processNext | TaskRunner.run | 15,840 | 66% |
| java.util.regex.Matcher.find | processNext | 9,360 | 39% |
| VectorizedParquetRecordReader.nextBatch | processNext | 1,680 | 7% |
| LZ4BlockOutputStream.write | TaskRunner.run | 2,160 | 9% |
| G1 GC worker threads | their own roots | 1,920 | 8% |
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?
- The task framework is the bottleneck: TaskRunner.run is the widest frame at 92%.
- The generated code is the bottleneck: processNext holds 35% and needs a rewrite.
- Compression is the bottleneck: turning off spill compression would win back 30%.
- Spilling is the bottleneck: it takes 41% of CPU, most of it compressing, so tasks need more memory.
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.