Understanding, querying, and setting the CPU affinity mask
The task structure – the root data structure for the thread (or task), containing several dozen thread attributes – has a few attributes directly pertaining to scheduling: the priority (the nice as well as the Real-Time (RT) priority values), the scheduling class structure pointer, the runqueue the thread is on (if any), and so on. (FYI, we covered generic details on the task structure back in Chapter 6, Kernel Internals Essentials – Processes and Threads).
Among these is an important member, the CPU affinity bitmask (the actual structure member is cpumask_t *cpus_ptr
. FYI, until the 5.3 kernel, it was a member named cpus_allowed
; this was changed in this commit: https://github.com/torvalds/linux/commit/3bd3706251ee8ab67e69d9340ac2abdca217e733). This bit mask is just that: a bit mask of the CPU cores that the thread (represented by that task structure) is allowed to run on. A simple visualization helps...