advanced32 min studyLesson 599 in Engineering

A profiler changes execution, so measure the observer's cost

Choose collection detail and capture windows while separating uninstrumented performance from replay, tracing, sampling, and memory-observation effects.

On this page

What you’ll understand

  • Quantify measured overhead for comparable completed work.
  • Explain why tracing, shape retention, replay, and sampling can perturb different mechanisms.
  • Design a limited capture that tests a hypothesis without claiming complete visibility.

A detailed profile can reveal why a step is slow while making that very step slower or changing its overlap. Timing events, stack collection, shape recording, trace buffering, metric replay, and synchronization have different costs. Some affect the host, some the device, and some the lifetimes of tensors. The right capture is the smallest one that can answer the current question, followed by a check that the proposed optimization improves ordinary execution. Instrumented duration is not automatically the application's unobserved duration.

Compare the same completed work with and without collection

Measure a warmed baseline and a comparable instrumented window with the same model work, data shapes, update count, and completion boundary. Report the ratio rather than assuming a tool's advertised overhead applies to every workload. A fixed per-event collection cost can be substantial for many short kernels and less visible for long operations. Collection can also change scheduling, induce waits, or add memory pressure, so subtracting one average overhead number from every event does not generally reconstruct an uninstrumented timeline.

Distinguish startup from recurring cost. Profiler initialization, metric configuration, trace export, and warmup can occur outside a chosen active window while still affecting a full-run benchmark. Record the scope of each overhead estimate. If the instrumented run changes batch size or drops work to fit its buffers, it is not a matched comparison. Keep an ordinary completed-work measurement as the authority for final throughput, and use the profile to explain mechanisms with its intervention clearly documented.

h=Tprofile−TbaseTbase,Tbase>0h=\frac{T_{\rm profile}-T_{\rm base}}{T_{\rm base}},\qquad T_{\rm base}>0
Both times complete the same defined work under comparable conditions; h is a dimensionless measured relative change. It does not establish causality from one run or imply a constant additive cost for each event. Startup and export must be included or excluded consistently with the stated measurement window.↔ Scroll wide equations horizontally.

Collection modes perturb different things

Framework shape and stack collection can add host work, and retaining references for shape recording can change tensor lifetime or optimizations. Kernel metric tools may replay launches or serialize execution to collect counters unavailable together. The profiled wall interval can then include save-and-restore work and repeated passes that ordinary execution never performs. Different replay modes have different concurrency semantics. Consult the deployed tool's versioned contract before comparing its wall clock with a timeline collector or assuming that a measured kernel duration includes the same boundary.

Sampling reduces how much is observed but can miss short or rare events, and periodic samples can alias a periodic workload. A simple random step-sampling model gives a useful expectation only if selection is independent of the event and inclusion probability is known. Capturing only visibly slow steps intentionally selects a conditional population; it cannot estimate the ordinary step distribution without an appropriate design. Buffer loss, unsupported activities, and capture windows create further missingness. Absence from a profile is therefore different from proof that an event never occurred.

E[K]=Np,Pr⁡(K=0)=(1−p)N\mathbb E[K]=Np,\qquad \Pr(K=0)=(1-p)^N
There are N fixed event-bearing opportunities, and each is independently included with common probability p. K counts included events. These dimensionless formulas describe the chosen Bernoulli inclusion model, not the generally more complicated overhead or missingness of a real profiler. Event-triggered or correlated selection needs another model.↔ Scroll wide equations horizontally.

Narrow collection after a broad low-detail pass

Use a short system-level timeline to identify whether readiness, host launch, device execution, communication, or memory deserves deeper study. Then select the ranks, operations, counters, and capture range needed for that hypothesis. Compare the low-detail and detailed execution paths for changed overlap or lifetimes. Export outside a critical measurement window when appropriate, but report full-run export cost if operational monitoring overhead is the question. Preserve collection settings and version details so another engineer can understand both what was visible and what the observer changed.

The queue lab illustrates one possible buffering pressure, not a profiler implementation. Reset, set both arrival λ=4 and service μ=2, predict initial backlog four and backlog five after one Step, then inspect. Reset and set both λ=1 and μ=3 for backlog three after one Step and zero after two seconds. The fluid formula is max(0,4+(λ−μ)t), with no finite buffer, drop policy, trace export, or replay. Its separate stable M/M/1 residence readout uses additional stochastic assumptions. The model can illustrate a collection pipeline falling behind, but cannot quantify actual profiler overhead or dropped records.

Compute collection overhead and a rare-event miss probability

A matched warmed window completes in 200 ms without profiling and 230 ms with a chosen tracing configuration. Separately, suppose a capture design independently includes each of twenty fixed event-bearing opportunities with probability 0.1. These are illustrative observations and a separate selection model.

  1. Observed relative timing change is (230−200)/200=0.15, or 15%. The paired scope includes the same completed work; repeated runs are still needed to estimate how stable that difference is.

  2. For the independent inclusion design, the expected captured count is 20×0.1=2 events. This expectation is not a guarantee that any particular capture contains two.

  3. The chance of capturing none is 0.9²⁰≈0.1215766546, about 12.16%. A capture with no event is therefore possible even though the twenty event-bearing opportunities are assumed to exist.

The result

Timing perturbation and observation probability are different quantities. Neither a fixed overhead correction nor a quiet sampled trace proves the ordinary execution path is fully known.

Explore the model

Interactive lab

Interactive lab · queue

A collection pipeline can fall behind its input

Reset, set both λ=4 and μ=2, predict backlog four initially and five after one Step, then inspect. Reset and re-enter both λ=1 and μ=3 for backlog three after one Step and zero at two seconds. The model is q=max(0,4+(λ−μ)t), starting from four; edits retain elapsed time. It has no finite buffer, dropped records, export, or replay. The separate stable M/M/1 residence readout is not a measured profiler delay.

A collection pipeline can fall behind its inputFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forObserve collection pressure and missingness directly rather than assuming the observer is free or complete.

Check your understanding

A profiler replays kernels and changes their concurrency while collecting detailed counters. Which timing conclusion is justified?

Try it yourself

A comparable baseline window takes 80 ms and a trace window takes 92 ms. An independent capture design includes each of ten event-bearing opportunities with p=0.2. Compute observed timing overhead, expected captured count, and chance of no capture.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Timing ratio and Bernoulli inclusion are separate calculations; use (1−p) to the number of opportunities for a complete miss.

Compare with the solution

Timing overhead is (92−80)/80=15%. Expected included events are 10×0.2=2. The no-capture probability is 0.8¹⁰=0.1073741824, about 10.74%. These numbers require the stated comparable-work and independent-inclusion assumptions; they do not establish a real profiler's missing-event law.

Take it with you

  • Measure observer cost on comparable completed work and state what the window includes.
  • Trace detail, reference retention, replay, and sampling perturb different aspects of execution.
  • Use profiles for scoped explanations and uninstrumented measurements for final performance claims.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.