NUMA Get Current Node/Core

前端 未结 3 1916
有刺的猬
有刺的猬 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:48

    You need to use getcpu() system call. As man page says:

    determine CPU and NUMA node on which the calling thread is running

    So, this should serve your purpose. Needs to include <linux/getcpu.h>, with kernel version greater than 2.6.19 and for x86_64, i386 arch.

    0 讨论(0)
  • 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);;
    }
    
    0 讨论(0)
  • 2020-12-30 16:06

    I found this solution:

    #include <stdio.h>
    #include <utmpx.h>
    
    int main(void) {
      printf("CPU: %d\n", sched_getcpu());
      return 0;
    }
    

    Then, if you need the node of the cpu, you can use numa.h:

    int cpu = sched_getcpu();
    int node = numa_node_of_cpu(cpu);
    
    0 讨论(0)
提交回复
热议问题