Friday, April 17, 2009

Linux Kernel: Speed Up Compilation

I have been having a hard time for the last few days in doing my "Linux Kernel Assignment". No, programming is not the hardest part. Waiting for the kernel to compile is the main problem. Its pathetically the moment when you become the sitting duck. :). So here are some of the things I came across to speed up the compilation.
  1. Make sure you have ccache installed and configured. As the name suggests, ccache dramatically speeds up subsequent compilations by caching the object files. In most new Fedora distros, its enabled by default. You can check it by doing $which gcc. If it says something like /usr/lib/ccache/gcc, you are all set up. If not, try google, :)
    You can see see its status by issuing command $ccache -s. Its good to set the cache amount a bigger than default, so $ccache -M 2G will set the cache to 2GB which is enough for everyone.
  2. Use gcc option -pipe whenever possible. Self explanatory, it uses pipes instead of temporary files.
  3. If you have dual core or quad core processor and a lot of RAM, you'll notice that while compiling, only a fraction of your processor and RAM gets used. This is because there is only one compile process running at a time. You can specify multiple make jobs by passing parameter like this $make -j 2. Here, make will run at most 2 jobs at a time, taking full advantage of your multi-core CPU and RAM. Some argue on setting higher value, but I do not recommend it, as it likely to have negative effect because of the large amount of context switched in the CPU. People say that this approach leads to race conditions but I haven't bumped into any of that yet.
  4. For Linux Kernel compilation, make sure you have CONFIG_DEBUG_KERNEL unset in .config file. If set, this builds your kernel with debugging symbol making your kernel a lot bigger and significantly longer to build. It will make kernel debugging impossible though, but when you are in a hurry, this really makes more sense. Also strip the .config file from any unnecessary drivers/modules you don't require, but this will require you to have a thorough knowledge about both your PC and running kernel.
  5. For those with high speed network (ethernet or more), give a try to distcc, that distributes the compilation among multiple machine. The configuration is quite straight forward. Check the references section for details. 
  6. Try tmpfs. It enables you to mount any folder in your file system in RAM, so theoretically speeding up the compilation. Try mounting your build folder with tmpfs like this $sudo mount -t tmpfs -o size=500M,mode=0777 tmpfs /usr/src/linux-build.
Using the above mentioned method, you can get a clean build of 2.6.29 kernel in less than 15 minutes. Let me know if you have any problems or come up with any better solutions. :)

References:

https://help.ubuntu.com/community/Kernel/Compile

http://www.linux.org/docs/ldp/howto/Modules/speedup.html

http://en.gentoo-wiki.com/wiki/Portage_TMPDIR_on_tmpfs

http://www.ibm.com/developerworks/linux/library/l-distcc.html

Back to Blogging

After a failed attempt last year to get back to blogging, I'm trying it again this year. I really wanted to get back, but got busy will...