[英]Java processes vs. threads for thread affinity
I recently wrote some code[1][2] that tried using JNA to make calls to sched_setaffinity
in an attempt to set the affinity of the process to a particular core. 我最近写了一些代码[1] [2],尝试使用JNA调用
sched_setaffinity
,以尝试将进程的亲和性设置为特定的内核。 The first argument of the function sched_setaffinity
is the process id. 函数
sched_setaffinity
的第一个参数是进程ID。
Calling the function with pid as 0(referring to the process itself) works fine. 调用pid为0的函数(指进程本身)可以正常工作。 However, I'd like to be able to set the affinity on a thread id basis rather than the process.
但是,我希望能够基于线程ID而不是进程来设置相似性。 Is there any way I could do that?
有什么办法可以做到吗?
There is a function called pthread_setaffinity_np that can set the CPU affinity mask of the thread thread to the CPU set pointed to by cpuset. 有一个名为pthread_setaffinity_np的函数可以将线程线程的CPU亲和力掩码设置为cpuset指向的CPU集。
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
pthread_t current_thread = pthread_self();
pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
This piece of code can set the a thread to the core(represented by core_id). 这段代码可以将一个线程设置为core(由core_id表示)。
As far as I know, a Java Thread is not always matched to a thread in OS. 据我所知,Java线程并不总是与OS中的线程匹配。 So I'm not quite sure whether this piece of native code can help you.
因此,我不太确定这段本地代码是否可以帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.