我正在尝试使用Linux上的pthreads调试一些性能问题,我认为sched_getcpu()可能对我说谎.它为每个线程报告一个恒定的CPU,而分析实验似乎表明线程实际上在其生命周期内从一个核心迁移到另一个核心.
我想知道sched_cpu()是否只报告线程开始运行的第一个CPU,并且不知道线程迁移?有没有人注意到这一点,或者看到任何证据表明sched_getcpu()的返回值可能会改变?如果它不可行,是否还有其他跟踪当前CPU的方法(可能使用CPUID?)?
解决方法:
http://man7.org/linux/man-pages/man2/getcpu.2.html表示sched_getcpu()只是getcpu()的包装器.
http://man7.org/linux/man-pages/man2/getcpu.2.html表明所提供的信息是准确的,因为不再使用旧的缓存选项:
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.
因此,除非您使用的是2.6.24之前的内核,否则您似乎不太可能看到旧的/缓存信息.