|Changing CPU affinity using Htop - example|
Tools like 'pbzip2' lets you manually change the threads to be used, before the execution, so you can free-up a CPU core or two, thus improving the OS's responsiveness. But sometimes, I forget do that, plus, not all multithreaded tools let you change the number of threads to be used either.
Now as most knows, in these occasions, the feature called CPU affinity comes in handy, as it lets us bind a process in a way, that it will be executed by using only a certain number of CPU cores (supports changing the CPU cores that should be used even after the process is running from the background).
So what I usually do is, I manually reduce the number of available cores for the processor intensive multithreaded utility, and as soon as I do that, the OS's responsiveness comes back.
Unlike in Windows, the system monitor utility in Ubuntu does not support it, and I use, another two built in tools that come pre-installed with Ubuntu for that. So I thought, writing a simple 'how to' guide might come in handy for someone who is new to GNU/Linux.
But please remember that, reducing available CPU cores for multithreaded applications will decrease their performance (obviosusly).For this example, I'm using 'bpzip2' as the utility that I want to change the CPU affinity, but of course, you can do this to any utility, by simply following the below steps.
In Ubuntu, the most easiest way to do this is to use 'Htop' (shown in the first screenshot) as it comes with somewhat a user friendly interface, also supports the mouse thus, you can basically track down the process from its interface and also change the affinity directly through there.
But, changing processor affinity for multithreaded processes is a bit hectic under 'Htop', as you will have to change the affinity value of each thread, manually.
Luckily, as mentioned above, by using two command-line tools, we can can achieve the same task extremely easily (I also assume that you are trying to do this when the multithreaded app is already running from the background).
The first tool is called 'top', which is nothing but a simple process monitor. And the other is called 'taskset' which lets us change the CPU affinity value.
Let's do it ...
Before changing the CPU affinity of a process, first we need to find the 'id' of that particular process (PID)'. Once that is retrieved, the rest is extremely simple, and all it requires is a single command.
Step 1: Finding the 'Process ID'Assuming that 'pbzip2' is running, consuming almost all of my CPU power, and I want to make it only use 3 CPU cores, then as said above, I need to find its 'process id' first. So, I open your Terminal and enter the below command.
top -d 1'-d 1' is optional (it just sets the update interval to a second). Now, because these are CPU intensive tools, they should be listed at the top of the process list in 'top', which makes locating them a breeze (even if you don't know its exact name).
In 'top', you can see the running processes and their names under the 'COMMAND' field, and their 'process id' under 'PID' field. As for this example, as highlighted below, my utility is listed as 'pbzip2' and its process id is '5964'.
|Finding the 'process id' and process name using 'top'|
Step 2: 'Binding' the process to a certain CPU core (s).As soon as I got its 'PID', then I can use 'taskset' tool to change the CPU affinity value.
I can close the 'top' utility (by pressing keyboard shortcut 'q') and use its Terminal window or, can open a new window and simply enters the below command, to get the job done.
taskset -pac 0,1,2 5964
|'taskset' running in Ubuntu 12.10|
Simple explanation of the above command ...
*. 'taskset' is the utility that does this job and '-pac' are its command-line arguments that enable few of its built in options.
*. The 'p' option lets us use 'process id' of a program for changing the CPU affinity value.
*. The 'a' option' enables applying the user requested changes to all of the threads of that particular program, so unlike with 'Htop', we do not have to set it in each thread, individually.
*. The 'c' option is used for entering the CPU cores that we are going to bind that program with, which is followed by the numbers '0,1,2' (there is a comma that separates each core number).
As a general rule, the first CPU core is represented by 0 (zero), second one is called 1, third one is called 2 etc.
Note: If you don't know how many cores your CPU has, then simply enter the below command and from its text output, take a note of the number under the option called 'CPU( S ) :'. This number lists the total CPU core that you have.
lscpu(4 in this example. It is 2 cores actually, but because they are multithreaded, they appear as 4 cores).
|Using 'lscpu' to get certain CPU related details in Ubuntu|
taskset -pac 0,1,2,3,4,5 process-id
*. Lastly, the number '5964', is the ID of the process that we want to hunt down ;-).
As soon 'taskset' changes the CPU affinity, through the Terminal it gave me an output (shown below), that lists the threads of 'pbzip2' and their old and new CPU affinity values.
|'taskset' showing the old and new CPU affinity values|
Note: As mentioned, this is an example, so make sure to replace the CPU core numbers and 'process id' according to the utility that you are using.And now I can confirm whether this has worked or not, by using the built in graphical system monitor or using the 'top' utility. For this example, under CPU usage, you should see that, except for the fourth core, all others are occupied by 'pbzip2'.
|'pbzip2' running without using the CPU core 4|
|System monitor in Ubuntu showing that the CPU core 4 is under low stress|
Though the article might make it look complicated, once you get a hold of it, it is a two step based, extremely simple process. Good luck.
About the Author: Gayan is computer geek who has been using various GNU/Linux distributions for the past 10 years. He runs a blog called 'HecticGeek.com', where he writes primarily about open-source desktop applications and simple tips & tricks for Ubuntu Linux users (including occasional Windows applications).