android boot time optimization

59
Connect your device to application

Upload: kan-ru-chen

Post on 05-Dec-2014

13.546 views

Category:

Technology


9 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Android Boot Time Optimization

Connect your device to application

Page 2: Android Boot Time Optimization

Android Boot Time Optimization

Kan-Ru [email protected]

Sep 09, 2011

Page 3: Android Boot Time Optimization

Agenda Motivation

Boot Time Measurement

Android Boot Time Analysis

Reduction Approach

Hibernation Based Technologies

We Don't Need Boot-loader

Demo

Future Work and Conclusions

Page 4: Android Boot Time Optimization

Motivation

Page 5: Android Boot Time Optimization

Boot Time Measurement

Page 6: Android Boot Time Optimization

Traditional Linux Environment

Page 7: Android Boot Time Optimization

Printk TimesLinux kernel feature

Built-in since Linux 2.6.11

How to enable? Add CONFIG_PRINTK_TIME=y to .config Or choose from menuconfig

Kernel hacking ---> [*] Show timing information on printks

Page 8: Android Boot Time Optimization

Printk TimesOutput Example

linux$ dmesg > timefilelinux$ scripts/show_delta timefile...[0.194488 < 0.194488 >] OMAP DMA hardware revision 5.0[0.259948 < 0.065460 >] bio: create slab <bio-0> at 0[0.267822 < 0.007874 >] SCSI subsystem initialized...

Analysis Tool

linux$ dmesg[0.000000] per task-struct memory footprint: 1152 bytes[0.003692] Calibrating delay loop... 506.27 BogoMIPS (lpj=1978368)[0.079833] pid_max: default: 32768 minimum: 301[0.080230] Security Framework initialized[0.080474] Mount-cache hash table entries: 512[0.083892] CPU: Testing write buffer coherency: ok

Page 9: Android Boot Time Optimization

initcall_debugKernel Parameter

Print the time spent for each initcall

Output Example

calling ipc_init+0x0/0x28 @ 1msgmni has been set to 42initcall ipc_init+0x0/0x28 returned 0 after 1872 usecs

Page 10: Android Boot Time Optimization

BootchartVisualize the booting process

Use “bootchartd” to collect CPU and IO utilization information.

On Ubuntu:apt-get install bootchart bootchart-view

Original “bootchartd” is not suitable for embedded usage.

Page 11: Android Boot Time Optimization

Bootchart$ bootchart bootchart.tgz -f png

Page 12: Android Boot Time Optimization

StraceTrace system calls during process execution and output timing information.

$ strace -tt ls15:11:04.243357 execve("/bin/ls", ["ls"], [/* 51 vars */]) = 015:11:04.244252 brk(0) = 0x234f00015:11:04.244458 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT15:11:04.244676 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f144479400015:11:04.244852 access("/etc/ld.so.preload", R_OK) = -1 ENOENT15:11:04.245096 open("/etc/ld.so.cache", O_RDONLY) = 3

Page 13: Android Boot Time Optimization

OProfileOProfile is a system-wide profiler for Linux systems.

Capable of profiling all running code at low overhead.

Supports wide number of hardwares.

Profiling daemon ported to Android and available in AOSP.

Page 14: Android Boot Time Optimization

OProfileOutput Example

$ opreport --exclude-dependentCPU: PIII, speed 863.195 MHz (estimated)Counted CPU_CLK_UNHALTED events (clocks processor is not halted)...450385 75.6634 cc1plus 60213 10.1156 lyx 29313 4.9245 XFree86 11633 1.9543 as 10204 1.7142 oprofiled 7289 1.2245 vmlinux 7066 1.1871 bash 6417 1.0780 oprofile 6397 1.0747 vim...

Page 15: Android Boot Time Optimization

PerfNew profiling tool based on the performance counter subsystem of Linux.

Very powerful and easy to use.

Included in Linux source code:tools/perf/

Page 16: Android Boot Time Optimization

PerfRecording:

Output

$ perf record -a -f^C[ perf record: Woken up 1 times to write data ][ perf record: Captured and wrote 0.288 MB perf.data (~12567 samples)]

$ perf report --sort comm,dso,symbol|head -10# Events: 1K cycles## Overhead Command Shared Object Symbol# ........ ........... ...................... ............# 35.47% firefox libxul.so [.] 0xc1b3a7 3.08% firefox libcairo.so.2.11000.2 [.] 0xff88 2.98% Xorg Xorg (deleted) [.] 0xe201c 2.51% firefox firefox [.] 0x2726 1.49% Xorg [kernel.kallsyms] [k] find_vma 0.93% perf_3.0.0 perf_3.0.0 [.] hex2u64

