Debugging the kernel time of a cached loader¶
This case shows how the signals described in Monitoring CPU, page faults, memory and pressure of a data loader were used to debug a production data loader. The loader kept up with training, but cost much more kernel CPU and memory than the loader it replaced. Reading the process-level signals together located the problem. Per-stage numbers then put the extra faults in two stages and led to an extra copy in one of them, per-worker numbers exposed an effect of the allocator’s purge timing, and CPU and memory compared between configurations priced the fix.
The pipeline¶
The pipeline fed a small model (roughly 0.5-1.5 M parameters), so a training step was cheap and the job was input-bound: with warm caches, a host with 8 GPUs consumed about 21-23 thousand items per second. Its performance-relevant properties:
Many loader worker processes: 64 per host (8 per GPU rank).
A node-local disk cache: each item’s media was read from a local cache of zstd-compressed files, about 0.74 MB per item, with
O_DIRECT. One cache lookup covered 256 items, about 190 MB.A large per-item temporary: the transform allocated one decompression buffer per item, sized to the decompressed content.
Allocator: jemalloc.
The pipeline looked like this in its final layout (function names are illustrative):
from concurrent.futures import ThreadPoolExecutor
from spdl.pipeline import PipelineBuilder
from spdl.pipeline.defs import MAIN_PROCESS, ProcessPoolExecutorConfig
pipeline = (
PipelineBuilder()
.add_source(sampler) # item keys for this GPU rank
.to(ProcessPoolExecutorConfig(max_workers=8)) # 8 loader workers per GPU rank
.aggregate(256) # one cache lookup per 256 items (~190 MB)
.pipe(split) # cut each lookup into ceil(rows / 8)-row slices: 8 slices of 32
.pipe(
read_cache, # read one slice
concurrency=8,
executor=ThreadPoolExecutor(8, thread_name_prefix="cache_read"),
)
.pipe(join) # reassemble the slices of one lookup
.pipe(write_cache) # cache misses: remote fetch and cache write
.disaggregate()
.pipe(
transform, # one decompression buffer per item
executor=ThreadPoolExecutor(1, thread_name_prefix="transform"),
)
.aggregate(batch_size)
.pipe(collate, executor=ThreadPoolExecutor(1, thread_name_prefix="collate"))
.to(MAIN_PROCESS)
.add_sink(buffer_size)
.build(num_threads=num_threads)
)
Each lookup is read as 8 slices in parallel on the cache-read pool, while splitting, joining
and writing the cache run one call at a time on the worker’s shared pool. Each worker of the
region builds its own thread pools from the stages’ executor=, so the cache reads, the
transform and the collate run on threads of their own, and show up under those names in
per-thread numbers. In the first build, all of a worker’s stages shared its one
thread pool.
The comparison runs trained on one host with 8 GPUs, starting each job with an empty disk cache and running one job at a time, against the loader the pipeline replaced (the “original loader” below). The code base moved during the investigation, so each comparison below is made against the original loader on the same base: the first build against the original on the older base, and the final configuration against the original on the newer base.
Step 1: the loader keeps up, so measure what it costs¶
Training loss, the trainer’s wait for data and the training time matched the original loader: the queue between the loader and the trainer stayed full. So the loader was not slowing training, and the question became what it cost.
The process-level signals answered that. With warm caches, the first build of the new pipeline used about 20.4 CPUs per host in kernel time against 13.3 for the original loader on the same base, while user CPU stayed flat. The loader’s anonymous memory grew from about 454 GiB to 751 GiB per host; in each worker it peaked within the first minute and was then retained. Kernel time rising with user time flat, next to 211 minor faults per row against the original’s 86.3, pointed at memory: pages allocated, faulted in and returned to the kernel, not more computation.
What told us something: kernel and user CPU, faults and anonymous memory read together, per process, against the original loader. None of them alone separates “more work” from “more memory churn”.
Finding 1: two stages doubled their faults (per stage)¶
Summed over all workers and divided by the rows each stage processed, the per-stage minor faults showed where the extra faults were. Against the original loader on the same base, the cache read had 62.2 faults per row against 9.7, and the transform 114 against 56.8, about twice as many; the collate had 29.0 against 14.5. The read and the transform each added about half of the extra faults. The transform’s kernel time per row rose with its faults, from 172 to 328 microseconds.
Minor faults per row of each stage, for the original loader and the first build on the same base (one run each).¶
The aggregated per-stage output of the two runs, per row (calls are summed over all workers):
calls user us sys us minflt nvcsw wall us
original loader
read 941626 224 109 9.7 6.6 2749
transform 117799 1744 172 56.8 5.9 2353
collate 19054 774 120 14.5 6.1 1916
first build
read 118009 211 259 62.2 6.1 2824
transform 118176 1811 328 114.0 6.5 2672
collate 19079 752 154 29.0 6.1 1438
In the transform, the extra faults traced to an extra per-row copy of a container. Removing the copy brought the transform’s faults back to the original loader’s level. The read’s extra faults were gone in the final configuration as well (9.7 per row against 9.5 for the original), which runs the cache reads on a pool of their own and keeps freed memory longer (Finding 2). In the final configuration the transform’s kernel time was 75-80 microseconds per row, against about 170 for the original loader.
What told us something: per-stage faults per row against the original loader. The excess was in two stages, which narrowed the search to the code of those stages.
Where the numbers came from: each stage separately. The transform then shared the workers’ thread pool with other stages, so its numbers came from measuring each op call (Advanced: measuring each op call). Giving the transform and the collate a thread of their own made them visible by thread name instead, with no instrumentation in the stage.
Finding 2: workers split into two groups (per worker)¶
The per-worker numbers were not uniform. The workers fell into two groups with clearly different faults per row, and the group a worker belonged to tracked its epoch time: the workers with more faults were the slower ones.
Workers running the same code on the same kind of data should not differ like that, so the
cause was per-process state. It was the allocator purging freed memory: jemalloc returns freed
dirty pages to the kernel after a decay time (dirty_decay_ms; the loader ran with
jemalloc’s default, 10 s), and memory that is purged and then needed again is faulted in
again.
Raising the decay time to 60 seconds (for example MALLOC_CONF=dirty_decay_ms:60000 in the
environment of the loader processes) removed the split. In the final configuration (three
runs, on the newer base), the transform had 6.9 faults per row, with per-worker medians of
7.2-7.8, and the whole loader 21.8, against 50.4 and 80.4 for the original loader on the same
base. Kernel CPU fell to 11.7 CPUs per host (11.5-11.9), below the original loader’s 14.3,
with about the same warm epoch time (68.1 s against 67.3 s).
What told us something: a bimodal per-worker distribution that tracked per-worker time. Summed totals alone would have shown only an average between the two groups.
Where the numbers came from: each worker process separately. Faults and kernel time per worker, read against each worker’s timing, showed the split without any per-stage instrumentation.
The numbers above, as two comparisons on two code bases. On the older base: the original loader against the first build of the new pipeline, one run each. On the newer base: the original loader against the final configuration, three runs each, whose range the lines show. Left: kernel CPU per host with warm caches. Right: minor faults per row for the whole loader and for the transform.¶
Finding 3: keeping freed memory costs memory (per configuration)¶
Not purging has a price, and the counters make it measurable. In another loader with a similar structure, jemalloc’s default decay time (10 s) made the loader about 11% slower, with 2.5 times the kernel CPU, compared with keeping freed memory. Keeping it held about 30% more resident memory.
What told us something: kernel CPU and resident memory measured together, per configuration. The right decay time is a trade-off between the two that only the measurement can settle for a given loader and machine; compare the resident memory with the cgroup’s memory limit before choosing.
Where the numbers came from: CPU and memory of the whole process family, compared between runs with the two decay times.
Other things that were tried¶
Running the cache reads, the transform and the collation on the worker’s shared pool instead of their own threads, with all other fixes applied, gave the same epoch time but more faults, kernel CPU and memory. This loader is loader B in Dedicated thread pools for stages, which has the numbers and how the effect varied between loaders.
What to look for¶
A loader that keeps up but costs more: compare kernel and user CPU, faults and anonymous memory per process with the reference before anything else.
Kernel time rising while user time is flat: measure faults per stage. Comparing the extra faults × page size with the bytes a stage creates per item helps tell a copy from other causes.
Workers that differ in faults per item while running the same code: look at per-process state, such as the allocator’s decay and purge behaviour.
A change that lowers faults and kernel time: report resident memory next to it, because keeping memory is often what lowered them.