Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-02-2010, 03:22 PM   #1
Registered: Jan 2004
Location: Georgia
Distribution: OS X, CentOS
Posts: 669

Rep: Reputation: 34
Question 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.


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

#define _GNU_SOURCE

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

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

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:

$ 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?


Last edited by student04; 07-02-2010 at 05:02 PM. Reason: Added code
Old 07-03-2010, 07:39 AM   #2
LQ Newbie
Registered: Apr 2008
Location: Hampshire, UK
Distribution: None
Posts: 27

Rep: Reputation: 16
Would sched_getaffinity() help?

Cheers, Steve
Old 07-03-2010, 07:47 AM   #3
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,777

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
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.
Old 07-03-2010, 09:44 AM   #4
Registered: Jan 2004
Location: Georgia
Distribution: OS X, CentOS
Posts: 669

Original Poster
Rep: Reputation: 34
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().

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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Am looking for problems with the cpu,that I can determine by using the command top!! shatemo5 Linux - Software 2 06-30-2008 07:57 PM
How to determine CPU usage Gayathri.P Programming 3 03-24-2008 11:20 PM
Determine cause of high IO Wait on CPU? danraider Linux - Software 2 10-15-2007 09:10 AM
how can I write a simple script to determine cpu? mtucker1337 Programming 5 05-24-2007 12:33 PM
how to determine cpu bit 32 or 64 dgag1 Linux - Software 4 01-30-2006 07:16 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration