Tuesday, 21 April 2015

How to make things faster

As we all know (at least all Linux programmers know that), in the Linux world some of the software configuration and/or compilation requires invocation of one or more Makefile targets by using the make command. If we talk about simple targets, like copy something here, overwrite file there, the actual execution is pretty fast. However, when we talk about source code compilation, the overall process can take forever. So, can we make make to work faster?

Good example of slow compilation is the invocation of make bzImage when you try to build the Linux kernel. Depending on the chosen kernel configuration parameters and the machine specs, the actual compilation can easily take more than 1 hour.

Recently I found out that we can use the magical option -j which speeds up the compilation process by running individual jobs in separate threads. Here is what the manual says:

man make

-j [jobs], --jobs[=jobs]

Specifies the number of jobs (commands) to run simultaneously. If there  is  more than one -j option, the last one is effective. If the -j option is given without an argument, make will not limit the number of jobs that can run simultaneously.

So, if you have multi-core processor and you know that on hardware level your machine can handle 4 parallel threads, you can run the following command:

make bzImage -j 4

The above command will speed up the overall compilation process approximately 4 times which is huge performance improvement! You can take a look at one real life example here:

https://github.com/ivandavidov/minimal/blob/master/src/2_build_kernel.sh

The make -j invocation in the above example automatically figures out the number of available processors (i.e. CPU cores) and provides this number as value for the -j parameter.

No comments:

Post a Comment