likely()与unlikely()是内核中定义的两个函数宏,具体定义如下所示:

    1. #define likely(x) __builtin_expect(!!(x), 1)
    2. #define unlikely(x) __builtin_expect(!!(x), 0)

    其中的__builtin_expect是gcc中提供的一个预处理命令(这个名词也是网上写的,我想叫函数更好些),有利于代码优化,它在gcc(version 4.4.0)具体定义如下:

    long __builtin_expect (long exp, long c) [Built-in Function]

    注解为: 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 semantics of the built-in are that it is expected that exp == c.

    意思为: 我们可以使用这个函数人为告诉编绎器一些分支预测信息“exp == c” 是“很可能发生的”,也就意味着告诉编译器可先进行判断“exp == c”是否真的发生了,这样先判断大概率事件,便可省去一些步骤去判断一些小概率事件的发生。

    同时对于#define likely(x) __builtin_expect(!!(x), 1)

    也就是说明x == 1这种情况是“经常发生的”或是“很可能发生的”。

    例:(内核版本2.6.22.6):/kernel/shed.c中有一段:

    • 使用likely后 ,执行if后面语句的可能性大些,编译器将if{}里的内容编译到前面。
    • 使用unlikely后,执行else后面语句的可能性大些,编译器将else{}里的内容编译到前面。

    这样便有利于cpu预取,提高预取指令的正确率,因而可提高效率。

    1. if (likely(!active_balance))
    2. {
    3. /* We were unbalanced, so reset the balancing interval */
    4. sd->balance_interval = sd->min_interval;
    5. }
    6. else
    7. {
    8. /*
    9. * If we've begun active balancing, start to back off. This
    10. * case may not be covered by the all_pinned logic if there
    11. * is only 1 task on the busy runqueue (because we don't call
    12. * move_tasks).
    13. */
    14. if (sd->balance_interval max_interval)
    15. sd->balance_interval *= 2;
    16. }

    编译过程中,会将if后面{}里的内容编译到前面,else 后面{}里的内容编译到后面。若将likely换成unlikely则正好相反。

    总之,likely与unlikely互换或不用都不会影响程序的正确性。但可能会影响程序的效率。

    注:同时对于__builtin_expect(!!(x),1)中的!!(x),有说法是说为转换为BOOL型,但C中对BOOL型的支持貌似不太严格,所以此处好像意义不大。

    这两个函数宏的功能就是通过先进行一些大概率事件的判断与处理,并将该部分处理内容编译到前面,同时将对小概率事件的判断与处理放至后面,并将该部分处理内容编译到后面。

    这样相应的在CPU中便可以省去一些的操作指令,以此来提升CPU的效率,属于一种效率优化手段。