Android 6.0 开机时间分析工具bootchart的使用

前言

最近分配到一个与开机优化的任务,无意中看到了bootchart这个工具,使用后感觉挺不错,特此记录分享下。

源码分析

进入系统源码system/core/init/目录下会有如下相关的源码文件

bootchart.cpp bootchart.h grab-bootchart.sh readme.txt

下面来一一分析这些文件 grab-bootchart.sh

#!/bin/sh # # This script is used to retrieve a bootchart log generated by init. # All options are passed to adb, for better or for worse. # See the readme in this directory for more on bootcharting. TMPDIR=/tmp/android-bootchart rm -rf $TMPDIR mkdir -p $TMPDIR LOGROOT=/data/bootchart TARBALL=bootchart.tgz FILES="header PRoc_stat.log proc_ps.log proc_diskstats.log kernel_pacct" for f in $FILES; do adb "${@}" pull $LOGROOT/$f $TMPDIR/$f 2>&1 > /dev/null done (cd $TMPDIR && tar -czf $TARBALL $FILES) bootchart ${TMPDIR}/${TARBALL} gnome-open ${TARBALL%.tgz}.png echo "Clean up ${TMPDIR}/ and ./${TARBALL%.tgz}.png when done"

这个脚本主要是将/data/bootchart目录下生成的文件,打包成压缩包bootchart.tgz。然后执行bootchart通过bootchart.tgz压缩包生成对应的png图片。这个脚本主要是针对linux系统,因为用的是Windows系统,所以我这里直接手动输入对应的命令就好了。 主要有用的两条命令: tar -czf bootchart.tgz header proc_stat.log proc_ps.log proc_diskstats.log kernel_pacct bootchart bootchart.tgz

readme.txt

Bootcharting ------------ This version of init contains code to perform "bootcharting": generating log files that can be later processed by the tools provided by www.bootchart.org. On the emulator, use the -bootchart <timeout> option to boot with bootcharting activated for <timeout> seconds. On a device, create /data/bootchart/start with a command like the following: adb shell 'echo $TIMEOUT > /data/bootchart/start' Where the value of $TIMEOUT corresponds to the desired bootcharted period in seconds. Bootcharting will stop after that many seconds have elapsed. You can also stop the bootcharting at any moment by doing the following: adb shell 'echo 1 > /data/bootchart/stop' Note that /data/bootchart/stop is deleted automatically by init at the end of the bootcharting. This is not the case with /data/bootchart/start, so don't forget to delete it when you're done collecting data. The log files are written to /data/bootchart/. A script is provided to retrieve them and create a bootchart.tgz file that can be used with the bootchart command-line utility: sudo apt-get install pybootchartgui # grab-bootchart.sh uses $ANDROID_SERIAL. $ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh One thing to watch for is that the bootchart will show init as if it started running at 0s. You'll have to look at dmesg to work out when the kernel actually started init.

readme中主要介绍了怎么收集bootchart的信息和安装bootchart,readme里安装的是pybootchartgui,而我实际安装的是bootchart,所以我这里主要是使用bootchart。 主要有用的两条命令: adb shell echo 120 > /data/bootchart/start sudo apt-get install bootchart

bootchart.cppbootchart.h就不贴完整的代码了,贴一个它怎么开始执行的部分代码。

#define LOG_ROOT "/data/bootchart" #define LOG_STARTFILE LOG_ROOT"/start" static int bootchart_init() { int timeout = 0; std::string start; //这里会读取/data/bootchart/start文件中的信息 android::base::ReadFileToString(LOG_STARTFILE, &start); if (!start.empty()) { timeout = atoi(start.c_str()); } else { // When running with emulator, androidboot.bootchart=<timeout> // might be passed by as kernel parameters to specify the bootchart // timeout. this is useful when using -wipe-data since the /data // partition is fresh. std::string cmdline; android::base::ReadFileToString("/proc/cmdline", &cmdline); #define KERNEL_OPTION "androidboot.bootchart=" if (strstr(cmdline.c_str(), KERNEL_OPTION) != NULL) { timeout = atoi(cmdline.c_str() + sizeof(KERNEL_OPTION) - 1); } } if (timeout == 0) return 0; .... }

通过bootchart.cpp源码我们知道,bootchart会读取/data/bootchart/start中的信息,因为之前我们使用了`echo 120 > /data/bootchart/start’。所以它读取处理的时间就是120秒。然后就会去执行下面的收集信息操作了。

实战开撸

首先,来介绍下我的开发环境:

Windows 7 Linux 服务器

OK,开撸。 1. 收集开机信息,在Windows环境下,使用adb命令。

adb shell //进入shell环境 echo 120 > /data/bootchart/start //向start文件中写入120 reboot //重启 adb shell //重启后再次进入shell环境 cd /data/bootchart/ ls //可以看到该目录下多了很多文件,那些文件就是我们需要的开机信息文件

这里写图片描述

2. 打包压缩开机信息

//在shell环境下,在/data/bootchart/目录下执行tar打包压缩 tar -czf bootchart.tgz proc_stat.log proc_ps.log proc_diskstats.log kernel_pacct //退出shell环境 exit //将打包好的压缩包拉取出来,并拷贝到我们的服务器上 adb pull /data/bootchart/bootchart.tgz D:\

这里写图片描述

3. 生成png图片

要生成png图片,需要用到bootchart工具,而这个工具的使用时需要linux环境的。如果你没有安装bootchart,就需要安装下。 安装命令如下:

sudo apt-get install bootchart

安装完后就可以使用bootchart命令了。在Linux服务器上执行

bootchart bootchart.tgz

命令结束后就生成对应的png图片。

这里写图片描述ok,大功告成。

总结

在这里总结下最重要的三步

echo 120 > /data/bootchart/start tar -czf bootchart.tgz header proc_stat.log proc_ps.log proc_diskstats.log kernel_pacct bootchart bootchart.tgz

通过这三步基本上可以获取到开机信息的图片了

猜你喜欢

转载自blog.csdn.net/u011006622/article/details/87624938