Friday, March 18, 2011

Speeding up Linux kernel compilation

Compiling a Linux kernel can take a long time to finish. Fortunately there are some utilities that can help us to speedup this process.

These tools are:
  1. make -jn: The easier way to speed-up Linux kernel compilation is executing make with the option -j (jobs). This option specifies the number of parallel jobs that make will use to split the build process. Because the kernel Makefile have correct dependency information, one doesn't have to use make's default single process execution.
  2. ccache: When compiling it is common to use make clean to create a clean build of the project. Unfortunately this deletes all the information from previous compilations. The compiler cache (ccache) detects when the same compilation is doing again and looks in its cache for objects files so you don't have to generate them again.
  3. distcc: If you have more than one machine available, you can distribute your builds using distcc. 

I ran a few experiments to measure compilation times with different configurations and compare performance improvement in each scenario.

The experiments where made in my development machine (Intel Core 2 Duo P870 2.53GHz - 4 GB DDR3 with Debian 6 Squeeze). Since I only had one machine I didn't try distcc.

Here are the results:

Using default make (with just one compilation job):

$ time make M=drivers/staging/

real    6m12.030s
user    5m55.002s
sys     0m30.818s


Using make with four compilation jobs:

$ time make -j4 M=drivers/staging/

real    3m39.378s
user    6m27.852s
sys     0m31.614s


Using make with one compilation job and ccache:

$ time make -M=drivers/staging/

real    0m46.364s
user    0m35.042s
sys     0m11.913s


Using make with four compilation jobs and ccache:

$ time make -j4 M=drivers/staging/

real    0m27.513s
user    0m36.374s
sys     0m11.465s


Using make with four compilation jobs and ccache we obtain a speedup of 8X which is really impressive. Also ccache statistics shows that direct cache hit was about 32%.

$ ccache -s
cache directory                     /home/javier/.ccache
cache hit (direct)                  1091
cache hit (preprocessed)              24
cache miss                             1
unsupported compiler option           52
no input file                         25
files in cache                      3455
cache size                          43.7 Mbytes
max cache size                       1.0 Gbytes



Keep in mind that the first time you compile with ccache, your compilation will not be faster. It will even be slower because ccache has to populate its cache (i.e: its cache hit will be zero).