In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what Linux compilation and optimization must master the knowledge points, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
01, compilation options and kernel compilation
The Linux kernel (English: linux kernel) is a computer operating system kernel written in C and assembly language that matches the POSIX standard and is issued under the GNU General Public license. Technically, Linux is just a kernel. "kernel" refers to a system software that provides hardware abstraction layer, disk and file control, multitasking, and so on.
So first of all, we all know that if the Linux kernel is compiled with O0, it cannot be compiled. The kernel compilation of Linux is either O2 or Os, which can be seen in the Makefile of Linux:
When you choose
CONFIG_CC_OPTIMIZE_FOR_SIZE
It would be Os, otherwise it would be O2.
In fact, O2 and Os are a collection of optimization options:
Gcc-c-Q-O2-- help=optimizers > / tmp/O2-optsgcc-c-Q-Os--help=optimizers > / tmp/Os-opts
The former tends to be based on speed optimization, while the latter tends to be based on smaller size optimizations. Compare the switch options between the two:
Meld / tmp/O2-opts / tmp/Os-opts
Find that the difference is pitiful:
Both O2 and Os enable the inline small function and the called once function, but-finline-functions is turned off in O2 and on in Os. Optimize-strlen is on in O2, and this option is off in Os. The meaning of the relevant options can be seen through "man gcc" (find a man if there is a problem), such as searching inline-functions after man gcc:
From O0 to O1, O2, and O3, it is a process of gradually increasing the number of optimization options that are enabled:
Kernel compiles with O0, however, because kernel itself does not want to compile with O0, and its design contains the assumption that compilation will be optimized. Let's use a simple example to illustrate.
02. A simple example
The following code:
The O0 compilation will report the following error, saying that the f () function is not defined:
$gcc-O0 cc.ccc.c:1:13: warning:'f'used but never defined [enabled by default] void f (void); ^ / tmp/ccTwwtHG.o: In function `main':cc.c: (.text + 0x19): undefined reference to `f'collect2: error: ld returned 1 exit status
But compiling with O2 has no problem:
$gcc-O2 cc.c
The reason is that O2 compiles, it is aware of axiomatic 1, so if (a > 2), it won't hold, so it doesn't matter if f () has no definition.
After a slight change in the code:
O2 will not work at this time:
$gcc-O2 cc.c/tmp/ccXiyBHn.o: In function `main':cc.c: (.text.startup + 0x7): undefined reference to `startup: error: ld returned 1 exit status
So, through this example, you can see why the same code can be passed with O2 and not with O0. There is a lot of code in the kernel that is similar to the assumption that the compiler will optimize.
3. We don't want inline anymore.
Due to the optimization of compilation, some functions (such as small functions and functions called by only one person in the whole project) are not explicitly written as inline, but the compiler is optimized to inline, which causes some trouble for debugging because the corresponding symbol for this function can not be found.
At this point, we can clearly state that some functions we don't want to inline:
Otherwise, the above two functions, even if inline is not written in your code, may be automatically inline by the compiler because O2 and Os enable the relevant inline options. If we want to reject inline, we can identify it through noline.
4. I don't want to be optimized.
When O1, O2, O3 and Os have been enabled globally, we do not want to optimize a single function. We can modify this function with _ _ attribute__ ((optimize ("O0"). For example, we modify the above code that can be compiled with O2 as follows:
Recompile with O2:
$gcc-O2 cc.c/tmp/cc8M338p.o: In function `main':cc.c: (.text + 0x19): undefined reference to `f'collect2: error: ld returned 1 exit status
5. To sum up
Here are a few practical guides:
Try not to try to compile the kernel with O0, which is not in line with real engineering practice and is not supported by the mainstream Linux community; the kernel relies on O2/Os for more optimization
The pursuit of your code in the case of O2 is still correct, and the code should be able to withstand compilation and optimization; for example, if O0 works normally and O2 is not normal, you should try your best to find the reason from yourself and analyze and compile it.
If you want to avoid optimization for a certain part in the case of global optimization, you can try to use noinline,__attribute__ ((optimize ("O0") to make surgical adjustments.
These are all the contents of this article entitled "what are the knowledge points that must be mastered in Linux compilation and optimization?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.