Build 为什么我要通过make-j作为论点?(而不是将其留空)

Build 为什么我要通过make-j作为论点?(而不是将其留空),build,compilation,makefile,Build,Compilation,Makefile,我已经看到了很多关于当你跑步时,什么是一个好的X值的讨论 make -j X 通常,人们认为X应该是系统内核数量的函数。在我的项目中,我发现通过省略X并简单地运行 make -j 如果您不想为其他流程保留资源,而只是想要最快的构建,是否有理由修复X?对于您的项目,使用无参数的-j可能是最佳解决方案。如果可以并行运行的作业数量相对较少,那么就可以了 然而,资源不是无限的。单独使用-j告诉make它应该运行所有可能构建的作业,而不考虑系统资源。它不看你有多少CPU,你有多少内存,你的系统负载有多

我已经看到了很多关于当你跑步时,什么是一个好的X值的讨论

make -j X
通常,人们认为X应该是系统内核数量的函数。在我的项目中,我发现通过省略X并简单地运行

make -j

如果您不想为其他流程保留资源,而只是想要最快的构建,是否有理由修复X?

对于您的项目,使用无参数的
-j
可能是最佳解决方案。如果可以并行运行的作业数量相对较少,那么就可以了

然而,资源不是无限的。单独使用
-j
告诉make它应该运行所有可能构建的作业,而不考虑系统资源。它不看你有多少CPU,你有多少内存,你的系统负载有多高,或者其他任何东西

因此,如果您有一个非递归的构建系统,并且/或者包含数百或数千个可以并行构建的文件(彼此不依赖),make将尝试同时运行它们。正如当你试图在你的系统中同时做太多的事情时,它会减慢速度,最终花费的时间比一次做几件事情要长,所以运行太多的作业会让你的系统崩溃


以使用
-j
构建Linux内核为例,看看它是如何为您工作的:-)。

更新:将使用指定的'-ln'标志工作,请参阅负载平均标志

 { 'l', floating, &max_load_average, 1, 1, 0, &default_load_average,
      &default_load_average, "load-average" },
看起来make尽量不消耗太多资源,请参见

要加载的注释太高()

/*确定系统上的平均负载是否过高,无法启动新作业。
实际系统平均负载每秒只重新计算一次。但是,
非常并行的make可以在一个小时内轻松启动数十个甚至数百个工作
第二,这会让系统在一段时间内屈服,直到第一次
一批作业被清除。
为了避免这种情况,我们使用加权算法来尝试考虑
已经从最后一秒开始启动,猜猜平均负载是多少
如果计算出来的话,现在就是了。
该算法由Thomas Riedl提供,
他写道:
!      计算负载oid并将其添加到观察到的系统负载中,
!      以便后者能够迎头赶上:
!      - 每开始一项工作都会增加作业中心;
!      - 每一个死亡的工作都会降低一个正的工作中心;
!      - jobctr值每秒钟更改一次就归零,
!        在其值*weight_b存储到'backlog'值last_sec之后
!      - 权重a乘以作业中心和最后一秒的总和
!        添加到观察到的sys.load。
!
!      这两种砝码已在24和48 proc上试用过。Sun Solaris-9
!      机器,使用cpp、cc、cxx和smallish的数千个作业组合
!      用于测试的子外壳命令(rm、echo、sed…)。
!      降低“直接影响”因素权重_a(例如,降至0.1)
!      导致显著超出负荷极限,提高负荷极限
!      (例如到0.5)将坏的作业变为小的、快速执行的作业,但没有
!      在大多数测试用例中达到极限。
!
!      降低“历史影响”权重_b(例如,降低到0.1)会导致
!      超过长时间运行的内容的限制(在
!0.5到1.5秒的范围),将其提高(例如提高到0.5秒)的比例过高
!      小工作的影响。
*/
#定义荷载重量为0.25
#定义荷载重量0.25
此外,正如人们所看到的,Windows上的作业计数仅限于最大的等待对象,即64。

只有在提供
-l
标志以设置最大负载级别时,才会执行“加载过高”检查。事实上,在没有参数的情况下使用
-j
时,通常情况下您也希望使用
-l
,但这不是问题的所在。
  /* If we are running at least one job already and the load average
     is too high, make this one wait.  */
  if (!c->remote
      && ((job_slots_used > 0 && load_too_high ())
#ifdef WINDOWS32
          || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
#endif
          ))
    {
      /* Put this child on the chain of children waiting for the load average
         to go down.  */
      set_command_state (f, cs_running);
      c->next = waiting_jobs;
      waiting_jobs = c;
      return 0;
    }
/* Determine if the load average on the system is too high to start a new job.
   The real system load average is only recomputed once a second.  However, a
   very parallel make can easily start tens or even hundreds of jobs in a
   second, which brings the system to its knees for a while until that first
   batch of jobs clears out.

   To avoid this we use a weighted algorithm to try to account for jobs which
   have been started since the last second, and guess what the load average
   would be now if it were computed.

   This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
   who writes:

!      calculate something load-oid and add to the observed sys.load,
!      so that latter can catch up:
!      - every job started increases jobctr;
!      - every dying job decreases a positive jobctr;
!      - the jobctr value gets zeroed every change of seconds,
!        after its value*weight_b is stored into the 'backlog' value last_sec
!      - weight_a times the sum of jobctr and last_sec gets
!        added to the observed sys.load.
!
!      The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
!      machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
!      sub-shelled commands (rm, echo, sed...) for tests.
!      lowering the 'direct influence' factor weight_a (e.g. to 0.1)
!      resulted in significant excession of the load limit, raising it
!      (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
!      reach the limit in most test cases.
!
!      lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
!      exceeding the limit for longer-running stuff (compile jobs in
!      the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
!      small jobs' effects.

 */

#define LOAD_WEIGHT_A           0.25
#define LOAD_WEIGHT_B           0.25