Skip to content
systemcoding.dev
GPU

Profile a CUDA Application Timeline With Nsight Systems

Capture a CUDA timeline with nsys profile, read kernel, copy and API rows, find gaps and serialized streams, and print summary tables from the command line.

6 min read
Summarize in AI
Flat illustration of a GPU die of compute units with one thread block lit, with the NVIDIA, Khronos Group and Vulkan logos.

Before you tune a kernel, you need to know whether the kernel is the problem. A program can spend most of its time copying data, waiting on the host or launching thousands of tiny kernels with gaps between them, and no amount of work on the arithmetic helps. Nsight Systems answers that question. It records a timeline of the whole application, with CPU threads, CUDA API calls, kernels and copies on one axis. This post uses the Nsight Systems that ships with CUDA 12.9 to capture a run from the command line, read the rows of the timeline, spot the three common problems and print summary tables without opening the GUI. The next step after it, drilling into one kernel, is the Nsight Compute post.

A program worth profiling

The pipeline from the streams post has the right shape: copies, kernels, several streams. Compile it with line information so the profiler can map kernels to source, and nothing else is needed:

Shell
nvcc -O2 -lineinfo pipeline.cu -o pipeline

-lineinfo records source line tables without disabling optimizations, so the profile reflects the code you ship. Never profile a -G debug build, which turns off most device optimizations and shows times that do not exist in release.

Capture with nsys profile

The command line tool is nsys. The profile command runs your program and writes a report file:

Shell
nsys profile --trace=cuda,nvtx,osrt --stats=true -o pipeline_report ./pipeline

--trace chooses what to record. cuda captures runtime and driver API calls, kernels and memory operations. nvtx captures the ranges you annotate in code, which the NVTX post covers, and osrt captures operating system runtime calls such as thread synchronization, which explains host stalls. -o names the output, producing pipeline_report.nsys-rep. --stats=true prints summary tables when the run ends. If the program takes arguments, put them after the executable.

Profiling adds overhead, mostly in the form of tracing every API call, so run a representative input that is long enough to show a pattern and short enough that the report stays small. A few seconds is plenty. For a long-running service, nsys can start and stop on demand, using --delay and --duration, or the cudaProfilerStart and cudaProfilerStop calls with --capture-range=cudaProfilerApi.

Open the report

Open the report with nsys-ui pipeline_report.nsys-rep on a machine with a display, which can be a different machine from the one that ran the program. The report is self-contained. The timeline view is the main screen. Its rows, from top to bottom, are:

RowWhat it shows
CPU threadsWhat each host thread was doing, with API calls and OS runtime calls
CUDA APIEach runtime call, such as cudaMemcpyAsync or a kernel launch, as a bar
CUDA HW, per deviceThe GPU's own activity: kernels, memory copies and memsets
Streams under the deviceOne row per stream, with kernels and copies placed in it
NVTXYour annotated ranges, if any

Two rows describe the same event from two sides. The launch bar in the CUDA API row shows when the host issued the call, and the kernel bar in the GPU row shows when it actually ran. The gap between them is how far the GPU is behind the host. A queue of work that the host built up ahead of the GPU is healthy, because the GPU never starves. A launch bar that sits right before its kernel with the GPU idle just before it means the GPU was waiting for the host.

Read the timeline for three problems

Gaps between kernels

White space in the GPU row means the device had nothing to run. Zoom in on a gap and look at the CPU thread above it. If the host was inside cudaDeviceSynchronize, cudaMemcpy or a pthread wait, it blocked the pipeline on purpose. If it was running its own code between launches, the host is the bottleneck. If the kernels are tiny and the gaps are the launch overhead, many small launches are the problem, and the guide's advice on fusion, batching and graphs applies.

Copies that do not overlap kernels

A healthy pipeline shows copy bars and kernel bars at the same time in different stream rows. If every bar sits end to end, the program serializes. The usual causes, from the streams post, are pageable host memory, use of the default stream or an implicit synchronization such as cudaMalloc inside the loop. Click a copy bar and read its properties: the memory kind field says Pageable or Pinned.

One huge kernel and short everything else

If one kernel takes 90 percent of the GPU time, that is your target for Nsight Compute. If the GPU row is mostly empty and the transfers are long, the program is transfer bound and kernel tuning is wasted effort.

You do not need the GUI to get the numbers. nsys stats reads a report and prints tables. The tables below are representative of the format, with illustrative numbers, since your values depend on the machine:

Shell
nsys stats --report cuda_gpu_kern_sum,cuda_gpu_mem_time_sum pipeline_report.nsys-rep
Text
 Time (%)  Total Time (ns)  Instances  Avg (ns)  Name
 --------  ---------------  ---------  --------  ------------------------
     71.4          920,512          4   230,128  square(float const*, float*, int)

 Time (%)  Total Time (ns)  Count  Operation
 --------  ---------------  -----  ------------------
     52.8        1,420,288      4  [CUDA memcpy HtoD]
     47.2        1,268,096      4  [CUDA memcpy DtoH]

Read the kernel summary first. Its first column tells you what fraction of GPU kernel time each kernel consumes, so you optimize the top row. Then read the memory summary: here copies total more than the kernel does, which says this program is transfer bound, and the right fix is overlap and fewer bytes, not a faster square. Other report names include cuda_api_sum, which tells you which runtime calls the host spent its time in, and nvtx_sum, which tables your own ranges. nsys stats --help-reports lists them all.

Add context with NVTX ranges

A timeline of anonymous kernels is hard to relate to your code. Annotating phases with NVTX ranges, as the NVTX post shows, draws labeled bars in their own row, so you can see that "preprocess" took 3 ms of host time and "inference" owns the kernels. Add them early; they cost almost nothing when no profiler is attached.

Compare before and after

Capture a report before each change and name it clearly, before.nsys-rep and after.nsys-rep. The GUI can open both in one session so you can compare rows. For automated checks, run nsys stats on both and diff the numbers. A change that moves the total GPU kernel time but not the wall-clock time of the program means your timeline had a different bottleneck than you assumed, which is a finding worth having.

What the profiler does to the program

Tracing perturbs timing. API calls take a little longer, so very short kernels and tight launch loops look slower than they really are, and a run under the profiler is not the number to quote as performance. Use it to find structure, such as which calls block, which streams overlap and where the gaps are, and use plain event timing for the final number.

Permission issues also appear. CPU sampling and some OS tracing require a kernel setting; if nsys warns that sampling is disabled, or that the kernel's perf_event_paranoid value is too restrictive, read the message, since the fix depends on your system, and note that tracing CUDA does not need it. The CUDA timeline works without those extras.

Share

Keep reading

Flat illustration of two options weighed side by side, with the C, C++ and Rust logos.
Performance

Spinlocks vs Mutexes: When Spinning Wins

A spinlock burns a core while it waits and a mutex sleeps. Compare their costs, write a correct C11 spinlock, and see when user-space spinning beats a mutex.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.