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: