Sunday, October 14, 2012

CPU affinity

taskset - retrieve or set a process's CPU affinity

Usage: taskset [-p] [MASK] [PID | PROG ARGS]
   Set or get CPU affinity
      -p Operate on an existing PID


Description
MASK:
   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.

For example:
   * 1 is CPU core #0 (1st processor)
   * 3 is CPU core #0 and #1
   * 4 is CPU core #2 (3rd processor)

To set the processor affinity of process 112 to CPU core #0 type following command:
   [ ]# taskset -p 1 112
To set the processor affinity of process 112 to CPU core #1 type following command:
   [ ]# taskset -p 2 112
To set the processor affinity of process 112 to CPU core #2 type following command:
   [ ]# taskset -p 4 112
To set the processor affinity of process 112 to CPU core #3 type following command:
   [ ]# taskset -p 8 112

Example:


[ ]# ./stress -c 2
[ ]# ps aux
 ......
 188 root       0:00 ./stress -c 2
 189 root       0:05 ./stress -c 2
 190 root       0:06 ./stress -c 2
 192 root       0:00 ps aux

[ ]# htop
 1  [################100.0%] Tasks: 10, 0 thr, 39 kthr; 3 running
 2  [################100.0%] Load average: 2.06 1.44 1.10
 3  [                  0.0%] Uptime: 01:10:55
 4  [##**             14.3%]
 Mem[||******       5/122MB]
 Swp[                 0/0MB]

# taskset -p 8 189
pid 189's current affinity mask: f
pid 189's new affinity mask: 8
# taskset -p 8 190
pid 190's current affinity mask: f
pid 190's new affinity mask: 8

[ ]# htop
 1  [                  0.0%] Tasks: 10, 0 thr, 39 kthr; 3 running
 2  [                  0.0%] Load average: 1.47 1.08 0.95
 3  [##**             12.7%] Uptime: 01:13:26
 4  [################100.0%]
 Mem[||******       5/122MB]
 Swp[                 0/0MB]