GPU
Seeing without slowing down
Where you watch from decides what it costs
Everything known about how CUDA drives a GPU came from one ability: catching each doorbell write and reading the command it points at. That sounds easy. The doorbell is one store to a known address. In practice the obvious way of watching it slows copies down almost 3,000 times, and the next most obvious way is fast and sees nothing. The method that works watches from exactly where the event happens, before anyone acts on it, and touches nothing else. Teams that run inference want the same thing from their stack, and they face the same two ways of getting it wrong.
- Goal
- see every doorbell and the command behind it
- Watched the obvious way
- 55 GB/s becomes 0.02 GB/s
- Watched too late
- full speed, nothing to see
- Watched in the right place
- 174 of 174 seen, speed unchanged
Read the whole lesson as text
1. The thing to watch
Inside gpu-2 the model server asks the GPU for a copy the usual way: commands in a pushbuffer, an entry in the ring, the write index moved forward, and then one 32-bit store to the doorbell. To understand what CUDA really sends, an observer needs to be told about that store and then read the ring entry and the command while they are still fresh. The store is the whole event. Everything interesting hangs off it.
- With nobody watching, the process rings the doorbell and the GPU starts the copy at once.
2. The obvious way: protect the page
The standard tool for being told when memory is written is page protection. Mark the page that holds the doorbell read-only. When the library stores to it, the CPU faults and a handler runs. Take the snapshot there, before the store has happened, and the picture is complete and correct: the GPU hasn't been told anything yet. Then let the store through by making the page writable, single-stepping one instruction, and protecting the page again. Every copy now passes through a page fault, a signal handler, a single-step trap and a second handler. Copy speed falls from 55 GB/s to 0.02.
- The store hits a read-only page, the CPU faults, and a handler takes its snapshot before letting the store through.
3. The cheap way: look afterwards
So do almost nothing in the fault handler and take the snapshot in the trap handler, after the store. Most of the overhead goes away and the copy runs at full speed again. But the doorbell has been written. By the time a handler can start, the GPU's front end has already fetched the ring entry. Its read index has caught up with the write index and the ring looks empty. Reading the doorbell register back doesn't help either, because it reads as zero. The GPU reacts to a doorbell faster than a signal handler can begin. One approach is correct and too slow. The other is fast and too late. That isn't a tuning problem. Both watch from the wrong place.
- This time the store reaches the GPU before the observer looks.
- Before the handler runs, the GPU has read the ring entry and moved its read index.
4. Divert and arm
The approach that works comes from published research on NVIDIA's command streams. It moves the observer out of the application and into the kernel, and uses hardware every x86 CPU has for this exact job. First, divert. When a process maps the region that holds the doorbell, a small kernel module backs that mapping with ordinary memory instead of the real register. The process doesn't know. Its doorbell store will land in memory the GPU never sees. Second, arm. The module sets a debug register, DR0, to watch those 4 bytes for writes. That check happens in the CPU's own circuitry on every store and costs nothing until one matches.
- A kernel module hands the process an ordinary page of memory where it expects the doorbell register.
5. Trap, inspect, forward
Now the library rings the doorbell. The store lands in the shadow page, and the CPU raises a debug exception right after it. Because the store went to RAM and not to the register, the GPU has been told nothing. The ring is exactly as the library left it. The handler reads the write index, finds the newest ring entry, decodes the pushbuffer's address and length from it, and logs it all. Then it writes the token to the real doorbell. The GPU wakes up and runs the copy as if nothing had happened. Out of 174 doorbells, 174 were decoded. Each trap costs a few microseconds and copy speed doesn't change, because the copy itself is untouched.
- The doorbell store lands in the shadow page and the CPU hands control to the module, with the GPU still unaware.
- The module reads the newest ring entry and the command it points at, and writes them to a trace.
- Only then does the module write the token to the real register, a few microseconds late.
6. The sharp edges
It is a research tool and its limits teach something too. A CPU has only four hardware breakpoint slots, DR0 to DR3, and the kernel hands them out. If something else holds them, arming quietly fails: an empty trace and no error. Hardware breakpoints are per thread, so threads created after arming slip past. And the tool found a bug in its own teardown, a freed mapping still cached after the channel was gone. Even the debugger needed debugging. None of that changes what made it work.
- If another program already holds the debug registers, the tool arms nothing and reports nothing.
7. Bolted on from the side
Inference teams want the same kind of sight. Which tenant sent this request? How many tokens in and out? What was the time to the first token? Which GPU, did the prefix hit, and why that server? The common answers bolt observers on from the side, and each maps onto one of the two failed approaches. A sidecar in every model server Pod, or one more tracing proxy in the chain, is the first: it sees the request, and every request pays for an extra hop, an extra copy and an extra queue. Scraping logs and metrics afterwards, or mirroring traffic off the fabric, is the second: cheap, and it sees the request after the routing decision was made. It can say which GPU served a request. It can't say why, or what the alternatives were.
- A log line written at the worker records that the request arrived. The decision that sent it there happened somewhere else.
8. In the path, at the decision
BNK is on the path of every inference request, because it is the path. It ends the client's connection, so it knows the tenant and the credentials. It applies the firewall and the token budget. It runs the endpoint picker, so it knows every candidate and the score each one got. It forwards to the chosen Pod and sees the tokens come back. Recording there adds no hop, because the request is already there, and it captures the decision with its reasons attached. That is the debug register trap again: at the exact point the event happens, before anything acts on it, touching nothing else. This is no argument against the model server's own metrics. The picker reads them. It is about where the view of a request gets put together.
- The data plane asks the picker, and now holds every candidate and why one of them won.
- When the answer has streamed back, the data plane writes one record that holds the whole story of the request.
Summary
The same observation cost almost 3,000 times the bandwidth from one place and nothing from another. Protecting the doorbell page saw everything and wrecked the thing being watched. Looking afterwards was free and found an empty ring. What worked was to stand exactly where the event happens: divert the store, trap it in hardware, read the state before the GPU has been told, then pass it on. An inference stack offers the same choice. Sidecars and extra proxies see requests and slow them all. Logs and taps are cheap and arrive after the routing decision. The router is the only place that saw all the options, and the gateway is the only place that knows both the tenant and the tokens.
Next lesson: When the GPU stops asking the CPU