Java-Thread-Affinity

Look! there's a flower!·2025년 4월 9일

source location: https://github.com/OpenHFT/Java-Thread-Affinity

1. isolcpus

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):

1) add sudo vi /etc/default/grub

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"

2) update GRUB configuration

$ sudo update-grub

3) sudo reboot

4) check CPU isolation

cat /proc/cmdline

2. Install java SDK and JNA

1) install java

$ 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

2) install JNA

$ sudo apt-get install libjna-java

3. Now work with Java-Thread-Affinity

1) assumption

working directory: ~/works
Java-Thread-Affinity: ~/works/Java-Thread-Affinity
Sample: ~/works/Java-Thread-Affinity/sample

2) git clone

$ cd ~/works
$ mkdir ~/works
$ git clone https://github.com/OpenHFT/Java-Thread-Affinity.git

3) install dependency

$ cd ~/works/Java-Thread-Affinity
$ mvn dependency:copy-dependencies -DoutputDirectory=lib

4) prepare simple sample

$ 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");
   }
}

5) compile and run

$ 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

profile
Why don't you take a look around for a moment?

0개의 댓글