In some purpose it is needed to make JVM
think about it runs on machine with N
cores 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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With