Benchmark: auto-pin threads when CPU affinity is restricted#171
Open
HFTrader wants to merge 1 commit intoefficient:masterfrom
Open
Benchmark: auto-pin threads when CPU affinity is restricted#171HFTrader wants to merge 1 commit intoefficient:masterfrom
HFTrader wants to merge 1 commit intoefficient:masterfrom
Conversation
When the benchmark detects that its CPU affinity has been restricted (e.g. via taskset, cpuset, or container --cpuset-cpus), it automatically pins each thread to its own physical core. HT siblings are detected via sysfs thread_siblings_list and skipped -- two benchmark threads will never share a physical core. If there are fewer physical cores than requested threads, the benchmark exits with a clear error. When affinity is unrestricted (all online CPUs available), behavior is unchanged -- no pinning is performed. This prevents a subtle benchmarking pitfall: containers (podman/docker) with --cpuset-cpus do NOT remap CPU numbers. Code that assumes CPUs start at 0 will call sched_setaffinity with invalid CPU IDs, which fails silently and leaves all threads unpinned on a single core. By reading the actual affinity mask from sched_getaffinity and pinning to those real CPU numbers, this issue is avoided entirely. Linux only; non-Linux platforms skip pinning.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the benchmark process has restricted CPU affinity (via
taskset,cpuset, or container--cpuset-cpus), automatically pin each thread to its own physical core.sched_getaffinityagainst online CPU countthread_siblings_list— two benchmark threads never share a physical core--num-threadsMotivation
Container runtimes (podman/docker) with
--cpuset-cpusdo not remap CPU numbers. Inside a container restricted to CPUs 9-11, the kernel still uses host CPU IDs. Code that assumes numbering starts at 0 and callssched_setaffinity(cpu=0)getsEINVAL— silently, if the return value isn't checked. All threads then run unpinned, potentially timesharing a single core, producing bogus benchmark results.By reading the actual affinity mask and pinning to those real CPU IDs, this issue is avoided. Usage:
Test plan
taskset -c 0-2: auto-pins 2 threads to CPUs 0, 1taskset -c 0,4(HT siblings on test machine): errors with "Need 2 physical cores but only 1 available"