How to speed up compilation time in linux

前端 未结 3 1126
鱼传尺愫
鱼传尺愫 2021-01-07 12:19

While compiling under linux I use flag -j16 as i have 16 cores. I am just wondering if it makes any sense to use sth like -j32. Actually this is a quesiton about scheduling

相关标签:
3条回答
  • 2021-01-07 12:55

    The rule of thumb is to use the number of processors+1. Hyper-Thready counts, so a quad core CPU with HT should have -j9

    Setting the value too high is counter-productive, if you do want to speed up compile times consider ccache to cache compiled objects that do not change in each compilation, and distcc to distribute the compilation across several machines.

    0 讨论(0)
  • 2021-01-07 13:01

    I use a non-recursive build system based on GNU make and I was wondering how well it scales.

    I ran benchmarks on a 6-core Intel CPU with hyper-threading. I measured compile times using -j1 to -j20. For each -j option make ran three times and the shortest time was recorded. Using -j9 results in shortest compile time, 11% better than -j6.

    In other words, hyper-threading does help a little, and an optimal formula for Intel processors with hyper-threading is number_of_cores * 1.5:

    enter image description here

    Chart data is here.

    0 讨论(0)
  • 2021-01-07 13:09

    We have a machine in our shop with the following characteristics:

    • 256 core sparc solaris
    • ~64gb RAM
    • Some of that memory used for a ram drive for /tmp

    Back when it was originally setup, before other users discovered its existence, I ran some timing tests to see how far I could push it. The build in question is non-recursive, so all jobs are kicked off from a single make process. I also cloned my repo into /tmp to take advantage of the ram drive.

    I saw improvements up to -j56. Beyond that my results flat lined much like Maxim's graph, until somewhere above (roughly) -j75 where performance began to degrade. Running multiple parallel builds I could push it beyond the apparent cap of -j56.

    The primary make process is single-threaded; after running some tests I realized the ceiling I was hitting had to do with how many child processes the primary thread could service -- which was further hampered by anything in the makefiles that either required extra time to parse (eg., using = instead of := to avoid unnecessary delayed evaluation, complex user defined macros, etc) or used things like $(shell).

    These are the things I've been able to do to speed up builds that have a noticeable impact:

    Use := wherever possible

    If you assign to a variable once with :=, then later with +=, it'll continue to use immediate evaluation. However, ?= and +=, when a variable hasn't been assigned previously, will always delay evaluation.

    Delayed evaluation doesn't seem like a big deal until you have a large enough build. If a variable (like CFLAGS) doesn't change after all the makefiles have been parsed, then you probably don't want to use delayed evaluation on it (and if you do, you probably already know enough about what I'm talking about anyway to ignore my advice).

    If you create macros you execute with the $(call) facility, try to do as much of the evaluation ahead of time as possible

    I once got it in my head to create macros of the form:

    IFLINUX = $(strip $(if $(filter Linux,$(shell uname)),$(1),$(2)))
    IFCLANG = $(strip $(if $(filter-out undefined,$(origin CLANG_BUILD)),$(1),$(2)))
    ...
    # an example of how I might have made the worst use of it
    CXXFLAGS = ${whatever flags} $(call IFCLANG,-fsanitize=undefined)
    

    This build produces over 10,000 object files, about 8,000 of which are from C++ code. Had I used CXXFLAGS := (...), it would only need to immediately replace ${CXXFLAGS} in all of the compile steps with the already evaluated text. Instead it must re-evaluate the text of that variable once for each compile step.

    An alternative implementation that can at least help mitigate some of the re-evaluation if you have no choice:

    ifneq 'undefined' '$(origin CLANG_BUILD)'
    IFCLANG = $(strip $(1))
    else
    IFCLANG = $(strip $(2))
    endif
    

    ... though that only helps avoid the repeated $(origin) and $(if) calls; you'd still have to follow the advice about using := wherever possible.

    Where possible, avoid using custom macros inside recipes

    The reasoning should be pretty obvious here after the above; anything that requires a variable or macro to be repeatedly evaluated for every compile/link step will degrade your build speed. Every macro/variable evaluation occurs in the same thread as what kicks off new jobs, so any time spent parsing is time make delays kicking off another parallel job.

    I put some recipes in custom macros whenever it promotes code re-use and/or improves readability, but I try to keep it to a minimum.

    0 讨论(0)
提交回复
热议问题