8

I'm trying to debug some performance issues with pthreads on Linux and I think sched_getcpu() may be lying to me. It reports a constant CPU for each thread, whereas profiling experiments seem to suggest the threads are actually migrating from one core to another during their life-time.

I wonder if sched_cpu() just reports the first CPU that the thread started running on, and is oblivious to thread migration ? Has anyone else noticed this, or seen any evidence that the the return value of sched_getcpu() might change ? If it's not realiable, are there any other methods for tracking current CPU (use CPUID maybe ?) ?

Paul R
  • 208,748
  • 37
  • 389
  • 560

2 Answers2

8

http://man7.org/linux/man-pages/man2/getcpu.2.html indicates sched_getcpu() is just a wrapper for getcpu().

http://man7.org/linux/man-pages/man2/getcpu.2.html suggests that the information provided is accurate, because an old caching option is no longer used:

The tcache argument is unused since Linux 2.6.24...it specified a pointer to a caller-allocated buffer in thread-local storage that was used to provide a caching mechanism for getcpu(). Use of the cache could speed getcpu() calls, at the cost that there was a very small chance that the returned information would be out of date. The caching mechanism was considered to cause problems when migrating threads between CPUs, and so the argument is now ignored.

So unless you are using a pre-2.6.24 kernel it seems unlikely you could be seeing old/cached information.

mc110
  • 2,825
  • 5
  • 20
  • 21
  • Thanks for the comprehensive answer - my kernel version appears to be 3.10 so it sounds like I should not be seeing this behaviour. I'll have to dig a little deeper... – Paul R Apr 29 '16 at 09:56
1

Calling sched_getcpu has two problems:

  1. It only tells you where the thread is running when it executes the call,
  2. Calling a system routine could cause a thread to migrate.

If you are using Intel runtime you could set KMP_AFFINITY=verbose as it will provide the same information (formatted differently) on stderr when the program executes its first parallel section.

robertm.tum
  • 350
  • 2
  • 12