博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[zhuan]Linux Kernel : likely/unlikely macros
阅读量:6141 次
发布时间:2019-06-21

本文共 3452 字,大约阅读时间需要 11 分钟。

Linux Kernel : likely/unlikely macros

Ever wondered what the likely and unlikely macros in the linux kernel are ?

The macros are defined as :

#define likely(x)       __builtin_expect((x),1)#define unlikely(x)     __builtin_expect((x),0)

The __builtin_expect is a method that gcc (versions >= 2.96) offer for programmers to indicate branch prediction information to the compiler. The return value of __builtin_expect is the first argument (which could only be an integer) passed to it.

To check it out how it could be beneficial, an excerpt from "info gcc" :

if (__builtin_expect (x, 0))                foo ();      [This] would indicate that we do not expect to call `foo', since we     expect `x' to be zero.

Based on this information the compiler generates intelligent code, such that the most expected result is favored.


Let us consider it with a simple example function :
[kedar@ashwamedha ~]$ cat abc.cinttestfun(int x){        if(__builtin_expect(x, 0)) {                              ^^^--- We instruct the compiler, "else" block is more probable                x = 5;                x = x * x;        } else {                x = 6;        }        return x;} [kedar@ashwamedha ~]$ gcc -O2 -c abc.c[kedar@ashwamedha ~]$ objdump  -d abc.o abc.o:     file format elf32-i386 Disassembly of section .text: 00000000 :   0:   55                      push   %ebp   1:   89 e5                   mov    %esp,%ebp   3:   8b 45 08                mov    0x8(%ebp),%eax   6:   85 c0                   test   %eax,%eax   8:   75 07                   jne    11 < testfun+0x11 >                                ^^^ --- The compiler branches the "if" block and keeps "else" sequential   a:   b8 06 00 00 00          mov    $0x6,%eax   f:   c9                      leave  10:   c3                      ret  11:   b8 19 00 00 00          mov    $0x19,%eax  16:   eb f7                   jmp    f < testfun+0xf >

And let us see what happens if we make the "if" block more likely.

[kedar@ashwamedha ~]$ cat abc.cinttestfun(int x){        if(__builtin_expect(x, 1)) {                              ^^^ --- We instruct the compiler, "if" block is more probable                x = 5;                x = x * x;        } else {                x = 6;        }        return x;}                                                                                                    [kedar@ashwamedha ~]$ gcc -O2 -c abc.c[kedar@ashwamedha ~]$ objdump  -d abc.o                                                                                                    abc.o:     file format elf32-i386                                                                                                    Disassembly of section .text:                                                                                                    00000000 :   0:   55                      push   %ebp   1:   89 e5                   mov    %esp,%ebp   3:   8b 45 08                mov    0x8(%ebp),%eax   6:   85 c0                   test   %eax,%eax   8:   74 07                   je     11 < testfun+0x11 >                                ^^^ --- The compiler branches the "else" block and keeps "if" sequential    a:   b8 19 00 00 00          mov    $0x19,%eax   f:   c9                      leave  10:   c3                      ret  11:   b8 06 00 00 00          mov    $0x6,%eax  16:   eb f7                   jmp    f < testfun+0xf >

转载于:https://www.cnblogs.com/jack204/archive/2012/02/20/2359373.html

你可能感兴趣的文章
windows 下编辑shell,到linux报错,也是windows换行等造成
查看>>
JavaScript 闯关记
查看>>
csv 中 数值被自动转换成科学计数法 的问题 excel打开后数字用科学计数法显示且低位变0的解决方法...
查看>>
php libevent 扩展使用示例
查看>>
Redis-Cluster实战--5.使用redis-cli安装
查看>>
Chapter 2 Open Book——30
查看>>
【mysql元数据库】使用information_schema.tables查询数据库和数据表信息
查看>>
北京大学2017年高等代数与解析几何考研试题
查看>>
torch基本操作
查看>>
SQL简繁转换函数
查看>>
android--------根据文件路径加载指定文件
查看>>
实时跟踪log变化的工具Apachetop
查看>>
spring源码分析之@Conditional
查看>>
SVG Stroke属性
查看>>
在Android 5.0中使用JobScheduler(转载)
查看>>
Maven:Generating Project in Batch mode 卡住问题
查看>>
009 搭建Spark的maven本地windows开发环境以及测试
查看>>
工作中使用==埋下的坑
查看>>
ewebeditor编辑器配合IIS6.0解析漏洞拿shell
查看>>
知乎和简书的夜间模式实现套路
查看>>