Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sched_getcpu() reliable on Linux?

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

like image 262
Paul R Avatar asked Apr 29 '16 09:04

Paul R


2 Answers

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.

like image 172
mc110 Avatar answered Nov 13 '22 03:11

mc110


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.

like image 27
robertm.tum Avatar answered Nov 13 '22 03:11

robertm.tum