Saturday, 1 February 2014

Memory usage of a kernel module

While trying to estimate the amount of memory consumed by a kernel module (usually device drivers),I tried using the size utility which gave the size of the static memory areas of the .ko ( .bss, .data, .text etc). So I was expecting the sum of these values to be exactly equal to the output given by the lsmodcommand immediately after inserting the module.
No dynamic memory allocation(kmalloc or vmalloc) is performed in the init() function to ensure that it isn't causing the difference.So why is there a mismatch?
Curiously the mismatch was found to be a fixed amount most of the time!!
The command outputs are listed below

size chardev.ko

text    data     bss     dec     hex   filename
172 448 1024016 1024636 fa27c chardev.ko

lsmod

Module  Size    Used by    Tainted: P
chardev 1025040 0 - Live 0xc009d000


May be the functions used by the module are counted into the module size ? Try

cat /proc/kallsyms | grep module_name
The difference between the two size is 404. Text + data + 404 = 1024. May be this is some kind of granularity problem ? I don't know how the size is calculated inside the kernel...
However, kernel code and data are allocated using dynamic memory. And kmalloc uses pre-allocated block of memory, so it is quite likely that there is some rounding up when code and data sections are allocated.
Try to increment the size of the data sections and see if the lsmod reported size change

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More