Section 14.31

Sampling bias

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.

  1. Phase locking. The sampler and the workload are both periodic, and their periods have a simple ratio. Write the sampling period as TsT_s, the workload period as TwT_w, and their ratio Ts/TwT_s / T_w as a fraction a/ba / b in lowest terms. The samples then fall at only bb different phases of the workload cycle, spaced Tw/bT_w / b apart, and activity between those phases is never seen. Equal periods give b=1b = 1: one phase, for ever.

  2. 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.

  3. 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.

  4. 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.

Worked example

Fixed phase. The task is active during [0,1)[0, 1), [10,11)[10, 11), [20,21)[20, 21) milliseconds and so on, where [x,y)[x, y) means from xx up to but not including yy. Its true active fraction is 1/10=0.11 / 10 = 0.1. Samples at 5, 15 and 25 milliseconds fall outside every active interval, so the observed fraction is 0/3=00 / 3 = 0. With 300 samples at the same spacing it is 0/300=00 / 300 = 0.

A different period. Sample every 7 milliseconds, starting at 0. The ratio 7/107 / 10 is already in lowest terms, so b=10b = 10. The phase of a sample is its time modulo 10, the remainder after dividing by 10: 0,7,14mod10=4,21mod10=10, 7, 14 \bmod 10 = 4, 21 \bmod 10 = 1, 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 [0,1)[0, 1), so the estimate is 1/10=0.11 / 10 = 0.1, the true value. Sampling 99 times a second gives Ts/Tw=(1/99)/(1/100)=100/99T_s / T_w = (1/99) / (1/100) = 100/99, so b=99b = 99 phases spaced 10/99≈0.10110 / 99 \approx 0.101 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 9/99≈0.0919/99 \approx 0.091 or 10/99≈0.10110/99 \approx 0.101, 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 1/0.01=1001 / 0.01 = 100 samples, about 1 second, and returning to the starting phase takes 10/0.01=100010 / 0.01 = 1000 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 [0,10)[0, 10) milliseconds, independently of the other cycles. One sample hits the task with probability 1/10=0.11 / 10 = 0.1. Ten samples are expected to hit 10×0.1=110 \times 0.1 = 1 time, so the expected estimate is 1/10=0.11 / 10 = 0.1, the true value: this design is unbiased. Yet all ten samples miss with probability 0.910≈0.34870.9^{10} \approx 0.3487, 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 TwT_w, and let it be active during a set AA of phases (positions within the cycle, from 0 up to TwT_w) of total length ℓ\ell. The true active fraction is f=ℓ/Twf = \ell / T_w. A sampler takes nn samples at moments t1,…,tnt_1, \dots, t_n. The phase of sample kk is tkmodTwt_k \bmod T_w, the remainder of tkt_k after dividing by TwT_w, and the estimate f̂\hat{f} is the fraction of samples whose phase lies in AA. The sampling design is the rule, including any randomness, that chooses the moments. The bias of the design is E[f̂]−f\mathrm{E}[\hat{f}] - f, where E\mathrm{E} is the average over that randomness.

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 a/ba / b in lowest terms, the samples visit only bb phases, spaced one bb-th of the workload period apart. With equal periods, b=1b = 1: 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).