Skip to content

Commit 1ce5d3a

Browse files
author
iamamyth
committed
build: auto-set GitHub runner make job count
Dynamically determine an appropriate number of make jobs for the GitHub runner container, based upon the number of available CPU cores and RAM, using the rule that each job requires a dedicated core and 2.25GiB of RAM.
1 parent 1179269 commit 1ce5d3a

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: 'set-make-job-count'
2+
description: 'Set the MAKE_JOB_COUNT environment variable to a value suitable for the host runner'
3+
runs:
4+
using: "composite"
5+
steps:
6+
# Each job runner requires 2.25 GiB (i.e. 1024 * 9/4 MiB) memory and
7+
# a dedicated logical CPU core
8+
- name: set-jobs-macOS
9+
if: runner.os == 'macOS'
10+
run: |
11+
echo MAKE_JOB_COUNT=$(expr $(printf '%s\n%s' $(( $(sysctl -n hw.memsize) * 4 / (1073741824 * 9) )) $(sysctl -n hw.logicalcpu) | sort -n | head -n1) '|' 1) >> $GITHUB_ENV
12+
shell: bash
13+
- name: set-jobs-windows
14+
if: runner.os == 'Windows'
15+
run: |
16+
echo MAKE_JOB_COUNT=$(expr $(printf '%s\n%s' $(( $(grep MemTotal: /proc/meminfo | cut -d: -f2 | cut -dk -f1) * 4 / (1048576 * 9) )) $(nproc) | sort -n | head -n1) '|' 1) >> $GITHUB_ENV
17+
shell: msys2 {0}
18+
- name: set-jobs-linux
19+
if: runner.os == 'Linux'
20+
run: |
21+
echo MAKE_JOB_COUNT=$(expr $(printf '%s\n%s' $(( $(grep MemTotal: /proc/meminfo | cut -d: -f2 | cut -dk -f1) * 4 / (1048576 * 9) )) $(nproc) | sort -n | head -n1) '|' 1) >> $GITHUB_ENV
22+
shell: bash

