28/36
Part 8 of 8 · Follow-upsLearn and do

What interviewers ask next

The practice problem's README lists the first ones: what a sampler misses, why it can't count calls, millions of samples, and several threads. The rest come up around the other stack questions.

What can a sampler miss? Any call that starts and ends between two samples

A sample every 10 ms, and f running in short bursts
0102030
f runs
samples

The samples see f, f, f and then main. Every call of f that starts and ends between two samples is never seen.

Here f ran 8 times in 30 ms, and a sample came every 10 ms. Three samples landed inside a call of f, and the other 5 calls fell between samples.

With names alone, f at 0, 10 and 20 looks like one long call from 0 to 30. If each frame carries a call id, it's 3 calls. Either way, 5 of the 8 never show up.

Python 3.15's sampling profiler documents this trade: very short calls may be missed if they finish between samples. It estimates time from counts instead. It counts how often each function appears in the samples and multiplies by the sampling interval. So the time comes out close, and the call count doesn't.

A bigger made-up run shows it. main calls f 996 times, each call 1 to 5 ms long, with 0.5 to 1.5 ms of main's own work between, sampled every 10 ms. The times are in microseconds. tagged holds the samples with a call id on each frame, a pair (name, id), and plain holds the same samples with names only:

import random

rng = random.Random(1)
calls, t = [], 0
while True:
    length, gap = rng.randint(1_000, 5_000), rng.randint(500, 1_500)
    if t + length + gap > 4_000_000:
        break
    calls.append((t, t + length))
    t += length + gap
end = t


def frames_at(time):
    for k, (start, stop) in enumerate(calls):
        if start <= time < stop:
            return [("main", -1), ("f", k)]
    return [("main", -1)]


tagged = [(time, frames_at(time)) for time in range(0, end, 10_000)] + [(end, [])]
plain = [(time, [name for name, _ in stack]) for time, stack in tagged]

The table runs three functions you write in the practice problem:

  • trace(samples), level 1, turns samples into start and end events: events_between on each neighboring pair.
  • trace_calls(samples), level 3, does the same for (name, id) frames, which match only when both parts do.
  • function_times(events, include_callees=True), level 2, gives each function's total time.
truthfrom names, tracewith call ids, trace_calls
calls of f99673 starts304 starts
total time in f2,982.1 ms3,037.3 ms3,037.3 ms

The 401 samples put f's time within 2 percent. With names alone, f returning and being called again between two samples looks like one long call, so 996 calls became 73. Call ids separate those, and trace_calls found 304. Still, 692 of the 996 calls never appeared in any sample. The docs say it plainly: when you need exact call counts, sampling can't give them.

TIMELINE in the editor is the 30 ms from the figure above, as a string: F is a millisecond with f running, and . one without. Write runs(timeline) to turn it into calls, then seen(calls, every) to count the calls a sampler sees.