Skip to content

IO.thread_count answers the worker pool's size, and 1 on the JS lanes - #1048

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
aldeni:io-thread-count
Sep 25, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
aldeni:io-thread-count

Conversation

@aldeni

@aldeni aldeni commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

What changes

IO.thread_count() -> IO(U32) is a new Base effect, as #971 proposes:

  • bend2/base.bend: the def, next to IO.now, two imports like every
    other Base effect.
  • bend2/effs/thread_count.c: answers pool_size. corpus_setup sets it
    once, before io_loop runs, to the --threads value or cpu_count(),
    clamped to 1..CUBE_T (128). Effect sources are spliced after the
    runtime, so the effect reads the global directly, as args.c reads
    io_argv. comp.ts does not change.
  • bend2/effs/thread_count.js: answers 1. A JS program runs one thread
    and ignores --threads (RUNTIME_MAIN's cli).
  • tests/io/thread_count.bend: the new test.

The guide is unchanged. IO.random_u32 (#837), TCP.poll (#858) and
File.read_at/File.size (#823) were added without a guide line, and I
followed them.

Why

A program cannot find out how many workers run its parallel work. The
runtime consumes --threads before IO.args sees the command line. The
default is the number of CPUs the process may use: the affinity mask,
under the cgroup quota. Without this effect, a program has to copy
cpu_count into a foreign effect to get it (#971).

Test

tests/io/thread_count.bend prints whether the count is in 1..128. The
gate runs each binary with no flag, and the CPU count differs by machine,
so the test does not print the count itself.

On this branch it prints True on all four lanes (check, interpreted,
JS, C). On main (95317d9) the check fails:

Error:
- expected : a defined name
- observed : IO.thread_count
Location: main

By hand, with a probe that prints the count
(n : U32 <- IO.thread_count() then IO.print(U32.show(n))), built with
bun bend2/main.ts count.bend -o count.js -o count on a machine with 4
CPUs:

$ ./count
4
$ ./count --threads 1
1
$ ./count --threads 3
3
$ ./count --threads 128
128
$ ./count --threads 129
128
$ ./count --threads 10000
128
$ ./count --threads 0
bend: expected a thread count of 1 or more after --threads
$ taskset -c 0,1 ./count
2
$ ./count -- --threads 2
4
$ bun count.js
1
$ bun count.js --threads 3
1
$ bun bend2/main.ts count.bend
1

A binary with a ! and a parallel sum before the call (BANGS 1, built
without a GPU on this machine) prints 4, 2 and 3 with no flag,
--threads 2 and --threads 3 --gpu off.

Regression

  • tests/io checkup (one process, as gates/test.ts runs it): the same
    output as main except for the new module.
  • tests/base, tests/check and tests/import checkup (307 modules): the
    same output as main.
  • args, now, random_u32, get_env, hello_print, sleep_order,
    spawn_sleep, fork_join and confluence_threads pass on all four
    lanes.
  • gates/repo.ts: 46/46. base.bend goes from 25375 to 25403 ttok. The
    two effect files are 102 and 38 ttok.

Not tested here

  • macOS was not run: this machine is Linux x86-64 (4 cores), Bun 1.3.11,
    clang 22. On macOS, cpu_count is sysconf(_SC_NPROCESSORS_ONLN), and
    the effect answers whatever corpus_setup stored.
  • No GPU: with --gpu on, pool_size is set the same way. The effect
    answers the CPU pool's size, not a GPU lane count. I could not run a
    device build here.
  • The cluster gates (gates/test.ts, gates/perf.ts) were not run.

Closes #971.

A program could not learn how many workers run its parallel work: the
native runtime consumes --threads before IO.args sees the command line,
and the default (the CPUs the process may use, under its affinity mask
and cgroup quota) could only be recomputed by copying cpu_count into a
foreign effect (bendlang#971).

IO.thread_count() -> IO(U32) is a Base effect with no arguments, in the
shape of IO.args and IO.now. Its C side answers pool_size, which
corpus_setup sets once, before io_loop runs, to --threads or
cpu_count() clamped to 1..CUBE_T (128); the effect is spliced after the
runtime, so it reads the global as args.c reads io_argv, and comp.ts
does not change. Its JS side answers 1: a JS program runs one thread
and ignores --threads.

tests/io/thread_count.bend prints whether the count is in 1..128, since
the gate passes no flag and the CPU count differs by machine. On the
branch it prints True on all four lanes (check, interpreted, JS, C); on
main its check fails with "expected : a defined name, observed :
IO.thread_count".

By hand, on Linux x86-64 with 4 CPUs: the binary prints 4 with no flag,
1, 3 and 128 with --threads 1, 3 and 128, and 128 with --threads 129
and 10000; --threads 0 is refused by the CLI as before; under
taskset -c 0,1 it prints 2. A binary with a `!` prints the same after
its parallel work. bun x.js and the interpreted run print 1.

Regression: the tests/io checkup differs from main only by the new
module, and the tests/base, tests/check and tests/import checkups (307
modules) are identical to main's; args, now, random_u32, get_env,
hello_print, sleep_order, spawn_sleep, fork_join and confluence_threads
pass all four lanes; gates/repo.ts 46/46. base.bend 25375 -> 25403
ttok.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you, this is merged. I ran it on macOS (10 CPUs), which you could not: tests/io/thread_count.bend prints True on all four lanes, on this branch and merged onto current main; the binary answers 10 with no flag, 1/3/128 with --threads 1/3/128, 128 with --threads 129, still refuses --threads 0, and leaves -- --threads 2 to the program; a binary that bangs on Metal answers the host pool's size after its GPU work, with --gpu on and --gpu off alike; the JS lanes answer 1. gates/repo.ts is 46/46 and the neighbouring IO tests pass on every lane. Small, in the shape of IO.now, and exactly what #971 asked for.

Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose the effective runtime worker count to Bend programs

2 participants