[转]likely,unlikely宏与GCC内建函数__builtin_expect() + 原创

来源:互联网 发布:淘宝上的ica麦片真货 编辑:程序博客网 时间:2024/05/22 07:45


[转]likely,unlikely宏与GCC内建函数__builtin_expect()
[转贴]
likely,unlikely宏与GCC内建函数__builtin_expect() 
先罗嗦几句
最近在读linux 2.6 内核,虽然以前已经看了很多相关的知识,>也看了2,3遍,但读2.6内核仍然感到很吃力。面对2.6如此庞大的内核,信心真的不是很足,而且好像也没有很好的、有帮助的论坛来一起探讨,哎!现在正在边看>,边看最新的内核,自>出版以来,内核已经有了很多的变化,好难读啊!如果这样读下去算不算“皓首穷经”呢,不得而知了!
言归正传
在读linux/kernel/fork.c的时候遇到了unlikely宏定义,一路追踪,最后找到了GCC内建函数__builtin_expect(),查阅GCC手册,发现其定义如下:
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
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提供了这个内建函数来帮助程序员处理分支预测,优化程序。其第一个参数exp为一个整型表达式,这个内建函数的返回值也是这个exp,而c为一个编译期常量,这个函数的语义是:你期望exp表达式的值等于常量c,从而GCC为你优化程序,将符合这个条件的分支放在合适的地方。
因为这个程序只提供了整型表达式,所以如果你要优化其他类型的表达式,可以采用指针的形式。
unlikely的定义如下:
#define unlikely(x)  __builtin_expect(!!(x), 0)
也就是说我们期望表达式x的值为0,从而如果我们用
…….
if(unlikely(x)){
bar();
}
来测试条件的话,我们就不期望bar()函数执行,所以该宏的名字用unlikely也就是不太可能来表示。
likely宏与次类似.
说到底__builtin_expect函数就是为了优化可能性大的分支程序。

+++++++++++++++++++++

[bob]
其实, 我总结一下上面的话的意思:
if() 语句你照用, 跟以前一样, 只是 如果你觉得if()是1 的可能性非常大的时候, 就在表达式的外面加一个likely() , 如果可能性非常小(比如几率非常小),就用unlikely() 包裹上。 
看kernel代码的时候, 看到很多这样的例子, 举一个:
if (likely(cpu == this_cpu)) {   //注意likely和unlikely的用法
                //这里用likely说明,cpu=this_cpu的可能性非常大
                
                                if (!(clone_flags & CLONE_VM)) {
                        /*
                         * The VM isn't cloned, so we're in a good position to
                         * do child-runs-first in anticipation of an exec. This
                         * usually avoids a lot of COW overhead.
                         */
                        if (unlikely(!current->array))
                                __activate_task(p, rq);
                        else {
                                p->prio = current->prio;
                                p->normal_prio = current->normal_prio;
                                list_add_tail(&p->run_list, &current->run_list);
                                p->array = current->array;
                                p->array->nr_active++;
                                inc_nr_running(p, rq);
                        }
                        set_need_resched();
                } else
                        /* Run child last */
                        __activate_task(p, rq);
                /*
                 * We skip the following code due to cpu == this_cpu
                  *
                 *   task_rq_unlock(rq, &flags);
                 *   this_rq = task_rq_lock(current, &flags);
                 */
                this_rq = rq;
        } else {


---

kernel 代码,有许多看起来多余的东西,其实不多余
kernel里面的代码, 有的时候看起来,很繁琐, 冗余, 但是仔细一看 ,还真的不错。 
在代码里面老能看到 BUG_ON() , WARN_ON() 这样的宏 , 类似 我们日常编程里面的断言(assert) 。 
---> bug.h
QUOTE:
extern void __WARN_ON(const char *func, const char *file, const int line);
#ifdef CONFIG_BUG
#ifndef HAVE_ARCH_BUG
#define BUG() do { \
        printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __FUNCTION__); \
        panic("BUG!"); \
} while (0)
#endif
#ifndef HAVE_ARCH_BUG_ON
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#endif
#ifndef HAVE_ARCH_WARN_ON
#define WARN_ON(condition) do { \
        if (unlikely((condition)!=0)) \
                __WARN_ON(__FUNCTION__, __FILE__, __LINE__); \
} while (0)
#endif
#else /* !CONFIG_BUG */
#ifndef HAVE_ARCH_BUG
#define BUG()
#endif
#ifndef HAVE_ARCH_BUG_ON
#define BUG_ON(condition) do { if (condition) ; } while(0)
#endif
#ifndef HAVE_ARCH_WARN_ON
#define WARN_ON(condition) do { if (condition) ; } while(0)
#endif
#endif
kernel 别的代码里面用到例子:
        BUG_ON(unlikely(atomic_read(&(kioctx)->users) 
[Copy to clipboard]

[ - ]
CODE:
#include 
#include 
#define likely(x)        __builtin_expect(!!(x), 1)
#define unlikely(x)        __builtin_expect(!!(x), 0)
#define BUG_ON(condition) do \
        { if (unlikely((condition)!=0)) \
                printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __FUNCTION__); \
        } while(0)
int main(void)
{
        int i = 7;
        BUG_ON(i!=10);
        return 0;
}
输出如下: 
BUG: failure at warn.c:18/main()!


__builtin_expect详解【转】  

2010-06-09 16:23:36|  分类: 开源库|字号 订阅

在GTK+2.0源码中有很多这样的宏:G_LIKELY和G_UNLIKELY。比如下面这段代码:

if (G_LIKELY (acat == 1))       /* allocate through magazine layer */      {        ThreadMemory *tmem = thread_memory_from_self();        guint ix = SLAB_INDEX (allocator, chunk_size);        if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))          {            thread_memory_swap_magazines (tmem, ix);            if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))              thread_memory_magazine1_reload (tmem, ix);          }        mem = thread_memory_magazine1_alloc (tmem, ix);      }

