Skip to content

Commit 0fd71e4

Browse files
fatherlinuxclaude
andauthored
fix: v0.3.2 CI gets the whole budget; halving it made lanes slower than no lanes (#7)
Measured on trentina, the `sdlc ci` step: v0.2.2, sequential, 2 CPUs 15m27s v0.3.0, two lanes, 1 CPU each 24m38s A 59% regression, and dividing the budget was not the mistake -- the budget is for the whole gate, so dividing it is right. The mistake is that `budget()` halves the machine on CI. It halves because a gate must not take the desktop away from the person running it; a GitHub runner has no desktop. That left a budget of 2 on a 4-vCPU runner, 1 CPU per lane, and both lanes are CPU-bound: main went 216s -> 416s, the image chain 699s -> 1457s. The image chain alone then exceeded the whole sequential job, so no amount of overlap could win. The budget is no longer halved where nobody is at a keyboard: CI is set, or a cpuset, a CPU affinity or a cgroup quota already narrows the machine, which is someone having already decided this process's share -- halving that again double-counts the same protection. A laptop is unchanged: still half, still divided between the lanes. The cap detection in the first attempt compared the allowance against `nproc`, which a cpuset has already narrowed, so it found nothing and silently halved anyway. It compares against the installed count now. The host contract test covers all three outcomes, including the cpuset case that first version missed. Claude-Session: https://claude.ai/code/session_01R7Y1jesjfgxHeqtKJCvay6 Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent a4281cf commit 0fd71e4

7 files changed

Lines changed: 125 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ All notable changes to this project are documented here, following
66

77
## [Unreleased]
88

9+
## [0.3.2] - 2026-11-04
10+
11+
### Fixed
12+
13+
- **Lanes were a 59% REGRESSION on a GitHub-hosted runner, and this is the fix.**
14+
v0.3.0 divided the CPU budget between the lanes, which was right -- the budget
15+
is for the whole gate. What was wrong is the budget. `budget()` halves the
16+
machine so a gate cannot take the desktop away from the person running it, and
17+
it did that on CI too, where there is no desktop. On a 4-vCPU runner that left
18+
a budget of 2, two lanes at 1 CPU each, and trentina's `sdlc ci` went from
19+
15m27s to 24m38s: both lanes are CPU-bound, so each took about twice as long,
20+
and the image chain alone (1457s) came out longer than the entire sequential
21+
job had been (927s). No amount of overlap wins from there.
22+
23+
The budget is no longer halved in two cases that have nobody at a keyboard to
24+
protect: `CI` is set, or a cpuset, a CPU affinity or a cgroup quota already
25+
narrows the machine -- which is someone having already decided this process's
26+
share, so halving it again double-counts the same protection. A developer's
27+
laptop is unchanged: still half, and still divided between the lanes.
28+
29+
Measured on trentina, `sdlc ci` step: 15m27s at v0.2.2, 24m38s at v0.3.0.
30+
31+
- The cap detection compared the allowance against `nproc`, which a cpuset has
32+
already narrowed, so it could never see one. It compares against the installed
33+
CPU count now.
34+
935
## [0.3.1] - 2026-11-04
1036

1137
### Fixed
@@ -262,7 +288,8 @@ cached tag was never refreshed — that gate had been running a two-week-old
262288
reviewer while CI pulled fresh. Local and CI were different reviewers on the
263289
same diff.
264290

265-
[Unreleased]: https://github.com/crunchtools/python-sdlc/compare/v0.3.1...HEAD
291+
[Unreleased]: https://github.com/crunchtools/python-sdlc/compare/v0.3.2...HEAD
292+
[0.3.2]: https://github.com/crunchtools/python-sdlc/releases/tag/v0.3.2
266293
[0.3.1]: https://github.com/crunchtools/python-sdlc/releases/tag/v0.3.1
267294
[0.3.0]: https://github.com/crunchtools/python-sdlc/releases/tag/v0.3.0
268295
[0.2.2]: https://github.com/crunchtools/python-sdlc/releases/tag/v0.2.2

‎Containerfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ LABEL description="Every CrunchTools Python SDLC check in one image: ruff, mypy,
4444
LABEL org.opencontainers.image.source="https://github.com/crunchtools/python-sdlc"
4545
LABEL org.opencontainers.image.description="Every CrunchTools Python SDLC check in one image."
4646
LABEL org.opencontainers.image.licenses="AGPL-3.0-or-later"
47-
LABEL version="0.3.1"
47+
LABEL version="0.3.2"
4848

4949
# What the image carries, readable without starting anything. `sdlc versions`
5050
# prints these and a consuming repo's CI pins them.

‎IMAGE_TAG‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.1
1+
0.3.2

‎README.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ default_install_hook_types: [pre-commit, pre-push]
3636

3737
repos:
3838
- repo: https://github.com/crunchtools/python-sdlc
39-
rev: v0.3.1
39+
rev: v0.3.2
4040
hooks:
4141
- id: sdlc-commit
4242
- id: sdlc-push
@@ -97,6 +97,14 @@ The CPU budget is for the whole gate, not for each container: it is divided
9797
between the lanes, floor of one CPU each. Two lanes are no busier on your
9898
laptop than one was.
9999

100+
The budget itself is half the machine, so a gate cannot take the desktop away
101+
from the person running it — except where there is no such person. In CI, or
102+
where a cpuset, a CPU affinity or a cgroup quota already narrows the machine,
103+
the whole allowance is used: something has already decided this process's
104+
share, and halving it again double-counts the same protection. Halving CI is
105+
what made lanes slower than no lanes on a 4-vCPU runner; see the v0.3.2
106+
changelog entry.
107+
100108
Each lane's output is buffered and printed whole, in the order `tiers` lists
101109
the checks, so two lanes never cut into each other's log. Each block ends with
102110
how long its check took, because buffering is what the `== <step>` timestamps

‎bin/sdlc‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,42 @@ _cgroup_cpu_ceiling() { # cpu.max from leaf to root, smallest quota wins
108108
}
109109

110110
budget() {
111+
# The INSTALLED count, which no cpuset or quota changes -- `nproc` alone is
112+
# already narrowed by our affinity, so comparing the allowance against it can
113+
# never detect a cpuset. That was the first version of this and it silently
114+
# found nothing.
115+
_machine=$(nproc --all 2>/dev/null || getconf _NPROCESSORS_CONF 2>/dev/null \
116+
|| nproc 2>/dev/null || printf 1)
111117
_all=$(nproc 2>/dev/null || printf 1)
112118
_allowed=$(_cpus_in_list "$(awk -F': *' '/^Cpus_allowed_list/{print $2}' /proc/self/status 2>/dev/null)")
113119
[ "${_allowed:-0}" -gt 0 ] 2>/dev/null && [ "$_allowed" -lt "$_all" ] && _all=$_allowed
114120
_ceil=$(_cgroup_cpu_ceiling)
115121
[ "${_ceil:-0}" -gt 0 ] 2>/dev/null && [ "$_ceil" -lt "$_all" ] && _all=$_ceil
116-
# Half the machine, floor of one: the desktop is not in the gate's cgroup.
122+
123+
# Has something already decided this process's share? One comparison catches
124+
# a cpuset, a CPU affinity and a cgroup quota alike.
125+
_capped=no
126+
[ "${_machine:-0}" -gt 0 ] 2>/dev/null && [ "$_all" -lt "$_machine" ] && _capped=yes
127+
128+
# Halving is there to keep a gate from taking the machine away from the person
129+
# running it -- the desktop is not in the gate's cgroup. Two cases have no
130+
# such person, and halving them is just throwing away half a machine:
131+
#
132+
# - CI. A runner is dedicated to the job; there is no desktop to protect.
133+
# - A cpuset or a cgroup quota narrower than the machine. Something has
134+
# ALREADY decided this process's share, and halving that again
135+
# double-counts the same protection.
136+
#
137+
# This is not a tuning preference, it is what made lanes a regression. On a
138+
# 4-vCPU GitHub runner the budget was 2, two lanes got 1 each, and trentina's
139+
# `sdlc ci` went from 15m27s to 24m38s: both lanes are CPU-bound, so each took
140+
# twice as long, and the image chain alone (1457s) came out longer than the
141+
# whole sequential job had been (927s). Overlap cannot win from there.
142+
if [ -n "${CI:-}" ] || [ "$_capped" = yes ]; then
143+
[ "$_all" -lt 1 ] && _all=1
144+
printf '%s' "$_all"
145+
return
146+
fi
117147
_half=$(( _all / 2 ))
118148
[ "$_half" -lt 1 ] && _half=1
119149
printf '%s' "$_half"

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[project]
1212
name = "python-sdlc"
13-
version = "0.3.1"
13+
version = "0.3.2"
1414
description = "One container image and one script for every CrunchTools Python SDLC check"
1515
requires-python = ">=3.14"
1616
license = "AGPL-3.0-or-later"

‎tests/test_host_contract.sh‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ esac
3636

3737
SHIM=$(mktemp -d)
3838
LOG="$SHIM/podman.log"
39-
trap 'rm -rf "$SHIM"' EXIT 0
39+
RUN=$(mktemp -d)
40+
trap 'rm -rf "$SHIM" "$RUN"' EXIT 0
4041

4142
# The four tools the contract allows. git is real: bin/sdlc genuinely asks git
4243
# where the repo root and the hooks are, and that is allowed.
@@ -164,14 +165,64 @@ else
164165
bad "a host step's word was evaluated as shell"
165166
fi
166167

168+
printf '\n== the CPU budget\n\n'
169+
170+
# Halving the machine protects the person at the keyboard. Two cases have no
171+
# such person, and halving them throws away half a machine -- which is what
172+
# made lanes a REGRESSION on a 4-vCPU GitHub runner: budget 2, two lanes at 1
173+
# each, and trentina's `sdlc ci` went from 15m27s to 24m38s.
174+
BUDGET="$RUN/budget.sh"
175+
# shellcheck disable=SC2016
176+
# Deliberate throughout this block: the sed patterns match literal `$_best`
177+
# in bin/sdlc, and the echoed line must reach the generated script as source
178+
# rather than be expanded here.
179+
{
180+
echo 'set -eu'
181+
echo 'die() { echo "$*" >&2; exit 2; }'
182+
sed -n '/^_cpus_in_list()/,/^}$/p' bin/sdlc
183+
sed -n '/^_cgroup_cpu_ceiling()/,/^ printf .%s. "$_best"$/p' bin/sdlc
184+
echo '}'
185+
sed -n '/^budget()/,/^}$/p' bin/sdlc
186+
echo 'printf "%s" "$(budget)"'
187+
} > "$BUDGET"
188+
189+
machine=$(nproc --all 2>/dev/null || nproc)
190+
half=$((machine / 2))
191+
[ "$half" -lt 1 ] && half=1
192+
193+
got=$(env -u CI sh "$BUDGET")
194+
if [ "$got" = "$half" ]; then
195+
ok "a developer machine still gets half of it ($got of $machine)"
196+
else
197+
bad "an unconstrained machine budgeted $got, want $half of $machine"
198+
fi
199+
200+
got=$(CI=true sh "$BUDGET")
201+
if [ "$got" = "$machine" ]; then
202+
ok "CI gets the whole machine, having no desktop to protect ($got)"
203+
else
204+
bad "CI budgeted $got, want all $machine"
205+
fi
206+
207+
# A cpuset is the case the first version of this missed: `nproc` is already
208+
# narrowed by it, so comparing the allowance against nproc found nothing.
209+
if command -v taskset >/dev/null 2>&1 && [ "$machine" -ge 2 ]; then
210+
got=$(env -u CI taskset -c 0-1 sh "$BUDGET")
211+
if [ "$got" = 2 ]; then
212+
ok "a cpuset is someone else's decision, so it is taken whole ($got)"
213+
else
214+
bad "a 2-CPU cpuset budgeted $got, want 2"
215+
fi
216+
else
217+
printf 'skip taskset unavailable or too few CPUs\n'
218+
fi
219+
167220
printf '\n== the lanes\n\n'
168221

169222
# Steps of one lane run in sequence; lanes run at the same time. What has to
170223
# hold is not the speed -- it is that the answer and the log do not depend on
171224
# which lane happened to finish first.
172225

173-
RUN=$(mktemp -d)
174-
trap 'rm -rf "$SHIM" "$RUN"' EXIT 0
175226

176227
lanes() { # target [stall-seconds] -> exit status, output in $RUN/out
177228
rm -f "$SHIM/must-not-run"

0 commit comments

Comments
 (0)