Android7.0调试Init进程(开机过程如何确认init耗时点)

来源:互联网 发布:js ajax获取json文件 编辑:程序博客网 时间:2024/04/30 08:16

      有时候分析log时会发现在init进程中耗时较长, 大约过了8s才启动zygote进程, 由于init.rc中的命令都必须按照顺序来执行, 并且是一个一个执行串行了. 这就会发生一个命令执行时间太长阻塞下一个命令执行。这时就需要将init进程执行命令花费时间的log打开, 来具体定位到底执行哪个命令耗时比较长.7.0具体代码位置在:system/core/init/action.cpp

void Action::ExecuteCommand(const Command& command) const {    Timer t;    int result = command.InvokeFunc();     //执行命令对应的函数    if (klog_get_level() >= KLOG_INFO_LEVEL) { //判断klog level是否大于KLOG_INFO_LEVEL        std::string trigger_name = BuildTriggersString();        std::string cmd_str = command.BuildCommandString();        std::string source = command.BuildSourceString();        INFO("Command '%s' action=%s%s returned %d took %.2fs\n",             cmd_str.c_str(), trigger_name.c_str(), source.c_str(),             result, t.duration());    //将命令执行时间,以及所执行的命令以及所在文件路径打印出.    }}
通过查看开机过程kernel log发现并没有这些log, 主要是由于要执行的命令太多了,整个系统在init进程中要做很多事情,如果都打印出来对开机性能有影响.

具体这个log打印不出有两个原因:

1.klog level小于KLOG_INFO_LEVEL

2.INFO级别log输不出

详细代码流程解析请阅读Klog机制一文.

所以我们就可以对其进行修改.

diff --git a/init/action.cpp b/init/action.cppindex 510ea89..5a11a51 100644--- a/init/action.cpp+++ b/init/action.cpp@@ -118,15 +118,15 @@ void Action::ExecuteCommand(const Command& command) const {     Timer t;     int result = command.InvokeFunc(); -    if (klog_get_level() >= KLOG_INFO_LEVEL) {+    //if (klog_get_level() >= KLOG_INFO_LEVEL) {         std::string trigger_name = BuildTriggersString();         std::string cmd_str = command.BuildCommandString();         std::string source = command.BuildSourceString(); -        INFO("Command '%s' action=%s%s returned %d took %.2fs\n",+        NOTICE("Command '%s' action=%s%s returned %d took %.2fs\n",              cmd_str.c_str(), trigger_name.c_str(), source.c_str(),              result, t.duration());-    }+    //} }

将判断条件去掉,将INFO 改为NOTICE.这样就可以看到具体哪个命令执行时间长了.

不过有可能kernel buffer size太小,输出这么多的log将前面的log冲掉,导致发生log丢失问题.

可以参考之前文章Android系统启动总览一文中有调整kernel buffer size的方法.

修改代码后make bootimage编译boot.img文件使用fastboot将boot.img替换到手机里.
1.adb reboot bootlader
2.fastboot flash boot (boot.img所在路径)/boot.img
3.fastboot reboot
这样就可以将每个命令的执行时间打印出来,进行定位问题.

0 0