liveBook · Manning

Instrumentation

As explained in Modern C

Instrumentation is the insertion of measurement-related operations into a program or execution environment.

Timing three loops

Place timespec_get calls before, between, and after three C loops. The resulting timestamps delimit each loop’s elapsed time; repeated runs provide samples rather than a single timing. section 15.3

Listing 15.6. Instrumenting three for loops with struct timespec
53      timespec_get(&t[0], TIME_UTC);
54      /* Volatile for i ensures that the loop is effected */
55      for (uint64_t volatile i = 0; i < iterations; ++i) {
56        /* do nothing */
57      }
58      timespec_get(&t[1], TIME_UTC);
59      /* s must be volatile to ensure that the loop is effected */
60      for (uint64_t i = 0; i < iterations; ++i) {
61        s = i;
62      }
63      timespec_get(&t[2], TIME_UTC);
64      /* Opaque computation ensures that the loop is effected */
65      for (uint64_t i = 1; accu0 < upper; i += 2) {
66        accu0 += i;
67      }
68      timespec_get(&t[3], TIME_UTC);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":20}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":20}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":20}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":20}],[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":36}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":36}],[{\"line\":15,\"ch\":28},{\"line\":15,\"ch\":36}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":1,\"ch\":20},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":8},{\"line\":2,\"ch\":11}],[{\"line\":7,\"ch\":8},{\"line\":7,\"ch\":11}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":11}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":21}],[{\"line\":7,\"ch\":13},{\"line\":7,\"ch\":21}],[{\"line\":12,\"ch\":13},{\"line\":12,\"ch\":21}],[{\"line\":2,\"ch\":22},{\"line\":2,\"ch\":30}],[{\"line\":6,\"ch\":21},{\"line\":6,\"ch\":29}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":20}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":20}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":20}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":20}],[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":36}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":36}],[{\"line\":15,\"ch\":28},{\"line\":15,\"ch\":36}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}],[{\"line\":1,\"ch\":20},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":8},{\"line\":2,\"ch\":11}],[{\"line\":7,\"ch\":8},{\"line\":7,\"ch\":11}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":11}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":21}],[{\"line\":7,\"ch\":13},{\"line\":7,\"ch\":21}],[{\"line\":12,\"ch\":13},{\"line\":12,\"ch\":21}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":2}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":2}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":20}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":20}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":20}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":20}],[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":36}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":36}],[{\"line\":15,\"ch\":28},{\"line\":15,\"ch\":36}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":19}],[{\"line\":7,\"ch\":17},{\"line\":7,\"ch\":19}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":12,\"ch\":17},{\"line\":12,\"ch\":19}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":1,\"ch\":20},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":8},{\"line\":2,\"ch\":11}],[{\"line\":7,\"ch\":8},{\"line\":7,\"ch\":11}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":11}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":21}],[{\"line\":7,\"ch\":13},{\"line\":7,\"ch\":21}],[{\"line\":12,\"ch\":13},{\"line\":12,\"ch\":21}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":20}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":20}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":20}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":20}],[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":36}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":36}],[{\"line\":15,\"ch\":28},{\"line\":15,\"ch\":36}]]"}
!@%STYLE%@!

The listing shows the timestamp calls and the different loop bodies they surround. section 15.3

Instrumentation inserts measurement operations into an execution. A timing call costs time, but its effects can reach further: the compiler may save registers and lose optimization opportunities; a system call may affect scheduling or caches. section 15.3

TAKEAWAY: Instrumentation changes compile-time and runtime properties. Assess measurement bias before interpreting timings. Repeat runs and examine the mean and standard deviation; substantial variation limits what a sample can support. section 15.3

How Modern C covers it

How deep it goes
Explains compiler and runtime interference, then demonstrates repeated timing and statistical collection; does not establish a universal correction for measurement bias.
Examples
Shows three timestamped loops and a 20-timing sample with substantial variation.
What it assumes
Assumes familiarity with C loops and function calls; defines measurement bias through their concrete effects.
What you can do after
Practice/perform repeated loop timings and assess whether measurement effects and sample variation weaken a performance claim.
How it connects
Connects instrumentation to compiler optimization, system calls, scheduling, caches, mean, and standard deviation.

Also explained in

Compare how 13 books explain instrumentation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.