What is and How does Setting CPU Affinity Improve Performance?
Whenever you run any application on your computer, it creates an instance of that application, called a process. And one process may contain multiple threads.
These process and threads get executed on CPU cores.
If you are reading this article, you might be already aware of the process and threads in the operating system.
There is one more term very important to know about the execution of the program- CPU affinity.
What is CPU affinity?
The 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 the best CPU core to be used to execute a particular thread. Sometimes, it denies some threads to execute on some CPU cores.
This is also called as Process Affinity or Processor Affinity.
Why does setting CPU Affinity require?
In certain cases, it is good to set CPU affinity.
For 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 share the 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 on the 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 scheduling a process on a certain subset of CPUs.
Setting CPU affinity is also called as CPU pinning.
This setting will allow running any of your application process on the selected CPU core.
Follow the simple steps.
- Go to the Task Manager.
- Click on the Processes tab.
- Select and right click on the process that you want for Setting CPU Affinity.
- Click on Go to details.
- Detail of the process will be displayed.
- Select the 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 the Core 2 Duo processor, there are 4 processors are available. By default, all the processes will be selected. It indicates the 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. Similar steps you have to follow for Windows 7 or Windows 10.
If you have any questions for Setting CPU Affinity, feel free to write a query in the comment section below. Keep learning!