source location: https://github.com/OpenHFT/Java-Thread-Affinity
Java-Thread-Affinity requires that you first isolate some CPU’s.
Once a CPU core is isolated, the Linux scheduler will not use the CPU core to run any user-space processes.
To isolate the 1st and 3rd CPU cores (CPU numbers start from 0) on your system, add the following to the kernel command line during boot:
isolcpus=1,3
For GRUB bootloader (most common on Linux):
find the line that starts with "GRUB_CMDLINE_LINUX_DEFAULT"
and add "isocpus" --> just add it to the end (keep any exisiting paramters that are already there)
ex) GRUB_CMDLINE_LINUX_DEFAULT="quiet splash isolcpus=1,3"
$ sudo update-grub
cat /proc/cmdline
$ sudo apt install openjdk-11-jdk-headless
// check JAVA_HOME
$ update-alternatives --display java
// it should display java related files
// typical directory for JAVA_HOME is /usr/lib/jvm
$ export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
// or make it permanent
$ echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' >> ~/.bashrc
$ source ~/.bashrc
$ sudo apt-get install libjna-java
working directory: ~/works
Java-Thread-Affinity: ~/works/Java-Thread-Affinity
Sample: ~/works/Java-Thread-Affinity/sample
$ cd ~/works
$ mkdir ~/works
$ git clone https://github.com/OpenHFT/Java-Thread-Affinity.git
$ cd ~/works/Java-Thread-Affinity
$ mvn dependency:copy-dependencies -DoutputDirectory=lib
$ mkdir ~/works/Java-Thread-Affinity/sample
$ cd ~/works/Java-Thread-Affinity/sample
$ cat > ATest.java
import net.openhft.affinity.AffinityLock;
public class ATest {
public static void main(String[] args) {
System.out.println("Starting CPU core affinity example");
System.out.println("Initial CPU layout: \n" + AffinityLock.dumpLocks());
System.out.println("Example completed");
}
}
$ cd ~/works/Java-Thread-Affinity/sample
$ javac -cp "../affinity/target/affinity-3.27ea1-SNAPSHOT.jar" ATest.java
$ java -cp ".:../affinity/target/affinity-3.27ea1-SNAPSHOT.jar:../affinity/lib/*" ATest.java
Starting CPU core affinity example
Initial CPU layout:
0: General use CPU
1: Reserved for this application
2: General use CPU
3: General use CPU
Example completed