setting the CPU affinity for a whole JVM on Linux

A

alexandre_paterson

Hi,

I decided (curiosity killed the cat) to do some testing
with the "CPU affinity" on my Linux system.

I read this (old, from 2003) article and compiled the
program they provide:

http://www.linuxjournal.com/article/6799

(the article explains how to use CPU affinity under
Linux and also gives some scenarios--there are
many others-- about *why* you would want to do
such a thing).

I then created a busy-looping ("single-threaded")
bash script and ran it. Sure enough my system
monitor (gkrellm) showed one of my two cores
being 100% used.

Then I could simply do :

..../alex # cpubind 13264 1

pid 13264's old affinity: 00000003 (bitmask, 3 means core 0 and
core 1)
pid 13264's new affinity: 00000001

and "cpubind 13264 2" and I could
see the busy-looping task consuming
100% of one core "jumping" from one core
to the other (and then staying on the
assigned core).

So CPU affinity is working, this is exactly
the result I wanted to see.

However I couldn't make it work for a Java
process and all its threads.

Did anyone here ever got CPU affinity to
work with Java on a Linux system and
if yes, how?

Does CPU affinity work under Windows
for a Java application? (task manager/
process/right-click on a Java process/
"set affinity")

(I've got no "real" multi-cores/multi-cpus Windows
machine in here, only VMWare ones).

For anyone running (native) Windows on
a multi-core machine, a simple:

while ( Math.abs(42) > 0 ) {}

does the trick to max one CPU.

(I know, I know, "while ( true ) {}" could do,
but my IDE would complain about my bad
coding practices ;)

Any infos on this appreciated as always,

Alex
 
A

alexandre_paterson

replying to myself...

Actually newew Linux distros have a command called "taskset",
which does the same as the C program I compiled.

But simply calling it using the PID of the Java program I want
to bind to some processor(s) ain't working.

However, if I get the IDs of all the threads belonging to
this program, I can force the CPU affinity I want.

For example:

# ps -efL | grep java

nonet 16696 7711 16696 0 12 Jan.. pts/5 00:00:00 java -jar
infiniteloop.jar
nonet 16696 7711 16697 99 12 Jan.. pts/5 00:04:29 java -jar
infiniteloop.jar
nonet 16696 7711 16698 0 12 Jan.. pts/5 00:00:00 java -jar
infiniteloop.jar
nonet 16696 7711 16699 0 12 Jan.. pts/5 00:00:00 java -jar
infiniteloop.jar
nonet 16696 7711 16700 0 12 Jan.. pts/5 00:00:00 java -jar
infiniteloop.jar

If I then invoke "taskset -p 16696 1", "taskset -p 16797 1" etc.
for all the threads then I can indeed have the CPU affinity working.

But... I'd have to do this everytime the JVM spawns a new Linux
thread.

Any idea as to how to make this work by only setting the CPU once for
a whole JVM under Linux?

Alex
 
R

Roger Lindsjö

Did anyone here ever got CPU affinity to
work with Java on a Linux system and
if yes, how?

I used taskset on my 4 cpu machine, and by using:
taskset -pc 0 java net.tilialacus.BusyThreads
I gbound all threads to cpu 0. (BusyThreads just runs a few threads with
while(true);

However, if I started the process first, then I have to bind all threads
manually to the CPU. Using ps -eLf I could list all threads (10 or so)
and I could bind each of them separately. I didn't find a way for
taskset to climb the process/thread tree automatically.
 
A

alexandre_paterson

I used taskset on my 4 cpu machine, and by using:
taskset -pc 0 java net.tilialacus.BusyThreads
I gbound all threads to cpu 0. (BusyThreads just runs a few threads with
while(true);

However, if I started the process first, then I have to bind all threads
manually to the CPU.

Excellent, it's working here too if I launch the program from taskset
instead of trying to "taskset" it after its launch.

Thanks a lot for trying on your quad-core and answering my question :)

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top