What is Processor Affinity?
Processor Affinity refers to a process with an affinity for the processor on which it is currently running. When a process executes on a specific CPU, the cache memory is affected. The processor’s cache is populated by the process’s most recently accessed data, so successive memory accesses are commonly satisfied in the cache memory. Assume the process is moved to a different CPU. In that situation, the contents of the first processor’s cache memory must be invalidated, and the second processor’s cache must be repopulated. Most symmetric multiprocessing (SMP) systems try to avoid process migration by keeping the same process running on the same processor due to the cause invalidating and repopulating caches is expensive.
Processor affinity is also known as “cache affinity”. It can bind and unbind a process or a thread to a CPU or a group of CPUs and ensure that the process or thread executes only on the designated CPU and not on any other CPU. It is a modification of the symmetric multiprocessing OS’s native central queue scheduling algorithm. Every item in the queue does have a tag that specifies its kin processor. When it comes to resource allocation, each task is assigned to its kin processor before others.
Processor affinity makes use of the fact that leftovers of a process that ran on a certain processor can persist in that processor’s state after another process has run on it. Scheduling a CPU-intensive process with few interrupts to run on the same processor might improve performance by decreasing degrading events like cache misses. However, it may slow down other programs as they must wait for that processor to become available again. Using many examples of a non-threaded application like graphics-rendering software is a real instance of processor affinity.
Implementations of scheduling algorithms differ in their adherence to processor affinity. Some implementations will enable a task to switch to another processor if it improves efficiency under certain conditions. For example, if two processor-intensive processes (A and B) have an affinity to one processor and another remains unused, many schedulers would shift job B to the second processor to maximize the CPU use. Task B will develop an affinity for the second processor, whereas Task A will retain affinity for the original processor.
What is the use of Processor Affinity?
Processor affinity can effectively decrease cache issues, but it does not resolve the persistent load-balancing issue. For instance, a system with two dual-core hyper-threaded CPUs provides a difficult task to a scheduling algorithm.
Hyper-threading creates a perfect affinity between two virtual CPUs running on the same core. There is some affinity among two cores on the same physical processor but no affinity among different physical processors. As other resources are shared, processor affinity alone may not be used to dispatch CPUs. Suppose a process has been executed on a virtual hyper-threaded CPU on a given core, and the virtual CPU is currently busy, and the CPU is not its partner. In this situation, the cache affinity suggests that the process be dispatched to the idle companion processor. Although, the two virtual processors compete for almost all compute, memory resources, and cache memory. If only one processor is available, it is usually more effective to route the process to a separate core or processor. It may incur a performance penalty when the process repopulates the cache but may improve overall performance because it will not have to compete for CPU resources.
Types of Processor Affinity
There are mainly two types of processor affinity. These are as follows:
- Soft Affinity
- Hard Affinity
The term “soft affinity” refers to an operating system’s policy of keeping a process running on the same processor, and it will not ensure that it will do so.
It enables a process to define a subset of processors on which it may execute. Some operating systems like Linux implement soft affinity and offer system calls, such as sched setaffinity(), that support hard affinity.