Definition
Tracing and sampling are the two basic ways to observe a running program. Tracing records an event each time execution passes a chosen point, such as a function’s entry, so it shows how often and in what order those events happened and, from their timestamps, how long the work between them took. Sampling records what the program is doing at chosen moments only, typically the address of the instruction being executed, and so estimates where the time goes. A trace sees every chosen event; a sampler sees only moments.
The idea
A checksum §4.32 function runs 1000 times a second, and each call takes 1 microsecond. A sampling profiler, a tool that interrupts the program at regular moments and notes which function it interrupted, takes 1000 samples a second. The function runs for only 1000 microseconds of each second, 0.1% of the time, so on average 1 sample in 1000 lands in it, and in many one-second runs none does. The profile then does not list a function that ran 1000 times. A trace of function entries records 1000 events per second, one for each call.
Sampling is like photographing a road once a second. The photographs show which lanes are usually busy, but a motorcycle that passes between two photographs never appears. Tracing is like a counter at a gate that clicks for every vehicle. It counts every passage, but only at the gates where counters were installed, and each click slows the traffic a little. The analogy stops in one place: a camera does not slow the road, but each sample interrupts the program briefly. Sampling also has a cost, which grows with the sampling rate rather than with the number of events.
How it works
Tracing works in three steps.
Probes, small pieces of code added by runtime instrumentation §14.24, run when execution reaches chosen points: the entry and exit of a function, the start of an interrupt handler §6.12, the sending of a message.
Each probe writes a record, usually an event identifier, a timestamp and some data, into a trace buffer in memory.
A consumer drains the buffer to storage or to another computer, where the records are analyzed.
A complete trace gives exact counts, the order of events and, from the timestamps at entry and exit, the duration of each call. Its cost grows with the number of events, because every event runs a probe and writes a record.
Sampling also works in three steps.
A trigger fires: a timer interrupt §6.11, a signal from a hardware timer that makes the processor suspend the running code and run a handler, typically hundreds or thousands of times a second; or a hardware counter that overflows after a set number of events, such as cache misses, memory accesses that the processor’s small fast cache memory could not serve.
The handler records the program counter §2.9 of the interrupted code, the register that holds the address of the instruction being executed, and often the chain of calls that led there.
Afterwards, a symbol table §12.8, which maps address ranges to function names, assigns each sample to a function.
The share of samples that fall in a function estimates the share of time the program spent in it, here called the function’s occupancy. The cost grows with the number of samples, whatever the number of events.
The two methods answer different questions.
How many times did the function run, in what order, and how long did each call take? A trace answers. A sampling profile cannot, because a sample does not show where one call ends and the next begins.
Which code takes most of the time? A sampling profile answers cheaply. A trace answers too, by summing durations, but at a cost per event.
Tools often combine the two: the profiler gprof, from the GNU free-software project, counts calls with code that the compiler inserts at every function entry and estimates time from program-counter samples.
Worked example
Missing a frequent function. The checksum function above runs 1000 calls per second of 1 microsecond each. In one second it runs for microseconds, which is 0.001 second, so its occupancy is , or 0.1%. Let the profiler take samples in that second, each at a moment independent of the others and equally likely to fall anywhere in the second. Each sample lands in the function with probability 0.001. The expected number of samples in the function is . The probability that none of the 1000 samples lands in it is . In about 37 runs out of 100, the profile lists nothing for a function that ran 1000 times.
How many samples are needed. To see the function at least once with probability 0.99, the probability of no hit must be at most 0.01: . Taking natural logarithms and dividing by the negative number , which reverses the inequality, gives . So 4603 samples are needed, about 4.6 times as many as before.
A large region can be missed too. A region with occupancy receives samples. Each sample misses it with probability , and all four miss with probability , although the expected number of hits is .
The cost of tracing instead. A trace of the checksum function records 1000 entries per second. If writing each record takes 0.2 microsecond, tracing adds microseconds per second. That is only 0.02% of the second, but , or 20%, of the time the function itself uses, so call durations measured under the trace are inflated by about that much.
Stated precisely
Let be the occupancy of a region of code, the fraction of an observation interval during which the program counter is in the region, and let be the number of samples. Assume that the sample moments are independent of one another and uniformly distributed over the interval, and that the sampler can observe every moment. Then the number of samples that land in the region follows the binomial distribution, the distribution of the number of successes in independent trials that each succeed with probability : where denotes probability and the expected value. The estimate of the occupancy has expected value and standard deviation §11.14 .
For tracing, let a probe stand at a point that every occurrence of an event passes. If every occurrence runs the probe and no record is lost, the number of records equals the number of occurrences exactly, and the records appear in the order in which the probes ran. Neither condition is automatic. The first is a question of instrumentation coverage §14.35; the second fails under tracing buffer loss §14.32.
Real profilers usually sample periodically, not at independent uniform moments. The binomial model then holds only if the program’s activity has no timing relation to the sampling clock. A workload that runs in step with the sampler breaks the model, as the Sampling bias §14.31 entry explains.
Limits and common mistakes
A function with no samples may still have run. Its occupancy may have been small compared with , or the sampler may have been unable to see it.
A sampling profile estimates time, not calls. Turning occupancy into a number of calls needs the duration of one call, which a sampler does not measure.
A sampler driven by a timer interrupt cannot interrupt code that runs with interrupts disabled. The pending sample is taken just after interrupts are enabled again, so that code’s time is charged to the instructions that follow it. Sampling on hardware counters has a similar effect, called skid: the recorded address can lie a few instructions after the one that caused the event.
A trace covers only instrumented points. An event at a point without a probe leaves neither a record nor any sign that a record is missing.
Tracing changes what it measures. Each probe adds time, and the added share is largest for short, frequent functions, exactly the ones a sampler tends to miss. The Instrumentation overhead §11.32 entry treats this cost.
Neither method gives a complete timeline. A sampling profile is a statistical summary, and a trace is complete only for the events it instruments and only if no record was lost.
Questions answered
Can a rare short function be absent from a sampling profile despite executing often?
Yes. A sampling profile records moments, not calls, so a function appears only if a sample moment falls while it runs, and a function that runs often but briefly is running for only a small share of the time. If the function occupies a fraction of the run and the profiler takes samples at independent, uniformly random moments, the probability that the function gets no sample at all is . For a function called 1000 times a second for 1 microsecond each, ; with 1000 samples over one second, the probability of no sample is . If the samples are periodic and fall into step with the function’s own schedule, the function can be missed in every run. To learn whether and how often a function runs, count its entries with a trace or a counter in the function; use sampling to estimate where the time goes.
Related entries: Runtime instrumentation (§14.24), Sampling bias (§14.31), Tracing buffer loss (§14.32), Instrumentation coverage (§14.35), Instrumentation overhead (§11.32).