Do Printk

Description
This page describes the "DO_PRINTK" feature. This is a small tweak on the already-supported printk configuration option in the kernel, which allows a developer to configure whether the kernel will be compiled with or without printk messages.

This feature allows you to turn off printk messages, while preserving the printk function itself. Then, you can turn on the printk messages from an individual file, or set of files. This allows you greater granularity of control over the printk messages that are compiled into the kernel.

Rationale
Control over the printk messages in the kernel is important because the kernel messages comprise a substantial portion of the overall kernel size (ranging from 5 to 10% of overall kernel image size.)

The kernel (as of this writing, version 2.6.17) already supports an option to turn off all printk messages. However, sometimes this is too much. Sometimes, an embedded developer may want to see a few printk messages from specific files or routines, while still not enabling every single printk message in the kernel. The DO_PRINTK feature provides this fine-grained control.

Patch
Patch for 2.6.17.4 is here: [[Media:do-printk.patch]] (3KB)

How To Use
To use DO_PRINTK, perform the following steps:

General Setup ---> [*] Configure standard kernel features (for small systems) ---> [*] Enable printk function in the kernel [ ] Enable support for printk messages
 * Apply the patch
 * Configure your kernel with CONFIG_PRINTK_FUNC=y and CONFIG_PRINTK=n
 * These options appear under "General Setup", "Configure standard kernel features (for small systems)" in 'make menuconfig'
 * With CONFIG_PRINTK=n, the compiler will by default omit (compile away) the calls to printk in all files
 * Select the individual files you would like to still emit printk messages from
 * Add "#define DO_PRINTK" to the files where you wish printk messages to be preserved
 * Make sure this definition appears before any  lines in the file.
 * Alternatively, add -DDO_PRINTK to the compiler flags for files you wish to preserve printks in.
 * (see below for details)
 * Compile the kernel
 * (optionally) Examine the kernel size, and rejoice at your size savings
 * Install the kernel

Ways to force printks to be preserved
There are actually several ways to get DO_PRINTK defined for a particular file:

CFLAGS_foo.o += -DDO_PRINTK EXTRA_CFLAGS += -DDO_PRINTK
 * 1) add to the top of the file a line consisting of:
 * 1) define DO_PRINTK
 * 1) * this must appear before any  lines for the file
 * 2) * this definition will override the CONFIG_PRINTK=n directive, and allow printks in the file to work normally
 * 3) add an option to CFLAGS to the Makefile which controls the building of the file(s) you want to affect
 * 4) * To affect an individual file, add -DDO_PRINTK for the CFLAGS for that file:
 * 5) * For example, in the Makefile that compiles foo.c, add the line:
 * 1) * To affect all the files in a directory, set EXTRA_CFLAGS, like so:

Sample Results
Examples of use with measurement of the effects.

Case Study 1
On an x86 machine, using kernel.org 2.6.17.4 kernel, I got the results below, using the DO_PRINTK feature in combination with the PRINTK configuration option.

Here is the key for the following tables:

Summary of image size results
Kernel sizes (sorted by decreasing size):

Image size results compared to baseline
The following tables show the image sizes of various kernels compared with a baseline kernel. The baseline kernel has a fairly normally i386 configuration, with CONFIG_PRINTK_FUNC=y and CONFIG_PRINTK=y.

Size delta results:

Size delta results:

Size delta results:

Size delta results:

Image size results compared to 'printk-off' baseline
The following tables show the image sizes of various kernels compared with a baseline kernel. The baseline kernel has 'printk-off' configuration, with CONFIG_PRINTK_FUNC=y and CONFIG_PRINTK=n.

Size delta results:

Size delta results:

Size delta results:

Size delta results:

Case Study 2
[need to post PPC and MIPS results here also]

Status
(for each arch, one of: unknown, patches apply, compiles, runs, works, accepted)
 * Status: tested on multiple platforms
 * Architecture Support:
 * i386: works
 * ARM: compiles, not run-tested yet
 * PPC: works
 * MIPS: works
 * SH: unknown

Future Work/Action Items
Here is a list of things that could be worked on for this feature: - nothing right now