setting the CPU affinity for a whole JVM on Linux

Discussion in 'Java' started by alexandre_paterson@yahoo.fr, Jan 28, 2009.

  1. Guest

    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
    , Jan 28, 2009
    #1
    1. Advertising

  2. Guest

    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
    , Jan 28, 2009
    #2
    1. Advertising

  3. 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ö
    Roger Lindsjö, Jan 28, 2009
    #3
  4. Guest

    On Jan 28, 9:42 pm, Roger Lindsjö <> wrote:
    > 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
    , Jan 28, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krishnan

    Processor affinity - HELP

    Krishnan, Jun 8, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    344
    Krishnan
    Jun 8, 2005
  2. SevDer
    Replies:
    1
    Views:
    489
    Steven Cheng[MSFT]
    Jan 30, 2006
  3. Laughlin, Joseph V

    Setting CPU Affinity

    Laughlin, Joseph V, May 3, 2004, in forum: Python
    Replies:
    1
    Views:
    654
    Mark Hammond
    May 4, 2004
  4. Laughlin, Joseph V

    RE: Setting CPU Affinity

    Laughlin, Joseph V, May 4, 2004, in forum: Python
    Replies:
    0
    Views:
    649
    Laughlin, Joseph V
    May 4, 2004
  5. Replies:
    0
    Views:
    136
Loading...

Share This Page