Page 17: Android Boot Time Optimization

PerfTimechart

$ perf timechart record$ perf timechart

Page 18: Android Boot Time Optimization

Android Environment

Page 19: Android Boot Time Optimization

BootchartOriginal “bootchartd” is not suitable on embedded devices.

Android re-implemented in its “init” program.

Page 20: Android Boot Time Optimization

BootchartTo build:$ cd system/core/init$ touch init.c$ mm INIT_BOOTCHART=true

Page 21: Android Boot Time Optimization

BootchartTo run:$ adb shell 'echo 120 > /data/bootchart-start'

Remember the /data directory must be write able during boot.

Use grab-bootchart.sh to retrieve the data.

Page 22: Android Boot Time Optimization

StraceAfter analyzed the bootchart, can use strace to analyze individual progarm.

Available in AOSP since Éclair

Page 23: Android Boot Time Optimization

StraceModify init.rc fromservice zygote /system/bin/app_process -Xzygote /system/bin \ --zygote --start-system-server

service zygote /system/xbin/strace -tt -o/data/boot.strace \ /system/bin/app_process -Xzygote /system/bin \ --zygote --start-system-server

To

Page 24: Android Boot Time Optimization

LogcatAndroid log utility

Can output timing information

Adjust loglevel to 6 in init.rc Displays time spent for each command

Page 25: Android Boot Time Optimization

Dalvik Method TracerMethod tracer is built into Dalvik

Use DDMS or using calls inside source to collect data.// start tracing to "/sdcard/calc.trace"Debug.startMethodTracing("calc");// ... // stop tracingDebug.stopMethodTracing();

Page 26: Android Boot Time Optimization

StopwatchNot real stopwatch

A utility in Android Framework for measuring C++ code.

Output result to system log

#include <utils/StopWatch.h>…{ StopWatch watch("blah"); /* your codes here */}

Page 27: Android Boot Time Optimization

Q&A

Page 28: Android Boot Time Optimization

Android Boot Time Analysis

Page 29: Android Boot Time Optimization

Boot-loader InitUsually constant time

Avoid init hardware multiple times

Ensure to use maximum CPU frequency

Use faster NAND/MMC reading mechanism

Page 30: Android Boot Time Optimization

Kernel InitMostly usual suspects ip_auto_config USB init Flash driver initialization

Fullow the standard Kernel optimizing guide: http://elinux.org/Boot_Time

Avoid loading unneeded kernel module at boot time

Page 31: Android Boot Time Optimization

Zygote Class PreloadingAndroid Framework has thousands of Java classes

Preloaded by Zygote and instantiated in its heap

To improve Application startup time and save memory

Controlled by resource: preloaded-classes frameworks/base/preloaded-classes

Page 32: Android Boot Time Optimization

Zygote Class PreloadingCan use the tool in framework to adjust the list:$ adb logcat > logcat.txt$ java -p preload.jar Compile logcat.txt logcat.compiled$ java -p preload.jar PrintCsv logcat.compiled

Google Android Developer Dianne Hackborn said: The content of the file is a “black art” You can adjust this as much as you like But the result maybe suboptimal

Page 33: Android Boot Time Optimization

PackageManager Package ScannigEvery APK is scanned at boot time

Package management code is inefficient

Uses mmaped files means each access will cause page fault

ParseZipArchive() scans entire APK for only one AndroidManifest.xml file

Page 34: Android Boot Time Optimization

System Services StartingLast stage of Android boot

Start every base service

Zygote start SystemServer process

Start native service (SurfaceFlinger, AudioFlinger) first

Start each service sequentially

Page 35: Android Boot Time Optimization

Q&A

Page 36: Android Boot Time Optimization

Reduction Approach

Page 37: Android Boot Time Optimization

Boot-loaderImprove U-Boot Reading multi-mmc-block Cache (I/D) enablement Optimize CRC32 Disable verification

Page 38: Android Boot Time Optimization

Qi Boot-loader Only one stage boot-loader Small footprint ~30K Currently support

− iMX31− Samsung 24xx− Beagleboard

KISS concept− Boot device and load kernel

Boot-loaderQiBoot-oader

U-Boot + XLoader

Size ~30K ~270K+20K

Time to Kernel < 1 s > 5s

