Section 14.35

Instrumentation coverage

Definition

Instrumentation coverage is the part of a running system’s behavior that its runtime instrumentation §14.24, the code or tools added to observe the system while it runs, actually observes, compared with the part that a claim about the measurement needs. Coverage has three dimensions: the categories of resource or event observed (for memory: static variables, stacks, heap, pools, device buffers), the code paths into each category that pass an observation point, and the period observed. A measurement can be exact for what it covers yet answer the claim only in part.

The idea

A developer wraps the C library functions malloc and free, which request and release heap memory, so that each call records its size. The tool reports a peak of 4 KiB, and a report then calls this the program’s peak RAM use. The 4 KiB can be exactly right for the calls the wrapper saw. But the wrapper never sees global variables, whose memory the linker §12.4 reserves when the program is built. It never sees the stacks, which grow and shrink with function calls. It never sees a receive buffer filled by DMA §4.36 (direct memory access), in which a hardware unit copies data into memory without the processor. And it never sees a pool that hands out fixed-size blocks without calling malloc. The number measures one category correctly and is presented as the total of all of them.

A census is a fair picture. A census taken only in the cities counts the city population exactly and says nothing about the countryside: the error lies in the claim, not in the counting. Instrumentation coverage asks the census question before any number is read: which population does the claim cover, and does every member of it pass a counter? A gap is either a whole category with no counter or a category whose counter some members bypass. A census can also count a person with two homes twice, and memory accounting has the same risk when two observers cover one region. The picture fails in one respect: a census counts on one date, whereas memory use changes from moment to moment, and the peaks of different categories need not coincide.

How it works

Check coverage in this order.

  1. State the claim and the categories it needs. “Peak RAM use during this test” needs every category of RAM the program uses; “peak heap use” needs only the heap.

  2. List the categories. On a small device the usual ones are: static storage §4.11 (global and static variables, reserved when the program is built); stack storage (one stack per thread and, on many processors, a separate stack used by interrupt handlers); heap storage (memory obtained while the program runs through the allocator functions); pools (allocators that hand out fixed-size blocks from a region reserved in advance); DMA buffers (regions that a peripheral reads or writes on its own); and memory that the operating system reserves for its own records of each thread.

  3. Give each category an observation method. The linker’s map file, a report of the address and size of every variable and function, gives static storage exactly. Stack use comes either from static stack analysis, which adds up the frame sizes that the compiler reports (the stack space each function call needs) along every chain of calls and takes the largest sum, giving a bound for the chains it can follow; or from stack painting, which fills a stack with a known byte pattern before the run and afterwards finds the deepest byte that was overwritten, giving the most that this run used. Heap use comes from a function wrapper §14.27 around each allocator function, or from the allocator’s own statistics. A pool usually counts its own blocks.

  4. Check path coverage within each category. List every way to obtain or release memory in the category and confirm that each one reaches the observation point. For the heap that means malloc, calloc, realloc, free, the aligned-allocation functions, C++ new and delete, and any allocator that a library or the operating system calls internally under a different name. Calls made before the instrumentation starts, calls from interrupt handlers §6.12 and calls on error paths are common gaps.

  5. Check that observed events are kept. An event that reaches a probe, the point at which the instrumentation records it, can still be lost when the record buffer is full; tracing buffer loss §14.32 is a separate check.

  6. Combine without overlap and at aligned times. Each byte must belong to exactly one category. Peaks taken at different moments add up to an upper bound on the simultaneous peak, not to the peak itself.

  7. Write down every gap. A category left out is named, with the reason and, where possible, a separate bound on its size.

Worked example

A test run on a small device produces four measurements. The claim to check is “peak RAM use”, defined so that every static region counts in full, because it is reserved for the whole run, and the heap and the stacks count by how much of them is in use.

The categories add up to 12+4+1.5+0.5=1812 + 4 + 1.5 + 0.5 = 18 KiB. The wrapper’s 4 KiB is 4/18≈0.224 / 18 \approx 0.22, about 22%, of that total. Reported as “peak RAM use”, it leaves out 18−4=1418 - 4 = 14 KiB.

Adding the pool counter as well would give 18+1.5=19.518 + 1.5 = 19.5 KiB. That counts 1.5 KiB twice: the 6 blocks lie inside the 2 KiB backing array, which the 12 KiB of static storage already includes. The pool counter answers a different question, how full the pool gets, and the total stays 18 KiB.

Path coverage comes next. The allocator’s own statistics count 130 allocation calls in the run, and the wrapper recorded 120. So 130−120=10130 - 120 = 10 calls bypassed the wrapper, here because a library calls calloc, which was not wrapped. The 4 KiB heap peak leaves out whatever those 10 calls obtained, so it cannot be relied on until they are covered.

