Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUMA Get Current Node/Core

Tags:

c++

linux

numa

I'm using libnuma on Linux. My threads should be aware of the node/core they're running on. Is it possible to get the current threads's node/core somehow? I've been through the documentation, but I didn't find such a function...

like image 230
Lovro Avatar asked May 31 '13 17:05

Lovro


1 Answers

A lighter-weight approach is to make use of the RDTSCP instruction (on x86 systems that support it -- it will be listed as "rdtscp" in the "flags" field of /proc/cpuinfo).

The RDTSCP instruction returns the time-stamp-counter value in a pair of 32-bit registers (%eax and %ebx), but also returns the contents of the IA32_TSC_AUX MSR in the %ecx register. The contents of the IA32_TSC_AUX MSR are theoretically arbitrary, but every version of Linux that recognizes the "rdtscp" processor flag pre-loads the IA32_TSC_AUX register on each logical processor with an encoding of both the logical processor number (bits 11:0 of %ecx) and the "node number" (bits 21:12 of %ecx). The instruction grabs the TSC and the IA32_TSC_AUX register atomically, so you are guaranteed that the TSC value and the IA32_TSC_AUX value were obtained on the same core (which is critical if the TSC has different offsets on different cores).

The nice thing about this approach is that RDTSCP is a user-space machine-language instruction, so you don't need to interact with the kernel or any libraries. Overhead is under 50 cycles on recent systems. The routine I use is:

unsigned long tacc_rdtscp(int *chip, int *core)
{
    unsigned long a,d,c;
    __asm__ volatile("rdtscp" : "=a" (a), "=d" (d), "=c" (c));
    *chip = (c & 0xFFF000)>>12;
    *core = c & 0xFFF;
    return ((unsigned long)a) | (((unsigned long)d) << 32);;
}
like image 89
John D McCalpin Avatar answered Oct 27 '22 21:10

John D McCalpin