Skip to content

Commit 8dc82cf

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 8dc82cf

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on:
1414
env:
1515
REMOVE_BUNDLED_PACKAGES : sudo rm -rf /usr/local
1616
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
17+
cmake -S . -B build -D ARCH="default" -D BUILD_TESTS=ON -D CMAKE_BUILD_TYPE=Release && cmake --build build
1818
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'
1919
APT_SET_CONF: |
2020
tee -a /etc/apt/apt.conf.d/80-custom << EOF
@@ -41,14 +41,15 @@ jobs:
4141
path: /Users/runner/Library/Caches/ccache
4242
key: ccache-${{ runner.os }}-build-${{ github.sha }}
4343
restore-keys: ccache-${{ runner.os }}-build-
44+
- uses: ./.github/actions/set-make-job-count
4445
- name: install dependencies
4546
run: |
4647
HOMEBREW_NO_AUTO_UPDATE=1 brew install boost@1.85 hidapi openssl zmq miniupnpc expat libunwind-headers protobuf@21 ccache
4748
brew link protobuf@21 boost@1.85
4849
- name: build
4950
run: |
5051
${{env.CCACHE_SETTINGS}}
51-
make -j3
52+
make -j${{env.MAKE_JOB_COUNT}}
5253
5354
build-windows:
5455
runs-on: windows-latest
@@ -78,10 +79,11 @@ jobs:
7879
curl -O https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-libs-1.86.0-7-any.pkg.tar.zst
7980
echo "4cb1d1066fffa6a5788b212ccb920c6d8cc93a8ecbbc633565bfc9b2ebc6feb5 mingw-w64-x86_64-boost-libs-1.86.0-7-any.pkg.tar.zst" | sha256sum -c
8081
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
82+
- uses: ./.github/actions/set-make-job-count
8183
- name: build
8284
run: |
8385
${{env.CCACHE_SETTINGS}}
84-
make release-static-win64 -j4
86+
make release-static-win64 -j${{env.MAKE_JOB_COUNT}}
8587
8688
build-debian:
8789
name: 'build-debian (debian-10)'
@@ -102,8 +104,9 @@ jobs:
102104
- uses: actions/checkout@v4
103105
with:
104106
submodules: recursive
107+
- uses: ./.github/actions/set-make-job-count
105108
- name: build
106-
run: ${{env.BUILD_DEFAULT_LINUX}}
109+
run: ${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
107110

108111
# See the OS labels and monitor deprecations here:
109112
# https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
@@ -125,6 +128,7 @@ jobs:
125128
path: ~/.ccache
126129
key: ccache-${{ runner.os }}-build-${{ matrix.os }}-${{ github.sha }}
127130
restore-keys: ccache-${{ runner.os }}-build-${{ matrix.os }}
131+
- uses: ./.github/actions/set-make-job-count
128132
- name: remove bundled packages
129133
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
130134
- name: set apt conf
@@ -136,7 +140,7 @@ jobs:
136140
- name: build
137141
run: |
138142
${{env.CCACHE_SETTINGS}}
139-
${{env.BUILD_DEFAULT_LINUX}}
143+
${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
140144
141145
libwallet-ubuntu:
142146
runs-on: ubuntu-20.04
@@ -151,6 +155,7 @@ jobs:
151155
path: ~/.ccache
152156
key: ccache-${{ runner.os }}-libwallet-${{ github.sha }}
153157
restore-keys: ccache-${{ runner.os }}-libwallet-
158+
- uses: ./.github/actions/set-make-job-count
154159
- name: remove bundled packages
155160
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
156161
- name: set apt conf
@@ -163,7 +168,7 @@ jobs:
163168
run: |
164169
${{env.CCACHE_SETTINGS}}
165170
cmake .
166-
make wallet_api -j4
171+
make wallet_api -j${{env.MAKE_JOB_COUNT}}
167172
168173
test-ubuntu:
169174
needs: build-ubuntu
@@ -180,6 +185,7 @@ jobs:
180185
path: ~/.ccache
181186
key: ccache-${{ runner.os }}-build-ubuntu-latest-${{ github.sha }}
182187
restore-keys: ccache-${{ runner.os }}-build-ubuntu-latest
188+
- uses: ./.github/actions/set-make-job-count
183189
- name: remove bundled packages
184190
run: ${{env.REMOVE_BUNDLED_PACKAGES}}
185191
- name: set apt conf
@@ -196,7 +202,7 @@ jobs:
196202
DNS_PUBLIC: tcp://9.9.9.9
197203
run: |
198204
${{env.CCACHE_SETTINGS}}
199-
${{env.BUILD_DEFAULT_LINUX}}
205+
${{env.BUILD_DEFAULT_LINUX}} --parallel ${{env.MAKE_JOB_COUNT}}
200206
cmake --build build --target test
201207
202208
# 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)