我参考过这个网页:https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops,以下我无法理解:
暂停指令向处理器提示调用线程处于“自旋等待”循环中。此外,暂停指令在不支持 Intel SSE2 的 x86 架构上使用时是无操作的,这意味着它仍然会执行,不会执行任何操作或引发错误。虽然这意味着不支持 Intel SSE2 的较旧 x86 架构将无法享受到暂停带来的好处,但这也意味着您可以保留一个可全面运行的简单代码路径。
我想知道,linux 中的 lscpu 会显示 cpu 信息,但我不知道我的 cpu 是否支持 SSE2,我如何自己检查?
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 24
On-line CPU(s) list: 0-23
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 63
Model name: Intel(R) Xeon(R) CPU E5-2643 v3 @ 3.40GHz
Stepping: 2
CPU MHz: 3599.882
BogoMIPS: 6804.22
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 20480K
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23
另外,目前我使用 _mm_pause 或 __asm volatile ("pause" ::: "memory");
该核心中的 cpu 空闲将耗尽为零,但是使用 nanosleep 的以下代码对我来说太慢了:
while(1){
nanosleep();
dosomething..... ;
}
我观察到 nanosleep 在我的盒子里会延迟 60 微秒,有没有比 nanosleep 更快的解决方案,而且不会耗尽 cpu 核心,如 _mm_pause() 或 __asm 易失性(“暂停”:::“内存”)?!
Edit :
struct timespec req={0};
req.tv_sec=0;
req.tv_nsec=100 ;
nanosleep(&req,NULL) ;
在我拥有的CPU位于上面的盒子中,这个nanosleep花费了60微秒,
我不知道怎么会发生这样的事?!