Optimize Embedded Boot Time Performance

WHITE PAPER

TABLE OF CONTENTS

Abstract ...... 3

What is “Embedded Linux”? ...... 4

What does Embedded Linux offer? ...... 4

What is Boot Time? ...... 5

Boot Sequence ...... 6

Optimizing Boot Time in an Embedded Linux System ...... 6

Reducing Kernel Boot Time Thereby Increasing Speed ...... 7

Increasing System Startup Time and Application Speed ...... 8

Reducing the Disk Footprint and RAM Size of the Linux Kernel ...... 9

Reducing Application Size and RAM Usage...... 10

Reducing Power Consumption ...... 10

Conclusion ...... 11

Reference ...... 12

Author Info ...... 112

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved.

Abstract Almost everyone has heard of Linux. It is open source, and has been much in discussion in the software development and system administration communities. Linux was originally targeted specifically for desktop PCs running an Intel 80x86 or compatible microprocessor. Linus Torvalds, the inventor of Linux, had said, “Linux will never run on anything but a PC with an IDE hard disk since that is all that I have”. At present, Linux is ported to different microprocessors and runs on multiple platforms that don’t have a hard disk. Most of these devices are not general purpose computer systems and include devices such as network routers and data collection units for tracking tornadoes. It is these types of systems that have collectively been labeled as “Embedded Linux”. Linux has won the race in the embedded device segment. By offering enormous advantages, Linux has crossed every hurdle to enter embedded systems across a wide variety of processor architectures. This has led developers to raise questions about how they could get more performance out of their embedded Linux design, which made “reducing boot time” one of the hot topics of discussion in the embedded Linux community. It is not necessary to invent an engineering resource to save boot time. It is possible to substantially reduce boot time by applying certain system configurations.

This whitepaper presents an insight into embedded Linux and a typical boot sequence. It also covers various techniques that can be used to optimize boot time.

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 3

What is “Embedded Linux”? Linux is itself referred to as a kernel but in daily usage it rarely means so. Embedded Linux generally refers to a complete Linux distribution aimed at embedded devices. In reference to embedded development, there is a Linux kernel and other software or an embedded Linux distribution which contains a package of applications and development tools meant for embedded systems. Linux was originally designed to be a general-purpose operating system (OS). Many approaches have commenced to convert the Linux OS into an embedded OS.

There are two ways to switch to embedded Linux: 1. Solutions provided and supported by vendors like MontaVista, Wind River or TimeSys contain their own development tools and environments, and use a few open- source components and proprietary tools. 2. Using community solutions that are open and supported by the community.

With Linux, the dimension of computing applications increases, and this includes IBM's tiny Linux wrist watch, hand-held devices (PDAs and cell phones), Internet appliances, robotics, telephony infrastructure equipment, and much more.

What does Embedded Linux offer? Though most Linux systems run on PC platforms, it can be a reliable OS for embedded systems. Linux is easier and more flexible to install and administer. Its “back-to-basics” approach is an added advantage. Though a fully featured Linux kernel requires about 1 MB of memory, the Linux micro-kernel actually consumes very little of

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 4

this memory - around 100K on a Pentium CPU, which also includes a virtual memory and all core operating system functions. It is considered a lightweight operating system. The core Linux OS has a simple micro-kernel architecture. It provides a modular block to build a custom embedded system. The File system is on the top of the micro-kernel. Drivers can be either compiled or added to the kernel at run-time. Linux supports multiprocessor systems and is best-suited for embedded Internet devices. It offers scalability that opens an option of running a real-time application on a dual processor system. In various market segments like consumer electronics, products are such that the device needs to be available for immediate use after power on. An OS for desktops offers boot times in the range of 20 seconds to a few minutes. This is highly unacceptable for various market segments. For a device to qualify for an embedded market, it must be available for use as soon as it is turned on.

What is Boot Time? Boot time is the time taken by the system to make it available from the time the power button is pressed. Boot time is one of the major factors in defining the usability of the system. The definition of system availability is different for different devices:  For display devices like cellphones – the appearance of home  For display of shell prompt on development systems with consoles There is no set criterion to measure boot time. In fact, boot time must be measured in the context of the device and its usage.

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 5

Boot Sequence

Power Applied

Pwr/CLK/Reset

Load and execute BootLoader

Load and execute Kernel

Kernel Init

Locate/Load/Mount RootFS

Init User Space

Application start

Optimizing Boot Time in an Embedded Linux

System Many factors are responsible for boot time performance. This calls for a set of techniques to be applied to optimize the boot time of a Linux system. We are using the term 'optimizing' - instead of 'reducing'. as ‘reducing’ means achieving less boot time by taking custom shortcuts, which are difficult to maintain across component versions. Optimization methods are generic in nature and are easy to maintain across different versions.

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 6

Areas of optimization Optimization methods fall under the following categories: 1. Size  Reducing the size of binaries for each component loaded  Removing features that are not required 2. Speed  Optimizing the target processor  Reducing the number of steps for booting  Removing features that are not required

To begin with optimizing the boot time of a device:  Identify the current boot-time  Set the target environment (processor)  Define boundary conditions that are specific to the end-product Optimization is an iterative process. To measure the boot time, a reliable source is required. We have listed a set of techniques that can be applied in order to achieve an optimized boot-time for an embedded Linux system.

Reducing Kernel Boot Time and thereby Increasing

