Skip to content

Commit 61bc185

Browse files
committed
Limit max build jobs when building the uv cache.
1 parent 31552e8 commit 61bc185

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

scripts/ci/build_and_push_uv_cache.sh

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PYTHON_MM="${PYTHON_MM:-3.11}"
99
UV_CACHE_RELEASE_TAG="${UV_CACHE_RELEASE_TAG:-prek-uv-cache}"
1010
UV_CACHE_ASSET_PREFIX="${UV_CACHE_ASSET_PREFIX:-prek-uv-cache}"
1111
BUILD_JOBS="${BUILD_JOBS:-auto}"
12+
AUTO_BUILD_JOBS_MAX="${AUTO_BUILD_JOBS_MAX:-16}"
1213
KEEP_COUNT="${KEEP_COUNT:-4}"
1314
PART_SIZE_MB="${PART_SIZE_MB:-1900}"
1415
SKIP_BUILD=0
@@ -111,6 +112,43 @@ require_cmd() {
111112
command -v "${cmd}" >/dev/null 2>&1 || fail "Required command not found: ${cmd}"
112113
}
113114

115+
detect_mem_info() {
116+
local out_source_var="$1"
117+
local out_kib_var="$2"
118+
local detected_source="proc_meminfo"
119+
local detected_kib="0"
120+
121+
local cgroup_v2_mem_max="/sys/fs/cgroup/memory.max"
122+
if [[ -r "${cgroup_v2_mem_max}" ]]; then
123+
local cgroup_v2_value
124+
cgroup_v2_value="$(<"${cgroup_v2_mem_max}")"
125+
if [[ "${cgroup_v2_value}" =~ ^[0-9]+$ ]] && ((cgroup_v2_value > 0)); then
126+
detected_source="cgroup_v2"
127+
detected_kib="$((cgroup_v2_value / 1024))"
128+
printf -v "${out_source_var}" '%s' "${detected_source}"
129+
printf -v "${out_kib_var}" '%s' "${detected_kib}"
130+
return
131+
fi
132+
fi
133+
134+
local cgroup_v1_mem_limit="/sys/fs/cgroup/memory/memory.limit_in_bytes"
135+
if [[ -r "${cgroup_v1_mem_limit}" ]]; then
136+
local cgroup_v1_value
137+
cgroup_v1_value="$(<"${cgroup_v1_mem_limit}")"
138+
if [[ "${cgroup_v1_value}" =~ ^[0-9]+$ ]] && ((cgroup_v1_value > 0)); then
139+
detected_source="cgroup_v1"
140+
detected_kib="$((cgroup_v1_value / 1024))"
141+
printf -v "${out_source_var}" '%s' "${detected_source}"
142+
printf -v "${out_kib_var}" '%s' "${detected_kib}"
143+
return
144+
fi
145+
fi
146+
147+
detected_kib="$(awk '/MemTotal/ {print $2}' /proc/meminfo 2>/dev/null || echo 0)"
148+
printf -v "${out_source_var}" '%s' "${detected_source}"
149+
printf -v "${out_kib_var}" '%s' "${detected_kib}"
150+
}
151+
114152
compute_fingerprint() {
115153
python3 "${REPO_ROOT}/scripts/ci/compute_uv_fingerprint.py" \
116154
--pyproject "${REPO_ROOT}/pyproject.toml" \
@@ -131,8 +169,9 @@ resolve_build_jobs() {
131169

132170
local cpu_count
133171
cpu_count="$(nproc 2>/dev/null || echo 1)"
172+
local mem_source
134173
local mem_kib
135-
mem_kib="$(awk '/MemTotal/ {print $2}' /proc/meminfo 2>/dev/null || echo 0)"
174+
detect_mem_info mem_source mem_kib
136175
local mem_gib="$((mem_kib / 1024 / 1024))"
137176
local mem_limited_jobs=1
138177
if ((mem_gib > 0)); then
@@ -145,6 +184,18 @@ resolve_build_jobs() {
145184
if ((mem_limited_jobs > cpu_count)); then
146185
mem_limited_jobs="${cpu_count}"
147186
fi
187+
if ! [[ "${AUTO_BUILD_JOBS_MAX}" =~ ^[1-9][0-9]*$ ]]; then
188+
fail "AUTO_BUILD_JOBS_MAX must be a positive integer."
189+
fi
190+
if ((mem_limited_jobs > AUTO_BUILD_JOBS_MAX)); then
191+
mem_limited_jobs="${AUTO_BUILD_JOBS_MAX}"
192+
fi
193+
printf '[ci-cache] Auto build jobs resolved: cpu_count=%s mem_source=%s mem_gib=%s cap=%s resolved=%s\n' \
194+
"${cpu_count}" \
195+
"${mem_source}" \
196+
"${mem_gib}" \
197+
"${AUTO_BUILD_JOBS_MAX}" \
198+
"${mem_limited_jobs}" >&2
148199
printf '%s\n' "${mem_limited_jobs}"
149200
}
150201

0 commit comments

Comments
 (0)