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

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