Skip to content

Commit 51e46c7

Browse files
keesJonathan Corbet
authored andcommitted
docs, parallelism: Rearrange how jobserver reservations are made
Rasmus correctly observed that the existing jobserver reservation only worked if no other build targets were specified. The correct approach is to hold the jobserver slots until sphinx has finished. To fix this, the following changes are made: - refactor (and rename) scripts/jobserver-exec to set an environment variable for the maximally reserved jobserver slots and exec a child, to release the slots on exit. - create Documentation/scripts/parallel-wrapper.sh which examines both $PARALLELISM and the detected "-jauto" logic from Documentation/Makefile to decide sphinx's final -j argument. - chain these together in Documentation/Makefile Suggested-by: Rasmus Villemoes <[email protected]> Link: https://lore.kernel.org/lkml/[email protected] Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Corbet <[email protected]>
1 parent dffd011 commit 51e46c7

File tree

4 files changed

+101
-62
lines changed

4 files changed

+101
-62
lines changed

Documentation/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ ifeq ($(HAVE_SPHINX),0)
3333

3434
else # HAVE_SPHINX
3535

36-
export SPHINX_PARALLEL = $(shell perl -e 'open IN,"sphinx-build --version 2>&1 |"; while (<IN>) { if (m/([\d\.]+)/) { print "auto" if ($$1 >= "1.7") } ;} close IN')
37-
3836
# User-friendly check for pdflatex and latexmk
3937
HAVE_PDFLATEX := $(shell if which $(PDFLATEX) >/dev/null 2>&1; then echo 1; else echo 0; fi)
4038
HAVE_LATEXMK := $(shell if which latexmk >/dev/null 2>&1; then echo 1; else echo 0; fi)
@@ -67,8 +65,9 @@ quiet_cmd_sphinx = SPHINX $@ --> file://$(abspath $(BUILDDIR)/$3/$4)
6765
cmd_sphinx = $(MAKE) BUILDDIR=$(abspath $(BUILDDIR)) $(build)=Documentation/media $2 && \
6866
PYTHONDONTWRITEBYTECODE=1 \
6967
BUILDDIR=$(abspath $(BUILDDIR)) SPHINX_CONF=$(abspath $(srctree)/$(src)/$5/$(SPHINX_CONF)) \
68+
$(PYTHON) $(srctree)/scripts/jobserver-exec \
69+
$(SHELL) $(srctree)/Documentation/sphinx/parallel-wrapper.sh \
7070
$(SPHINXBUILD) \
71-
-j $(shell python $(srctree)/scripts/jobserver-count $(SPHINX_PARALLEL)) \
7271
-b $2 \
7372
-c $(abspath $(srctree)/$(src)) \
7473
-d $(abspath $(BUILDDIR)/.doctrees/$3) \
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0+
3+
#
4+
# Figure out if we should follow a specific parallelism from the make
5+
# environment (as exported by scripts/jobserver-exec), or fall back to
6+
# the "auto" parallelism when "-jN" is not specified at the top-level
7+
# "make" invocation.
8+
9+
sphinx="$1"
10+
shift || true
11+
12+
parallel="$PARALLELISM"
13+
if [ -z "$parallel" ] ; then
14+
# If no parallelism is specified at the top-level make, then
15+
# fall back to the expected "-jauto" mode that the "htmldocs"
16+
# target has had.
17+
auto=$(perl -e 'open IN,"'"$sphinx"' --version 2>&1 |";
18+
while (<IN>) {
19+
if (m/([\d\.]+)/) {
20+
print "auto" if ($1 >= "1.7")
21+
}
22+
}
23+
close IN')
24+
if [ -n "$auto" ] ; then
25+
parallel="$auto"
26+
fi
27+
fi
28+
# Only if some parallelism has been determined do we add the -jN option.
29+
if [ -n "$parallel" ] ; then
30+
parallel="-j$parallel"
31+
fi
32+
33+
exec "$sphinx" "$parallel" "$@"

scripts/jobserver-count

Lines changed: 0 additions & 59 deletions
This file was deleted.

scripts/jobserver-exec

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: GPL-2.0+
3+
#
4+
# This determines how many parallel tasks "make" is expecting, as it is
5+
# not exposed via an special variables, reserves them all, runs a subprocess
6+
# with PARALLELISM environment variable set, and releases the jobs back again.
7+
#
8+
# https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html#POSIX-Jobserver
9+
from __future__ import print_function
10+
import os, sys, errno
11+
import subprocess
12+
13+
# Extract and prepare jobserver file descriptors from envirnoment.
14+
claim = 0
15+
jobs = b""
16+
try:
17+
# Fetch the make environment options.
18+
flags = os.environ['MAKEFLAGS']
19+
20+
# Look for "--jobserver=R,W"
21+
# Note that GNU Make has used --jobserver-fds and --jobserver-auth
22+
# so this handles all of them.
23+
opts = [x for x in flags.split(" ") if x.startswith("--jobserver")]
24+
25+
# Parse out R,W file descriptor numbers and set them nonblocking.
26+
fds = opts[0].split("=", 1)[1]
27+
reader, writer = [int(x) for x in fds.split(",", 1)]
28+
# Open a private copy of reader to avoid setting nonblocking
29+
# on an unexpecting process with the same reader fd.
30+
reader = os.open("/proc/self/fd/%d" % (reader),
31+
os.O_RDONLY | os.O_NONBLOCK)
32+
33+
# Read out as many jobserver slots as possible.
34+
while True:
35+
try:
36+
slot = os.read(reader, 8)
37+
jobs += slot
38+
except (OSError, IOError) as e:
39+
if e.errno == errno.EWOULDBLOCK:
40+
# Stop at the end of the jobserver queue.
41+
break
42+
# If something went wrong, give back the jobs.
43+
if len(jobs):
44+
os.write(writer, jobs)
45+
raise e
46+
# Add a bump for our caller's reserveration, since we're just going
47+
# to sit here blocked on our child.
48+
claim = len(jobs) + 1
49+
except (KeyError, IndexError, ValueError, OSError, IOError) as e:
50+
# Any missing environment strings or bad fds should result in just
51+
# not being parallel.
52+
pass
53+
54+
# We can only claim parallelism if there was a jobserver (i.e. a top-level
55+
# "-jN" argument) and there were no other failures. Otherwise leave out the
56+
# environment variable and let the child figure out what is best.
57+
if claim > 0:
58+
os.environ['PARALLELISM'] = '%d' % (claim)
59+
60+
rc = subprocess.call(sys.argv[1:])
61+
62+
# Return all the reserved slots.
63+
if len(jobs):
64+
os.write(writer, jobs)
65+
66+
sys.exit(rc)

0 commit comments

Comments
 (0)