在源码中,宏G_LIKELY和G_UNLIKELY 是这么定义的:

#define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))  #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))

宏_G_BOOLEAN_EXPR的作用是把expr转换为0和1,即真假两种。要理解宏G_LIKELY和G_UNLIKELY ,很明显必须理解__builtin_expect。__builtin_expect是GCC(version>=2.9)引进的宏,其作用就是帮助编译器判断条件跳转的预期值,避免跳转造成时间乱费。拿上面的代码来说:

if (G_LIKELY (acat == 1))     //表示大多数情况下if里面是真,程序大多数直接执行if里面的程序

if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))//表示大多数情况if里面为假,程序大多数直接执行else里面的程序

可能大家看到还是一头雾水,看下面一段就会明白其中的乐趣啦;

//test_builtin_expect.c #define LIKELY(x) __builtin_expect(!!(x), 1)#define UNLIKELY(x) __builtin_expect(!!(x), 0)
int test_likely(int x){ if(LIKELY(x)) {    x = 5; } else {    x = 6; }   return x;}
int test_unlikely(int x){ if(UNLIKELY(x)) {    x = 5; } else {    x = 6; }   return x;}
[lammy@localhost test_builtin_expect]$ gcc -fprofile-arcs -O2 -c test_builtin_expect.c [lammy@localhost test_builtin_expect]$ objdump -d test_builtin_expect.o
test_builtin_expect.o:       file format elf32-i386
Disassembly of section .text:
00000000 <test_likely>:     0: 55                      push     %ebp     1: 89 e5                   mov      %esp,%ebp     3: 8b 45 08                mov      0x8(%ebp),%eax     6: 83 05 38 00 00 00 01  addl     $0x1,0x38     d: 83 15 3c 00 00 00 00  adcl     $0x0,0x3c  14: 85 c0                   test     %eax,%eax  16: 74 15                   je       2d <test_likely+0x2d>//主要看这里  18: 83 05 40 00 00 00 01  addl     $0x1,0x40  1f: b8 05 00 00 00          mov      $0x5,%eax  24: 83 15 44 00 00 00 00  adcl     $0x0,0x44  2b: 5d                      pop      %ebp  2c: c3                      ret        2d: 83 05 48 00 00 00 01  addl     $0x1,0x48  34: b8 06 00 00 00          mov      $0x6,%eax  39: 83 15 4c 00 00 00 00  adcl     $0x0,0x4c  40: 5d                      pop      %ebp  41: c3                      ret        42: 8d b4 26 00 00 00 00  lea      0x0(%esi,%eiz,1),%esi  49: 8d bc 27 00 00 00 00  lea      0x0(%edi,%eiz,1),%edi
00000050 <test_unlikely>:  50: 55                      push     %ebp  51: 89 e5                   mov      %esp,%ebp  53: 8b 55 08                mov      0x8(%ebp),%edx  56: 83 05 20 00 00 00 01  addl     $0x1,0x20  5d: 83 15 24 00 00 00 00  adcl     $0x0,0x24  64: 85 d2                   test     %edx,%edx  66: 75 15                   jne      7d <test_unlikely+0x2d>//主要看这里  68: 83 05 30 00 00 00 01  addl     $0x1,0x30  6f: b8 06 00 00 00          mov      $0x6,%eax  74: 83 15 34 00 00 00 00  adcl     $0x0,0x34  7b: 5d                      pop      %ebp  7c: c3                      ret        7d: 83 05 28 00 00 00 01  addl     $0x1,0x28  84: b8 05 00 00 00          mov      $0x5,%eax  89: 83 15 2c 00 00 00 00  adcl     $0x0,0x2c  90: 5d                      pop      %ebp  91: c3                      ret        92: 8d b4 26 00 00 00 00  lea      0x0(%esi,%eiz,1),%esi  99: 8d bc 27 00 00 00 00  lea      0x0(%edi,%eiz,1),%edi
000000a0 <_GLOBAL__I_65535_0_test_likely>:  a0: 55                      push     %ebp  a1: 89 e5                   mov      %esp,%ebp  a3: 83 ec 08                sub      $0x8,%esp  a6: c7 04 24 00 00 00 00  movl     $0x0,(%esp)  ad: e8 fc ff ff ff          call     ae <_GLOBAL__I_65535_0_test_likely+0xe>  b2: c9                      leave    b3: c3                      ret      [lammy@localhost test_builtin_expect]$

两个函数编译生成的汇编语句所使用到的跳转指令不一样,仔细分析下会发现__builtin_expect实际上是为了满足在大多数情况不执行跳转指令,所以__builtin_expect仅仅是告诉编译器优化,并没有改变其对真值的判断。

这种用法在linux内核中也经常用到,国外也有一篇相关的文章,大家不妨看看:http://kernelnewbies.org/FAQ/LikelyUnlikely

不知大家注意到没有,我在生产汇编时用的是gcc -fprofile-arcs -O2 -c test_builtin_expect.c,而不是gcc -O2 -c test_builtin_expect.c,具体可以参考http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html。


http://babybandf.blog.163.com/blog/static/619935320105942336930/

http://linux.chinaunix.net/techdoc/develop/2007/08/31/966838.shtml


http://www.oschina.net/code/explore/firefox-4.0b7/memory/mozalloc/mozalloc.cpp