Skip to content

Commit d5a71e9

Browse files
committed
guix: Use --cores instead of --max-jobs
In Guix, there are two flags for controlling parallelism: Note: When I say "derivation," think "package" --cores=n - controls the number of CPU cores to build each derivation. This is the value passed to `make`'s `--jobs=` flag. - defaults to 0: as many cores as is available --max-jobs=n - controls how many derivations can be built in parallel - defaults to 1 Therefore, if set --max-jobs=$MAX_JOBS and don't set --cores, Guix could theoretically spin up $MAX_JOBS * $(nproc) number of threads, and that's no good. So we could either default to --cores=1, --max-jobs=$MAX_JOBS - Pro: --cores=1 means that `make` will be invoked with `-j1`, avoiding problems with package whose build systems and test suites break when running multi-threaded. - Con: There will be times when only 1 or 2 derivations can be built at a time, because the rest of the dependency graph all depend on those 1 or 2 derivations. During these times, the machine will be severely under-utilized. or --cores=$MAX_JOBS, --max-jobs=1 - Pro: We don't encounter prolonged periods of severe under-utilization mentioned above. - Con: Many packages' build systems and test suites break when running multi-threaded. or --cores=1, --max-jobs=1 and let the user override with $ADDITIONAL_GUIX_COMMON_FLAGS
1 parent 66daf4c commit d5a71e9

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

