Skip to content

Commit 07774e6

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 2e8a128 commit 07774e6

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=$(printf '%s\n%s' $(( $(sysctl -n hw.memsize) * 4 / (1073741824 * 9) )) $(sysctl -n hw.logicalcpu) | sort -n | head -n1 | sed -e 's/^0$/1/') >> $GITHUB_ENV
12+
shell: bash
13+
- name: set-jobs-windows
14+
if: runner.os == 'Windows'
15+
run: |
16+
echo MAKE_JOB_COUNT=$(printf '%s\n%s' $(( $(grep MemTotal: /proc/meminfo | cut -d: -f2 | cut -dk -f1) * 4 / (1048576 * 9) )) $(nproc) | sort -n | head -n1 | sed -e 's/^0$/1/') >> $GITHUB_ENV
17+
shell: msys2 {0}
18+
- name: set-jobs-linux
19+
if: runner.os == 'Linux'
20+
run: |
21+
echo MAKE_JOB_COUNT=$(printf '%s\n%s' $(( $(vmstat -s --unit M | grep 'total memory' | cut -dM -f1) * 4 / (1024 * 9) )) $(nproc) | sort -n | head -n1 | sed -e 's/^0$/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
@@ -41,14 +40,15 @@ jobs:
4140
path: /Users/runner/Library/Caches/ccache
4241
key: ccache-${{ runner.os }}-build-${{ github.sha }}
4342
restore-keys: ccache-${{ runner.os }}-build-
43+
- uses: ./.github/actions/set-make-job-count
4444
- name: install dependencies
4545
run: |
4646
HOMEBREW_NO_AUTO_UPDATE=1 brew install boost@1.85 hidapi openssl zmq miniupnpc expat libunwind-headers protobuf@21 ccache
4747
brew link protobuf@21 boost@1.85
4848
- name: build
4949
run: |
5050
${{env.CCACHE_SETTINGS}}
51-
make -j3
51+
make -j${{env.MAKE_JOB_COUNT}}
5252
5353
build-windows:
5454
runs-on: windows-latest
@@ -78,10 +78,11 @@ jobs:
7878
curl -O https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-libs-1.86.0-7-any.pkg.tar.zst
7979
echo "4cb1d1066fffa6a5788b212ccb920c6d8cc93a8ecbbc633565bfc9b2ebc6feb5 mingw-w64-x86_64-boost-libs-1.86.0-7-any.pkg.tar.zst" | sha256sum -c
8080
pacman --noconfirm -U mingw-w64-x86_64-boost-1.86.0-7-any.pkg.tar.zst mingw-w64-x86_64-boost-libs-1.86.0-7-any.pkg.tar.zst
81+
- uses: ./.github/actions/set-make-job-count
8182
- name: build
8283
run: |
8384
${{env.CCACHE_SETTINGS}}
84-
make release-static-win64 -j4
85+
make release-static-win64 -j${{env.MAKE_JOB_COUNT}}
8586
8687
build-debian:
8788
name: 'build-debian (debian-10)'
@@ -102,8 +103,9 @@ jobs:
102103
- uses: actions/checkout@v4
103104
with:
104105
submodules: recursive
106+
- uses: ./.github/actions/set-make-job-count
105107
- name: build
106-
run: ${{env.BUILD_DEFAULT_LINUX}}
108+
run: ${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
107109

108110
# See the OS labels and monitor deprecations here:
109111
# 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
runs-on: ubuntu-20.04
@@ -151,6 +154,7 @@ jobs:
151154
path: ~/.ccache
152155
key: ccache-${{ runner.os }}-libwallet-${{ github.sha }}
153156
restore-keys: ccache-${{ runner.os }}-libwallet-
157+
- uses: ./.github/actions/set-make-job-count
154158
- name: remove bundled packages
155159
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
156160
- name: set apt conf
@@ -163,7 +167,7 @@ jobs:
163167
run: |
164168
${{env.CCACHE_SETTINGS}}
165169
cmake .
166-
make wallet_api -j4
170+
make wallet_api -j${{env.MAKE_JOB_COUNT}}
167171
168172
test-ubuntu:
169173
needs: build-ubuntu
@@ -180,6 +184,7 @@ jobs:
180184
path: ~/.ccache
181185
key: ccache-${{ runner.os }}-build-ubuntu-latest-${{ github.sha }}
182186
restore-keys: ccache-${{ runner.os }}-build-ubuntu-latest
187+
- uses: ./.github/actions/set-make-job-count
183188
- name: remove bundled packages
184189
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
185190
- name: set apt conf
@@ -196,7 +201,7 @@ jobs:
196201
DNS_PUBLIC: tcp://9.9.9.9
197202
run: |
198203
${{env.CCACHE_SETTINGS}}
199-
${{env.BUILD_DEFAULT_LINUX}}
204+
${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
200205
cmake --build build --target test
201206
202207
# 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
@@ -80,6 +80,7 @@ jobs:
8080
restore-keys: |
8181
depends-${{ matrix.toolchain.host }}-${{ hashFiles('contrib/depends/packages/*') }}
8282
depends-${{ matrix.toolchain.host }}-
83+
- uses: ./.github/actions/set-make-job-count
8384
- name: set apt conf
8485
run: ${{env.APT_SET_CONF}}
8586
- name: install dependencies
@@ -92,7 +93,7 @@ jobs:
9293
- name: build
9394
run: |
9495
${{env.CCACHE_SETTINGS}}
95-
make depends target=${{ matrix.toolchain.host }} -j4
96+
make depends target=${{ matrix.toolchain.host }} -j${{env.MAKE_JOB_COUNT}}
9697
- uses: actions/upload-artifact@v4
9798
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' }}
9899
with:

0 commit comments

Comments
 (0)