3

I'm struggling with getting a multi-threaded app to run on multiple cores. I've looked into affinity, scheduling, etc. Is there a way to find out the CPU Id that any thread is running on? I'm using sched_getaffinity now - but I think that is related to the process id, not the thread within the process. The mulit-threaded app works great on Windows, but seems to be CPU bound (using only one CPU) on linux

Update:

If my linux app launches 64 threads - I still only have one pid right? It's still my understanding that each thread launched can run on a different CPU/core on the target hardware, right?

A sample app is here : How do I make a multi-threaded app use all the cores on Ubuntu under VMWare?

Community
  • 1
  • 1
Jeff
  • 1,969
  • 3
  • 21
  • 35
  • possible duplicate of [What core is a given thread running on?](http://stackoverflow.com/questions/5819655/what-core-is-a-given-thread-running-on) – Ben Voigt Jul 01 '11 at 15:31

1 Answers1

2

Your first question

Is there a way to find out the CPU Id that any thread is running on? I'm using sched_getaffinity now

sched_getaffinity doesn't return the CPU, it returns a mask of eligible CPUs. It says:

The affinity mask is actually a per-thread attribute that can be adjusted independently for each of the threads in a thread group.

And then

If you are using the POSIX threads API, then use pthread_setaffinity_np(3) instead of sched_setaffinity().

To simply find out the CPU used, /proc/[pid]/stat has a "processor" field:

processor %d (since Linux 2.2.8) CPU number last executed on.

Your second question:

The mulit-threaded app works great on Windows, but seems to be CPU bound (using only one CPU) on linux

Show the smallest example that exhibits this problem.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Thanks for the reply - I am trying pthread_setaffinity_np now. For an example app see my question: http://stackoverflow.com/questions/6488432/how-do-i-make-a-multi-threaded-app-use-all-the-cores-on-ubuntu-under-vmware – Jeff Jul 01 '11 at 15:20
  • The information in `/proc/[pid]/stat` is about the process with the given PID. To get the same statistics about a thread, see `/proc/[pid]/task/[tid]/stat` instead where `[tid]` is the thread ID. The [`proc(5)` man page](http://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html) documents the fields in these `stat` files. – FooF May 28 '12 at 04:40