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...
3 Answers
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);

- 712
- 1
- 10
- 20
-
sched_getcpu() is the most stable way to get cpuid. Since, you were explicitly looking for both cpu and node id, that's why I replied with getcpu(). Actually, getcpu() don't have libc wrapper, you need to use syscalls() system call. And, this is another of reason sched_getcpu() is better than getcpu(), along with portability issues. – rakib_ Jun 03 '13 at 16:18
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);;
}

- 2,106
- 16
- 19
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.
-
Thanks, that should work, but I didn't manage to run it. There's no
, and the documentation says it should be directly invoked with syscall(). So I tried the other solution with – Lovro Jun 03 '13 at 12:47and sched_getcpu()...