Do Printk

From eLinux.org
Jump to: navigation, search

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.

Downloads

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:

  • 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'
    General Setup --->
    [*] Configure standard kernel features (for small systems)  --->
    [*] Enable printk function in the kernel
      [ ]  Enable support for printk messages
    • 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 #include 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:

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

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:

kernel CONFIG_PRINTK_FUNC CONFIG_PRINTK DO_PRINTK used with files... Notes
func.yes-printk.yes-do.na/vmlinux yes yes not applicable This is a pretty regularly-configured kernel (used for baseline in first set of comparisons)
func.yes-printk.no-do.none/vmlinux yes no none Turn off all messages, but leave printk function in place
func.no-printk.na-do.na/vmlinux no not applicable not applicable Turn off all messages and remove printk function
func.yes-printk.no-do.init.star/vmlinux yes no init/*.c Turn off most messages, preserve messages from files in 'init' source directory
func.yes-printk.no-do.init.main.c/vmlinux yes no init/main.c Turn off most messages, preserve messages from init/main.c

Summary of image size results

Kernel sizes (sorted by decreasing size):

text data bss dec hex filename
2621433 564958 137072 3323463 32b647 func.yes-printk.yes-do.na/vmlinux (a.k.a. baseline)
2376317 564958 137072 3078347 2ef8cb func.yes-printk.no-do.init.star/vmlinux
2375508 564958 137072 3077538 2ef5a2 func.yes-printk.no-do.init.main.c/vmlinux
2374733 564958 137072 3076763 2ef29b func.yes-printk.no-do.none/vmlinux (a.k.a. printk-off baseline)
2371463 564514 120688 3056665 2ea419 func.no-printk.na-do.na/vmlinux

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:

section baseline/vmlinux func.no-printk.na-do.na/vmlinux change percent
text 2621433 2371463 -249970 -9%
data 564958 564514 -444 0%
bss 137072 120688 -16384 -11%
total 3323463 3056665 -266798 -8% *
* Full size reduction using printk elimination is 266K or 8% of vmlinux

Size delta results:

section baseline/vmlinux func.yes-printk.no-do.init.main.c/vmlinux change percent
text 2621433 2375508 -245925 -9%
data 564958 564958 0 0%
bss 137072 137072 0 0%
total 3323463 3077538 -245925 -7%

Size delta results:

section baseline/vmlinux func.yes-printk.no-do.init.star/vmlinux change percent
text 2621433 2376317 -245116 -9%
data 564958 564958 0 0%
bss 137072 137072 0 0%
total 3323463 3078347 -245116 -7%

Size delta results:

section baseline/vmlinux func.yes-printk.no-do.none/vmlinux change percent
text 2621433 2374733 -246700 -9%
data 564958 564958 0 0%
bss 137072 137072 0 0%
total 3323463 3076763 -246700 -7%

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:

section func.yes-printk.no-do.none/vmlinux func.no-printk.na-do.na/vmlinux change percent
text 2374733 2371463 -3270 * 0%
data 564958 564514 -444 * 0%
bss 137072 120688 -16384 * -11%
total 3076763 3056665 -20098 * 0%
* Size of printk function alone is about 3.6k in image and 20k in static memory

Size delta results:

section func.yes-printk.no-do.none/vmlinux func.yes-printk.no-do.init.main.c/vmlinux change percent
text 2374733 2375508 775 * 0%
data 564958 564958 0 0%
bss 137072 137072 0 0%
total 3076763 3077538 775 0%
* Size of printk messages from a single C file (init/main.c in this case) was 775 bytes. Obviously, this is heavily dependent on the number of printk calls and the size of the messages in that file.

Size delta results:

section func.yes-printk.no-do.none/vmlinux func.yes-printk.no-do.init.star/vmlinux change percent
text 2374733 2376317 1584 0%
data 564958 564958 0 0%
bss 137072 137072 0 0%
total 3076763 3078347 1584 0%

Size delta results:

section func.yes-printk.no-do.none/vmlinux func.yes-printk.yes-do.na/vmlinux change percent
text 2374733 2621433 246700 10%
data 564958 564958 0 0%
bss 137072 137072 0 0%
total 3076763 3323463 246700 8%


Case Study 2

[need to post PPC and MIPS results here also]

Status

  • Status: tested on multiple platforms
  • Architecture Support:
  (for each arch, one of: unknown, patches apply, compiles, runs, works, accepted)
    • 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