Reputation: 420
I'm writing a NUMA-aware algorithm and need this information for optimal memory keeping. It would be nice if you know a solution for JVM(for example using oshi), but I can't find it even for C/C++
Upvotes: 0
Views: 846
Reputation: 50488
Threads are not bound to a given core by default (so not to a NUMA node). Thus, it does not make sense to get the NUMA node of a thread since it can switch from once node to another at any time. If your threads are bound to cores or at least to NUMA nodes (possibly using taskset
or a pthread system call on Linux for example), then this is possible but AFAIK it is not possible in Java, but possible in C though it is certainly not portable.
On Linux, in C, you can get the current CPU of a running thread using sched_getcpu()
. Note that AFAIK a "CPU" does not mean a micro-processor but a core or even an hardware thread in practice (this is what can be seen in /proc/cpuinfo
for example). Then, you can use libnuma so to get the NUMA node information. More specifically, the numa_node_of_cpu
function should give you the NUMA node of the target CPU.
The only portable C library I known which is able to do that is libhwloc
(which uses libnuma internally on Linux). You can get more information about it here and there.
AFAIK controlling the NUMA allocation policy from the JVM is not possible in general (especially if threads are not bound). If threads are bound (done manually) and the JVM performs local allocation (very likely but not garanteed), then the default first-touch policy (which can be tuned by numactl
on Linux and may be a different one on some platforms) should map the pages of the referenced data on the NUMA node doing the write.
Upvotes: 1