Last, timing. The 18 KiB adds four maxima that may have occurred at different moments. With complete inputs it would be an upper bound on the true simultaneous peak; with a heap figure that may be too low, it is a safe bound in neither direction. The repair is to wrap calloc as well, confirm that the two call counts agree, and run the test again.

Stated precisely

Let RR be the set of memory categories that a claim requires, and OO the set that the instrumentation observes, with O⊆RO \subseteq R. For each category cc and each time tt during the run, let uc(t)u_c(t) be the number of bytes of category cc in use at time tt. The claim “the peak memory §11.37 use of this run is PP” means P=maxt∑c∈Ruc(t).P = \max_t \sum_{c \in R} u_c(t). A reported total supports that claim only if four conditions hold.

  1. Category coverage: O=RO = R, or each category that is in RR but not in OO has its own bound, obtained by another method such as an analysis.

  2. Path coverage: for each category in OO, every operation that changes ucu_c reaches the observation point, and its record is kept.

  3. Disjointness: no byte is counted in two categories.

  4. Time alignment §4.22: the values added were taken at the same instant.

When condition 4 fails, a weaker statement is still true. The largest value of a sum is never more than the sum of the largest values of its terms: maxt∑c∈Ruc(t)≤∑c∈Rmaxtuc(t).\max_t \sum_{c \in R} u_c(t) \le \sum_{c \in R} \max_t u_c(t). Adding the peaks of the categories therefore gives an upper bound on PP, provided conditions 1 to 3 hold, and the bound equals PP only when all the category peaks happen at the same moment. Every quantity here describes the observed run. A bound for every possible run needs an analysis, such as static stack analysis, or a test shown to exercise the worst path.

Limits and common mistakes

Coverage of one run is not coverage of all runs. Stack painting and a heap wrapper report what the test inputs caused; a deeper chain of calls or a larger message that the test never produced can use more. Static stack analysis bounds every chain of calls it can follow, but it cannot bound recursion, a function that calls itself, or calls through function pointers unless the developer supplies extra information.

Coverage changes when the build changes. If the compiler inlines a function, copying its body into each caller, a probe on that function’s entry no longer fires at those call sites. A library update can switch to a different allocator function. Coverage must be checked again for every build that is measured.

Requested bytes are not consumed bytes. A wrapper records the sizes the program asked for; the allocator also spends bytes on its own headers and on alignment, and heap fragmentation §4.9 can make a request fail while enough free bytes exist in total. A claim that memory suffices needs the allocator’s view, not only the requested sizes.

The instrumentation uses memory too. The wrapper’s record buffer and the probes’ code occupy RAM, and the claim must say whether that memory is counted. This is one part of instrumentation overhead §11.32.

Complete coverage does not make each record correct. A probe that sees every event can still lose records, stamp them with the wrong time or attribute them to the wrong thread.

Instrumentation coverage is not code coverage §10.18. Code coverage measures which parts of a program a test executed; instrumentation coverage measures which events an observer sees. The two interact: a probe on a path that the test never executes observes nothing.

The same questions apply beyond memory. A claim about every message received, every interrupt or every update needs an inventory of the event categories and of every path into each one.

Questions answered

Are stack, heap, DMA, static buffers, and all allocator APIs covered?

They are covered only if each one has its own observation method and every allocator entry point is shown to reach it; covering one category, such as the heap, says nothing about the others. Static buffers, including DMA (direct memory access) buffers declared as global arrays, are covered by the linker’s map file, which lists every variable’s size. Each thread’s stack and the interrupt stack are covered by stack painting, which fills the stack with a known pattern before a run and finds afterwards how much of it was overwritten, giving the most that run used; or by static stack analysis, which adds up the compiler’s frame sizes along every chain of calls, giving a bound for the chains it can follow. The heap is covered by a wrapper around the allocator functions or by the allocator’s own statistics, and a pool, an allocator that hands out fixed-size blocks from a reserved region, by its own block counter.

For the allocator APIs, list every entry point: malloc, calloc, realloc, free, aligned allocation, C++ new and delete, and any allocator that a library or the operating system calls internally. Confirm that each reaches the observation point, for example by checking that the wrapper’s call count equals the allocator’s own count. Then check that no region is counted twice (blocks taken from a pool lie inside the pool’s backing array, which the static total already includes), treat peaks measured at different moments as adding up to an upper bound rather than the simultaneous peak, and report any category left uncovered as a named gap.

Related entries: Runtime instrumentation (§14.24), Function wrapper (§14.27), Peak versus retained memory (§11.37), Static versus dynamic footprint (§11.38), Tracing buffer loss (§14.32), Static, stack, and heap storage (§4.11), Direct memory access (DMA) (§4.36).