views:

299

answers:

1

I had a problem a while ago whilst using Solaris that I wanted to limit the number of CPUs that Java could see (that was returned by Runtime.getRuntime().availableProcessors(). I thought I had it and I was given some info for Linux and took it on faith. I was told that using taskset would limit the number of CPUs that java would use (which is true, it does) but it does not change what is returned by availableProcessors().

I've searched quite a bit to find some way of changing this availableProcessors() return value and I haven't managed to find anything. Does anyone know a way to limit it or why taskset -c 0,1 java -cp ./ Test would not work.

Test.java

public class Test {
  public static void main(String args[]) {
    System.out.println(Runtime.getRuntime().availableProcessors());
  }
}

Cheers

A: 

Not surprising that taskset won't work here, since it's a command that sets scheduler parameters, whereas availableProcessors() would not have any reason to query the scheduler.

Note that in your example, taskset isn't merely limiting you to two processors; it is limiting you to two specific processors (0 and 1).

I wonder if you can use crosscutting (AOP) or byte code generation to wrap the availableProcessors() call?

jdigital