Android Boot Time Optimization


Published on

Published in: Technology

Android Boot Time Optimization

  1. 1. Connect your device to application
  2. 2. Android Boot TimeOptimizationKan-Ru Chen Sep 09, 2011
  3. 3. Agenda Motivation Boot Time Measurement Android Boot Time Analysis Reduction Approach Hibernation Based Technologies We Dont Need Boot-loader Demo Future Work and Conclusions
  4. 4. Motivation
  5. 5. Boot Time Measurement
  6. 6. Traditional Linux Environment
  7. 7. Printk TimesLinux kernel featureBuilt-in since Linux 2.6.11How to enable? Add CONFIG_PRINTK_TIME=y to .config Or choose from menuconfig Kernel hacking ---> [*] Show timing information on printks
  8. 8. Printk TimesOutput Examplelinux$ 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: okAnalysis Toollinux$ 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...
  9. 9. initcall_debugKernel ParameterPrint the time spent for each initcallOutput Examplecalling ipc_init+0x0/0x28 @ 1msgmni has been set to 42initcall ipc_init+0x0/0x28 returned 0 after 1872 usecs
  10. 10. BootchartVisualize the booting processUse “bootchartd” to collect CPU and IO utilizationinformation.On Ubuntu: apt-get install bootchart bootchart-viewOriginal “bootchartd” is not suitable for embeddedusage.
  11. 11. Bootchart$ bootchart bootchart.tgz -f png
  12. 12. StraceTrace system calls during process execution andoutput 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/", 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/", R_OK) = -1 ENOENT15:11:04.245096 open("/etc/", O_RDONLY) = 3
  13. 13. 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 inAOSP.
  14. 14. 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...
  15. 15. PerfNew profiling tool based on the performance countersubsystem of Linux.Very powerful and easy to use.Included in Linux source code:tools/perf/
  16. 16. PerfRecording:$ perf record -a -f^C[ perf record: Woken up 1 times to write data ][ perf record: Captured and wrote 0.288 MB (~12567 samples)]Output$ perf report --sort comm,dso,symbol|head -10# Events: 1K cycles## Overhead Command Shared Object Symbol# ........ ........... ...................... ............# 35.47% firefox [.] 0xc1b3a7 3.08% firefox [.] 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
  17. 17. Perf Timechart$ perf timechart record $ perf timechart
  18. 18. Android Environment
  19. 19. BootchartOriginal “bootchartd” is not suitable on embeddeddevices.Android re-implemented in its “init” program.
  20. 20. BootchartTo build:$ cd system/core/init$ touch init.c$ mm INIT_BOOTCHART=true
  21. 21. BootchartTo run:$ adb shell echo 120 > /data/bootchart-startRemember the /data directory must be write ableduring boot.Use to retrieve the data.
  22. 22. StraceAfter analyzed the bootchart, can use strace toanalyze individual progarm.Available in AOSP since Éclair
  23. 23. StraceModify init.rc fromservice zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-serverTo service zygote /system/xbin/strace -tt -o/data/boot.strace /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
  24. 24. LogcatAndroid log utilityCan output timing informationAdjust loglevel to 6 in init.rc Displays time spent for each command
  25. 25. Dalvik Method TracerMethod tracer is built into DalvikUse DDMS or using calls inside source to collect data.// start tracing to "/sdcard/calc.trace"Debug.startMethodTracing("calc");// ...// stop tracingDebug.stopMethodTracing();
  26. 26. StopwatchNot real stopwatchA utility in Android Framework for measuring C++code.Output result to system log#include <utils/StopWatch.h>…{ StopWatch watch("blah"); /* your codes here */}
  27. 27. Q&A
  28. 28. Android Boot Time Analysis
  29. 29. Boot-loader InitUsually constant timeAvoid init hardware multiple timesEnsure to use maximum CPU frequencyUse faster NAND/MMC reading mechanism
  30. 30. Kernel InitMostly usual suspects ip_auto_config USB init Flash driver initializationFullow the standard Kernel optimizing guide: loading unneeded kernel module at boot time
  31. 31. Zygote Class PreloadingAndroid Framework has thousands of Java classesPreloaded by Zygote and instantiated in its heapTo improve Application startup time and save memoryControlled by resource: preloaded-classes frameworks/base/preloaded-classes
  32. 32. 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.compiledGoogle 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
  33. 33. PackageManager Package ScannigEvery APK is scanned at boot timePackage management code is inefficientUses mmaped files means each access will causepage faultParseZipArchive() scans entire APK for only oneAndroidManifest.xml file
  34. 34. System Services StartingLast stage of Android bootStart every base serviceZygote start SystemServer processStart native service (SurfaceFlinger, AudioFlinger) firstStart each service sequentially
  35. 35. Q&A
  36. 36. Reduction Approach
  37. 37. Boot-loaderImprove U-Boot Reading multi-mmc-block Cache (I/D) enablement Optimize CRC32 Disable verification
  38. 38. Boot-loaderQi Boot-loader Qi U-Boot + XLoader Boot-oader Size ~30K ~270K+20K Only one stage boot-loader Time to Kernel <1s > 5s Small footprint ~30K Usage Product Engineering Code Simple Complicated Currently support − iMX31 − Samsung 24xx − Beagleboard KISS concept − Boot device and load kernel
  39. 39. Kernel Boot TimeFullow the standard Kernel optimizing guide: kernel sizeUse compression or notEnable embedded optionsAvoid loading unneeded kernel module at boot time
  40. 40. Optimize Android InitParallize init tasks insmod cannot be parallized Use external scripts to init at backgroundStart services on demand
  41. 41. Optimize Class PreloadingTrade-off between preload class and applicationstartup timeSplit class to more packages to reduce dependencySave inited heap for later useShare heaps between zygote and children
  42. 42. Filesystem OptimizationAccording to reasearch by Linaro Kernel WGUse correct NAND configuration will improve theperformanceMMC controllers are often optimized for particularusage / filesystemAdjust the filesystem partition scheme
  43. 43. Toothpaste EffectObserved by Sony Developer Tim Bird“When you squeeze a tube of toothpaste, sometimesit just moves the toothpaste somewhere else in thetube, and nothing actually comes out.”
  44. 44. Q&A
  45. 45. Hibernation Based Technologies
  46. 46. QuickBootDeveloped by Japanesecompany UbiquitousDemand loading ofrequired page from flashRequires deepintegration of hardwareand software
  47. 47. Fast-OnDeveloped by CCUBased on existing technologies thus requires littlemodification to userspaceRelease clean-pages before suspendSwap out dirty-pages before save imageImage size reduced leads to faster resume time.
  48. 48. Android Wakelocks & TuxOnIce
  49. 49. TuxOnIce PatchTuxOnIce (was Software Suspend 2) is a hibernationpatchsetCan save images to different locationsCan use different compresion algorithmPorting to ARM is possible
  50. 50. Android WakelocksAn aggressive approach to save device powerUse wakelocks to prevent device going suspendPort TOI to Android have to deal with wakelocksbecause MMC driver might hold a wakelock
  51. 51. Linux Suspend ArchitectureDocumentation/power/devices.txt: 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); };
  52. 52. Q&A
  53. 53. We Dont Need Boot-loader
  54. 54. R-LoaderNormal suspend-to-disk approach has manyduplicated 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
  55. 55. R-Loader0xlab Developer Matt Proposed “Resume-Loader”R-Loader inits some hardware then reads thesuspended kernel image as fast as possibleJump directly to the resume pointKernel will takeover the job and inits reset hardwares
  56. 56. DemoGet 0xdroid 0x7 release TOI Patch for 0x7 release
  57. 57. Future Work and ConclusionsSave the heap image (like core dump) of Zygote afterpreloading classesModify Dalvik to make hibernation image after systeminit and before Launcher startupParallize Android initCache & Share JITed code fragment
  58. 58. Q&A
  59. 59.