Skip to content

Count tasks, not process leaders - #9

Merged
PhillThomas merged 1 commit into
mainfrom
count-tasks-not-processes
Nov 9, 2026
Merged

PhillThomas merged 1 commit into
mainfrom
count-tasks-not-processes

Conversation

@PhillThomas

Copy link
Copy Markdown
Owner

Follows #8, and it is a correction to it rather than an addition.

The interference field I merged an hour ago counted processes. /proc/<pid>/stat reports the state of the thread group leader, so it said what the leader was doing and nothing about the threads underneath it. On an idle box a run of membench reported:

start | runnable 0 | running: []
end   | runnable 0 | running: []

while its own worker threads were the busiest things on the machine. I noticed because the number looked wrong on a machine where nothing else was running, and it was: the leader of a benchmark that pins worker threads and joins on them is asleep for most of the run.

That inverts the one thing the field was for. The complaint in issue #5 was that a cell moved by 25% and nothing looked like it was running. A process with a sleeping leader and eight memory-hungry threads is precisely what the leader-only scan reports as an idle machine, and it is what most of the interference anyone has complained about actually looks like from /proc.

The scan now walks /proc/<pid>/task/<tid>/stat, so the count is tasks. RunningProcess became RunningTask; the pid in each entry stays the process id, because the comm already says which thread it was and a reader follows it up with ps -p. The list stays capped at eight and the README now says "tasks", which the previous paragraph did not.

After the change, on the same idle box:

start | runnable 3 | running: ['yes', 'yes', 'membench-worker']
end   | runnable 3 | running: ['yes', 'yes', 'membench-worker']

including the run's own worker, still runnable at the end sample because the end sample is taken before the last of them has been joined.

Cost. A thousand small reads is not free: 656 ms → 679 ms per tiny run (ten runs, one process, against a binary built before this feature existed at all) on a box with 113 processes. Both figures are dominated by process startup, so the honest statement is that the whole feature lands inside about 25 ms of a run that does nearly half a second of work. On a machine with far more threads than this one it will cost more, and it happens twice per run, outside every timed region.

A test spins a named thread inside the test process and requires the sample to count it and to name it, so this cannot quietly regress to leaders again. It asserts the name only when the capped list was not full, because on a busy machine our thread may fairly miss the cap — a test that failed for that reason would be blaming the wrong thing.

scripts/ci.sh is green end to end, including the AArch64 clippy step from #8. 117 core + 23 binary tests.

The interference field merged an hour ago counted processes, and `/proc/<pid>/stat`
reports the state of the thread group leader. A run of membench on an idle box
reported `runnable: 0` while its own worker threads were the busiest things on
the machine, because the leader of a benchmark that pins workers and joins on
them is asleep for most of the run.

That is the exact shape of the thing the field was added for. A process with a
sleeping leader and eight memory-hungry threads is what "nothing was running"
looks like from a leader-only scan, and the complaint in issue #5 was that a
cell moved 25% with nothing apparently running.

So the scan walks /proc/<pid>/task/<tid>/stat and the count is tasks.
RunningProcess is RunningTask; the pid in each entry stays the process id
because the comm already says which thread it was, and a reader follows it up
with `ps -p`. Ten runs of a tiny grid on a 113-process box: 656 ms before,
679 ms after, against a binary built before this feature existed at all, so the
whole instrument costs about 25 ms of a run that spends most of its time
starting up. Twice per run, outside every timed region.

A test spins a named thread and requires the sample to count it, so this cannot
regress back to leaders unnoticed. It asks for the name only when the capped
list was not full: on a busy machine our thread may fairly miss the cap, and a
test that failed for that reason would be blaming the wrong thing.
@PhillThomas
PhillThomas merged commit 18de4f8 into main Nov 9, 2026
4 checks passed
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.

2 participants