歡迎您光臨本站 註冊首頁

Linux 內核中的 likely與unlikely

←手機掃碼閱讀     火星人 @ 2014-03-26 , reply:0

看內核時總遇到if(likely( )){}或是if(unlikely( ))這樣的語句,最初不解其意,現在有所了解,所以也想介紹一下。

likely() 與 unlikely()是內核(我看的是2.6.22.6版本,2.6的版本應該都有)中定義的兩個宏。位於/include/linux/compiler.h中,
具體定義如下:
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

__builtin_expect是gcc(版本>=2.96,網上寫的,我沒驗證過)中提供的一個預處理命令(這個名詞也是網上寫的,我想叫函數更好些),有利於代碼優化。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」 是「很可能發生的」。

#define likely(x) __builtin_expect(!!(x), 1)也就是說明x==1是「經常發生的」或是「很可能發生的」。
使用likely ,執行if後面語句的可能性大些,編譯器將if{}是的內容編譯到前面, 使用unlikely ,執行else後面語句的可能性大些,編譯器將else{}里的內容編譯到前面。這樣有利於cpu預取,提高預取指令的正確率,因而可提高效率。

舉個例子(內核版本2.6.22.6):/kernel/shed.c中有一段:
if (likely(!active_balance)) {
/* We were unbalanced, so reset the balancing interval */
sd->balance_interval = sd->min_interval;
} else {
/*
* If we've begun active balancing, start to back off. This
* case may not be covered by the all_pinned logic if there
* is only 1 task on the busy runqueue (because we don't call
* move_tasks).
*/
if (sd->balance_interval max_interval)
sd->balance_interval *= 2;
}

編譯過程中,會將if後面{}里的內容編譯到前面,else 後面{}里的內容編譯到後面。若將likely換成unlikely 則正好相反。

總之,likely與unlikely互換或不用都不會影響程序的正確性。但可能會影響程序的效率。

感謝各位光顧!
不知道有沒有寫清楚,望指正!
疑惑:
為什麼likely或是unlikely要定義成__builtin_expect(!!(x), 1),而不直接用__builtin_expect(x, 1)?" !!(x) "與" x "有什麼不同?

另外內核2.6.31.5中likely和unlikely還有一種定義:
# ifndef likely
# define likely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
# endif

# ifndef unlikely
# define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
# endif
這個還沒看明白。

[火星人 ] Linux 內核中的 likely與unlikely已經有10948次圍觀

http://coctec.com/docs/linux/show-post-184401.html