Macro definition parsing of likely and unlikely in Linux kernel

Source: Internet
Author: User

Likely () and unlikely () can be seen everywhere in the 2.6 kernel, so why use them? What's the difference between them?

The first thing to understand:

     if(likely(valueif(value)     if(unlikely(valueif(value)

That is to say likely () and unlikely () from the perspective of reading and understanding code. is the same!!!

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

__builtin_expect () is a GCC (version >= 2.96) provided to the program ape to provide the "branch transfer" information to the compiler so that the compiler can optimize the code. To reduce the performance degradation caused by the instruction jump.

__builtin_expect ((x), 1) indicates that the value of x is more likely to be true;
__builtin_expect ((x), 0) means that the value of x is more likely to be false.

That is, with likely (), the chance to run the statement after the if is greater, with unlikely (), the chance to run the statement after else is greater.
such as the following code, the author thinks Prev is not equal to next is more likely,

ifnext)) {       next->timestamp = now;        ...else {        ...; }

In this way, the compiler will be in the process of compiling, the more likely the code followed by the face of the code, thereby reducing the performance of the instruction jump caused by the decline.

另外内核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

For example (kernel version number 2.6.22.6): There is a paragraph in/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 could 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 ;} 

During compilation, the contents of the IF {} are later compiled to the front. else the contents of {} are later compiled into the following. The opposite is true if the likely is replaced with unlikely.

In short, likely and unlikely interchange or not will not affect the correctness of the program.

However, the efficiency of the program may be affected.

Macro definition parsing of likely and unlikely in Linux kernel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.