Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > setting the CPU affinity for a whole JVM on Linux

Reply
Thread Tools

setting the CPU affinity for a whole JVM on Linux

 
 
alexandre_paterson@yahoo.fr
Guest
Posts: n/a
 
      01-28-2009
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











 
Reply With Quote
 
 
 
 
alexandre_paterson@yahoo.fr
Guest
Posts: n/a
 
      01-28-2009

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

 
Reply With Quote
 
 
 
 
Roger Lindsjö
Guest
Posts: n/a
 
      01-28-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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.

--
Roger Lindsjö
 
Reply With Quote
 
alexandre_paterson@yahoo.fr
Guest
Posts: n/a
 
      01-28-2009
On Jan 28, 9:42 pm, Roger Lindsjö <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 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.


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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System Process CPU Affinity on 64-bit O/S Ged Windows 64bit 5 04-13-2006 05:00 PM
How to maintain affinity in webgarden (multi CPU)? SevDer ASP .Net 1 01-30-2006 03:48 AM
Windows multi-CPU "Set Affinity" can this be applied as a saved setting? Atreju Computer Support 10 08-18-2005 03:25 AM
RE: Setting CPU Affinity Laughlin, Joseph V Python 0 05-04-2004 07:50 PM
Setting CPU Affinity Laughlin, Joseph V Python 1 05-04-2004 11:51 AM



Advertisments