NUMA Get Current Node/Core

前端 未结 3 1937
有刺的猬
有刺的猬 2020-12-30 15:17

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 thro

3条回答
  •  死守一世寂寞
    2020-12-30 15:57

    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);;
    }
    

提交回复
热议问题