Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a cpu core to a process - Linux

Tags:

Is there any way to force a process with specific PID, to be executed and run on only one of the cpu s of a server? I know that there is a command like this

taskset -cp <Cpu_Number> <Pid> 

but the above command does not work on my system. So please let me know if there is any other command.

like image 699
Admia Avatar asked Nov 30 '15 09:11

Admia


People also ask

How do I dedicate CPU cores to a program?

To set CPU Priority, right-click on any process in Task Manager and select Go to details. Next, right-click on the highlighted process and click on Set Priority. Now, choose priority from the list that pops up. If you want your process to run as soon as it needs, select Realtime.

How do I run a process on a specific core in Linux?

The command to run or assign a specific command to a particular core is taskset . Embed it in your startup script or use from the command line like: taskset -c 0,5 command_name -c is a list of one or more CPUs to run the command on; in this case, core 0 and 5.

How to assign a process to a core in Linux?

Linux: Assign Process to CPU Core 1 Install taskset on Linux. The taskset utility is part of the “ util-linux ” package in Linux. ... 2 Retrieve CPU Affinity of Running Process. ... 3 Set CPU Affinity of Running Process. ... 4 Launch a Process on Specific CPU Cores. ... 5 Dedicate / Restrict CPU Core to Process. ...

How to pin a running process to a particular CPU core?

Follow the below command to pin a running process to particular CPU cores. # taskset -p <COREMASK> <PID> # taskset -cp <CORE-LIST> <PID> For example, assign a process to CPU core 0 and 4. # taskset -p 0x11 6075 pid 6075's current affinity mask: ff pid 6075's new affinity mask: 11 OR # taskset -cp 0,4 6075

How to run a program on specific CPU cores in Linux?

To run a process or program on specific CPU cores you, you can use taskset. It is a command line tool for setting a process CPU affinity in Linux. Taskset tools is part of util-linux package in Linux, and it comes with pre-installed in most Linux distros by default.

How to find which process is assigned to which CPU core?

To demonstrate, let me consider crond process with process ID 24868. You can use ps command to find out which process is currently assigned to which CPU core. Lookout for the PSR field in ps command output. The above command output indicates that the process with PID 24868 (crond) is assigned to CPU core 2.


1 Answers

There are two ways of assigning cpu core/cores to a running process.

First method:

taskset -cp 0,4 9030 

Pretty clear ! assigning cpu cores 0 and 4 to the pid 9030.

Second Method:

taskset -p 0x11 9030 

This is a bit more complex. The hexadecimal number that follows -p is a bitmask. An explanation can be found here, an excerpt of which is given below :

The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. Not all CPUs may exist on a given system but a mask may specify more CPUs than are present. A retrieved mask will reflect only the bits that correspond to CPUs physically on the system. If an invalid mask is given (i.e., one that corresponds to no valid CPUs on the current system) an error is returned. The masks are typically given in hexadecimal.

Still confused? Look at the image below :

enter image description here

I have added the binaries corresponding to the hexadecimal number and the processors are counted from left starting from zero. In the first example there is a one in the bitmask corresponding to the zeroth processor, so that processor will be enabled for a process. All the processors which have zero to their corresponding position in the bitmask will be disabled. In fact this is the reason why it is called a mask.

Having said all these, using taskset to change the processor affinity requires that :

A user must possess CAP_SYS_NICE to change the CPU affinity of a process. Any user can retrieve the affinity mask.

Please check the Capabalities Man Page.

You might be interested to look at this SO Question that deals with CAP_SYS_NICE.

My Resources

  1. Tutorials Point

  2. XModulo

like image 51
sjsam Avatar answered Oct 21 '22 02:10

sjsam