How to Diagnose NVMe SSD Latency Spikes on Linux
Use fio, Linux block traces, and OCP latency monitoring to find where a slow NVMe read spent its time.
By QA Maestro · · 6 min read
Start with the missed deadline
The symptom is a 4 KiB read that occasionally takes 20 ms while a background writer runs. Median latency and aggregate IOPS are fine. Before changing firmware, pick a deadline from the application and record the count of reads that miss it. In the example below, 2 ms is an illustrative deadline. Use the real service budget for qualification.
Run on a dedicated lab device. Put the read probe on an initialized, read-only range and use a separate scratch range for the writer. Record the controller serial, namespace, filesystem or raw-device path, kernel, firmware, CPU affinity, and starting media state. The ranges should not overlap. They still share device resources, which is what the test exercises.
Get a baseline you can rerun
Run the probe alone, then run the same probe while stepping the background write load. Recreate the initial media state before comparing runs. Keep block size, read seed, address range, and runtime fixed. Start with a small run and increase duration until you capture multiple excursions or reach the agreed exposure budget.
This fio job is a read-only, closed-loop probe for an existing initialized test file of at least 4 GiB on the lab SSD. Run it with fio’s --readonly flag and save JSON+ output to a different device. The one-second histogram is useful for finding a time window. It does not give every request an individual timestamp.
fio --readonly --output-format=json+ --output=probe.json probe.fio
[probe]
filename=/mnt/ssd-lab/initialized-probe.bin
allow_file_create=0
ioengine=io_uring
direct=1
rw=randread
bs=4k
size=4g
iodepth=1
time_based=1
runtime=120
randseed=317
write_hist_log=probe
log_hist_msec=1000
percentile_list=50:99:99.9:99.99Measure requests the probe never issued
QD1 is useful for isolating a single outstanding request, but the next read waits for the previous completion. During a 20 ms stall, a service expecting one request every millisecond may accumulate roughly 20 arrivals while the probe produces only one slow completion. This is why a reassuring percentile from a closed-loop run can coexist with a missed application deadline.
For the application check, use a paced trace replay or a harness that timestamps intended arrivals independently of completions. Log scheduled time, actual submit time, completion, and whether the request expired or was dropped. Count arrivals that missed the deadline even if they never entered the kernel. Report offered rate and achieved rate separately. fio documents an offload mode for submission pacing and warns against relying on it with asynchronous engines. Verify the generator’s behavior before using its results as an application SLO.
Locate the time in the Linux path
Capture block request insertion, issue, requeue, and completion for the target namespace, plus the workload’s timestamps. Use a common clock or record the offset and uncertainty. Where insertion is observable, insert-to-issue estimates time in the block queue. Issue-to-final-completion also includes transport and host completion handling. Handle merged requests, requeues, and partial completions explicitly. A sector number alone is an unsafe key when reads overlap.
Run one reproduction without tracing. If tracing changes the failure rate, narrow the device filter or capture only around the event. Save trace loss counters. If matching is ambiguous, compare distributions and windows rather than claiming a per-command breakdown.
Read the OCP log as corroboration
Where the drive implements OCP latency monitoring, read the configuration and capture the raw C3h log before and after the event window. Check that the feature is enabled, the command type is monitored, the thresholds cover the event, and timestamp units are understood. Check for bucket rollover before treating a counter difference as an event count.
The active measured latency, where supported, covers controller command fetch through completion queue update. The log’s mode and minimum window affect which timestamp and measured latency it preserves. The log cannot reconstruct every host request, and a flat bucket is weak evidence if the event fell below its threshold or outside coverage.
Work one trace before changing anything
These are illustrative timestamps in milliseconds, measured from the scheduled read. In A, the request spends 17 ms between insertion and issue. Investigate queue contention, I/O controls, and the writer’s submission pattern first. In B, most of the delay occurs after issue. Look for driver resets or PCIe errors and compare a supported device latency bucket over the same interval.
To discriminate between host and device causes in B, change one variable. For example, rerun with the same achieved write load while changing only completion CPU placement, then compare the trace. If the delay follows the drive across controlled host paths and device telemetry independently records an excursion, escalate with both captures. That still does not identify an internal firmware mechanism.
Scheduled Insert Issue Final completion
A: queue delay 0.00 0.10 17.10 17.45
B: after issue 0.00 0.10 0.18 19.90If the trace includes a reset and you suspect a supply event, use the SSD voltage-monitor validation guide to check measurement placement and timing before attributing the reset to power.
What to attach to a firmware bug
Include the smallest reproducing workload, the control run, the achieved write rate, and the starting media state. Attach the raw host trace, fio histograms, controller logs, and clock alignment notes. Report deadline misses as a numerator and denominator, plus longest completion gap. A firmware report should say exactly what was ruled out and which next experiment would change the conclusion.
