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).

2 thoughts on “Speeding up Linux kernel compilation

  1. Good question. Debian's xutils-dev package contains the makedepend tool that creates a dependency graph for a source file. Also AFAIK automake uses depcomp to analyze each source file dependency and create a correct Makefile without missing dependencies. But I don't know if the Makefiles created are always "missing dependencies" free in practice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s