What is and How does Setting CPU Affinity Improve Performance?
Today’s term that we are sharing in CSEStack is CPU affinity. We also check how actually Setting CPU Affinity is useful.
What is CPU affinity?
CPU affinity is a setting provided by the operating system. It helps to reserve particular core of CPU for a particular thread to be executed on.
If you don’t set affinity , a thread can be run on any core of CPU. So it will be helpful as it doesn’t wait for a particular core of CPU to be available. Threads get to run on any of the core whichever available at that time.
It chooses best CPU core to be used to execute particular thread. It denies some threads to execute on some CPU cores.
Why does setting CPU Affinity require?
In certain cases, it is good to set CPU affinity. For an instance, there are two threads which use cache intensively. So to prevent cache coherence problem, it is preferable to run these two threads on separate cores that don’t shares common cache.
Suppose particular cache is shared by two cores. If a cache-intensive thread is running on two cores, execution of both threads on two cores can depend on the availability of shared cache resource. So it is preferred to use these two threads on two cores that do not share common CPU.
Like a cache, the same situation can have occurred for other shared resources like memory banks, IO devices…
To be in safer side, CPU affinity is used.
In case if there are multiple “hungrier” threads are running. If the important thread comes, Affinity is useful to set and reserve a core.
Affinity helps to ensure that thread always runs on the “best” core.
Sometimes it is worth to deny a process to run on the particular core of CPUs or CPUs itself. In this case, even if the CPU core is available to run the process, it will not run. The Process even will not be put on the ready list of denied CPUs.
Many times it is good to wait for particular CPUs and get the work done well.
How to use and setting CPU Affinity in Windows OS?
Setting Affinity is nothing but the scheduling a process on a certain subset of CPUs.
Setting CPU affinity is also called as CPU pinning.
- Go to the Task Manager
- Click on processes tab
- Select and right click on the process that you want for Setting CPU Affinity
- Click on Go to detail
- Detail of the process will be displayed.
- Select process for setting CPU affinity. You can differentiate different processes using different process ID (PID).
- You can see the list of available CPUs for setting CPU Affinity.
- As I am using Core 2 duo processor, there are 4 processors are available. By default, all the processes will be selected. It indicates process can run on any of the available processors.
Note: All the screenshots has taken on Windows 8.1 OS with core 2 duo processor.
If you have any questions for Setting CPU Affinity, feel free to share query in the comment section below.