Saturday, May 5, 2012

Using ccache to speed up kernel compilation

We are going to use "ccache" to speed up our Linux kernel compilation. This neat tool caches the output of C/C++ compilation so the next time the results can be taken from the cache. It can result in 5-10 times speed improvements.


Update : Recorded a short video on it !

1. First we need to install ccache which is already available in Ubuntu repositories
$sudo apt-get install ccache
2. Now let us configure our system to use ccache. Edit your ~/.bashrc
$vim ~/.bashrc
And add the following lines to the end of the file
# ccache
export CCACHE_DIR="/home/prashants/.ccache"
export CC="ccache gcc"
export CXX="ccache g++"
export PATH="/usr/lib/ccache:$PATH"
Replace the /home/prashant/.ccache folder to your own home folder. It is a empty folder that ccache will use to store the result of the compilation. You can use any other folder name of your choice.

The /usr/lib/ccache folder is automatically created by ccache and it contains symlinks to ccache for gcc/g++ compiler names. By adding it to our PATH environment variable, ccache will be called instead of gcc compiler.
$ls /usr/lib/ccache
Now to apply the changes in the ~/.bashrc file
$source ~/.bashrc
3. Next we need to setup the maximum cache size
$ccache -M 2G
This limits the cache size to 2GB. You can change it to something more if you have enough free space.

4. You are done ! Next time you run gcc/g++/make commands it will use ccache. You can monitor the current usage of ccache by executing
$watch -n1 -d ccache -s
5. Lets try it out ! Download the latest linux kernel source from http://kernel.org and compile it. Simultaneously keep a terminal open with the above command running and you will see this


Combining ccache along with distributed kernel compilation can give really amazing results ! Read about the distributed kernel compilation in my previous article here.

21 comments:

  1. Any danger in doing this on a ramdisk? Or does this get rid of the whole idea?

    ReplyDelete
  2. I am very glad to read your informative blog. thanks a lot for your valuable sharing..please keep update..
    you can also visit here linux training in india

    ReplyDelete
  3. I AM EMME AND I LOVE YOU ALL WITH BEAUTIFUL SMILE.

    ReplyDelete
  4. OMAR SOLIMAN IS SPEAKING.
    RESPECT.

    ReplyDelete
  5. ABDELFATTAH EL SISY IS SPEAKING.
    RESPECT.

    ReplyDelete
  6. SARA OMAR SOLIMAN IS SPEAKING .
    RESPECT.
    RESPECT.
    RESPECT.

    ReplyDelete
  7. GAMAL ABDELNASSER IS SPEAKING.
    RESPECT.
    RESPECT.
    RESPECT.

    ReplyDelete
  8. SAAD ELDINELSHAZLY IS SPEAKING.
    RESPECT.

    ReplyDelete
  9. FROM GLADIATOR TO ANOTHER ONE THAT CALLED BRAVE HEART AND LION HEART AND TRICKER GENTLEMAN RESPECT AND I LOVE YOU MORE AND MORE AND MORE.
    AND I WANT TO TOUCH THE SUN WITH YOUR HAND,THIS IS NOT AN ORDER THIS IS AN INQUIREY ONLY. I LOVE YOU.

    ReplyDelete
  10. POTIN THE PRESIDENT OF RUSSIA IS SPEAKING.
    RESPECT.
    RESPECT.
    RESPECT.

    ReplyDelete
  11. TRUMP THE PRESIDENT OF AMERICA IS SPEAKING.
    RESPECT.
    RESPECT.
    RESPECT.
    غموض ادراكى.

    ReplyDelete
  12. MARC FROM FACEBOOK GATE IS SPEAKING.
    RESPECT.

    ReplyDelete
  13. MIRCLE THE PRESIDENT OF THE DUTSCHLAND IS SPEAKING.
    RESPECT.
    RESPECT.
    RESPECT.

    ReplyDelete
  14. SEIFELDIN BIN SOLIMAN IS SPEAKING.
    RESPECT.
    RESPECT.
    RESPECT.

    ReplyDelete
  15. NAGIB SAWIRAS IS SPEAKING.
    RESPECT.

    ReplyDelete