Likely () and unlikely () in the kernel ()

Source: Internet
Author: User

Likely () and unlikely () can be seen everywhere in the 2.6 kernel. Why should they be used? What are the differences between them?

First, make it clear:

If (likely (value) is equivalent to If (value)

If (unlikely (value) is also equivalent to If (value)

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

These two macros are defined in the kernel as follows:

# Define likely (x) _ builtin_exact CT (x), 1)
# Define unlikely (x) _ builtin_exact CT (x), 0)

_ Builtin_ct CT () is provided to programmers by GCC (version> = 2.96) to provide "branch transfer" information to the compiler, so that the compiler can optimize the code, to reduce the performance degradation caused by command redirection.

_ Builtin_ct (x), 1) indicates that the value of X is more likely to be true;
_ Builtin_exact CT (x), 0) indicates that the value of X is more likely to be false.

That is to say, when likely () is used, the chance of executing the statement after if is greater. When unlikely () is used, the chance of executing the statement after else is greater.
For example, in the following code, the author thinks that Prev is more likely than next,

If (likely (prev! = NEXT )){
Next-> timestamp = now;
...
} Else {
...;
}
In this way, the compiler will keep the code that is more likely to follow in the compilation process, thus reducing the performance degradation caused by instruction redirection.

The following two examples are used to deepen this understanding:

Example 1: example1.c

Int testfun (int x)
{
If (_ builtin_ct (x, 0 )){
^ --- We instruct the compiler, "else" block is more probable
X = 5;
X = x * X;
} Else {
X = 6;
}
Return X;
}
In this example, we think X is more likely to be 0.

After compilation, use objdump to observe Assembly commands. On my 2.4 kernel machine, the results are as follows:

# Gcc-O2-C example1.c
# Objdump-D example1.o

Disassembly of section. Text:

00000000 <testfun>:
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>
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>

As you can see, the compiler uses the JNE (unequal jump) command, and the code in the else block follows.

8: 75 07 JNE 11 <testfun + 0x11>
A: B8 06 00 00 00 mov $0x6, % eax

Example 2: example2.c

Int testfun (int x)
{
If (_ builtin_ct (x, 1 )){
^ --- We instruct the compiler, "if" block is more probable
X = 5;
X = x * X;
} Else {
X = 6;
}
Return X;
}
In this example, we think that X is more likely than 0.

After compilation, use objdump to observe Assembly commands. On my 2.4 kernel machine, the results are as follows:

# Gcc-O2-C example2.c
# Objdump-D example2.o

Disassembly of section. Text:

00000000 <testfun>:
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>
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>

This time, the compiler uses the JE (equal jump) command, and the code in the IF block follows.

8: 74 07 je 11 <testfun + 0x11>
A: B8 19 00 00 00 mov $0x19, % eax

 

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.