Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nginx的ngx_shmtx_lock()原子锁 pause指令? #48

Open
vaynedu opened this issue Oct 13, 2019 · 0 comments
Open

nginx的ngx_shmtx_lock()原子锁 pause指令? #48

vaynedu opened this issue Oct 13, 2019 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@vaynedu
Copy link
Owner

vaynedu commented Oct 13, 2019

1. pause指令介绍

这里不直接谈nginx原子锁如何实现,只谈pause指令。
intel的pause指令, 减少cpu的消耗,节省电量。指令的本质功能:让加锁失败时cpu睡眠30个(about)clock,从而使得读操作的频率低很多。流水线重排的代价也会小很多。

Description
Improves the performance of spin-wait loops. When executing a “spin-wait loop,” a Pentium 4 or Intel Xeon processor suffers a severe performance penalty when exiting the loop because it detects a possible memory order violation. The PAUSE instruction provides a hint to the processor that the code sequence is a spin-wait loop. The processor uses this hint to avoid the memory order violation in most situations, which greatly improves processor performance. For this reason, it is recommended that a PAUSE instruction be placed in all spin-wait loops.

PAUSE指令提升了自旋等待循环(spin-wait loop)的性能。当执行一个循环等待时,Intel P4或Intel Xeon处理器会因为检测到一个可能的内存顺序违规(memory order violation)而在退出循环时使性能大幅下降。PAUSE指令给处理器提了个醒:这段代码序列是个循环等待。处理器利用这个提示可以避免在大多数情况下的内存顺序违规,这将大幅提升性能。因为这个原因,所以推荐在循环等待中使用PAUSE指令。

PAUSE的另一个功能就是降低Intel P4在执行循环等待时的耗电量。Intel P4处理器在循环等待时会执行得非常快,这将导致处理器消耗大量的电力,而在循环中插入一个PAUSE指令会大幅降低处理器的电力消耗。

2.nginx使用pause指令?

nginx的原子锁ngx_shmtx_lock()函数中使用 ngx_cpu_pause()

#if ( __i386__ || __i386 || __amd64__ || __amd64 )
#define ngx_cpu_pause()             __asm__ ("pause") 
#else
#define ngx_cpu_pause()
#endif

3. 其他代码使用pause指令

intel的pause指令  __asm__ (".byte 0xf3, 0x90") 

#define NOP_CPU3(n)  {int i = 0; while(i++ < (n)) cpu_pause();}

#define cpu_pause()         __asm__ (".byte 0xf3, 0x90")
@vaynedu vaynedu added the documentation Improvements or additions to documentation label Oct 13, 2019
@vaynedu vaynedu self-assigned this Oct 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant