Default worker count ignores CPU affinity and cgroup CPU quotas on Linux

Aperta
#16,284 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
34/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
linux

Direzione di ricerca

Start with runtime/object.cpp and Lean/Shell.lean, which the issue names as the places setting the current defaults; compare those paths with the described LEAN_NUM_THREADS behavior. The proposed change also touches Linux CPU affinity and cgroup v1/v2 quota detection, plus the chunk count for importedRelevantConstants. The issue says the patch has tests; done means defaults reflect visible CPU limits while explicit -j and LEAN_NUM_THREADS still take precedence.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Prerequisites
  • Check that your issue is not already filed: https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release (nightly-2026-11-05).
  • When reporting a perceived soundness bug: not applicable.
Description

On Linux, the default number of worker threads is std::thread::hardware_concurrency(), the number of logical CPUs of the machine. This is used by the lean executable and by every compiled Lean program, including Lake. It ignores both the CPU affinity of the process and any cgroup CPU quota.

On my test host, lean defaults to 255 workers when pinned to 4 CPUs with taskset, and also when running under a cgroup quota of 24 CPUs. In a container or batch job this oversizes every pool, and lake build multiplies the effect by starting one compiler process per ready module.

Context
  • #4241 asks for a -j option in Lake. This issue is about the default used when nothing is set, and does not ask for a new option.
  • #13856 adds an opt-in cross-process limit based on a POSIX semaphore. It is complementary: it bounds the total across processes, whereas this issue concerns the size of each process's default pool.
  • #13767 reports resource pressure from nested Lake and compiler concurrency on macOS. This issue is Linux-specific and does not claim to address it.

LEAN_NUM_THREADS works around the problem for compiled programs such as Lake (see this Zulip thread). It does not change the pool of the lean executable, which is set by -j and otherwise defaults to the hardware concurrency. Lake does not pass -j to the compilers it starts.

Steps to Reproduce

Pin the process to four CPUs that are actually allowed, and print the default:

cpus=$(python3 -c 'import os; print(",".join(map(str, sorted(os.sched_getaffinity(0))[:4])))')
cat > Defaults.lean <<'LEAN'
module
import all Lean.Shell
public def main : IO Unit :=
  IO.println s!"hardware={System.Platform.Internal.getHardwareConcurrency ()} shell_default={Lean.defaultNumThreads}"
LEAN
taskset -c "$cpus" lean --run Defaults.lean

For a Lake build, generate a project with sixteen independent modules that each do a fixed amount of work at elaboration time:

from pathlib import Path
Path('P').mkdir()
Path('lakefile.toml').write_text('name = "lakeprobe"\n[[lean_lib]]\nname = "P"\n')
Path('P.lean').write_text(''.join(f'import P.M{i}\n' for i in range(16)))
for i in range(16):
    Path(f'P/M{i}.lean').write_text(f'''def burn{i} (n : Nat) : Nat := Id.run do
  let mut acc := 0
  for i in [0:n] do
    acc := (acc * 31 + i) % 1000000007
  return acc
#eval burn{i} 1000000
''')

Run taskset -c "$cpus" lake build --no-cache +P:leanArts in a fresh copy of this project, once as is and once with LEAN_NUM_THREADS=4, and count the processes whose command line contains --setup (the compilers).

Expected behavior: The default worker count reflects the CPUs the process may use. An explicit -j or LEAN_NUM_THREADS still takes precedence.

Actual behavior: hardware=255 shell_default=255 with four CPUs allowed.

Versions
  • Lean 4.37.0-nightly-2026-11-05 (67dafde474fbaf278d6d7f822068549bf6e532fa); the same defaults are in 4.35.0 and in current master (Lean/Shell.lean, and get_lean_num_threads in runtime/object.cpp).
  • Linux x86-64, cgroup v1, 255 logical CPUs, a visible cgroup quota of 24 CPUs.
Additional Information

Peak number of concurrent compiler processes for the Lake project above with four CPUs allowed (nightly-2026-11-05, wall time of one run):

Setting Compilers Wall (s)
default 16 2.32
LEAN_NUM_THREADS=4 4 2.32
LEAN_NUM_THREADS=2 2 4.16

On this small project the default is as fast as four workers, so I am not claiming a speedup: the difference is the number of processes, and the memory they hold.

I plan to fix this by adding a separate System.Platform.Internal.getAvailableParallelism, leaving getHardwareConcurrency unchanged. On Linux it returns the minimum of the calling thread's CPU affinity and the CPU quotas of the process's cgroup and all of its ancestors that are visible in the mount namespace (cgroup v1 and v2). A fractional quota is rounded up, so the result is never below one. Other platforms keep returning the hardware concurrency. It would be used for the default of the lean executable, for the default of compiled programs (when LEAN_NUM_THREADS is unset), and for the chunk count in library suggestions' importedRelevantConstants. Explicit settings are untouched.

The result is a default pool size, not a cap: Task.get compensation and dedicated tasks can still create more threads. Limits that are not visible from inside the container cannot be detected and are not guessed. The value is computed once at startup, as it is today.

I have a patch with tests for this and would be glad to submit it. Since the repository only accepts PRs from a limited set of contributors, how would you like me to proceed if the approach looks reasonable?

AI assistance: Codex and Claude Code helped with the investigation, the patch and this text. I have reviewed the code and the results myself.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
19h 8m
PR unite (30g)
421

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Operating Systems

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.