.github/workflows/build.yml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ on:
1313
# The below variables reduce repetitions across similar targets
1414
env:
1515
REMOVE_BUNDLED_PACKAGES : sudo rm -rf /usr/local
16-
BUILD_DEFAULT_LINUX: |
17-
cmake -S . -B build -D ARCH="default" -D BUILD_TESTS=ON -D CMAKE_BUILD_TYPE=Release && cmake --build build --parallel 4
16+
BUILD_DEFAULT_LINUX: 'cmake -S . -B build -D ARCH="default" -D BUILD_TESTS=ON -D CMAKE_BUILD_TYPE=Release && cmake --build build'
1817
APT_INSTALL_LINUX: 'apt -y install build-essential cmake libboost-all-dev miniupnpc libunbound-dev graphviz doxygen libunwind8-dev pkg-config libssl-dev libzmq3-dev libsodium-dev libhidapi-dev libusb-1.0-0-dev libprotobuf-dev protobuf-compiler ccache git'
1918
APT_SET_CONF: |
2019
tee -a /etc/apt/apt.conf.d/80-custom << EOF
@@ -42,14 +41,15 @@ jobs:
4241
path: /Users/runner/Library/Caches/ccache
4342
key: ccache-${{ runner.os }}-build-${{ github.sha }}
4443
restore-keys: ccache-${{ runner.os }}-build-
44+
- uses: ./.github/actions/set-make-job-count
4545
- name: install dependencies
4646
run: |
4747
HOMEBREW_NO_AUTO_UPDATE=1 brew install boost hidapi openssl zmq miniupnpc expat libunwind-headers protobuf@21 ccache
4848
brew link protobuf@21 boost
4949
- name: build
5050
run: |
5151
${{env.CCACHE_SETTINGS}}
52-
make -j3
52+
make -j${{env.MAKE_JOB_COUNT}}
5353
5454
build-windows:
5555
name: 'Windows (MSYS2)'
@@ -76,7 +76,7 @@ jobs:
7676
- name: build
7777
run: |
7878
${{env.CCACHE_SETTINGS}}
79-
make release-static-win64 -j4
79+
make release-static-win64 -j${{env.MAKE_JOB_COUNT}}
8080
8181
build-debian:
8282
name: 'Debian 10'
@@ -97,8 +97,9 @@ jobs:
9797
- uses: actions/checkout@v4
9898
with:
9999
submodules: recursive
100+
- uses: ./.github/actions/set-make-job-count
100101
- name: build
101-
run: ${{env.BUILD_DEFAULT_LINUX}}
102+
run: ${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
102103

103104
# See the OS labels and monitor deprecations here:
104105
# https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
@@ -125,6 +126,7 @@ jobs:
125126
path: ~/.ccache
126127
key: ccache-${{ runner.os }}-build-${{ matrix.os }}-${{ github.sha }}
127128
restore-keys: ccache-${{ runner.os }}-build-${{ matrix.os }}
129+
- uses: ./.github/actions/set-make-job-count
128130
- name: remove bundled packages
129131
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
130132
- name: set apt conf
@@ -136,7 +138,7 @@ jobs:
136138
- name: build
137139
run: |
138140
${{env.CCACHE_SETTINGS}}
139-
${{env.BUILD_DEFAULT_LINUX}}
141+
${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
140142
141143
libwallet-ubuntu:
142144
name: "Ubuntu 20.04 (libwallet)"
@@ -152,6 +154,7 @@ jobs:
152154
path: ~/.ccache
153155
key: ccache-${{ runner.os }}-libwallet-${{ github.sha }}
154156
restore-keys: ccache-${{ runner.os }}-libwallet-
157+
- uses: ./.github/actions/set-make-job-count
155158
- name: remove bundled packages
156159
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
157160
- name: set apt conf
@@ -164,7 +167,7 @@ jobs:
164167
run: |
165168
${{env.CCACHE_SETTINGS}}
166169
cmake .
167-
make wallet_api -j4
170+
make wallet_api -j${{env.MAKE_JOB_COUNT}}
168171
169172
test-ubuntu:
170173
name: "Ubuntu 20.04 (tests)"
@@ -182,6 +185,7 @@ jobs:
182185
path: ~/.ccache
183186
key: ccache-${{ runner.os }}-build-ubuntu-latest-${{ github.sha }}
184187
restore-keys: ccache-${{ runner.os }}-build-ubuntu-latest
188+
- uses: ./.github/actions/set-make-job-count
185189
- name: remove bundled packages
186190
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
187191
- name: set apt conf
@@ -198,7 +202,7 @@ jobs:
198202
DNS_PUBLIC: tcp://9.9.9.9
199203
run: |
200204
${{env.CCACHE_SETTINGS}}
201-
${{env.BUILD_DEFAULT_LINUX}}
205+
${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
202206
cmake --build build --target test
203207
204208
# ARCH="default" (not "native") ensures, that a different execution host can execute binaries compiled elsewhere.

.github/workflows/depends.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ jobs:
7979
restore-keys: |
8080
depends-${{ matrix.toolchain.host }}-${{ hashFiles('contrib/depends/packages/*') }}
8181
depends-${{ matrix.toolchain.host }}-
82+
- uses: ./.github/actions/set-make-job-count
8283
- name: set apt conf
8384
run: ${{env.APT_SET_CONF}}
8485
- name: install dependencies
@@ -91,7 +92,7 @@ jobs:
9192
- name: build
9293
run: |
9394
${{env.CCACHE_SETTINGS}}
94-
make depends target=${{ matrix.toolchain.host }} -j4
95+
make depends target=${{ matrix.toolchain.host }} -j${{env.MAKE_JOB_COUNT}}
9596
- uses: actions/upload-artifact@v4
9697
if: ${{ matrix.toolchain.host == 'x86_64-w64-mingw32' || matrix.toolchain.host == 'x86_64-apple-darwin' || matrix.toolchain.host == 'aarch64-apple-darwin' || matrix.toolchain.host == 'x86_64-unknown-linux-gnu' }}
9798
with:

0 commit comments

Comments
 (0)