Kernel branching optimization macro likely and unlikely and __read_mostly

Source: Internet
Author: User

1.likely && unlikely

Branch declarations for conditional selection statements, GCC builds an instruction for optimization, which can be optimized for conditional branching when a condition is often present, or if the condition is rarely present. The kernel encapsulates this instruction as a macro, such as likely () and unlikely (), which makes it easier to use.
For example, the following is a conditional selection statement:
if (foo) {
/* .. */
}
If you want to mark this selection as a rarely occurring branch:
* * We think that most of the time Foo will be 0. */
if (Unlikely (foo)) {
/* .. */
}
Conversely, if we want to mark a branch as a usually true choice:
/* We don't think Foo is usually 0 * *
if (likely (foo)) {
/* .. */

}

It should be noted that likely and unlikely did not change the logic of the program, only to provide a certain basis for branch prediction.


2.__read_mostly

__read_mostly is used when the kernel is linked and is closely related to the cache mechanism of the architecture. defined in Linux kernel in the Parisc of x86, IA64, PowerPC, s390, asm/cache.h, SH, SPARC architectures:


#define __read_mostly __attribute__ (__section__ (". data.read_mostly"))


The literal meaning of the definition can be understood as linking the data that needs to be read frequently to the kernel's. data.read_mostly segment. In x86, IA64, PowerPC, Parisc, s390, SH, SPARC's link script (ARCH/XXX/KERNEL/VMLINUX.LDS.S) There is a definition of. data.read_mostly. The data in the data.read_mostly segment is placed in the cache of the CPU when the kernel is loaded. On architectures where there is no cache, __read_mostly is defined as NULL, namely:


#define __read_mostly


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.