13

In some purpose it is needed to make JVM think about it runs on machine with Ncores on board instead of real number of cores (e.g. 4 cores instead of 16).

JVM runs under some Linux build, based on Mandriva/Red Hat Linux core.

This question is borderline case because I expect various solutions of this problem. This is not pure linux-administration question, and it isn't pure programmer's question.

So... any ideas?

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • `man cpuset` should help. – Joachim Isaksson Mar 30 '14 at 08:30
  • @JoachimIsaksson It would be nice, and I think helpful, if you could provide some live example how to do this with running `java` jar – Andremoniy Mar 30 '14 at 08:31
  • 1
    Should the JVM only be scheduled to 4 cores? Or should the JVM think there are only 4 cores, so that it starts only 4 threads for parallel execution? Does it have to be a process-specific setting, or is a (temporary) system-wide change also okay? – nosid Mar 30 '14 at 09:06
  • @nosid `JVM` should think and see only `4` cores instead of their real number – Andremoniy Mar 30 '14 at 16:35
  • 1
    If cgroups is available on Mandriva use it. With cgroups, you can allocate resources per process group. In your case you wanted only 4 cpus allocated to your JVM. Here's a wiki about cgroups http://en.wikipedia.org/wiki/Cgroups – alvits Mar 30 '14 at 19:04

2 Answers2

11

In order to make Runtime.getRuntime().availableProcessors() return whatever you want, you can override JVM_ActiveProcessorCount function using LD_PRELOAD trick. Here is a tiny program to do this:

#include <stdlib.h>
#include <unistd.h>

int JVM_ActiveProcessorCount(void) {
    char* val = getenv("_NUM_CPUS");
    return val != NULL ? atoi(val) : sysconf(_SC_NPROCESSORS_ONLN);
}

First, make a shared library of this:

gcc -O3 -fPIC -shared -Wl,-soname,libnumcpus.so -o libnumcpus.so numcpus.c

Then run Java as follows:

$ LD_PRELOAD=/path/to/libnumcpus.so _NUM_CPUS=2 java AvailableProcessors
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Note that JVM also uses `sysconf()` to calculate the number of compiler and GC thread. However you can set the number of JVM threads explicitly using `-XX:CICompilerCount=n -XX:ConcGCThreads=m -XX:ParallelGCThreads=m` – apangin Mar 31 '14 at 13:23
  • In Java 9 it will actually inspect the cpuset on Linux. – eckes Sep 13 '16 at 10:08
  • Also @apangin pointed to [JDK-8u-121](https://bugs.openjdk.java.net/browse/JDK-6515172) that fixes this problem. – Kedar Mhaswade Mar 31 '17 at 13:11
6

The following Java program prints the number of processors as seen by the Java VM:

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

If I execute this program on my home computer, it prints 4, which is the actual number of cores (including hyper threading). Now let's trick the Java VM into believing there are only two processors:

$ echo '0-1' > /tmp/online
$ mount --bind /tmp/online /sys/devices/system/cpu/online

If I run the above program again, it prints 2 instead of 4.

This trick affects all processes on your system. However, it's possible to restrict the effect only to certain processes. Each process on Linux can have its own namespace of mount points. See for example the section Pre-process namespaces in the man page of mount(2). You can for example use lxc to start new processes with their own mount namespace.

nosid
  • 48,932
  • 13
  • 112
  • 139