Section 18.2.7

Figure 6b: repeated-update latency against save ordinal

Definition

A plot of repeated-update latency against save ordinal is a figure about a sequence of updates, each saving a new value of the same kind of object to storage. The vertical axis gives how long each save took, its latency. The horizontal axis gives the save’s position in the sequence, its ordinal: first, second, and so on. The ordinal says which save came next, not how much time passed between saves. All saves act on the same changing storage, so each point depends on the saves before it.

Where it fits

Main entry: Garbage collection (§8.16).

The garbage collection entry describes storage that saves every change as a new record and later reclaims space: it copies the records that still hold current values, the live records, out of an old region and then erases that region. Such storage can explain a common shape, latency that rises over many saves and then drops. The cost of finding existing records can grow as records accumulate, and garbage collection can move the needed records to where a search finds them sooner. A plot of this shape, a sawtooth of slow rises and sudden drops, makes that mechanism plausible but does not identify it: other mechanisms fit the same shape.

A toy model with invented numbers makes the assumptions visible. Let each save cost 400 microseconds of fixed work plus 2 microseconds for each record its search must pass, and let nn be the number of records written after the record that the search looks for. A save then takes T=400+2nT = 400 + 2n microseconds. Suppose the sought record was written before the first save, and each save writes one new record after its search. The 50th save then searches past the 49 records of saves 1 to 49, so n=49n = 49 and the search model gives T=400+2×49=498T = 400 + 2 \times 49 = 498 microseconds. Suppose garbage collection runs during the 50th save and copies the sought record forward, so that only the record of save 50 follows it. The 51st save then has n=1n = 1 and T=400+2×1=402T = 400 + 2 \times 1 = 402 microseconds. The 50th save also pays for copying and erasing, so the plot shows one tall point at save 50. Save 51 then lies 498−402=96498 - 402 = 96 microseconds below the level that the rise had reached.

The Zephyr real-time operating system §6.33 includes a storage library of this kind, NVS (Non-Volatile Storage), and a Settings subsystem that can store named values in NVS. Each offers an optional lookup cache: a table in RAM whose size is fixed when the software is built.

With both caches off, each save of an existing name searches back past every record written after that name’s record, a cost that grows as the storage fills, until garbage collection copies the name record forward. With both caches on, the search for the name record starts at the newest record whose ID selects the same cache entry, so its length no longer grows with every record written, unless a frequently written ID selects that entry too.

All data in this entry’s examples are invented.

Limits and common mistakes

Saves in one sequence share changing storage state, so they are not independent repetitions. Treating 100 saves from one run as 100 independent samples is pseudoreplication §11.6. The independent unit is the run, started from a fresh, known storage state.

Report the pauses caused by garbage collection separately from the average cost per save. Amortized analysis §14.4, which spreads the cost of rare expensive steps over many cheap ones, can give a low average per save while single saves still stall for a whole collection.

The ordinal hides elapsed time. Two neighboring saves on the axis may be milliseconds or hours apart, and anything that changes with time, such as temperature or background work, is invisible in the plot.

Questions answered

What would establish the cause of a latency drop?

An experiment that changes only the suspected mechanism, together with an independent measurement of the storage state, would establish the cause of a latency drop; the drop in the plot alone does not. A claim such as “latency drops because garbage collection shortened the search for existing records” needs three kinds of evidence.

  1. Measure the mechanism. For every save, record the storage state: how many records the search passed, which region was being written, and whether a collection ran during that save. The drops should come exactly at the saves after a collection, and the latency should follow the number of records passed.

  2. Intervene on it. Build two versions of the system that differ only in the suspected factor, for example storage lookup caches off and on, and run the same save sequence with the same data from the same starting storage state. If search cost explains the rise and the drop, the version with caches loses the rise but still shows collection pauses; if the rise persists with the caches on, the search explanation is wrong. Turning each cache on separately, four builds in all, shows how much of the effect each cache carries.

  3. Exclude rivals. Hold constant every confounder §11.10, a factor that changes together with the suspected cause and could produce a similar trace: the size of the saved data, other work per save such as checking a signature, temperature, and other activity on the device. Repeat the comparison over several runs, each from a fresh storage state, because the run, not the single save, is the experimental unit §11.3, the thing to which a condition is applied independently.

Related entries: Causal inference (§11.11), Pseudoreplication (§11.6), Amortized analysis (§14.4), NVS record (§8.15), Confounding (§11.10), Experimental unit (§11.3).