在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:OpenHFT/Java-Thread-Affinity开源软件地址:https://github.com/OpenHFT/Java-Thread-Affinity开源编程语言:Java 96.0%开源软件介绍:Thread AffinityOverviewLets 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
DependenciesJava-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 versionJava-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 Installing JNA on Ubuntusudo apt-get install libjna-java Installing JNA on CentOSsudo yum install jna How does CPU allocation work?The library will read your 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. ReferencesisolcpusJava-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 AffinityLockAcquiring a CPU lock for a threadYou 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 threadYou 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 layoutYou 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 idYou can get the current thread id using int threadId = AffinitySupport.getThreadId(); Determining which CPU you are running onYou can get the current CPU being used by int cpuId = AffinitySupport.getCpu(); Controlling the affinity more directlyThe 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 stateFor a detailed of view of the current affinity state (as seen by the library), execute the following script on Linux systems:
Support MaterialFor 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 AnswersQuestion: How to lock a specific cpuIdI 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 cpuIdI have the cpuId in a configuration file, how can I set it using a string? Answer: use one of the followingtry (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);
} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论