Skip to content

Commit f18b966

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 f18b966

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-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: 13 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)'
@@ -73,10 +73,11 @@ jobs:
7373
with:
7474
update: true
7575
install: mingw-w64-x86_64-toolchain make mingw-w64-x86_64-cmake mingw-w64-x86_64-ccache mingw-w64-x86_64-boost mingw-w64-x86_64-openssl mingw-w64-x86_64-zeromq mingw-w64-x86_64-libsodium mingw-w64-x86_64-hidapi mingw-w64-x86_64-protobuf mingw-w64-x86_64-libusb mingw-w64-x86_64-unbound git pkg-config
76+
- uses: ./.github/actions/set-make-job-count
7677
- name: build
7778
run: |
7879
${{env.CCACHE_SETTINGS}}
79-
make release-static-win64 -j4
80+
make release-static-win64 -j${{env.MAKE_JOB_COUNT}}
8081
8182
build-debian:
8283
name: 'Debian 10'
@@ -97,8 +98,9 @@ jobs:
9798
- uses: actions/checkout@v4
9899
with:
99100
submodules: recursive
101+
- uses: ./.github/actions/set-make-job-count
100102
- name: build
101-
run: ${{env.BUILD_DEFAULT_LINUX}}
103+
run: ${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
102104

103105
# See the OS labels and monitor deprecations here:
104106
# https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
@@ -125,6 +127,7 @@ jobs:
125127
path: ~/.ccache
126128
key: ccache-${{ runner.os }}-build-${{ matrix.os }}-${{ github.sha }}
127129
restore-keys: ccache-${{ runner.os }}-build-${{ matrix.os }}
130+
- uses: ./.github/actions/set-make-job-count
128131
- name: remove bundled packages
129132
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
130133
- name: set apt conf
@@ -136,7 +139,7 @@ jobs:
136139
- name: build
137140
run: |
138141
${{env.CCACHE_SETTINGS}}
139-
${{env.BUILD_DEFAULT_LINUX}}
142+
${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
140143
141144
libwallet-ubuntu:
142145
name: "Ubuntu 20.04 (libwallet)"
@@ -152,6 +155,7 @@ jobs:
152155
path: ~/.ccache
153156
key: ccache-${{ runner.os }}-libwallet-${{ github.sha }}
154157
restore-keys: ccache-${{ runner.os }}-libwallet-
158+
- uses: ./.github/actions/set-make-job-count
155159
- name: remove bundled packages
156160
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
157161
- name: set apt conf
@@ -164,7 +168,7 @@ jobs:
164168
run: |
165169
${{env.CCACHE_SETTINGS}}
166170
cmake .
167-
make wallet_api -j4
171+
make wallet_api -j${{env.MAKE_JOB_COUNT}}
168172
169173
test-ubuntu:
170174
name: "Ubuntu 20.04 (tests)"
@@ -182,6 +186,7 @@ jobs:
182186
path: ~/.ccache
183187
key: ccache-${{ runner.os }}-build-ubuntu-latest-${{ github.sha }}
184188
restore-keys: ccache-${{ runner.os }}-build-ubuntu-latest
189+
- uses: ./.github/actions/set-make-job-count
185190
- name: remove bundled packages
186191
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
187192
- name: set apt conf
@@ -198,7 +203,7 @@ jobs:
198203
DNS_PUBLIC: tcp://9.9.9.9
199204
run: |
200205
${{env.CCACHE_SETTINGS}}
201-
${{env.BUILD_DEFAULT_LINUX}}
206+
${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
202207
cmake --build build --target test
203208
204209
# 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)