Speed 1. Disable IP auto-config – Instead of initializing the IP address on the kernel command line, use the /etc/init.d/rcS script. 2. The output of kernel bootup messages to the console consumes time. This is normally not needed in the production environment. Disable unwanted console output messages using the “quiet” argument in the Linux kernel bootloader settings.

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 7

Example: root=/dev/ram0 rw init=/startup.sh quiet 3. Directly boot from the bootstrap code to directly load and execute the Linux kernel image instead of the Uboot. 4. Reduce the kernel size by removing things like features, debugging facilities, etc., which are not from the dedicated system. A smaller kernel loads fast and also executes fast. This reduces the time spent on initializing drivers. 5. Spare memory at boot time by skipping the memory allocation part and manage it yourself. Example: there is 32 MB of RAM Boot your kernel with mem=30 The remaining 2 MB can be claimed by the driver code buf = ioremap ( 0x1e00000, /* Start: 30 MB */ 0x200000 /* Size: 2 MB */ ); This saves a critical amount of time allocating memory.

Increasing System Startup Time and Application

Speed 1. SysV vs Init – SysV starts the services sequentially. It waits for the current startup script to be completed before starting the next one. In case of Init, if the pre-requisites are fulfilled, it starts the services in parallel. System utilization is also better in the case of Init. 2. Compiler speed optimizations – Most tools are compiled using compiler optimizations. “O2” is the most common optimization switch of gcc. “O3” can also be used to optimize speed for critical .

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 8

3. Prelink – For executables using shared libraries, it takes a lot of time to load and start an . The dynamic has to perform a significant amount of work mainly in terms of address . It has to perform the same steps over and over again each time the executable is loaded. In order to reduce this, “prelink” modifies executables and shared libraries. This reduces the memory relocation task of the . This, in turn reduces the loading and startup time for large applications using many shared libraries. This also reduces the memory consumed by relocations. Prelink needs to be run every time the executable is updated. 4. Choose appropriate file systems to run fast – Use SquashFS which is a compressed read-only file system. For NAND flash storage, use UBIFS - the successor of JFFS2. 5. Avoid system calls which are heavy – Because of heavy system calls, calls to executables from the shell becomes slow. fork / exec system calls are very heavy. The underlying implementation of Pipes is done using fork / exec. To reduce the delay, avoid their usage in scripts. Example: cat /etc/bin | grep sysfile Replace it with: grep sysfile /etc/bin

Reduce the Disk Footprint and RAM Size of the

Linux Kernel 1. Tiny Linux – Select the features in kernel configuration (CONFIG_EMBEDDED). Remove unnecessary kernel messages (printk, BUG). Certain features that are not needed in some systems can be removed. Use the

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 9

compiling optimizations for size. A smaller kernel executable also saves RAM. 2. Replace initrd by initramfs – This reduces overheads and RAM waste. It allows no block and file system overhead and also no duplication in RAM.

Reduce the Application Size and RAM Usage 1. Static vs Dynamic linking – In static linking, all shared code is duplicated in the executables whereas in dynamic linking, shared library code is not duplicated in the executables. This makes the executable smaller in size. Static linking is useful when there are few executables. Synamic linking is best for medium to big systems (> 500 KB 1MB) 2. Using a light C library – Use uClibc instead of glibc. uClibc exists in embedded Linux systems. 3. “strip” executables – Executables and libraries contain extra information needed to identify problems in a debugger. This is generally used by the developer but is of no use to an end user. The “Strip” command is used to remove debugging information. This helps in saving a significant amount of space. Example : gcc -o test test.c (output size: 4740 bytes) strip test (output size: 2760 bytes)

Reducing Power Consumption 1. Tickless system – The processor receives a timer interrupt at a given frequency (every 4 ms by default on Linux 2.6). This is the way multitasking is achieved. If the system is

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 10

idle, disable the timer interrupt and re-enable it when an event occurs. This saves power in embedded devices. 2. Cpufreq – This enables the user to change the CPU frequency on the fly. Supported architectures (2.6.20): i386, sh, ia64, sparc64, x86_64, powerpc, arm (i.MX only). This is done from userspace through /sys by a user configurable governor process.

Conclusion There are many techniques that developers and system designers can employ in order to meet early boot requirements. To start with, it is good to get a stable baseline measurement of system boot speed. Then start with applying any of the techniques described here. This way, one can be sure that applying techniques helps in achieving real progress towards the goal.

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 11

References

 http://www.ibm.com/developerworks/library/l-embl/  http://www.embedded.com/electronics-blogs/open- mike/4420567/Learning-Linux-for-embedded-systems

Author Info

Harsha Vachher Harsha has been working with HCL’s ERS Practice as a Senior Technical Lead, helping to drive forward a number of new internal initiatives.

©2014, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author. All rights reserved. 12

Hello, I'm from HCL's Engineering and R&D Services. We enable technology led organizations to go to market with innovative products and solutions. We partner with our customers in building world class products and creating associated solution delivery ecosystems to help bring market leadership. We develop engineering products, solutions and platforms across Aerospace and Defense, Automotive, Consumer Electronics, Software, Online, Industrial Manufacturing, Medical Devices, Networking & Telecom, Office Automation, Semiconductor and Servers & Storage for our customers.

For more details contact: [email protected] Follow us on Twitter: http://twitter.com/hclers & LinkedIn: http://lnkd.in/bt8hDXM View our blog-site: http://www.hcltech.com/blogs/engineering -and-rd-services Visit our website: http://www.hcltech.com/engineering-rd-services