Skip to main content

Module affinity

Module affinity 

Source
Expand description

CPU affinity: which cores a process may run on.

The supported platforms disagree about what affinity is, so this module models the platform honestly instead of pretending they match:

  • Linux has hard affinity. sched_getaffinity reads the mask, and the mask is enforced by the scheduler, so it can be set without privilege on your own processes.
  • FreeBSD also has hard affinity, through cpuset_getaffinity. The API is a cpuset_t (1024 bits) rather than a sized byte array, and the level and which arguments select the target: CPU_LEVEL_WHICH + CPU_WHICH_PID means “this process’s set”, which is what the panel edits.
  • macOS has only a hint. thread_policy_set(THREAD_AFFINITY_POLICY) tags a thread with an affinity group, and the scheduler uses it as a preference. There is no per-process mask, and reading it back is not supported. Leaving the mask unset on macOS is therefore correct, not a missing feature.

The mask is a Vec<u64>, one bit per core, which is how Linux stores it. The platform glue lives behind #[cfg] at the bottom so the rendering and the toggle logic can be tested anywhere.

Structs§

CpuMask
A set of CPUs, one bit per core.

Functions§

read
Read the affinity mask of pid.
supported
Whether this build can change affinity at all.
write
Write the affinity mask of pid.