微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > linux内核中的likely和unlikely

linux内核中的likely和unlikely

时间:11-22 来源:互联网 点击:
Kernel version:2.6.14

CPU architecture:ARM920T

Author:ce123(http://blog.csdn.net/ce123)

GCCversion:arm-linux-gcc-3.4.1

看内核时经常遇到if(likely( )){}或是if(unlikely( ))这样的语句,不甚了解,例如(选自kernel/fork.c中copy_process):

[plain]view plaincopyprint?

  1. SET_LINKS(p);
  2. if(unlikely(p->ptrace&PT_PTRACED))
  3. __ptrace_link(p,current->parent);

下面详细分析一下。

likely() 与 unlikely()是内核中定义的两个宏。位于/include/linux/compiler.h中,具体定义如下:

[plain]view plaincopyprint?

  1. #definelikely(x)__builtin_expect(!!(x),1)
  2. #defineunlikely(x)__builtin_expect(!!(x),0)

__builtin_expect是GCC(version>=2.9)引进的内建函数,其作用就是帮助编译器判断条件跳转的预期值,避免跳转造成时间乱费,有利于代码优化。查阅GCC手册,发现其定义如下(http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html):

-- Built-in Function: long __builtin_expect (long EXP, long C)
You may use `__builtin_expect to provide the compiler with branch
prediction information. In general, you should prefer to use
actual profile feedback for this (`-fprofile-arcs), as
programmers are notoriously bad at predicting how their programs
actually perform. However, there are applications in which this
data is hard to collect.

The return value is the value of EXP, which should be an integral
expression. The value of C must be a compile-time constant. The
semantics of the built-in are that it is expected that EXP == C.
For example:

if (__builtin_expect (x, 0))
foo ();

would indicate that we do not expect to call `foo, since we
expect `x to be zero. Since you are limited to integral
expressions for EXP, you should use constructions such as

if (__builtin_expect (ptr != NULL, 1))
error ();

when testing pointer or floating-point values.

大致意思是:可以使用。由于大部分程序员在分支预测方面做得很糟糕,所以GCC提供了__builtin_expect这个内建函数,给编译器提供分支预测信息,以帮助程序员处理分支预测,优化程序。其第一个参数EXP为一个整型表达式,这个内建函数的返回值也是这个EXP,而C为一个编译期常量,这个函数的语义是:你期望EXP表达式的值等于常量C,从而GCC为你优化程序,将符合这个条件的分支放在合适的地方。由于该内建函数只提供了整型表达式,所以如果你要优化其他类型的表达式,可以采用指针的形式。

当GCC的版本较低时(_GNUC_MINOR__ < 96),__builtin_expect直接返回EXP。下面的代码摘自/include/linux/compiler-gcc2.h。

[plain]view plaincopyprint?

  1. /*ThesedefinitionsareforGCCv2.x.*/
  2. /*SomewhereinthemiddleoftheGCC2.96developmentcycle,weimplemented
  3. amechanismbywhichtheusercanannotatelikelybranchdirectionsand
  4. expecttheblockstobereorderedappropriately.Define__builtin_expect
  5. tonothingforearliercompilers.*/
  6. #include
  7. #if__GNUC_MINOR__<96
  8. #define__builtin_expect(x,expected_value)(x)
  9. #endif

总结一下:if() 语句照常用, 和以前一样, 只是 如果你觉得if()是1 的可能性非常大的时候, 就在表达式的外面加一个likely(),如果可能性非常小(比如几率非常小),就用unlikely()包裹上。下面我们看一个例子。

[plain]view plaincopyprint?

  1. //test_builtin_expect.c
  2. #definelikely(x)__builtin_expect(!!(x),1)
  3. #defineunlikely(x)__builtin_expect(!!(x),0)
  4. inttest_likely(intx)
  5. {
  6. if(likely(x))
  7. x=5;
  8. else
  9. x=6;
  10. returnx;
  11. }
  12. inttest_unlikely(intx)
  13. {
  14. if(unlikely(x))
  15. x=5;
  16. else
  17. x=6;
  18. returnx;
  19. }

root@czu:~/桌面/socket# arm-linux-gcc -fprofile-arcs -O2 -c test.c

root@czu:~/桌面/socket# arm-linux-gcc -fprofile-arcs -O2 -o test test.c

root@czu:~/桌面/socket# arm-linux-objdump -D test > test.dis

[plain]view plaincopyprint?

  1. 000088cc:
  2. 88cc:e3500000cmpr0,#0;0x0
  3. 88d0:e92d4010stmdbsp!,{r4,lr}
  4. 88d4:e59fc044ldrip,[pc,#68];8920<.text+0x148>
  5. 88d8:e59fe044ldrlr,[pc,#68];8924<.text+0x14c>
  6. 88dc:e3a00005movr0,#5;0x5
  7. 88e0:0a000006beq8900//前面通过cmp将r0和0进行比较,因为x=1的概率很大,优先执行不等于0的分支
  8. 88e4:e89c0018ldmiaip,{r3,r4}
  9. 88e8:e3a02000movr2,#0;0x0
  10. 88ec:e3a01001movr1,#1;0x1
  11. 88f0:e0933001addsr3

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top