Kernel Diet -- Filtering Printk Messages Based on Verbosity Level
As part of my upcoming boot menu/distro for Openmoko’s Freerunner I searched for relatively simple and generic applicable ways to reduce the Linux Kernel binary size. An obvious candidate for size reduction are the possibly unnecessary kernel printk messages.
I therefore wrote a little patch
series which
introduces a new configuration option (CONFIG_PRINTK_VERBOSITY
) to
selectively compile out printk message strings based on a verbosity
level. The implementation works by wrapping printk with a macro which
evaluates to a constant if condition which the compiler will be able to
optimize out.
Thanks to the feedback from the linux-embedded folks the second version of the patch series works as a drop in replacement for the regular printk and no longer changes existing kernel code.
However unfortunately there is a problem with continued kernel messages. They don’t contain the loglevel which means it’s impossible to filter them correctly based on a verbosity setting without the information from the first printk which started the message.
For a solution we would need a way to pass the loglevel from the first printk which started the message to all following ones until a message is terminated by a new line. This could be achieved by a local variable, however such a variable would have to be available in every kernel function which calls printk and there is no way (I know of) which which would guarantee that.
Anyway it was great fun to play around with the CPP. I will think about a solution for the continued message problem although I am rather sceptical whether a CPP based solution is possible at all. I may or may not submit the patch series to LKML after the 2.6.31 release is out.
Marc