# Ninja build using too many cores

**URL:** <https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304>\
**Category:** Development\
**Created:** [March 13, 2018, 4:22pm UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304 "2018-03-13T16:22:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [March 13, 2018, 4:22pm UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304/1 "2018-03-13T16:22:13Z")

</div>

Is anyone else using ninja to build on mac? Lately I’m noticing that `ninja -j2` in `Slicer-build` runs 8 clang processes simultaneously, which floors the (2-core) computer and the build absolutely crawls (one `.o` file every 2-3 seconds). I expect this for some parallel projects during superbuild, because I don’t think we’re using job pool support. But within `Slicer-build` I thought it should only be building one project so cmake+ninja should respect `-j2`.

(CMake 3.9.4, macOS 10.12)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 13, 2018, 5:12pm UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304/2 "2018-03-13T17:12:26Z")

</div>

The way forward could be to:

- Use CMake 3.11 to benefit from this fix: [Ninja: add CMAKE\_JOB\_POOLS variable as default for JOBS\_POOLS](https://github.com/Kitware/CMake/commit/07185055d57f28347a1850a1f06787ac93f20afd)

- Improve [ExternalProjectDependency](https://github.com/commontk/Artichoke/blob/master/ExternalProjectDependency.cmake) (and update the version of the module user in Slicer and CTK) so that it pass the variables `CMAKE_JOB_POOLS`, `CMAKE_JOB_POOL_COMPILE` and `CMAKE_JOB_POOL_LINK` to any CMake based external project project if their are set.

You could then configure the top-level project using `-DCMAKE_JOB_POOLS:STRING=compile=5;link=2`.

This would mean that each instance of ninja would be limited to 5 compile jobs and 2 linker job.

Would that be helpful ? It may be worth exploring what would be sensible setup for Slicer.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [March 14, 2018, 3:52am UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304/3 "2018-03-14T03:52:03Z")

</div>

Do you know of other projects using job pools + externalproject? From my understanding, I don’t think it would help because each EP spawns a separate ninja instance, and ninja/cmake don’t have any way to communicate a rate-limit across sub-projects (from what I can tell job pool is designed for rate-limit when a single top-level ninja is in control). I guess fixing this at the superbuild level would need [job server support in ninja](https://github.com/ninja-build/ninja/issues/1139).

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 30, 2018, 5:09pm UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304/4 "2018-03-30T17:09:39Z")

</div>

Support for the `CMAKE_JOB_*` variables was added to Slicer in [r27117](http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&revision=27117)

That alone should help reduce the relative number of link job vs build job, considering that link job are require more memory, it is sensible to set a lower number.

Now, the good news is that I created a project to distribute the version of ninja with Jobserver support, see

> **[Releases · dockbuild/ninja-jobserver](https://github.com/dockbuild/ninja-jobserver/releases)**
>
> a small build system with a focus on speed \[with GNU make jobserver client support\] - dockbuild/ninja-jobserver

I already have binary for Linux available, the macOS and Windows still need to be generated.

Cc: @thewtex

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [March 30, 2018, 7:49pm UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304/5 "2018-03-30T19:49:04Z")

</div>

> [@jcfr](#):
>
> That alone should help reduce the relative number of link job vs build job

👍

> [@jcfr](#):
>
> ninja with Jobserver support

Did you happen to test if it works with the cmake job pool variable already?

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 30, 2018, 10:52pm UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304/6 "2018-03-30T22:52:14Z")

</div>

Look like it is working as expected.

- Install [CMake 3.11](https://cmake.org/download/)
- Install [ninja with job server support](https://github.com/dockbuild/ninja-jobserver/releases)
- Configure using:

```auto
~/Software/cmake-3.11.0-Linux-x86_64/bin/cmake \
  -GNinja \
  -DQt5_DIR:PATH=$Qt5_DIR \
  -DCMAKE_BUILD_TYPE:STRING=Release \
  '-DCMAKE_JOB_POOLS:STRING=compile=4;link=3' \
  -DCMAKE_JOB_POOL_COMPILE:STRING=compile \
  -DCMAKE_JOB_POOL_LINK:STRING=link \
../Slicer-Qt5-VTK9

```

Make sure you add the extra quote around `-DCMAKE_JOB_POOLS:STRING=compile=4;link=3`

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 30, 2018, 10:53pm UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304/7 "2018-03-30T22:53:42Z")

</div>

All of that should enable us to fixup CircleCI, and also release Slicer on our Linux server using [https://github.com/Slicer/SlicerBuildEnvironment/](https://github.com/Slicer/SlicerBuildEnvironment/)

---

<div class="post-metadata">

**Author:** ![phcerdan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/phcerdan/32/1559_2.png) [@phcerdan](https://discourse.slicer.org/u/phcerdan)\
**Post date:** [May 24, 2018, 9:36pm UTC](https://discourse.slicer.org/t/ninja-build-using-too-many-cores/2304/8 "2018-05-24T21:36:42Z")

</div>

As a related note, if you use Archlinux, I just uploaded [ninja-kitware](https://aur.archlinux.org/packages/ninja-kitware/) to the AUR. It is my second package ever, so please, report any issue if you see one.
