Parallel build: tuning the performance

Do you want to build LibreOffice alongside other applications that are open on your computer like the IDEs, and you want to keep the programs responsive during the build? Then you need to know how to tune the parallel build. Here I discuss how.

Parallel Build

By default, LibreOffice uses the number of CPU cores as the number of parallel builds. In this way, if your CPU has 32 cores, you will get 32 parallel processes during the build. This parallelism usually speeds up the whole build. You can override this setting by using this parameter in the autogen.input.Here, x is the number of parallel processes:

--with-parallelism=x

The optimal number of parallel build processes is usually less than the number of CPU cores. If you want to have other applications like IDEs and browsers open, you should reserve some CPU cores for those applications.

Problems with Parallel Build

Previously, in some situations make invoked several jobs, in which themselves created multiple other jobs. This resulted to a huge number of processes, which lead to system slowness. The problem was fixed earlier this year, by a patch from Christian. Now, by using -flto=jobserver instead of fixed $(PARALLELISM), we make sure that the number of parallel jobs are limited.

When facing excessive number of parallel builds, one symptom is that those processes may consume all the memory of the system. Then, the operating system starts to use the swap memory in hard disk. As the swapping on disk is much slower than the memory operations, your system may hang for several seconds, or even minutes! On Linux systems with OOM (out of memory) killer, the OOM service may kill some of the processes to get the system out of the hang situation. Otherwise, you may have hard times doing it manually. Some developers suggest that you should disable the swap completely, so that in case of out of memory, the processes that may fall into swap will fail as soon as possible.

To avoid such problems, you can limit the load of the build. This is because limiting the maximum number of parallel builds is not enough. In this case, an option from make is handy. You can use the -l option to limit the load, and when the load is above that, make will not create new jobs. The man page for make says:

-l [load], --load-average[=load]

specifies that no new jobs (commands) should start if there are others jobs running and the load average is at least load (a floating-point number). With no argument, removes a previous load limit.

For example, on a system with 20 cores, you can use it with a load around 90% of the number of cores, to keep CPU available for other programs:

make -l 18

Avoiding Swapping on Disk

In the end, there are situations that you unfortunately see your system is actually using swap space. For example, in Linux, you can check that with:

$ free -h
               total        used        free      shared  buff/cache   available
Mem:            32Gi       9.7Gi      17.5Gi       795Mi       4.1Gi       20.5Gi
Swap:          3.9Gi       743Mi       3.2Gi

In this way, you have to free the swap memory. You can do that with commands like swapoff, and then make sure that the swap in use is zero. You should save the below commands to a shell script file, and run it with root permission, for example with sudo:

$ cat clearswap.sh
echo 3 > /proc/sys/vm/drop_caches
swapoff -a
swapon -a -d

Then, check the situation with:

$ free -h
               total        used        free      shared  buff/cache   available
Mem:            32Gi       9.7Gi      17.5Gi       795Mi       4.1Gi       20.5Gi
Swap:          3.9Gi          0B       3.2Gi

This shows that the swap is cleared, and the system should be much faster, and much more responsive.

Final Words

LibreOffice has around 6 million lines of C++ and some Java code (<280k) for Android viewer, tests and other things while the goal is to port most of Java code to C++. If you want to build it in a reasonable amount of time, for example roughly less than 1 hour, you need a decent computer with enough RAM. Recent (2-3 years old) ARM or Intel CPU with 32 GB of RAM should be fine. But, with less RAM there is a good chance that you hit swapping, and experience slowness.

I hope that you will have a good time building LibreOffice and doing LibreOffice development, to make it better for you, and for all the others!

Comments
  1. 12 months ago