This library spoofs the processor count in the JVM allowing applications that auto-configure thread pools based on detected CPU core count to configure to a value that you specify.
This library was presented as an answer on Stack Overflow by apangin in 2014, so it falls under the CC-BY-SA.
Run GNU Make:
$ make
This will output the library libnumcpus.so
In the environment scope in which you execute your Java application, set
the enviroment variable _NUM_CPUS
to the value of the number of CPUs
that you want to visualize to Java. Then set the path in LD_PRELOAD
(or LD_PRELOAD_64
on illumos/SmartOS/Solaris) to the libnumcpus.so
library. For example:
public class AvailableProcessors {
public static void main(String... args) {
System.out.println(Runtime.getRuntime().availableProcessors());
}
}
$ LD_PRELOAD=/path/to/libnumcpus.so _NUM_CPUS=2 java AvailableProcessors
2
Included in this repository is a script that will query a zone to find out a non-pathological (and non-optimized) value for the number of CPU cores to run an application with. This utility can be combined with the libnumcpus
as so:
#!/usr/bin/env sh
# This shell script launches my application
LD_PRELOAD=/path/to/libnumcpus.so _NUM_CPUS=$(/path/to/proclimit.sh) java AvailableProcessors