Many Times,
there will be a need of multiple processors on a server system to provide better
performance and availability. When we use more processors, a subset of these
can be made available to specific applications. This can be useful for some
applications which are highly timing-sensitive, or when it is desired otherwise
that an application always have one or more processors available to it. In Linux,
it is possible to effectively dedicate a subset of processors to servicing one
or more applications, which may help improve performance.
In
Order to find out how many processors are available, we can use the proc file
system like,
localhost:root-~
$ cat /proc/cpuinfo | grep processor
processor : 0
processor : 1
I
Have 2 processors available. In Order to find on which processor a process is running,
we can use ‘ps –F ax’ like this,
UID PID
PPID C SZ RSS PSR STIME TTY STAT
TIME CMD
root 1
0 0 2591
640 0 May31 ? Ss
0:06 init [3]
root 2
1 0
0 0 0 May31
? S< 0:05 [migration/0]
root 3
1 0
0 0 0 May31
? SN 0:00 [ksoftirqd/0]
root 4
1 0
0 0 0 May31
? S< 0:00 [watchdog/0]
root 5917 1 0 1315 122 1 Aug23
? Sl 1:09 /software/java/jdk1.6.0_16/bin/java
-Djbs.name=TMP-C3 -server -Xms256m -Xmx256m –
We
can see an Column in the output ‘PSR’, which actually says on which processor the
process is running.
So
the last java process is running on Processor 1.Linux provides the ability to
change the processor for a process at runtime. This can be done by using ‘taskset’
command like,
localhost:root
-~ $ taskset -p -c 0 5917
pid
5917's current affinity list: 1
pid
5917's new affinity list: 0
I
have made the process that is running on the Processor ‘1’ to move to processor
‘0’.
According
to Man pages,
taskset
is used to set or retrieve the CPU affinity of a running process given its PID
or to launch a new COMMAND with a given CPU affinity. CPU affin-ity is a scheduler property that
"bonds" a process to a given set of CPUs on the system.
Now
once the taskset is done, we can check the process
UID PID PPID C
SZ RSS PSR
STIME TTY STAT TIME CMD
root 5917
1 0 131520 122744 0 Aug23 ? Sl 1:09 java32/jdk1.6.0_16/bin/java
-Djbs.name=TMP-C3
Some
More Examples,
To
set an application which is already running, having pid number 12345, to
execute on CPU cores 5-8 (CPU ID's 4-7):
#
taskset -p -c 4-7 12345
or
this would be equally valid syntax:
#
taskset -p -c 4,5,6,7 12345
Start
application with specified CPU affinity
#
taskset -c 4-7 /opt/foobar1.3/my-special-program
Note:
The CPU affinity of a child process created via the fork system function call
is inherited from its parent process, so if the application launches several
related processes via fork, they will also run on the isolated CPUs.
Happy
Learning , More To Come J