LINUX下的幾種CPU讓渡策略

CPU作爲服務器重要的計算資源,因爲其資源的稀缺性,所以其對所有程序來說都是彌足寶貴的;尤其是對性能要求極高的應用程序而言,如何更好的利用CPU將是 提升性能的一個關鍵因素。

本文將探討幾種應用程序讓渡CPU的策略,以此來研究如何讓CPU更好的被不同用途的應用程序或者相同協作的一組應用程序使用,從而更好的使用CPU這種寶貴的資源。

  1. cpu_relax

cpu_relax在LINUX內核中使用較多,其定義如下:

static inline void rep_nop(void)
{
asm volatile(“rep; nop” ::: “memory”);
}

其中,asm volatile(“rep; nop” ::: “memory”); 大部分情況下等同於asm volatile(“pause” ::: “memory”); 其作用就是讓CPU暫停工作,但是並不會讓渡CPU到其他的進程,而只會暫停工作一段時間,CPU會繼續在當前的進程進行忙等待。

爲什麼沒有直接使用pause來代替req; nop指令,應該還是基於向前兼容的原因。

其實,asm裏面的pause指令的作用,intel的參考手冊裏面說的比較相信,可以參考如下:

rep; nop is indeed the same as the pause instruction (opcode F390). It might be used for assemblers which don’t support the pause instruction yet. On previous processors, this simply did nothing, just like nop but in two bytes. On new processors which support hyperthreading, it is used as a hint to the processor that you are executing a spinloop to inc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章