Modern CPUs with multiple cores can handle several programs concurrently by switching program loads among cores & threads. Such switching processes cause unoptimized cache performance.
Setting Processor Affinity is an effective solution in this case, which is a simple process that a new Linux user can perform effortlessly.
So, a step-by-step guide can smoothly direct you to CPU pinning, AKA Processor Affinity setup.
How to set Processor Affinity on Linux
Since the Terminal is the most obvious function for a renowned Open Source OS like Linux, it owns administrative control of the entire system. In addition, some simple commands can execute a way more complicated task in a snap.
For becoming an open source operating system, it’s much easier to tweak, set or set a program. That’s the reason many users prefer using Linux.
Likewise, the Taskset Command can easily set CPU affinity for a program and seems like child’s play. It’s so easy to understand and execute that you can do it yourself, right after you finish this article once.
Here are the methods with a step-by-step guide and brief command elaboration:
Set CPU Affinity using Taskset Command
The taskset command is short, easy to understand, precise, and worthwhile. But first, you need to know about CPU core count. A Quad CPU has four cores consecutively named cpu 0, cpu 1, cpu 2, and cpu 3.
Again, an eight-core CPU has Cores starting from cpu 0 to cpu 7, following the Unix Numbering System. Now that you know how cpu cores are named, you can allocate or dedicate a core or multiple cores for a program.
Here are the steps to set processor Affinity using the Teskset command:
- Launch Terminal with the Search bar’s help.
Single Core Pinning Command:
- Follow this command format:
For Example: if you want to bind cpu 2 with Audacity Software, you just need to insert the Core serial number replacing {Core Number} & and vlc replacing {Program Name}.
Command example: taskset -c 2 audacity
Multiple Core Pinning Command:
- Here, you need to assign core numbers一 Such as 2,3 replacing {Core Numbers}, then replace {Program Name} with firefox as an example.
Command example: taskset -c 2,3 firefox
Moreover, you can insert core numbers from 1–7 to pin the program ‘Steam’. Just replace {Core Number} with 1-7, then again steam replacing {Program Name}.
Command example: taskset -c 1-7 firefox
This way, you can easily set CPU affinity with a single core, randomly chosen cores, and serially selected cores.
Affinity Settings for a Running Program
To set CPU affinity for the currently running programs, you’re required to know about PID (Process Identification Number). Then you can follow the previous guide to set cores according to your need.
And rest alteration will appear in the {Program Name} section, replacing it with PID.
Here is the step-by-step process to set the CPU Affinity for a currently running program:
- Run Terminal Using Linux Search Bar.
- Use the command ‘top’ in the terminal.
- Find the PID of the program you want to pin the CPU.
- Type & Run the Command following the instruction:
Simply run this command following previous instructions to manipulate {Core Number} section. And replace {PID} section with the specific program’s PID.
Suppose, you want to pin a running program VLC media player with a PID of 3688, now type the Core numbers as per need & PID, And done!
The command should look like this 一 taskset -c 3-5 3688
It’s simple and easy if you follow this tutorial without skipping. But what if you want to know which threads or cores your current program is running on?
Here is a Pro Tip一’ taskset -cp 3688 ‘ this command will show how many and which cores are pinned or used by vlc. But you can check for any program by switching the PID in this similar command.
There are lots of commands you need to type accurately. In case you don’t have a keyboard or have an issue in it, check out the article to enable on-screen keyboard on Linux to continue your work without any problem.
Why Do We Need to Set Processor Affinity?
The necessity of setting Processor Affinity is promising for most users. It can optimize the overall performance of pinned programs. But before we know its need, we must understand how this CPU Affinity works.With the help of CPU Affinity, you can assign specific CPU threads for individual programs to prioritize usage according to program needs. This process can optimize the program’s behavior, as switching threads & cores can reduce cache efficiency.
It becomes more efficient on an intel CPU because it has efficient core and performance cores. You can set your low CPU-demanding programs to efficient cores or threads, leaving the Performance cores idle.
This way, I save some electricity while using my PC, plus my CPU generates less heat. PC becomes more responsive and frequent while opening and closing pinned apps.
Moreover, efficient use can extend CPU longevity, an organized system launch applications faster, the system response gets fluent, and a lot more.
So, you can earn benefits in many ways if you set Processor Affinity in your system. On top of that, the Linux system optimizes this process to a new level in my experience.
However, an unwise set-up of CPU affinity can leave your system in a bizarre state that you should consider before pinning programs to the CPU. The possible symptoms and precautions are explained in the following section.
But before taking a decision, check the CPU temperature on Linux to be sure about the problem in your PC.
What Problems May Arise Setting CPU Affinity?
Every program uses CPU at a different percentage, which varies over files & use cases. A media player won’t use all the cores & threads, whereas Video editing or 3D modeling software requires more.
Depending on a video file resolution, a media player like VLC Player can push its usage demand also. Besides, high-demanding video editors scale down their CPU usage, which decreases over the use case.
As a result, you can’t bind your CPU core & thread randomly. You must ensure some basic knowledge about your own CPU and the applications you want to pin.
Here are the things you must consider before setting processor affinity:
- Find out how many cores & threads your CPU has.
- You need to know which cores & threads are performance centric & which are efficient.
- Seek for CPU usage limit of the program you want to pin.
- Decide which program should run on efficient cores and which on performance.
- Leave the higher CPU-demanding programs, as they might exceed the limit.
Regarding these facts, you can successfully set Processor Affinity adequately. And enjoy the snappy experience from your system.
To pin the usage limit, you can head towards the article on limiting CPU usage process on Linux and find all your required information.
Despite that, you might wonder if I dedicate a CPU core or thread to a program, can I reallocate another program on that Core or thread?
Yes, you can, Although a program is pinned to a specific CPU thread, it won’t take the entire resource all by itself. Another program can also run with that same thread as well. Such confusing matters are answered shortly in the added FAQs section.
FAQs
Can I set CPU Affinity on Windows?
Yes, you can set Processor Affinity on windows with the help of a registry editor tweak and Task manager.
Can CPU Affinity improve Gaming FPS?
Heavy resource-demanding programs or games may not gain any noticeable performance improvement, but the overall system can improvise better user experience.
Does cache affinity cause system instability?
CPU Affinity indeed improves performance, but the amateur set-up of CPU pinning can cause system instability.
Can I set Processor Affinity in every Linux Distro?
Yes, you can set processor affinity in almost every Linux-distro, since nearly all distros have the terminal built-in.
Wrap-Ups
An organized system constantly performs better than the default settings-based system. Since Linux OSes are renowned for their Performance-Centric environment, the Processor Affinity setting adds an effective boost.
Correctly thread-allocated programs can give you a snappier user experience, making CPU pinning worth it.
We appreciate your comment on this article, as you’ll find this beneficial. And you can