Setting processor affinity using taskset

Setting processor affinity can be very useful, for example where you have several processes running on a multi-processor server but you have one process that is important enough to warrant giving it an exclusive processor, while letting other less important processes use other CPUs.  Setting the affinity for a process will also improve cache performance by not allowing a process to ‘bounce’ between available processors.

Changing the processor affinity of a running process

Do your thing to see the running process:

<br />
$ ps -ef | grep jboss<br />
orfels 26080 26056 0 07:03 ? 00:00:00 /usr/local/java/bin/java<br />

Or more reasonably:

<br />
$ pgrep<br />
26080<br />

Here you can see that the PID (process ID) of Jboss is 26080.

Set Jboss to use only the first CPU

<br />
$ /usr/bin/taskset -c 0 -p 26080<br />

What we did here was to set the process with ID 26080 to use CPU number zero, i.e. the first CPU.

  • -c or –cpu-list is used to specify the CPU by number. It allows one CPU, more than one separated by commas, or a range separated by a hyphen.
  • -p is used to specify the running process which should be assigned to the CPU(s).

To confirm the changes, check which processor the process is using:

<br />
$ taskset -c -p 26080<br />
pid 26080's current affinity list: 0<br />

More examples

Set Jboss to use the first three CPUs in a multi-processor server:

The three options are comma-separated.

<br />
$ /usr/bin/taskset -c 0,1,2 -p 26080<br />

Or a range of processors can be specified

<br />
$ /usr/bin/taskset -c 0-2 -p 26080<br />

So far we have only looked at changing a process that is running. You can add taskset to a service startup script to have it included on starting a process

<br />
$ vi /etc/init.d/jboss</p>
<p>...<br />
echo "starting jboss, setting CPU affinity to 4th CPU..."<br />
        /usr/bin/taskset -c 3 /usr/local/jboss-4.0.5.GA/bin/ start<br />
...<br />

Be Sociable, Share!

No related posts.