Embedded Linux Conference 2017 Embedded Linux size reduction techniques Michael Opdenacker michael.opdenacker@free-electrons.com http://free-electrons.com 1/1 free electrons free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
time, and one of its prerequisites: small system Michael Opdenacker size. Penguin from Justin Ternet ( https://openclipart.org/detail/182875/pinguin ) http://free-electrons.com 2/1 ▶ Michael Opdenacker ▶ Founder and Embedded Linux engineer at free electrons ▶ Embedded Linux expertise ▶ Development , consulting and training ▶ Strong open-source focus ▶ Long time interest in embedded Linux boot ▶ From Orange , France free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Why reduce size? There are multiple reasons for having a small kernel and system Even conceivable to run the whole system in CPU internal RAM or cache (DRAM is power hungry and needs refreshing) See https://tiny.wiki.kernel.org/use_cases http://free-electrons.com 3/1 ▶ Run on very small systems (IoT) ▶ Run Linux as a bootloader ▶ Boot faster (for example on FPGAs) ▶ Reduce power consumption ▶ Security: reduce the attack surface ▶ Cloud workloads: optimize instances for size and boot time. ▶ Spare as much RAM as possible for applications and maximizing performance. free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Reasons for this talk LTO, new gcc versions, compiling with Clang... in size, to help them and to collect good ideas. http://free-electrons.com 4/1 ▶ No talk about size since ELCE 2015 ▶ Some projects stalled (Linux tinifjcation, LLVM Linux...) ▶ Opportunity to have a look at solutions I didn’t try: musl library, Toybox, gcc ▶ Good to have a look again at that topic, and gather people who are still interested ▶ Good to collect and share updated fjgures too. free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
How small can a normal Linux system be? complex) http://free-electrons.com 5/1 ▶ RAM ▶ You need 2-6 MB of RAM for an embedded kernel ▶ Need at least 8-16 MB to leave enough space for user-space (if user-space is not too ▶ More RAM helps with performance! ▶ Storage ▶ You need 2-4 MB of space for an embedded kernel ▶ User space can fjt in a few hundreds of KB. ▶ With a not-too-complex user-space, 8-16 MB of storage can be suffjcient. free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Compiler optimizations See https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html for all available optimizations http://free-electrons.com 6/1 ▶ gcc ofgers an easy-to-use -Os option for minimizing binary size. ▶ It is essentially the optimizations found in -O2 without the ones that increase size free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Using a recent compiler Compiling for ARM versatile, Linux 4.10 A minor gain! http://free-electrons.com 7/1 ▶ With gcc 4.7: 407512 bytes (zImage) ▶ With gcc 6.2: 405968 bytes (zImage, -0.4%) free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Using gcc LTO optimizations LTO: Link Time Optimizations time, linking multiple object fjles together. In particular, this allows to remove unused code. http://people.csail.mit.edu/smcc/projects/single-file- programs/oggenc.c (1.7 MB!) gcc -Os -flto oggenc.c -lm See again https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html for details. http://free-electrons.com 8/1 ▶ Allows gcc to keep extra source information to make further optimizations at link ▶ Even works with programs built from a single source fjle! Example: oggenc from ▶ How to compile with LTO: free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Note: the x86_64 size is not meant to be compared with arm code. 64 bit code is gcc LTO optimizations results bigger than 32 bit code, that’s expected. http://free-electrons.com 9/1 Compiling oggenc.c ▶ With gcc 6.2 for x86_64: ▶ Without LTO : 2122624 bytes (unstripped), 1964432 bytes (stripped) ▶ With LTO : 2064480 bytes (unstripped, -2.7%), 1915016 bytes (stripped, -2.6% ) ▶ With gcc 6.2 for armelhf: ▶ Without LTO : 1157588 bytes (unstripped), 1018972 bytes (stripped) ▶ With LTO : 1118480 bytes (unstripped, -3.4%), 990248 bytes (stripped, -2.8% ) free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
gcc vs clang Let’s try to compile oggenc.c again: gcc oggenc.c -lm -Os; strip a.out Size: 1964432 bytes clang oggenc.c -lm -Os; strip a.out Size: 1865592 bytes (-5%) gcc oggenc.c -lm -flto -Os; strip a.out Size: 1915016 bytes (-2.7%) Note that gcc can win for very small programs (-1.2 % vs clang on hello.c ). http://free-electrons.com 10/1 ▶ Compiled with gcc 6.2.0 on x86_64: ▶ Compiled with clang 3.8.1 on x86_64: ▶ gcc can catch up a little with the LTO option: free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
ARM: arm vs thumb instruction sets http://free-electrons.com 11/1 ofgers the Thumb instruction set , which is supposed to be more compact. ▶ In addition to the arm 32 bit instruction set , the ARM 32 bit architecture also ▶ You can use arm-linux-objdump -S to distinguish between arm and thumb code. Thumb code Arm code 16 bit instructions 32 bit instructions 00011288 <main>: 00011288 <main>: 1128 8 : e92d4870 push {r4, r5, r6, fp, lr} 1128 8 : b5f0 push {r4, r5, r6, r7, lr} 1128 a : b0e5 sub sp, #404 ; 0x194 1128 c : e28db010 add fp, sp, #16 1128 c : af06 add r7, sp, #24 1129 0 : e24ddf61 sub sp, sp, #388 ; 0x184 Addresses Addresses multiples of 2 multiples of 4 free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
ARM: arm vs thumb instruction sets (2) arm-linux-gnueabihf-gcc -marm oggenc.c -lm Result: 1323860 bytes arm-linux-gnueabihf-gcc -mthumb oggenc.c -lm Result: 1233716 bytes ( -6.8% ) limited size reduction. http://free-electrons.com 12/1 ▶ To compile in arm mode: ▶ To compile in thumb mode (default mode for my compiler!): ▶ Notes: ▶ Thumb instructions are more compact but more are needed, which explains the ▶ Thumb mode can be the default for your compiler! ▶ In my tests with -marm , the binary was a mix of Arm and Thumb code. free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
How to get a small kernel? kernel size system features you need. tinyconfig: $(Q)$(MAKE) -f $(srctree)/Makefile allnoconfig tiny.config http://free-electrons.com 13/1 ▶ Run make tinyconfig (since version 3.18) ▶ make tinyconfig is make allnoconfig plus confjguration settings to reduce ▶ You will also need to add confjguration settings to support your hardware and the free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
kernel/confjgs/tiny.confjg # CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE is not set CONFIG_CC_OPTIMIZE_FOR_SIZE=y # CONFIG_KERNEL_GZIP is not set # CONFIG_KERNEL_BZIP2 is not set # CONFIG_KERNEL_LZMA is not set CONFIG_KERNEL_XZ=y # CONFIG_KERNEL_LZO is not set # CONFIG_KERNEL_LZ4 is not set CONFIG_OPTIMIZE_INLINING=y # CONFIG_SLAB is not set # CONFIG_SLUB is not set CONFIG_SLOB=y http://free-electrons.com 14/1 free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
arch/x86/confjgs/tiny.confjg CONFIG_NOHIGHMEM=y # CONFIG_HIGHMEM4G is not set # CONFIG_HIGHMEM64G is not set http://free-electrons.com 15/1 free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
tinyconfjg Linux kernel size (arm) http://free-electrons.com 16/1 900000 800000 700000 600000 text 500000 data Bytes 400000 bss full 300000 200000 100000 0 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 Version free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
tinyconfjg Linux kernel size (x86) http://free-electrons.com 17/1 2500000 2000000 1500000 text bytes data 1000000 bss total 500000 0 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 version free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Linux kernel size notes RAM. smaller results. the stripped kernel size would be too optimistic. http://free-electrons.com 18/1 ▶ We reported the vmlinux fjle size, to refmect the size that the kernel would use in ▶ However, the vmlinux fjle was not stripped in our experiments. You could get ▶ On the other hand, the kernel will make allocations at runtime too. Counting on free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Kernel size on a system that boots Linux 4.10 booting on QEMU ARM VersatilePB Minimum RAM I could boot this kernel with: 4M (3M was too low). Not worse than 10 years back! http://free-electrons.com 19/1 ▶ zImage : 405472 bytes ▶ text: 972660 ▶ data: 117292 ▶ bss: 22312 ▶ total: 1112264 free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support.
Recommend
More recommend