之前分析各个部分耗时,都是通过分析log,在SecureCRT中设置时间戳,打印出如下log
[ 22.266201] c1 [saudio] saudio_wait_monitor_cmd error -5
[ 22.266205] c1 sblock_receive:sblock-5-13 not ready!
[ 22.266209] c1 [saudio] Error: sblock_receive dst 5, channel 13 result is -5
[ 22.266226] c1 [saudio] saudio_wait_monitor_cmd error -5
[ 22.266229] c1 sblock_receive:sblock-5-17 not ready!
[ 22.266232] c1 [saudio] Error: sblock_receive dst 5, channel 17 result is -5
[ 22.302337] c3 init: Parsing file /system/etc/init/ims_bridged.rc...
[ 22.309790] c3 init: Parsing file /system/etc/init/init-debug.rc...
[ 22.316932] c3 init: Parsing file /system/etc/init/installd.rc...
---------------------
有效,但数据不全面,不利于分析。百度到了bootchart这个工具,很实用。
1) bootchart工具
在Android O当中,bootchart已经默认集成在Init当中。所以收集数据可以用以下步骤:
只需要你通过开关打开即可生效:“adb shell ‘touch /data/bootchart/enabled’”
重启设备
$ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh抓取数据生成一张png图片,即可进行分析。
sp8541e_srvm:/data/bootchart $ tar -czf bootchart.tgz enabled header proc_diskstats.log proc_ps.log proc_stat.log
adb pull /data/bootchart/bootchart.tgz .\Desktop\
拷贝bootchart.tgz 到Ubuntu服务器上
Ubuntu服务器上 sudo apt-get install bootchart
bootchart bootchart.tgz
命令结束后就生成对应的png图片。
但是如果碰到bootchart工具不能使用的情况,就需要自己下载bootchart源码:http://www.bootchart.org/download.html,解压后:
1. ant编译
2. java -jar bootchart.jar ../bootchart/bootchart.tgz 生成分析结果图片:
3.
在这里总结下最重要的三步
echo 120 > /data/bootchart/start
tar -czf bootchart.tgz bootchart.tgz enabled header proc_diskstats.log proc_ps.log proc_stat.log
bootchart bootchart.tgz
通过这三步基本上可以获取到开机信息的图片了
从这张启动图片里,我们能够看到:
1. 总共的启动时间是30s,这里指的是从kernel开始启动的时间开始计时,一直到Android 启动结束.
2. Init 进程一直到13s左右才是启动其他进程,如ueventd。
3. Zygote64/Zygote进程大约在22s左右启动。
4. 这样计算下来Android fw的启动时间大致为8s左右。
这样看起来就比较清晰了,Init进程13秒才开始工作的原因是什么?这个时间如果能缩短的话启动时间会效果很显著。
通过dmesg信息,可以看到:
[32m[ 13.227842] [0m[33minit[0m: init second stage started!
[32m[ 13.237508] [0m[33mselinux[0m: SELinux: Loaded file_contexts\x0a
[32m[ 13.243342] [0m[33mselinux[0m: SELinux: Loaded property_contexts from /plat_property_contexts & /nonplat_property_contexts.\x0a
[32m[ 13.254522] [0m[33minit[0m: Running restorecon…
[32m[ 13.593214] [0m[33mselinux[0m[31m: SELinux: Could not stat /dev/block: No such file or directory.\x0a[0m
[32m[ 13.601607] [0m[33minit[0m[31m: waitpid failed: No child processes[0m
[32m[ 13.606477] [0m[33minit[0m: Couldn’t load properties from /system/etc/prop.default: No such file or directory
[32m[ 13.615416] [0m[33minit[0m: Couldn’t load properties from /prop.default: No such file or directory
[32m[ 13.623768] [0m[33minit[0m: Couldn’t load properties from /odm/default.prop: No such file or directory
[32m[ 13.631946] [0m[33minit[0m: Couldn’t load properties from /vendor/default.prop: No such file or directory
[32m[ 13.641126] [0m[33minit[0m: Created socket ‘/dev/socket/property_service’, mode 666, user 0, group 0
[32m[ 13.648925] [0m[33minit[0m: Parsing file /init.rc…
[32m[ 13.653220] [0m[33minit[0m: Added ‘/init.environ.rc’ to import list
[32m[ 13.658291] [0m[33minit[0m: Added ‘/init.usb.rc’ to import list
[32m[ 13.663329] [0m[33minit[0m: Added ‘/init.angler.rc’ to import list
[32m[ 13.668660] [0m[33minit[0m: Added ‘/vendor/etc/init/hw/init.angler.rc’ to import list
[32m[ 13.675611] [0m[33minit[0m: Added ‘/init.usb.configfs.rc’ to import list
[32m[ 13.681379] [0m[33minit[0m: Added ‘/init.zygote64_32.rc’ to import list
[32m[ 13.688215] [0m[33minit[0m: Parsing file /init.environ.rc…
[32m[ 13.692099] [0m[33minit[0m: Parsing file /init.usb.rc…
[32m[ 13.696775] [0m[33minit[0m: Parsing file /init.angler.rc…
[32m[ 13.701323] [0m[33minit[0m: Added ‘init.angler.usb.rc’ to import list
[32m[ 13.706678] [0m[33minit[0m: Added ‘init.angler.diag.rc’ to import list
[32m[ 13.712305] [0m[33minit[0m: Added ‘init.angler.sensorhub.rc’ to import list
[32m[ 13.719668] [0m[33minit[0m: Parsing file init.angler.usb.rc…
[32m[ 13.724264] [0m[33minit[0m: Parsing file init.angler.diag.rc…
确实是13s左右init进程才开始工作。所以第一步我们来想办法把这个时间看看能不能缩短。
通过这个工具,我们能够大致清楚了从系统启动到Android启动完成后的主要时间,同时也能够简单的分析出kernel启动时间,zygote启动时间,sysetmserver启动时间,但是这个工具还是不能把Android 启动阶段毕竟清晰的展现出来,这个时候就需要使用到第二个工具了。
2) boot mesg
我们很高兴的可以看到在Android中已经加入了一系列的mesg,我们可以通过这一系列的mesg可以抓出android fw启动的相关时间。
到此为止,我们大致能够得出android 启动的每个阶段所耗费的时间了,后续我们来仔细分析每个阶段的代码,看看都干了啥。
第一步,关闭kernel log输出:
65 int console_printk[4] = {
66 0,//DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
67 0,//DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
68 0,//MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
69 0,//DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
70 };
简单粗暴,全部关闭掉,重新编译boot.img测试下,快了将近10s.。。。
通过查看dmesg:
[32m[ 5.276684] [0m[33minit[0m: Parsing file /init.rc...
[32m[ 5.276945] [0m[33minit[0m: Added '/init.environ.rc' to import list
[32m[ 5.276959] [0m[33minit[0m: Added '/init.usb.rc' to import list
[32m[ 5.276978] [0m[33minit[0m: Added '/init.angler.rc' to import list
[32m[ 5.276997] [0m[33minit[0m: Added '/vendor/etc/init/hw/init.angler.rc' to import list
[32m[ 5.277010] [0m[33minit[0m: Added '/init.usb.configfs.rc' to import list
[32m[ 5.277026] [0m[33minit[0m: Added '/init.zygote64_32.rc' to import list
[32m[ 5.278233] [0m[33minit[0m: Parsing file /init.environ.rc...
[32m[ 5.278331] [0m[33minit[0m: Parsing file /init.usb.rc...
[32m[ 5.278675] [0m[33minit[0m: Parsing file /init.angler.rc...
[32m[ 5.278851] [0m[33minit[0m: Added 'init.angler.usb.rc' to import list
[32m[ 5.278865] [0m[33minit[0m: Added 'init.angler.diag.rc' to import list
[32m[ 5.278878] [0m[33minit[0m: Added 'init.angler.sensorhub.rc' to import list
[32m[ 5.280094] [0m[33minit[0m: Parsing file init.angler.usb.rc...
[32m[ 5.280726] [0m[33minit[0m: Parsing file init.angler.diag.rc...
[32m[ 5.280817] [0m[33minit[0m: Parsing file init.angler.sensorhub.rc...
[32m[ 5.280908] [0m[33minit[0m: Parsing file /vendor/etc/init/hw/init.angler.rc...
[32m[ 5.280934] [0m[33minit[0m[31m: could not import file '/vendor/etc/init/hw/init.angler.rc' from '/init.rc': No such file or directory[0m
[32m[ 5.280951] [0m[33minit[0m: Parsing file /init.usb.configfs.rc...
[32m[ 5.281447] [0m[33minit[0m: Parsing file /init.zygote64_32.rc...
[32m[ 5.281581] [0m[33minit[0m: Parsing file /system/etc/init...
[32m[ 5.281607] [0m[33minit[0m: Parsing file /vendor/etc/init...
[32m[ 5.281632] [0m[33minit[0m: Parsing file /odm/etc/init...
[32m[ 5.281702] [0m[33minit[0m: processing action (early-init)
[32m[ 5.282227] [0m[33minit[0m: starting service 'ueventd'...
[32m[ 5.282722] [0m[33minit[0m[31m: failed to open /acct/uid_0/pid_330/cgroup.procs: No such file or directory[0m
[32m[ 5.282756] [0m[33minit[0m[31m: createProcessGroup(0, 330) failed for service 'ueventd': No such file or directory[0m
[32m[ 5.282980] [0m[33minit[0m: processing action (wait_for_coldboot_done)
[32m[ 5.283890] [0m[31mmsm_rng:Data not available![0m
[32m[ 5.284876] [0m[33mueventd[0m: ueventd started!
发现Init进程确实提前到kernel启动5s左右。。
参考:
https://blog.csdn.net/ljp1205/article/details/78360701
https://blog.csdn.net/qqxiaoqiang1573/article/details/56839031