Skip to content

dekobon/libnumcpus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libnumcpus.so

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.

Building

Run GNU Make:

$ make

This will output the library libnumcpus.so

Installing

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

Determining Number of CPUs Base on CPU CAP

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