RC Scripts Speed-Up

This material was excerpted from a document with the following copyright statement:

Copyright 2002, 2003, 2004 Sony Corporation

Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.

Copyright 2002-2004 by MontaVista Software.

It was submitted as input to the forum by Sony Corporation, on April 8, 2004.

Introduction

The init scripts of the existing Embedded Linux distribution are the shell scripts to be executed with ’bash’. To reduce system boot time, some modifications to be applied to the scripts and faster shell interpreter to be used. The document describes the BusyBox optimizations; the init scripts modifications; how to reduce the system boot time using the optimized BusyBox, and to optimize shell scripts for BusyBox. Also, the benchmark procedure and optimization results are described.

Purpose of Feature

The init scripts execution time, i.e. the time interval between the start of the init process and the start of user applications, must be reduced.

Feature requirements

The modified init scripts must be run with "bash" as well as the BusyBox "ash" shell.

The execution time of the init scripts and the total system boot time must be reduced.

The guide to speed-optimization of the init scripts must be provided.

Acceptance Criteria

The modified init scripts are able to successfully run with "bash" as well as the BusyBox "ash" shell.

The execution time of the init scripts with BusyBox is reduced in the comparison with the original init scripts with "bash"; the total system boot time is not greater then 5 sec.

The guide to speed-optimization of the init scripts is available.

BusyBox Optimization

Since ’bash’ and GNU utilities are very heavy applications, BusyBox is useful to reduce system boot time. BusyBox combines tiny versions of many common UNIX utilities (shellutils, fileutils, etc.) into a single small executable. The commands and utilities included in BusyBox are divided into the classes: built-ins and applets. The built-ins are simply invoked as functions, and applets are invoked by means of the ’fork/exec’ system calls. Also, BusyBox scripts can use external commands and utilities.

Usage of the BusyBox built-ins is rather than the applets and external commands by performance reasons, because the ’fork/exec’ system calls are very heavy and they make the main contribution to shell inefficiency. Because the original BusyBox is only size-optimized, the following features must be considered from the performance standpoint:

Each command including built-ins within pipes are forked.

Each back-quoted command is forked.

The ’echo’, ’test’, and ’[’ commands and other most frequent commands in the scripts are implemented as applets.

To avoid such drawbacks, we optimized BusyBox 1.00-pre3 in order to speed up script execution. The BusyBox optimizations for the ’ash’ shell are listed below:

The set of shell commands and utilities is implemented as built-ins.

The invoked ’cat’ command at the beginning of pipes is eliminated and file descriptors are passed only into the next command of the pipe.

This need seems to be addressed for all applets at least in Busybox 1.1.1 (and perhaps in earlier versions). The "Shells -> Standalone shell" configuration setting is supposed to address this need. See the menuconfig help for details.

In newer BusyBox releases (1.13.0 and maybe even earlier releases) the most frequently used applets, 'test', 'echo' can be configured as being 'built-in'. The newer BusyBox releases are also smaller in size and can save some extra milliseconds in execution of startup scripts.

Known Problems

The following BusyBox commands work in the different manner from the ’bash’ commands and
GNU utilities: ’nice’, ’find’, ’mount’, ’umount’, ’init’, ’halt’, ’shutdown’, ’syslogd’,
’klogd’, ’hwclock’, ’cron’, ’anacron’, ’crontab’, 'pidof'.

If the different behaviour is unwanted or these BusyBox applets do not provide the necessary utility, use the external commands instead of them, or simply do not configure them as applets.

How to Optimize Init Scripts for BusyBox

Follow the rules listed below to reduce execution time for the init scripts:

Do not use unnecessary codes in the scripts.

Replace external commands and utilities with the BusyBox built-ins as far as possible.

Do not use the piped commands as far as possible.

Reduce the number of commands within a pipe.

Do not use the back-quoted commands as far as possible.

The main goal of such optimization is to reduce the number of the "fork/exec" calls during a script execution.

Examples of Init Scripts Optimization

The following examples demonstrate how the recommendations of shell scripts optimization can be applied to the init scripts.

Unnecessary codes elimination

This example demonstrates the elimination of duplicate codes from the "mountswap.sh" and "checkrootfs.sh" scripts: the command "swapon" runs one time in the modified scripts.

Piped command usage

This example demonstrates the elimination of the piped commands (the example is hypothetic, because the init scripts do not contain such inefficiencies).

Before optimization:

cat /proc/mounts | grep ext3 | cut -d’ ’ -f2,3

After optimization:

sed -n ’s/^[^ ]* \([^ ]*\) \(ext3\) .*$/\1 \2/p’ /proc/mounts

This example demonstrates the reduction of the commands in the pipe (the example is also hypothetic). Note, the optimized version does not invoke the "fork" call, because the "cat" optimization is used.

Before optimization:

cat /etc/passwd | grep user | wc -l | tr -d ’’ | sed ’s/ *//’

After optimization:

cat /etc/passwd | grep -c user

Back-quoted command usage

This example demonstrates the back-quoted command elimination (the example is hypothetic, because the init scripts do not contain such inefficiencies).

Init Scripts Optimization

The existing init scripts were modified to reduce their execution time following the recommendations, which are described above.

Benchmark Environment and Procedure

To estimate the results of the init script optimization and the BusyBox usage, the TI OMAP 1510 Innovator platform is used.
To measure the duration of the kernel loading, the KFI support is used (to measure the init script execution time, the KFI support is disabled).
The measurements are performed on the systems with/without XIP support
To take measurements without the XIP support, the following kernel configuration is used:

The set of the init scripts of the consumer packages is divided into the minimal and optional subsets. The following scripts belong to the minimal subset: ’bootmisc.sh’, ’checkfs.sh’, ’checkroot.sh’, ’hwclock.sh’, ’modutils.sh’, ’mountall.sh’, ’networking.sh’, ’urandom.sh’.