[转帖]Thread Affinity

thread,affinity · 浏览次数 : 0

小编点评

**How to Lock a Specific CPU** ```java try (AffinityLock lock = AffinityLock.acquireLock(n)) {} ``` where: * `n` is the CPU ID you want to lock. **Using a Configuration File** ```java try (AffinityLock lock = AffinityLock.acquireLock("last")) { // Use the CPU ID specified in the configuration file assertEquals(PROCESSORS - 1, Affinity.getCpu()); } ``` **Setting the CPU ID in a Configuration File** You can set the CPU ID in a configuration file using a string variable: ```java String cpuId = "1"; // Replace with the actual CPU ID AffinityLock lock = AffinityLock.acquireLock(cpuId); ``` **Example Configuration File (config.properties)** ``` cpuId=1 ``` **Note:** * The ` Affinity.getCpu()` method returns the ID of the CPU that the thread is running on. * The ` AffinityLock.acquireLock()` method blocks the thread until the lock is released. * The ` AffinityLock.acquireLockLastMinus()` method returns the ID of the last CPU that was locked. * You can also use the ` AffinityLock.acquireLock(null)` method to lock any available CPU. * Setting the CPU ID directly using the `AffinityLock.setAffinity()` method is also possible, but it is not recommended for this purpose.

正文

https://github.com/OpenHFT/Java-Thread-Affinity/releases

 

 

Overview

Lets you bind a thread to a given core, this can improve performance (this library works best on linux).

OpenHFT Java Thread Affinity library

See affinity/src/test/java for working examples of how to use this library.

Changes

  • V3.2.0 - Add support for text configuration

  • V3.1.1 - Upgraded JNA dependency to 4.4.0

  • V2.0.1 - Added getThreadId for the process if of the thread.

Dependencies

Java-Thread-Affinity will try to use JNA to provide access to native thread-handling functions. JNA should be installed on your system to get the most from this library.

JNA version

Java-Thread-Affinity currently depends on JNA version 4.4.0, which in turn depends on a version of GLIBC >= 2.14. If your operating system is an old one, with a version of GLIBC released before 2011, this library will not be able to invoke native functions.

To work around this problem, fork the repository, and override the <version> tag for the artifacts jna and jna-platform in the project’s pom file.

Installing JNA on Ubuntu

sudo apt-get install libjna-java

Installing JNA on CentOS

sudo yum install jna

How does CPU allocation work?

The library will read your /proc/cpuinfo if you have one or provide one and it will determine your CPU layout. If you don’t have one it will assume every CPU is on one CPU socket.

The library looks for isolated CPUs determined by looking at the CPUs you are not running on by default. i.e. if you have 16 CPUs but 8 of them are not available for general use (as determined by the affinity of the process on startup) it will start assigning to those CPUs.

Note: if you have more than one process using this library you need to specify which CPUs the process can use otherwise it will assign the same CPUs to both processes. To control which CPUs a process can use, add -Daffinity.reserved={cpu-mask-in-hex} to the command line of the process.

Note: the CPU 0 is reserved for the Operating System, it has to run somewhere.

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. The isolated CPUs will not participate in load balancing, and will not have tasks running on them unless explicitly assigned.

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

Using AffinityLock

Acquiring a CPU lock for a thread

You can acquire a lock for a CPU in the following way:

In Java 6

AffinityLock al = AffinityLock.acquireLock();
try {
     // do some work locked to a CPU.
} finally {
     al.release();
}

In Java 7 or 8

try (AffinityLock al = AffinityLock.acquireLock()) {
    // do some work while locked to a CPU.
}

You have further options such as

Acquiring a CORE lock for a thread

You can reserve a whole core. If you have hyper-threading enabled, this will use one CPU and leave it’s twin CPU unused.

try (AffinityLock al = AffinityLock.acquireCore()) {
    // do some work while locked to a CPU.
}

Controlling layout

You can chose a layout relative to an existing lock.

try (final AffinityLock al = AffinityLock.acquireLock()) {
    System.out.println("Main locked");
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try (AffinityLock al2 = al.acquireLock(AffinityStrategies.SAME_SOCKET,
                                                   AffinityStrategies.ANY)) {
                 System.out.println("Thread-0 locked");
            }
        }
    });
    t.start();
}

In this example, the library will prefer a free CPU on the same Socket as the first thread, otherwise it will pick any free CPU.

Getting the thread id

You can get the current thread id using

int threadId = AffinitySupport.getThreadId();

Determining which CPU you are running on

You can get the current CPU being used by

int cpuId = AffinitySupport.getCpu();

Controlling the affinity more directly

The affinity of the process on start up is

long baseAffinity = AffinityLock.BASE_AFFINITY;

The available CPU for reservation is

long reservedAffinity = AffinityLock.RESERVED_AFFINITY;

If you want to get/set the affinity directly you can do

long currentAffinity = AffinitySupport.getAffinity();
AffinitySupport.setAffinity(1L << 5); // lock to CPU 5.

Debugging affinity state

For a detailed of view of the current affinity state (as seen by the library), execute the following script on Linux systems:

# change to the affinity lock-file directory (defaults to system property java.io.tmpdir)
$ cd /tmp

# dump affinity state
$ for i in "$(ls cpu-*)";
      do PID="$(cat $i | head -n1)"; TIMESTAMP="$(cat $i | tail -n1)";
      echo "pid $PID locked at $TIMESTAMP in $i"; taskset -cp $PID;
      cat "/proc/$PID/cmdline"; echo; echo
  done

  pid 14584 locked at 2017.10.30 at 10:33:24 GMT in cpu-3.lock
  pid 14584's current affinity list: 3
  /opt/jdk1.8.0_141/bin/java ...

