warpspan: GPU Time per Request
warpspan links GPU kernels to OpenTelemetry traces. It tags every CUDA kernel with the span that launched it, so Grafana can show a flame graph of the kernels any span in a trace ran on the GPU. The app's code doesn't change.
CUDA_INJECTION64_PATH. No code changes in the app.Traces only see the CPU
Last month I used Nsight Systems to speed up argus. Nsight is great when you sit next to one process and record a few seconds of it. It doesn't help when you run a GPU service for other people and one request out of thousands is slow. For that, people use tracing: every request becomes a trace, made of spans, and you can see which step took long.
A trace records when your code called model(x) and when that call returned. But CUDA is asynchronous: model(x) only queues kernels on the GPU and returns. The GPU runs them later, sometimes long after the span has ended.
What I wanted was every kernel the GPU runs, tagged with the span that launched it, and exported in a format Grafana already understands.
A trace is one request's path through a system. It's made of spans: named, timed steps like preprocess or forward, nested inside each other. While a span runs, it is the current span on its thread, and any span started there becomes its child. OpenTelemetry is the standard library and wire format for all of this.
Where the GPU time went
In the trace view of the figure above, my test request has three child spans, and postprocess takes the longest: 1.36 ms, about four times the others, even though it only runs a softmax and a top-5 and copies five numbers back to the CPU.
With "What the GPU did" selected, the reason shows up. forward lasted 0.32 ms on the CPU, but the matmuls it queued kept the GPU busy for 1.43 ms, and 1.26 ms of that ran after forward had already ended. postprocess spent most of its time in .cpu(), waiting for forward's kernels to finish before its own could run. Its own kernels took 0.10 ms.
GPU time per span, by kernel
Mean over requests 2 to 10 (the first one includes CUDA warm-up). This is the same data Grafana shows when you click a span.
Getting inside any CUDA process
I didn't want a library people have to call. It had to work like auto-instrumentation: set something at startup and you're done, in any language. NVIDIA already supports this. Every CUDA program goes through the driver library, libcuda.so, and when the environment variable CUDA_INJECTION64_PATH points at a shared library, the driver loads it during CUDA initialisation and calls its InitializeInjection(). Nsight uses the same hook.
So warpspan's capture library, libwarpspan.so, is just that: an injected library. It subscribes to CUPTI, starts one background thread, and returns. If anything fails, it logs one line and the app runs as if warpspan weren't there.
CUPTI is NVIDIA's profiling interface, the layer Nsight is built on. Two parts matter here. The callback API calls your function on the app's own thread every time it launches a kernel. The activity API later hands you records of finished kernels: name, GPU start and end time. They come from different threads at different times, and one ID connects them.
Joining launches to finished kernels
Since the kernel runs after the span may have ended, the span has to be captured at launch time. The activity record that arrives later doesn't know about spans at all. So warpspan joins the two on CUPTI's correlation ID.
The launch callback runs on the app's thread, on every single launch, so it has to be cheap. The table is a fixed array of 131,072 slots indexed by the correlation ID, and each slot is a tiny seqlock: writing it is a few atomic stores, with no lock and no allocation. If more than 131,072 launches happen before the activity records come back, old slots get reused and those kernels are counted without a span. I made the same choice everywhere in warpspan: when a buffer or queue is full, records are dropped and counted, and the app never waits.
Kernels inside a CUDA graph share the graph launch's correlation ID, so they get the span that was current at cudaGraphLaunch.
Finding the current span
The launch callback needs "this thread's current span". The library can't know that: the span lives inside the app's OpenTelemetry library, in Python objects or Java objects. So each language gets a small span bridge that hooks the place where the current span changes and calls one C function the library exports:
void warpspan_set_span(const uint8_t *trace_id, const uint8_t *span_id); /* NULL clears */
It's a write to a thread-local variable, a few nanoseconds. The bridge never sends anything anywhere. In Python it wraps OpenTelemetry's runtime context, so every attach and detach (which is what start_as_current_span does) publishes the new span. It loads through sitecustomize, or as an opentelemetry-instrument plugin, so the app still has no code changes.
Only sampled spans that started in this process get published. An unsampled span never reaches the trace backend, so linking kernels to it just adds IDs nobody can look up.
A thread_local variable has one copy per thread. The bridge writes the span on the thread where it became current, and the launch callback reads it on the thread that launches the kernel. In most apps those are the same thread. When they're not, the kernel isn't linked; more on that below.
Exporting to Pyroscope and Grafana
The tagged records leave the app in batches, about once a second, over a unix socket to a small Go agent on the same node. The send never blocks: if the agent is busy, the batch is dropped and counted. I didn't want gRPC, TLS and retries inside every CUDA process.
The agent sums GPU time and launch counts per process, span and kernel, and every 10 seconds exports them as OTLP profiles to an OpenTelemetry Collector, which forwards them to Pyroscope. Each sample's stack is the device and the kernel name, and the launching span goes into the sample's link. Pyroscope turns that link into span_id labels, and Grafana's "Profiles for this span" queries by it.
Profiles are the newest OpenTelemetry signal, next to traces, metrics and logs. A profile is a set of stacks with values (here, nanoseconds of GPU time), plus links from samples to spans. The format is still alpha: the Collector needs a feature gate, and Pyroscope ignored my data until I followed its exact rules, like naming the profile through its period type (that's why the profile type is called gpu).
The first time everything ran end to end, Pyroscope had the data and I could query it by span ID by hand. But Grafana's trace view had no "Profiles for this span" button anywhere. I ended up reading Grafana's source: the button only appears on spans that carry an attribute called pyroscope.profile.id, which Pyroscope's own span-profiling libraries set. So now the bridge sets it to the span ID, once per span, and the button shows up.
Shortening kernel names
Kernel names arrive mangled, the way the C++ compiler encodes them. Demangled, PyTorch's kernels are template instantiations, and some are huge. The first time I hovered a frame in the flame graph, the tooltip was a wall of text.
Cutting all the template arguments isn't an option: for PyTorch, the operation lives inside them. elementwise_kernel is add, multiply, divide and everything else; only DivFunctor or GeluCUDAKernelImpl deep in the arguments tells you which. So the agent walks the demangled name as a tree. It keeps the kernel's full name and the top level of arguments, drops namespaces and lambda noise inside them, and only if the result is still over 160 characters does it start replacing the deepest arguments with <…>. randn's 5,050 characters become 81.
When I had the code reviewed, the reviewer found a problem I hadn't thought of. Mangled names compress repeated parts with back-references, so the demangled form is a graph, and printing it as a tree can grow exponentially. Any pod that can reach the agent's socket can send a kernel name. A crafted 436-byte name made the agent run out of memory. The old demangling code had the same hole. Now printing stops after 640 characters, and that name comes out as f<A<…>, A<…>, …>, 61 characters, in under a millisecond.
How far a short mangled name expands
Generated names of 9 nested levels, each repeating the previous one w times through back-references. Demangled length, log scale. Hover a bar.
asyncio and thread pools in Python
The thread-local model has one weak spot in Python: asyncio. One thread runs many tasks, and switching between them swaps the current contextvars context directly. OpenTelemetry's attach and detach aren't involved, so the bridge never hears about it. Thread pools that run work in a copied context (FastAPI's sync endpoints, asyncio.to_thread) have the same problem.
Python 3.14 added context watchers: a C callback that runs on every context switch. The bridge now also keeps the span's IDs in a context variable, and a watcher written in C inside libwarpspan.so reads it on every switch and updates the thread-local. To test it I ran two requests as asyncio tasks that interleave, each launching one kernel after the other task had attached its own span.
await.My first version passed every test I wrote, and the review still found two serious bugs. The watcher also runs while an exception is on its way out of a context, and my callback cleared it: ctx.run(something_that_raises) turned the real error into a meaningless SystemError. And a sub-interpreter loading the bridge took over the library's single watcher slot, then crashed the process when it closed. Both have tests now, and both tests fail if I put the bugs back.
Bridges for other languages
The capture library doesn't care about languages, only the bridge does. Each OpenTelemetry API keeps the current span somewhere different, so each bridge looks different, and some needed a bit of digging.
Wraps the runtime context's attach and detach; a context watcher on 3.14. Loads without code changes.
A RuntimeContextStorage that wraps the default one. One Install() call at startup, header-only.
A ContextStorage wrapper, calling the library through Java 22's foreign function API, so there's no JNI library to build.
A tracing layer. Instrumented futures enter their span on every poll, so async Rust gets the right span per poll.
Nothing to hook: Go carries the span in a context.Context and goroutines hop between threads. So you wrap the cgo call: warpspan.Do(ctx, fn) locks the thread and sets the span around it.
My first Go test printed no kernels at all. The library sends its last batch from an atexit handler, and Go programs exit without running C atexit handlers. A short program lost everything; a long-running server would lose its last second. The fix was a new exported function, warpspan_flush(), that the Go bridge calls from warpspan.Flush() before exiting.
What's still not solved
One kernel can only carry one span. When a server batches many requests into one forward pass, like vLLM does, the kernels serve all of them. How to split that cost per request isn't designed yet.
Kernels launched on threads the span never reaches are recorded without a span. PyTorch's autograd backward pass, which runs on its own threads, is the common case.
No memory copies, and no CPU or Python stack at the launch. The stack in the flame graph is just the device and the kernel.
Everything here ran on one laptop GPU with CUDA 13.4, plus a CUDA 12.6 build of the library. Other CUDA versions and real servers are next.
The launch path is designed to be cheap, but I haven't benchmarked warpspan against an uninstrumented run yet. Until I do, "cheap" is a design goal, not a measurement.
CUPTI allows one subscriber per process, so Nsight or torch.profiler may fail to attach while warpspan is on. WARPSPAN_DISABLE=1 turns it off for those runs.
Running it yourself
On a Linux machine with an NVIDIA GPU and Docker. The full guide, with every bridge and the Kubernetes setup, is in the docs.
git clone https://github.com/zraisan/warpspan && cd warpspan && make docker compose -f deploy/docker-compose.yaml up -d # Collector, Pyroscope, Tempo, Grafana ./bin/warpspan --socket /tmp/warpspan/warpspan.sock --otlp-endpoint http://127.0.0.1:4317 & export CUDA_INJECTION64_PATH=$PWD/capture/libwarpspan.so export WARPSPAN_SOCKET=/tmp/warpspan/warpspan.sock OTEL_SERVICE_NAME=torch-spans-example export PYTHONPATH=$PWD/bridge/python/autoload:$PWD/bridge/python/src python examples/torch-spans/app.py --requests 50
Build the library against a CUDA toolkit no newer than your driver supports, or CUPTI won't start. At exit the app prints how many kernels it saw and how many were linked to spans.