LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Determine what CPU my thread is on (https://www.linuxquestions.org/questions/programming-9/determine-what-cpu-my-thread-is-on-817697/)

student04 07-02-2010 03:22 PM

Determine what CPU my thread is on
 
Is there a library/system call that will return to me what CPU core a thread of execution is running on? I've looked for a bit on the net already and also in /usr/include and couldn't find one.

getcpu() and sched_getcpu() are two that I found, but when I include the appropriate header files (linux/getcpu.h, and sys/sched.h respectively), gcc says getcpu.h doesn't exist and the linker complains it cannot find the implementation in sched.h.

I'm sure I'm doing something really stupid or overthinking...

What I am doing is running an OpenMP application and specifying a list of cpu cores to run on with GOMP_CPU_AFFINITY. I want to make sure that each core is getting the same number of threads.

**UPDATE**

It seems that this code works, but only with glibc >= 2.6, and my machine has 2.5:

Code:

#define _GNU_SOURCE

#include <stdio.h>
#include <utmpx.h>

int main( void )
{
        printf( "cpu = %d\n", sched_getcpu() );
        return 0;
}

**UPDATE2**

It seems that getcpu() exists, but there is no glibc wrapper for it. The manpage for getcpu states that I must directly invoke it with syscall() and only with kernels 2.6.19+ (I have 2.6.27). Here's more of what I tried:

Code:

$ grep -rin SYS_getcpu /usr/include
/usr/include/bits/syscall.h:310:#define SYS_getcpu __NR_getcpu
$ grep -rin __NR_getcpu /usr/include
/usr/include/asm-i386/unistd.h:326:#define __NR_getcpu                318
/usr/include/bits/syscall.h:310:#define SYS_getcpu __NR_getcpu
$ cat testgetcpu.c
#define _GNU_SOURCE

#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main( void )
{
        int cpu = syscall(318);
        if (cpu < 0)
        {
                printf( "Error: errno = %d\n", errno ); fflush(stdout);
                printf( "Error: errno: %s\n", strerror(errno) ); fflush(stdout);
                return -1;
        }
        printf( "cpu = %d\n", cpu );

        return 0;
}
$ gcc testgetcpu.c
$ ./a.out
Error: errno = 38
Error: errno: Function not implemented

Anyone have any ideas?

-AM

stevexyz 07-03-2010 07:39 AM

Would sched_getaffinity() help?

Cheers, Steve

syg00 07-03-2010 07:47 AM

Use containers (without CPU affinity) - let the scheduler worry about the details. Go find some (other) real work to do - for yourself I mean.
This is micro-management - if your manager did it to you you would (or should) complain like hell. Let algorithms handle that crap, not carbon based life forms (i.e. you).

Look up cgroups.

student04 07-03-2010 09:44 AM

@stevexyz
I thought about that, but haven't given it a try yet. The benchmark I run spawns 24 threads using OpenMP and I use an environment variable to tell it to put each thread on its own core. I'm getting weird results and want to verify that it is doing what I am telling it to. Thinking about it now, that may be what it is using, sched_setaffinity().

@syg00
I am doing high-performance computing research and need to know for a fact that thread N is on CPU core N. So no, I cannot be ignorant about what the scheduler is doing. Pinning is required for me. I will take a look at cgroups, thanks for the suggestion.


All times are GMT -5. The time now is 03:01 AM.