Tuesday, April 25, 2017

How to run a program on specific CPU core on linux ?

Now i am using watch command set  taskset to core 3.
Before going set the taskset first find out the pid of the particular process using following commands.

[root@linuxforfreshers.com~]# ps -ef | grep watch
root     14063 27683  0 16:29 pts/47   00:00:00 watch -db ifconfig
root     14172 14070  0 16:30 pts/49   00:00:00 grep --color=auto watch

or

[root@linuxforfreshers.com~]# pidof watch
14063

Example:

[root@linuxforfreshers.com~]# taskset 03 -p 14063

Verifying

[root@linuxforfreshers.com~]# taskset -p 14063
pid 14063's current affinity mask: 3

or

ps -o pid,psr,comm -p <pid>

Example:

[root@linuxforfreshers.com~]#  ps -o pid,psr,comm -p 14063
  PID PSR COMMAND
14063   3 watch

If u want to bind the particular process to particular core u can use following command.

taskset -pc 1 <PID>

Example:
[root@linuxforfreshers.com~]# taskset -pc 1 14063

Where p is -p, --pid
              Operate on an existing PID and do not launch a new task.
             -c, --cpu-list
              Specify a numerical list of processors instead of a bitmask.  The numbers are       separated by commas and may include ranges.  For example: 0,5,7,9-11.

Verifying

[root@linuxforfreshers.com~]# ps -o pid,psr,comm -p 14063
  PID PSR COMMAND
14063   1 watch

Or

[root@linuxforfreshers.com~]# taskset -p 14063
pid 14063's current affinity mask: 1


If We want to dedicate a whole CPU core to a particular program and no other then your process should use this core.
Then use "isolcpus" kernel parameter in grub option.

GRUB_CMDLINE_LINUX_DEFAULT="cpuidle.off=1 idle=poll isolcpus=1-5 nohz_full=5 maxcpus=6"

update-grub

Note: below are meaning

cpuidle.off=1 (Do not make cpu idle)
isolcpus=5  (Isolate cpu core are 1-5)
maxcpus=6 (Use only 6 core (out of 8 cores of system)) 
idle=poll (Poll forces a polling idle loop)

No comments:

Post a Comment