加入收藏 | 设为首页 | 会员中心 | 我要投稿 江门站长网 (https://www.0750zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Linux内核中的likely和unlikely分析

发布时间:2021-11-21 21:47:53 所属栏目:教程 来源:互联网
导读:Kernel version:2.6.14 CPU architecture:ARM920T GCC version:arm-linux-gcc-3.4.1 看内核时经常遇到if(likely( )){}或是if(unlikely( ))这样的语句,不甚了解,例如(选自kernel/fork.c中copy_process): SET_LINKS(p); if (unlikely(p-ptrace PT_PTRACED
Kernel version:2.6.14
 
CPU architecture:ARM920T
 
GCC version:arm-linux-gcc-3.4.1
 
看内核时经常遇到if(likely( )){}或是if(unlikely( ))这样的语句,不甚了解,例如(选自kernel/fork.c中copy_process):
 
 SET_LINKS(p);
 if (unlikely(p->ptrace & PT_PTRACED))
  __ptrace_link(p, current->parent);
 
下面详细分析一下。
 
likely() 与 unlikely()是内核中定义的两个宏。位于/include/linux/compiler.h中,具体定义如下:
 
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(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.

(编辑:江门站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读