A pricing service's async-profiler CPU flame graph shows a wide regex plateau, while a wall-clock profile of the same minute shows request threads in a socket read: which explains slow requests, and why?
answer
- different denominators
- blocked threads burn no CPU
- request latency is elapsed time
- filter wall graph to request threads
- confirm with the downstream timing
basics
~20 sThe wall-clock profile explains the slow requests: latency is elapsed time, and threads blocked in a socket read use no CPU, so a CPU profile cannot see them. The regex plateau is the largest share of CPU, not of request time.
solid answer
~50 sThey disagree because they count different things. The CPU profile divides only the CPU the process consumed, so its regex plateau says the regex is the largest CPU cost, which matters for capacity. A thread blocked in a socket read consumes no CPU and leaves no samples there. Request latency is elapsed time on request threads, which is what `-e wall` samples, so if the wall graph, read per thread with `-t` and filtered to request-handling threads, is dominated by the read on the rate-service call, that call is where requests wait. Before acting I check that idle pool threads are excluded, confirm the downstream call's latency from the client side, and check CPU headroom: if the service is near its CPU limit, the regex can add queueing delay too. Recording both modes into one JFR file keeps the two views on the same window.
code
bash · 3 linesasprof -e cpu --wall 100ms -d 60 -f combined.jfr <pid>
jfrconv --cpu combined.jfr cpu.html
jfrconv --wall -t combined.jfr wall.htmlgo deeper
Recall that a CPU profile only sees running threads, and that time spent waiting on the network needs a wall-clock profile.
Explain what one sample means in each mode and why the same minute produces two different but correct graphs.
Show the method: read the wall graph per thread, exclude idle threads, confirm the downstream timing and CPU headroom, then fix one cause and re-profile both modes.
Make sure both CPU and wall evidence exist for an incident window by default, so teams do not optimise the loudest CPU frame while requests wait elsewhere.
## Two correct profiles that answer different questions The scenario is a JSON-heavy pricing service. Its async-profiler **CPU** flame graph has one wide plateau inside a regular-expression call. A **wall-clock** profile of the same minute shows most request threads sitting in a socket read on a synchronous call to a downstream rate service. Neither graph is wrong; they measure different resources. - **`-e cpu`** (and `itimer`/`ctimer`) takes a sample each time a thread has consumed one interval of CPU time. One sample means "one core was busy for one interval here". The graph is the breakdown of **CPU spent**. - **`-e wall`** samples all threads at a fixed period **regardless of state**: running, sleeping or blocked. The graph is the breakdown of **elapsed thread time**. A request thread blocked in a socket read is off the CPU, so it produces **no CPU samples at all**, however long it waits. It produces wall-clock samples for every period it waits. ## Which one explains slow requests **Request latency is elapsed time on the threads that serve requests.** That is the quantity a wall-clock profile measures, so the wall graph is the evidence for the latency question, and its answer here is the downstream call. The regex plateau answers a different question: of the CPU this process burned, where did it go? It is the right target if the problem is CPU cost, saturation or capacity, and it can still contribute to latency when the service runs near its CPU limit, because requests then wait for a core. It does not show that requests are slow *because* of the regex. ## Reading the wall-clock graph honestly A wall graph is easy to misread, because it samples idle threads too: 1. **Profile per thread.** Run with `-t` so each stack ends in a frame for its thread; the documentation calls wall-clock mode "most useful in per-thread mode". 2. **Exclude idle threads.** Worker threads parked waiting for the next task, selector threads and scheduler threads are sampled as often as busy ones. Filter them out (`-X` on a park frame, `-I` on request-handling frames, `--filter` with thread IDs, or `jfrconv --state` on a JFR recording) before judging proportions. 3. **Look at the top of the request-thread stacks.** A plateau ending in the JDK's socket read, entered from the rate-service client code, is time spent waiting for bytes from that service. 4. **Distinguish the waits.** Threads waiting to *borrow* a connection from a pool sit in that pool's wait, not in the socket read; that is a pool-exhaustion story the thread-dump patterns cover. Here the threads are already connected and waiting for a response. ## What to collect next, and what to rule out | Hypothesis | Evidence that discriminates | Instrument | |---|---|---| | downstream service is slow | client-side timing of the call, the downstream's own latency | request metrics, tracing | | the regex adds queueing under CPU saturation | process CPU against its limit, run-queue or throttling | CPU metrics, the CPU profile | | threads queue for connections | wall stacks in the pool's borrow, not the read | wall profile, thread dumps | | the two profiles covered different traffic | one recording holding both modes | `asprof -e cpu --wall 100ms -f combined.jfr` | Recording both views in one session with `-e cpu --wall 100ms` into JFR, then converting with `jfrconv --cpu` and `jfrconv --wall`, guarantees the same window. Converting without a filter would sum CPU and wall samples into one graph that, as the converter documentation warns, would not make much sense. ## Common misreadings - **"The widest frame is the problem."** Only within the question its mode answers. The widest CPU frame is the largest CPU cost; it is the latency cause only if requests are waiting for CPU. - **"The CPU graph would show the wait as kernel time."** A thread asleep in a read is off the CPU; kernel frames in a CPU profile are time spent *running* in the kernel, such as copying data or handling faults. - **"The wall graph proves the rate service is slow."** It proves request threads wait for its responses. The downstream's own latency, the network path and a too-small number of connections all look the same from this side, which is why the client-side timing comes next. - **"Reading the whole-JVM wall graph is enough."** Idle threads distort every proportion until they are filtered out. ## Then change one thing Once the downstream timing confirms it, the fix belongs to the call: a timeout that matches the latency budget, caching rates that change slowly, or issuing independent calls concurrently. The regex is a separate, real optimisation (for example compiling a `java.util.regex.Pattern` once instead of per call), worth doing for CPU cost. Fix one, re-profile both modes under the same load, and compare.
- The CPU graph's regex plateau is 60% of CPU samples, and the process uses 15% of its CPU limit. Should the regex be the latency fix?Not for latency. At 15% of its limit the process is unlikely to be waiting for cores (a throttling check confirms it), so the regex adds its own execution time but little queueing, and that time is small next to a blocking downstream read. It is still a valid CPU-cost optimisation; it simply does not explain the slow requests.
- Why is a wall-clock flame graph of a whole JVM often dominated by park and poll frames?Wall-clock mode samples every thread at a fixed period whatever its state, and most threads in a server are idle: pool workers parked waiting for tasks, selector threads polling, schedulers sleeping. Their samples outnumber those of busy request threads, so you read per thread with `-t` and filter to the threads serving requests.
- What would it mean if the wall graph showed request threads waiting in a connection pool's borrow instead of the socket read?That the threads are not waiting for the rate service to answer but for a free connection, which points at pool exhaustion or connections held too long. The next evidence is a series of thread dumps and the pool's active and pending counts, not the downstream service's latency.
A CPU profile is a timesheet of billable hours: it shows which task consumed the staff's working time. A wall-clock profile is a series of photos of where everyone is standing. If the photos show most staff waiting at the supplier's counter, the timesheet's biggest task is not why orders are late.
- The widest CPU plateau is always what makes requests slow.
- A blocked thread still shows its waiting time in a CPU profile.
- A wall-clock graph can be read as-is, without filtering idle threads.
- Merging CPU and wall samples into one graph gives the full picture.
- CPU and wall profiles from different minutes compare like one window.