Support Material

For an article on how much difference affinity can make and how to use it http://vanillajava.blogspot.com/2013/07/micro-jitter-busy-waiting-and-binding.html

Questions and Answers

Question: How to lock a specific cpuId

I am currently working on a project related to deadlock detection in multithreaded programs in java. We are trying to run threads on different processors and thus came across your github posts regarding the same. https://github.com/peter-lawrey/Java-Thread-Affinity/wiki/Getting-started Being a beginner, I have little knowledge and thus need your assistance. We need to know how to run threads on specified cpu number and then switch threads when one is waiting.

Answer

// lock a cpuId
try (AffinityLock lock = AffinityLock.acquireLock(n)) {

}

where n is the cpu you want to run the thread on.

OR

// lock one of the last CPUs
try (AffinityLock lock = AffinityLock.acquireLockLastMinus(n)) {

}

Question: how to use a configuration file to set the cpuId

I have the cpuId in a configuration file, how can I set it using a string?

Answer: use one of the following

try (AffinityLock lock = AffinityLock.acquireLock("last")) {
    assertEquals(PROCESSORS - 1, Affinity.getCpu());
}
try (AffinityLock lock = AffinityLock.acquireLock("last-1")) {
    assertEquals(PROCESSORS - 2, Affinity.getCpu());
}
try (AffinityLock lock = AffinityLock.acquireLock("1")) {
    assertEquals(1, Affinity.getCpu());
}
try (AffinityLock lock = AffinityLock.acquireLock("any")) {
    assertTrue(lock.bound);
}
try (AffinityLock lock = AffinityLock.acquireLock("none")) {
    assertFalse(lock.bound);
}
try (AffinityLock lock = AffinityLock.acquireLock((String) null)) {
    assertFalse(lock.bound);
}
try (AffinityLock lock = AffinityLock.acquireLock("0")) { // prints a warning
    assertFalse(lock.bound);
}

与[转帖]Thread Affinity相似的内容:

[转帖]Thread Affinity

https://github.com/OpenHFT/Java-Thread-Affinity/releases Overview Lets you bind a thread to a given core, this can improve performance (this library w

[转帖]实例分析 Scheduled Thread Pool Executor 与 Timer 的区别

https://my.oschina.net/u/4526289/blog/5584251 摘要:JDK 1.5 开始提供 Scheduled Thread PoolExecutor 类,Scheduled Thread Pool Executor 类继承 Thread Pool Executor

[转帖]ESXi命令行shell下最常用的命令

https://forum.huawei.com/enterprise/zh/thread/580931293695328256 services.sh – Linux服务通常使用services命令管理,管理ESXi服务是通过使用services.sh命令实现的。Services.sh命令支持的参

[转帖]【技术剖析】15. Native Memory Tracking 详解(1):基础介绍

https://bbs.huaweicloud.com/forum/thread-0246998875346680043-1-1.html 0.引言 我们经常会好奇,我启动了一个 JVM,他到底会占据多大的内存?他的内存都消耗在哪里?为什么 JVM 使用的内存比我设置的 -Xmx 大这么多?我的内存

[转帖]【技术剖析】16. Native Memory Tracking 详解(2):追踪区域分析(一)

https://bbs.huaweicloud.com/forum/thread-0295101552606827089-1-1.html 上篇文章 Native Memory Tracking 详解(1):基础介绍 中,分享了如何使用NMT,以及NMT内存 & OS内存概念的差异性,本篇将介绍NM

[转帖]【技术剖析】17. Native Memory Tracking 详解(3):追踪区域分析(二)

https://bbs.huaweicloud.com/forum/thread-0227103792775240073-1-1.html 应用性能调优 发表于 2022-11-14 15:19:36143查看 上篇文章 Native Memory Tracking 详解(2):追踪区域分析(一) 

[转帖]【技术剖析】18. Native Memory Tracking 详解(4):使用 NMT 协助排查内存问题案例

https://bbs.huaweicloud.com/forum/thread-0211103793043202049-1-1.html 其他 发表于 2022-11-14 15:38:571174查看 从前面几篇文章,我们了解了 NMT 的基础知识以及 NMT 追踪区域分析的相关内容,本篇文章将

[转帖]【技术剖析】8. 相同版本 JVM 和 Java 应用,在 x86 和AArch64 平台性能相差30%,何故?

https://bbs.huaweicloud.com/forum/thread-168532-1-1.html 作者: 吴言 > 编者按:目前许多公司同时使用 x86 和 AArch64 2 种主流的服务器。这两种环境的算力相当,内存相同的情况下:相同版本的 JVM 和 Java 应用,相同的 J

[转帖]编译器优化那些事儿(7):Cache优化

https://bbs.huaweicloud.com/forum/thread-02101103793043210063-1-1.html 引言 软件开发人员往往期望计算机硬件拥有无限容量、零访问延迟、无限带宽以及便宜的内存,但是现实却是内存容量越大,相应的访问时间越长;内存访问速度越快,价格也更

[转帖]编译器优化那些事儿(6):别名分析概述

https://bbs.huaweicloud.com/forum/thread-0211985213969460007-1-1.html 应用性能调优 发表于 2022-09-14 15:03:17298查看 1.简介 别名分析是编译器理论中的一种技术,用于确定存储位置是否可以以多种方式访问。如果