Usage Product Engineering

Code Simple Complicated

Page 39: Android Boot Time Optimization

Kernel Boot TimeFullow the standard Kernel optimizing guide: http://elinux.org/Boot_Time

Minimize kernel size

Use compression or not

Enable embedded options

Avoid loading unneeded kernel module at boot time

Page 40: Android Boot Time Optimization

Optimize Android InitParallize init tasks insmod cannot be parallized Use external scripts to init at background

Start services on demand

Page 41: Android Boot Time Optimization

Optimize Class PreloadingTrade-off between preload class and application startup time

Split class to more packages to reduce dependency

Save inited heap for later use

Share heaps between zygote and children

Page 42: Android Boot Time Optimization

Filesystem OptimizationAccording to reasearch by Linaro Kernel WG

Use correct NAND configuration will improve the performance

MMC controllers are often optimized for particular usage / filesystem

Adjust the filesystem partition scheme

Page 43: Android Boot Time Optimization

Toothpaste EffectObserved by Sony Developer Tim Bird

“When you squeeze a tube of toothpaste, sometimes it just moves the toothpaste somewhere else in the tube, and nothing actually comes out.”

Page 44: Android Boot Time Optimization

Q&A

Page 45: Android Boot Time Optimization

Hibernation Based Technologies

Page 46: Android Boot Time Optimization

QuickBootDeveloped by Japanese company Ubiquitous

Demand loading of required page from flash

Requires deep integration of hardware and software

Page 47: Android Boot Time Optimization

Fast-OnDeveloped by CCU

Based on existing technologies thus requires little modification to userspace

Release clean-pages before suspend

Swap out dirty-pages before save image

Image size reduced leads to faster resume time.

Page 48: Android Boot Time Optimization

Android Wakelocks & TuxOnIce

Page 49: Android Boot Time Optimization

TuxOnIce PatchTuxOnIce (was Software Suspend 2) is a hibernation patchset

Can save images to different locations

Can use different compresion algorithm

Porting to ARM is possible

Page 50: Android Boot Time Optimization

Android WakelocksAn aggressive approach to save device power

Use wakelocks to prevent device going suspend

Port TOI to Android have to deal with wakelocks because MMC driver might hold a wakelock

Page 51: Android Boot Time Optimization

Linux Suspend Architecture

struct dev_pm_ops { int (*prepare)(struct device *dev); void (*complete)(struct device *dev); int (*suspend)(struct device *dev); int (*resume)(struct device *dev); int (*freeze)(struct device *dev); int (*thaw)(struct device *dev); int (*poweroff)(struct device *dev); int (*restore)(struct device *dev); int (*suspend_noirq)(struct device *dev); int (*resume_noirq)(struct device *dev); int (*freeze_noirq)(struct device *dev); int (*thaw_noirq)(struct device *dev); int (*poweroff_noirq)(struct device *dev); int (*restore_noirq)(struct device *dev); int (*runtime_suspend)(struct device *dev); int (*runtime_resume)(struct device *dev); int (*runtime_idle)(struct device *dev);};

Documentation/power/devices.txt:

Page 52: Android Boot Time Optimization

Q&A

Page 53: Android Boot Time Optimization

We Don't Need Boot-loader

Page 54: Android Boot Time Optimization

R-LoaderNormal suspend-to-disk approach has many duplicated effort Boot-loader inits some hardwares Boot-loader loads the normal kernel image Kernel inits some hardwares again Kernel loads the suspended kernel image Kernel resumes, inits some hardwares again

Page 55: Android Boot Time Optimization

R-Loader0xlab Developer Matt Proposed “Resume-Loader”

R-Loader inits some hardware then reads the suspended kernel image as fast as possible

Jump directly to the resume point

Kernel will takeover the job and inits reset hardwares

Page 56: Android Boot Time Optimization

DemoGet 0xdroid 0x7 release https://code.google.com/p/0xdroid/wiki/0x7_leb_gingerbread

Get TOI Patch for 0x7 release https://gitorious.org/0xlab-kernel/kernel/commits/toi/linaro-android.38

Page 57: Android Boot Time Optimization

Future Work and ConclusionsSave the heap image (like core dump) of Zygote after preloading classes

Modify Dalvik to make hibernation image after system init and before Launcher startup

Parallize Android init

Cache & Share JITed code fragment

Page 58: Android Boot Time Optimization

Q&A

Page 59: Android Boot Time Optimization

http://0xlab.org