Powered By Blogger

Thursday, March 15, 2018

Profiling the kernel and modules

There are several facilities to see where the kernel spends its resources. A simple one is the profiling function, that stores the current EIP (instruction pointer) at each clock tick.

Build a kernel (2.5.43 or later) with CONFIG_PROFILING=y.

Boot the kernel with command line option profile=2 (or some other number instead of 2). This will cause a file /proc/profile to be created. The number given after profile= is the number of positions EIP is shifted right when profiling. So a large number gives a coarse profile.

If command line option is not possible hard-code the profile variable in the kernel/profile.c

The counters are reset by writing to /proc/profile.

The utility readprofile will output statistics for you. It does not sort - you have to invoke sort explicitly. But given a memory map it will translate addresses to kernel symbols.
See kernel/profile.c and fs/proc/proc_misc.c and readprofile(1).
For example:
# echo > /proc/profile
...
# readprofile -m System.map-2.5.59 | sort -nr | head -2
510502 total                                      0.1534
508548 default_idle                           10594.7500
The first column gives the number of timer ticks. The last column gives the number of ticks divided by the size of the function.
The command readprofile -r is equivalent to echo > /proc/profile.

For profiling a module we need to build the module with kernel.This can be done by replacing obj-m with obj-y in module Makefile.
See that System.map file has symbols for the module.

No comments:

Post a Comment