Processor affinity

{{Short description|Assignment of a task to a given core of a CPU}}

{{more sources|date=June 2021}}

In computer science, processor affinity, also called CPU pinning or cache affinity, enables the binding and unbinding of a process or a thread to a central processing unit (CPU) or a range of CPUs, so that the process or thread will execute only on the designated CPU or CPUs rather than any CPU. This can be viewed as a modification of the native central queue scheduling algorithm in a symmetric multiprocessing operating system. Each item in the queue has a tag indicating its kin processor. At the time of resource allocation, each task is allocated to its kin processor in preference to others.

Processor affinity takes advantage of the fact that remnants of a process that was run on a given processor may remain in that processor's state (for example, data in the cache memory) after another process was run on that processor. Scheduling a CPU-intensive process that has few interrupts to execute on the same processor may improve its performance by reducing degrading events such as cache misses, but may slow down ordinary programs because they would need to wait for that CPU to become available again.{{cite web|url=https://www.ibm.com/docs/en/aix/7.2?topic=architecture-processor-affinity-binding|title=Processor affinity and binding|website=IBM |access-date=2021-06-08}} A practical example of processor affinity is executing multiple instances of a non-threaded application, such as some graphics-rendering software. {{citation needed|date=June 2021}}

Scheduling-algorithm implementations vary in adherence to processor affinity. Under certain circumstances, some implementations will allow a task to change to another processor if it results in higher efficiency. For example, when two processor-intensive tasks (A and B) have affinity to one processor while another processor remains unused, many schedulers will shift task B to the second processor in order to maximize processor use. Task B will then acquire affinity with the second processor, while task A will continue to have affinity with the original processor.{{citation needed|date=June 2021}}

Usage

Processor affinity can effectively reduce cache problems, but it does not reduce the persistent load-balancing problem.[http://www.tmurgent.com/WhitePapers/ProcessorAffinity.pdf "White Paper - Processor Affinity"] - From [http://www.tmurgent.com tmurgent.com]. Accessed 2007-07-06. Also note that 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 processor (as the cores share some, but not all, cache), and no affinity between separate physical processors. 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 CPU is not, cache affinity would suggest that the process should be dispatched to the idle partner CPU. However, the two virtual CPUs compete for essentially all computing, cache, and memory resources. In this situation, it would typically be more efficient to dispatch the process to a different core or CPU, if one is available. This could incur a penalty when process repopulates the cache, but overall performance could be higher as the process would not have to compete for resources within the CPU.{{citation needed|date=June 2021}}

Specific operating systems

On Linux, the CPU affinity of a process can be altered with the taskset(1) program{{man|1|taskset|Linux}} and the sched_setaffinity(2) system call. The affinity of a thread can be altered with one of the library functions: pthread_setaffinity_np(3) or pthread_attr_setaffinity_np(3).

On SGI systems, [https://web.archive.org/web/20070701091047/http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=linux&db=man&fname=%2Fusr%2Fshare%2Fcatman%2Fman1%2Fdplace.1.html dplace] binds a process to a set of CPUs.[http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=linux&db=man&fname=/usr/share/catman/man1/dplace.1.html dplace.1] {{webarchive|url=https://web.archive.org/web/20070701091047/http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=linux&db=man&fname=%2Fusr%2Fshare%2Fcatman%2Fman1%2Fdplace.1.html |date=2007-07-01 }} - From [http://www.sgi.com sgi.com]. Accessed 2007-07-06.

On DragonFly BSD 1.9 (2007) and later versions, usched_set system call can be used to control the affinity of a process.{{cite web |url= http://mdoc.su/d/usched_set.2 |title= usched_set(2) — setting up a proc's usched |work= DragonFly System Calls Manual |publisher= DragonFly BSD |access-date= 2019-07-28 }}{{cite web |url= http://bxr.su/d/sys/kern/kern_usched.c#sys_usched_set |title= kern/kern_usched.c § sys_usched_set |website= BSD Cross Reference |publisher= DragonFly BSD |access-date= 2019-07-28 }} On NetBSD 5.0, FreeBSD 7.2, DragonFly BSD 4.7 and later versions can use pthread_setaffinity_np and pthread_getaffinity_np.{{URL|http://mdoc.su/n,f,d/pthread_setaffinity_np.3|pthread_setaffinity_np(3)}}NetBSD, FreeBSD and DragonFly BSD Library Functions Manual In NetBSD, the psrset utility{{man|8|psrset|NetBSD}} to set a thread's affinity to a certain CPU set. In FreeBSD, cpuset{{man|1|cpuset|FreeBSD}} utility is used to create CPU sets and to assign processes to these sets. In DragonFly BSD 3.1 (2012) and later, usched utility can be used for assigning processes to a certain CPU set.{{cite web |url= http://mdoc.su/d/usched.8 |title= usched(8) — run a program with a specified userland scheduler and cpumask |work= DragonFly System Manager's Manual |publisher= DragonFly BSD |access-date= 2019-07-28 }}

On Windows NT and its successors, thread and process CPU affinities can be set separately by using SetThreadAffinityMask[http://msdn2.microsoft.com/en-us/library/ms686247(VS.85).aspx SetThreadAffinityMask] - MSDN Library and SetProcessAffinityMask[http://msdn2.microsoft.com/en-us/library/ms686223(VS.85).aspx SetProcessAffinityMask] - MSDN Library API calls or via the Task Manager interface (for process affinity only).

macOS exposes an affinity API{{cite web|url=https://developer.apple.com/library/mac/#releasenotes/Performance/RN-AffinityAPI/index.html|title=Thread Affinity API Release Notes|website=Developer.apple.com}} that provides hints to the kernel how to schedule threads according to affinity sets.

On Solaris it is possible to control bindings of processes and LWPs to processor using the pbind(1)[http://docs.oracle.com/cd/E23824_01/html/821-1462/pbind-1m.html pbind(1M)] - Solaris man page program. To control the affinity programmatically processor_bind(2)[http://docs.oracle.com/cd/E19109-01/tsolaris8/817-0881/6mglbsoma/index.html processor_bind(2)] - Solaris man page can be used. There are more generic interfaces available such as pset_bind(2)[http://docs.oracle.com/cd/E26502_01/html/E29032/pset-bind-2.html#REFMAN2pset-bind-2 pset_bind(2)] - Oracle Solaris 11.1 Information Library - man pages section 2 or lgrp_affinity_get(3LGRP)[http://docs.oracle.com/cd/E19120-01/open.solaris/820-1691/lgroups-193/index.html lgrp_affinity_get(3LGRP)] - Memory and Thread Placement Optimization Developer's Guide using processor set and locality groups concepts.

On AIX it is possible to control bindings of processes using the bindprocessor command{{cite web|url=https://www.ibm.com/developerworks/aix/library/au-processinfinity.html|title=Processor affinity on AIX|author1=Umesh Prabhakar Gaikwad|author2=Kailas S. Zadbuke|website=IBM |date=November 16, 2006}}{{cite web|url=https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/b_commands/bindprocessor.html|title=bindprocessor Command|website=IBM}} and the bindprocessor API.{{cite web|url=https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/b_bostechref/bindprocessor.html|title=bindprocessor Subroutine|website=IBM}}

See also

References