Definition
Sampling bias is a systematic error in an estimate that comes from the way the sample was chosen: some members of the population that the estimate describes are more likely to be observed than others, and the estimate does not correct for it. In profiling, the population is usually every moment of a run, and the bias arises when the sampling moments favor or avoid some activity, for example by running in step with a periodic task. Unlike random error, sampling bias does not shrink as the sample grows.
The idea
A task runs for the first 1 millisecond of every 10-millisecond cycle, so it is active 10% of the time. A profiler samples every 10 milliseconds, and its first sample falls 5 milliseconds into a cycle. Every later sample then also falls 5 milliseconds into a cycle, where the task is idle. The profiler reports that the task never runs. Taking 100 times as many samples at the same spacing reports the same 0%, and only makes the wrong answer look more certain.
An estimate is fair only if every part of the population has the chance of being observed that the estimate assumes. A survey that telephones homes at 11 in the morning on weekdays reaches the people who are at home then, and no number of extra calls at 11 reaches the others. A sampler that fires at a fixed phase of a periodic workload, meaning a fixed position within each cycle, is the same survey. Randomness in the choice of moments repairs this kind of bias: if each sample moment is equally likely to fall anywhere, the task’s 10% of the time receives, on average, 10% of the samples.
How it works
Sampling bias in profiling has four common causes.
Phase locking. The sampler and the workload are both periodic, and their periods have a simple ratio. Write the sampling period as , the workload period as , and their ratio as a fraction in lowest terms. The samples then fall at only different phases of the workload cycle, spaced apart, and activity between those phases is never seen. Equal periods give : one phase, for ever.
Slow drift. Periods that almost match, for example because two clocks run at slightly different rates (clock drift §6.18), make the sampled phase move slowly. The samples stay in one part of the cycle for a long stretch, so a short run sees only that part.
Blind spots. The sampler cannot run in some contexts. A sampler driven by a timer interrupt §6.11, a hardware signal that suspends the running code, cannot interrupt code that has interrupts disabled or a handler of higher priority. Those moments are never sampled, and their time is charged to whatever runs just after them.
Shared trigger. The sampler and the workload are driven by the same event. If the profiler samples on the same timer interrupt that the operating system uses to switch between tasks, every sample falls at a switch, so work that starts or stops at each switch is always observed at the same point.
Three remedies apply.
Randomize the sampling moments: add a random delay within each period, or draw each interval at random. Every moment of the run then has the same chance of being sampled, whatever the workload’s period.
If sampling must be periodic, choose a period without a simple ratio to the known periods of the system. Sampling 99 times a second instead of 100 is a common choice: it does not fall into step with activity that repeats every 10 milliseconds.
Remove blind spots, for example by sampling from an interrupt that software cannot disable, where the processor offers one, or else state them with the results.
Worked example
Fixed phase. The task is active during , , milliseconds and so on, where means from up to but not including . Its true active fraction is . Samples at 5, 15 and 25 milliseconds fall outside every active interval, so the observed fraction is . With 300 samples at the same spacing it is .
A different period. Sample every 7 milliseconds, starting at 0. The ratio is already in lowest terms, so . The phase of a sample is its time modulo 10, the remainder after dividing by 10: , then 8, 5, 2, 9, 6, 3, and then 0 again. The ten phases are the whole milliseconds 0 to 9, each visited once in every 10 samples. One of them, phase 0, lies in the active interval , so the estimate is , the true value. Sampling 99 times a second gives , so phases spaced millisecond apart. Depending on where the first sample falls, 9 or 10 of them lie in the 1-millisecond active interval, and the estimate is or , close to 0.1.
Slow drift. Sample every 10.01 milliseconds while the task repeats every 10 milliseconds. Each sample falls 0.01 millisecond later in the cycle than the one before. Crossing the 1-millisecond active interval takes samples, about 1 second, and returning to the starting phase takes samples, about 10 seconds. A 1-second run whose first sample falls at phase 0 reports an active fraction of nearly 1; a 1-second run whose first sample falls at phase 5 milliseconds reports 0.
Random phase. In each 10-millisecond cycle, take one sample at a moment drawn uniformly from milliseconds, independently of the other cycles. One sample hits the task with probability . Ten samples are expected to hit time, so the expected estimate is , the true value: this design is unbiased. Yet all ten samples miss with probability , and such a run reports 0. Randomization §11.8 removes the systematic error; it does not remove the chance error of a small sample.
Stated precisely
Let a workload repeat with period , and let it be active during a set of phases (positions within the cycle, from 0 up to ) of total length . The true active fraction is . A sampler takes samples at moments . The phase of sample is , the remainder of after dividing by , and the estimate is the fraction of samples whose phase lies in . The sampling design is the rule, including any randomness, that chooses the moments. The bias of the design is , where is the average over that randomness.
If every phase is uniformly distributed over , then , so the design is unbiased. If the phases are also independent, the number of hits follows the binomial distribution, the distribution of the number of successes in independent trials that each succeed with the same probability, here , and the standard deviation §11.14 of is .
If with a fixed start , and in lowest terms, the phases take only values, spaced apart. As grows, approaches , where is the number of those phases that lie in . When is a single interval, is rounded down or rounded up, so the limit differs from by less than . With this bound says nothing: the estimate is 0 or 1.
Drawing the start at random makes even phase-locked sampling unbiased on average over many runs, but within one run all samples stay on one grid of phases. With , every run reports 0 or 1, never in the example above.
Limits and common mistakes
More samples do not reduce bias. They reduce only the random scatter around a wrong value.
Unbiased does not mean accurate in one run. A design can be right on average and wrong in a given run, as the probability 0.3487 of seeing nothing in ten random samples shows. Report the uncertainty, for example as a confidence interval §11.18, a range computed from the data by a method that contains the true value in a stated share of repeated experiments.
Randomizing the moments does not repair blind spots. A moment the sampler cannot observe stays unobserved, whatever its probability of being chosen.
The population must be named. Time occupancy, numbers of events and numbers of runs are different populations, and a sample that represents one of them fairly can misrepresent another.
A workload can change during a run instead of staying periodic. Samples taken only during a warm-up §11.27 phase, the start of a run while caches fill and behavior settles, or only after it, represent that phase alone.
Bias can also enter after sampling. If only the runs that finished produce a profile, the runs that crashed are missing from the results, which is survivorship bias §11.30.
Sampling bias concerns which observations are made. It differs from measurement error, in which an observation is made but recorded wrongly.
Questions answered
Can periodic sampling synchronize with a periodic workload?
Yes. When the sampling period and the workload period have a simple ratio, the samples fall at the same few phases of the workload cycle, cycle after cycle, so the profile represents only those phases. If the ratio of the sampling period to the workload period is in lowest terms, the samples visit only phases, spaced one -th of the workload period apart. With equal periods, : a task that is active during the first 1 millisecond of every 10 milliseconds, sampled every 10 milliseconds at 5 milliseconds into the cycle, is never seen, although it runs 10% of the time, and more samples at the same spacing change nothing. Periods that almost match drift slowly and have the same effect over short runs. The remedy is to randomize the sampling moments, or to choose a period without a simple ratio to the system’s known periods (99 samples a second instead of 100 is a common choice), and to report what the sampler cannot see.
Related entries: Trace versus sampling (§14.30), Population and sample (§11.1), Randomization (§11.8), Independent and identically distributed samples (§11.5), Survivorship bias (§11.30).