Section 6.44

Firmware build profile

Definition

A firmware build profile is a named set of build inputs and settings that turns one source tree into one firmware image. It fixes the configuration values, the compiler and its optimization level, the hardware description, whether logging and run-time checks are compiled in, and the link and signing settings. Typical profiles are debug, for diagnosis, and release, for shipping. Two profiles of the same source can differ in size, timing, memory use and checks, so every measured result belongs to the profile that produced it.

The idea

A developer measures how long the firmware takes to handle one event and gets 3.5 ms. The image measured was the debug image: it prints a log line for each event over a serial port and checks assertions. The product ships the release image, which does neither and handles the same event in 0.05 ms. The source code is the same; the two images are not. A result is a fact about the image that was built, so it carries over to another profile only when the difference between the profiles cannot affect it.

A test car fitted with measuring equipment is a fair picture. The equipment adds weight, so fuel-consumption figures must be measured again without it, while the color of the paint is the same with or without it. The picture stops at one point. Removing equipment from a car changes nothing else, but removing logging from firmware also changes the timing of everything around it, and removing an assertion removes the check itself: a fault that the debug image would have caught passes silently through the release image.

How it works

A profile fixes these inputs, and each changes the image in its own way.

In Zephyr, a profile is usually a set of configuration fragments, for example prj.conf plus a debug.conf named in the build variable EXTRA_CONF_FILE. Projects built with CMake §12.12, a widely used generator of build scripts, name build types such as Debug and Release.

Logging. Zephyr’s logging macros, such as LOG_INF(), are filtered by a level fixed at build time; a message below the compiled level is removed by the compiler and costs nothing. A message that is kept costs time in one of two ways. In deferred mode the call only copies the message into a buffer, and a logging thread formats and sends it later, so the caller pays little, but the thread’s work and its buffer remain in the image. In immediate mode the caller formats and sends the message before the call returns. printk(), Zephyr’s simple print function, writes formatted text to the console; with CONFIG_PRINTK off it compiles to an empty function.

Assertions. An assertion is a check, written into the code, that a condition the programmer believes always true really holds.

Worked example

Logging changes timing. The debug profile prints one 40-character line per event through a console that sends each character over a UART §6.23 (universal asynchronous receiver-transmitter) at 115200 baud and waits for the hardware to take it. Each character is a 10-bit frame, so the line takes 40×10/115200s≈3.4740 \times 10 / 115200\ \text{s} \approx 3.47 ms. Suppose the event’s real work takes 0.05 ms. The debug image then needs about 3.47+0.05=3.523.47 + 0.05 = 3.52 ms per event and can handle at most 1/0.00352≈2841 / 0.00352 \approx 284 events per second; the release image, at 0.05 ms, can handle 1/0.00005=200001 / 0.00005 = 20000. The debug latency is about 3.52/0.05≈703.52 / 0.05 \approx 70 times the release latency. No timing result transfers from this debug image to the release image.

Stack use changes. A thread has a 512-byte stack, and on its deepest path three functions are active at once. In the release profile each of their stack frames, the memory that one function call reserves on the stack, takes 128 bytes: 3×128=3843 \times 128 = 384 bytes, leaving 512−384=128512 - 384 = 128 bytes spare. The debug profile keeps more variables in memory, adding 32 bytes per frame, and a log call at the deepest point needs another 256 bytes for formatting: 3×(128+32)+256=480+256=7363 \times (128 + 32) + 256 = 480 + 256 = 736 bytes, which overflows the stack by 736−512=224736 - 512 = 224 bytes. The release measurement of 384 bytes does not bound the debug image. The reverse fails too: optimization can merge functions into one larger frame, so a debug measurement does not bound the release image either.

Checks disappear. The debug image handles one million events with CONFIG_ASSERT on, and an assertion that a buffer index is below 16 never fails. That shows the index stayed below 16 in those runs of the debug image. In the release image the assertion is gone. If the index can exceed 15 under some timing that the slower debug image never produced, nothing stops the out-of-bounds write §4.4. A property that must hold in the shipped product needs a check that stays in every profile and handles failure on purpose, or an argument that the condition holds for every input, or, for a fact fixed at build time, a compile-time assertion.

Stated precisely

Let a conclusion QQ, such as “each event is handled within 1 ms” or “the message bytes follow the format”, rest on a list of premises. Each premise is a fact about one input of the build (the source, the toolchain, a configuration value, the hardware description) or about the running conditions. QQ transfers from profile AA to profile BB when every premise holds in BB: either the input it concerns is identical in AA and BB, or a stated argument shows that the difference cannot change the premise. Otherwise QQ must be established again for BB.

Applied to the usual differences between a debug and a release profile:

Build parity in comparisons

A comparison between two images, such as with and without a feature, measures the feature only if everything else in the two profiles is equal: the same source apart from the feature, the same toolchain versions, the same optimization level and compiler options §12.3, the same logging and assertion settings, and the same hardware description and clock settings. This equality is called build parity. Two differences that would each invalidate a latency comparison are an -Og image compared with an -Os image, and an image with immediate-mode logging compared with one without logging; either difference alone can move latency by more than the feature does.

A difference that cannot be avoided, such as the feature’s own code shifting the addresses of other functions, is not hidden. The report names it, measures its effect separately, for example with a control image that adds the same amount of code without the feature, and states how large the effect is.

Limits and common mistakes

A profile’s name proves nothing. The resolved configuration file, the compiler command lines and a hash of the built image identify what was actually built; a build fingerprint §15.17 records them.

A debug-only check protects only the debug image. If the release image needs the protection, the check must stay in every profile.

Code with a side effect inside __ASSERT() or assert() behaves differently when assertions are off, because the whole expression vanishes.

Higher optimization can expose undefined behavior that a lower level happened to hide, so a release image can fail where the debug image worked.

Measuring changes what is measured. Instrumentation §14.24 added for a measurement is itself a profile difference, and its overhead belongs in the report.

The same source and the same profile name still give different images when the toolchain differs. A reproducible build §13.8, in which identical inputs give identical bytes, is what makes a rebuilt image the same image.

Questions answered

Which conclusions transfer between profiles, and which require revalidation?

A conclusion transfers from one firmware build profile to another only when every fact it rests on is the same in both; everything else must be checked again. A build profile is the set of settings, such as optimization level, logging, assertions and configuration values, that turns one source tree into one firmware image, so two profiles of the same source give different images.

For a comparison between two images, both must come from the same profile apart from the factor under test. Any difference that cannot be avoided is named, measured separately and reported, not hidden.

Related entries: Compiler correctness (§3.40), Kconfig (§6.40), Measurement uncertainty (§11.33), Compiler optimization (§3.39), Reproducible build (§13.8), Build fingerprint (§15.17).