Processor affinity
Encyclopedia
Processor affinity is a modification of the native central queue scheduling algorithm in a symmetric multiprocessing
operating system. Each task (be it process or thread) in the queue has a tag indicating its preferred / kin processor
. At allocation time, each task is allocated to its kin processor in preference to others.
Processor affinity takes advantage of the fact that some remnants of a process may remain in one processor's state (in particular, in its cache
) from the last time the process ran. Scheduling it to run on the same processor the next time could result in the process's running more efficiently by reducing performance-degrading situations such as cache misses. A practical example might be running multiple instances of an application which does not use multiple threads, such as some graphics-rendering software. Overall system efficiency increases.
Scheduling algorithm implementations vary in adherence to processor affinity. Under certain circumstances some implementations will allow a task to change to another processor if this is deemed to be most efficient. This may be the case if two processor-intensive tasks (A & B) have affinity to one processor while another processor lies unused. Many algorithms would shift task B to the second processor in order to maximize processor use. Task B would then acquire affinity with the second processor while task A would continue to have affinity with the original processor.
Processor affinity can effectively reduce cache problems but it does not curb the persistent load-balancing
problem. Further, processor affinity becomes more complicated in systems with non-uniform architectures. For example, a system with two dual-core hyper-threaded
CPUs presents a challenge to a scheduling algorithm. There is complete affinity between two virtual CPUs implemented on the same core via hyper-threading, partial affinity between two cores on the same physical chip (as the cores share some, but not all, cache), and no affinity between separate physical chips.
As other resources are also shared, processor affinity alone cannot be used as the basis for CPU dispatching. If a process has recently run on one virtual hyper-threaded CPU in a given core, and that virtual CPU is currently busy but its partner is not, cache affinity would suggest that the process should be dispatched to the idle partner. However, the two virtual CPUs compete for essentially all computing, cache, and memory resources. It would typically be more efficient in this case to dispatch the process to a different core or CPU if one is available. This would likely incur a penalty when process repopulates the cache, but overall performance would likely be higher as the process would not have to compete for resources within the CPU.
On Linux
the CPU affinity of a process might be altered with the taskset(1) program.
On SGI
systems, dplace binds a process to a set of CPUs.
NetBSD
5.0, FreeBSD
7.2 and later versions can use pthread_setaffinity_np and pthread_getaffinity_np. In NetBSD
, the psrset utility to set a thread's affinity to a certain CPU set. In FreeBSD
, cpuset utility is used to create CPU sets and to assign processes to these sets.
On Windows NT
, thread and process CPU affinities can be set separately by using SetThreadAffinityMask and SetProcessAffinityMask API calls or via the Task Manager interface (for process affinity only). Mac OS X
exposes an affinity API that provides hints to the kernel how to schedule threads according to affinity sets.
Symmetric multiprocessing
In computing, symmetric multiprocessing involves a multiprocessor computer hardware architecture where two or more identical processors are connected to a single shared main memory and are controlled by a single OS instance. Most common multiprocessor systems today use an SMP architecture...
operating system. Each task (be it process or thread) in the queue has a tag indicating its preferred / kin processor
Central processing unit
The central processing unit is the portion of a computer system that carries out the instructions of a computer program, to perform the basic arithmetical, logical, and input/output operations of the system. The CPU plays a role somewhat analogous to the brain in the computer. The term has been in...
. At allocation time, each task is allocated to its kin processor in preference to others.
Processor affinity takes advantage of the fact that some remnants of a process may remain in one processor's state (in particular, in its cache
CPU cache
A CPU cache is a cache used by the central processing unit of a computer to reduce the average time to access memory. The cache is a smaller, faster memory which stores copies of the data from the most frequently used main memory locations...
) from the last time the process ran. Scheduling it to run on the same processor the next time could result in the process's running more efficiently by reducing performance-degrading situations such as cache misses. A practical example might be running multiple instances of an application which does not use multiple threads, such as some graphics-rendering software. Overall system efficiency increases.
Scheduling algorithm implementations vary in adherence to processor affinity. Under certain circumstances some implementations will allow a task to change to another processor if this is deemed to be most efficient. This may be the case if two processor-intensive tasks (A & B) have affinity to one processor while another processor lies unused. Many algorithms would shift task B to the second processor in order to maximize processor use. Task B would then acquire affinity with the second processor while task A would continue to have affinity with the original processor.
Processor affinity can effectively reduce cache problems but it does not curb the persistent load-balancing
Load balancing (computing)
Load balancing is a computer networking methodology to distribute workload across multiple computers or a computer cluster, network links, central processing units, disk drives, or other resources, to achieve optimal resource utilization, maximize throughput, minimize response time, and avoid...
problem. Further, processor affinity becomes more complicated in systems with non-uniform architectures. For example, a system with two dual-core hyper-threaded
Hyper-threading
Hyper-threading is Intel's term for its simultaneous multithreading implementation in its Atom, Intel Core i3/i5/i7, Itanium, Pentium 4 and Xeon CPUs....
CPUs presents a challenge to a scheduling algorithm. There is complete affinity between two virtual CPUs implemented on the same core via hyper-threading, partial affinity between two cores on the same physical chip (as the cores share some, but not all, cache), and no affinity between separate physical chips.
As other resources are also shared, processor affinity alone cannot be used as the basis for CPU dispatching. If a process has recently run on one virtual hyper-threaded CPU in a given core, and that virtual CPU is currently busy but its partner is not, cache affinity would suggest that the process should be dispatched to the idle partner. However, the two virtual CPUs compete for essentially all computing, cache, and memory resources. It would typically be more efficient in this case to dispatch the process to a different core or CPU if one is available. This would likely incur a penalty when process repopulates the cache, but overall performance would likely be higher as the process would not have to compete for resources within the CPU.
On Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...
the CPU affinity of a process might be altered with the taskset(1) program.
On SGI
Silicon Graphics
Silicon Graphics, Inc. was a manufacturer of high-performance computing solutions, including computer hardware and software, founded in 1981 by Jim Clark...
systems, dplace binds a process to a set of CPUs.
NetBSD
NetBSD
NetBSD is a freely available open source version of the Berkeley Software Distribution Unix operating system. It was the second open source BSD descendant to be formally released, after 386BSD, and continues to be actively developed. The NetBSD project is primarily focused on high quality design,...
5.0, FreeBSD
FreeBSD
FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...
7.2 and later versions can use pthread_setaffinity_np and pthread_getaffinity_np. In NetBSD
NetBSD
NetBSD is a freely available open source version of the Berkeley Software Distribution Unix operating system. It was the second open source BSD descendant to be formally released, after 386BSD, and continues to be actively developed. The NetBSD project is primarily focused on high quality design,...
, the psrset utility to set a thread's affinity to a certain CPU set. In FreeBSD
FreeBSD
FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...
, cpuset utility is used to create CPU sets and to assign processes to these sets.
On Windows NT
Windows NT
Windows NT is a family of operating systems produced by Microsoft, the first version of which was released in July 1993. It was a powerful high-level-language-based, processor-independent, multiprocessing, multiuser operating system with features comparable to Unix. It was intended to complement...
, thread and process CPU affinities can be set separately by using SetThreadAffinityMask and SetProcessAffinityMask API calls or via the Task Manager interface (for process affinity only). Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...
exposes an affinity API that provides hints to the kernel how to schedule threads according to affinity sets.