contrib/guix/README.md

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,50 @@ at the end of the `guix pull`)
8080
export PATH="${HOME}/.config/guix/current/bin${PATH:+:}$PATH"
8181
```
8282

83+
### Controlling the number of threads used by `guix` build commands
84+
85+
By default, the scripts under `./contrib/guix` will invoke all `guix` build
86+
commands with `--cores="$JOBS"`. Note that `$JOBS` defaults to `$(nproc)` if not
87+
specified. However, astute manual readers will also notice that there is a
88+
`--max-jobs=` flag (which defaults to 1 if unspecified).
89+
90+
Here is the difference between `--cores=` and `--max-jobs=`:
91+
92+
> Note: When I say "derivation," think "package"
93+
94+
`--cores=`
95+
96+
- controls the number of CPU cores to build each derivation. This is the value
97+
passed to `make`'s `--jobs=` flag.
98+
99+
`--max-jobs=`
100+
101+
- controls how many derivations can be built in parallel
102+
- defaults to 1
103+
104+
Therefore, the default is for `guix` build commands to build one derivation at a
105+
time, utilizing `$JOBS` threads.
106+
107+
Specifying the `$JOBS` environment variable will only modify `--cores=`, but you
108+
can also modify the value for `--max-jobs=` by specifying
109+
`$ADDITIONAL_GUIX_COMMON_FLAGS`. For example, if you have a LOT of memory, you
110+
may want to set:
111+
112+
```sh
113+
export ADDITIONAL_GUIX_COMMON_FLAGS='--max-jobs=8'
114+
```
115+
116+
Which allows for a maximum of 8 derivations to be built at the same time, each
117+
utilizing `$JOBS` threads.
118+
119+
Or, if you'd like to avoid spurious build failures caused by issues with
120+
parallelism within a single package, but would still like to build multiple
121+
packages when the dependency graph allows for it, you may want to try:
122+
123+
```sh
124+
export JOBS=1 ADDITIONAL_GUIX_COMMON_FLAGS='--max-jobs=8'
125+
```
126+
83127
## Usage
84128

85129
### As a Tool for Deterministic Builds
@@ -125,12 +169,16 @@ find output/ -type f -print0 | sort -z | xargs -r0 sha256sum
125169
the actual SDK (e.g. SDK_PATH=$HOME/Downloads/macOS-SDKs instead of
126170
$HOME/Downloads/macOS-SDKs/Xcode-11.3.1-11C505-extracted-SDK-with-libcxx-headers).
127171

128-
* _**MAX_JOBS**_
172+
* _**JOBS**_
173+
174+
Override the number of jobs to run simultaneously, you might want to do so on
175+
a memory-limited machine. This may be passed to:
176+
177+
- `guix` build commands as in `guix environment --cores="$JOBS"`
178+
- `make` as in `make --jobs="$JOBS"`
179+
- `xargs` as in `xargs -P"$JOBS"`
129180

130-
Override the maximum number of jobs to run simultaneously, you might want to
131-
do so on a memory-limited machine. This may be passed to `make` as in `make
132-
--jobs="$MAX_JOBS"` or `xargs` as in `xargs -P"$MAX_JOBS"`. _(defaults to the
133-
value of `nproc` outside the container)_
181+
_(defaults to the value of `nproc` outside the container)_
134182

135183
* _**SOURCE_DATE_EPOCH**_
136184

contrib/guix/guix-build.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ done
134134

135135
# Determine the maximum number of jobs to run simultaneously (overridable by
136136
# environment)
137-
MAX_JOBS="${MAX_JOBS:-$(nproc)}"
137+
JOBS="${JOBS:-$(nproc)}"
138138

139139
# Usage: host_to_commonname HOST
140140
#
@@ -152,7 +152,7 @@ host_to_commonname() {
152152
# Download the depends sources now as we won't have internet access in the build
153153
# container
154154
for host in $HOSTS; do
155-
make -C "${PWD}/depends" -j"$MAX_JOBS" download-"$(host_to_commonname "$host")" ${V:+V=1} ${SOURCES_PATH:+SOURCES_PATH="$SOURCES_PATH"}
155+
make -C "${PWD}/depends" -j"$JOBS" download-"$(host_to_commonname "$host")" ${V:+V=1} ${SOURCES_PATH:+SOURCES_PATH="$SOURCES_PATH"}
156156
done
157157

158158
# Determine the reference time used for determinism (overridable by environment)
@@ -164,7 +164,7 @@ time-machine() {
164164
# shellcheck disable=SC2086
165165
guix time-machine --url=https://github.com/dongcarl/guix.git \
166166
--commit=490e39ff303f4f6873a04bfb8253755bdae1b29c \
167-
--max-jobs="$MAX_JOBS" \
167+
--cores="$JOBS" \
168168
--keep-failed \
169169
${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
170170
${ADDITIONAL_GUIX_COMMON_FLAGS} ${ADDITIONAL_GUIX_TIMEMACHINE_FLAGS} \
@@ -218,7 +218,7 @@ for host in $HOSTS; do
218218
cat << EOF
219219
INFO: Building commit ${GIT_COMMIT:?not set} for platform triple ${HOST:?not set}:
220220
...using reference timestamp: ${SOURCE_DATE_EPOCH:?not set}
221-
...running at most ${MAX_JOBS:?not set} jobs
221+
...running at most ${JOBS:?not set} jobs
222222
...from worktree directory: '${PWD}'
223223
...bind-mounted in container to: '/bitcoin'
224224
...in build directory: '$(distsrc_for_host "$HOST")'
@@ -304,12 +304,12 @@ EOF
304304
${SOURCES_PATH:+--share="$SOURCES_PATH"} \
305305
${BASE_CACHE:+--share="$BASE_CACHE"} \
306306
${SDK_PATH:+--share="$SDK_PATH"} \
307-
--max-jobs="$MAX_JOBS" \
307+
--cores="$JOBS" \
308308
--keep-failed \
309309
${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
310310
${ADDITIONAL_GUIX_COMMON_FLAGS} ${ADDITIONAL_GUIX_ENVIRONMENT_FLAGS} \
311311
-- env HOST="$host" \
312-
MAX_JOBS="$MAX_JOBS" \
312+
JOBS="$JOBS" \
313313
SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:?unable to determine value}" \
314314
${V:+V=1} \
315315
${SOURCES_PATH:+SOURCES_PATH="$SOURCES_PATH"} \

contrib/guix/libexec/build.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ cat << EOF
2626
Required environment variables as seen inside the container:
2727
HOST: ${HOST:?not set}
2828
SOURCE_DATE_EPOCH: ${SOURCE_DATE_EPOCH:?not set}
29-
MAX_JOBS: ${MAX_JOBS:?not set}
29+
JOBS: ${JOBS:?not set}
3030
DISTSRC: ${DISTSRC:?not set}
3131
OUTDIR: ${OUTDIR:?not set}
3232
EOF
@@ -173,7 +173,7 @@ esac
173173
####################
174174

175175
# Build the depends tree, overriding variables that assume multilib gcc
176-
make -C depends --jobs="$MAX_JOBS" HOST="$HOST" \
176+
make -C depends --jobs="$JOBS" HOST="$HOST" \
177177
${V:+V=1} \
178178
${SOURCES_PATH+SOURCES_PATH="$SOURCES_PATH"} \
179179
${BASE_CACHE+BASE_CACHE="$BASE_CACHE"} \
@@ -267,7 +267,7 @@ mkdir -p "$DISTSRC"
267267
sed -i.old 's/-lstdc++ //g' config.status libtool src/univalue/config.status src/univalue/libtool
268268

269269
# Build Bitcoin Core
270-
make --jobs="$MAX_JOBS" ${V:+V=1}
270+
make --jobs="$JOBS" ${V:+V=1}
271271

272272
# Perform basic ELF security checks on a series of executables.
273273
make -C src --jobs=1 check-security ${V:+V=1}
@@ -344,7 +344,7 @@ mkdir -p "$DISTSRC"
344344
{
345345
find "${DISTNAME}/bin" -type f -executable -print0
346346
find "${DISTNAME}/lib" -type f -print0
347-
} | xargs -0 -n1 -P"$MAX_JOBS" -I{} "${DISTSRC}/contrib/devtools/split-debug.sh" {} {} {}.dbg
347+
} | xargs -0 -n1 -P"$JOBS" -I{} "${DISTSRC}/contrib/devtools/split-debug.sh" {} {} {}.dbg
348348
;;
349349
esac
350350

0 commit comments

Comments
 (0)