[Chapter 1 - knowledge] 2. Performance optimization - Bootchart analysis of Android 7.1 Bug

The author of the original text: a detailed account of the past and the future
Article link: link

Article catalog

What is Bootchart

Bootchart is an open source software tool for Linux startup process performance analysis

What is it in Android

Google has integrated Bootchart in Android system by default to record boot time
You can see where the boot time is to optimize the boot time

Android 5.1 has its own bootchart before, but it will not be compiled by default. You need to explicitly specify compilation

After 5.1, all are compiled into init by default

Google introduction


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.

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.


//This script is due to the need to package these data in the Linux environment and do visual processing output performance analysis chart. For this reason, Google has written a grab to us in the source / system/core/init / directory- bootchart.sh Script, which is specially used to process these data and do visual processing

Google translation in Chinese

You can use the following name to open Bootchart to collect boot performance data (mainly log files)

//Create a new start file in the data/bootchart / directory
adb shell 'touch /data/bootchart/start'

* Write adoption time timeout in start file=120s
* The time here can be customized. By viewing the source code, we can see that the maximum time cannot exceed 10*60 s
adb shell 'echo 120 > /data/bootchart/start'

//Create a new stop file in the data/bootchart / directory
adb shell 'touch /data/bootchart/stop'

//Write a 1 tag in the stop file to stop collecting data
adb shell 'echo 1 > /data/bootchart/stop'

After setting start, you can see that there should be corresponding collection files in the data/bootchart / directory immediately after restart

But at this time in 7.1, bugs appear. The machine is down all the time and cannot be started (there is a solution later)

Code catalog


Execution time


on post-fs-data

    # Start bootcharting as soon as possible after the data partition is
    # mounted to collect more data.
    mkdir /data/bootchart 0755 shell shell

At init.rc When the data partition is mounted, bootchart is executed_ init

So bootchart_init is bootchart.cpp The entry function in

bootchart.cpp The code in is easy to understand

Read the set time from data/bootchart/start, and execute the read record according to the default detection interval set in the code

Read the information of related files, such as proc / PID / CMD proc / PID / stat, and write


Stop if data/bootchart/stop = 1 is read
It's probably like this, but the read file still needs to be checked and analyzed with tools, so it's not concerned~~

Bug solution

After setting the start time, restart it and find it can't be started

After Baidu, Google

Some blogs say that system/core/init/bootchart.cpp In

stat.replace(open + 1, close - open - 1, full_name);

Just delete it

I've tried. I'm sure. I can turn it on

But think again, Google will not find this problem? Not at all

There is only one truth!

Then compared Google 7.08.1 or even 9.0 source code, opened a long 2 hours of carpet search

Finally, find the relevant repair Submission on Google's submission record




LOCAL_SANITIZE := signed-integer-overflow

Those with ladders can directly see the original submission


I don't know what this is, but I guess it's related to memory overflow


Then baidu searches for a wave of LOCAL_SANITIZE signed-integer-overflow

What is it?

Official explanation

Android's build system also uses UBSan's integer overflow checking feature. UBSan also supports unsigned integer overflow, which is not strictly undefined behavior, but is included in the eraser. In the build file, LOCAL_SANITIZE is set to signed integer overflow, unsigned integer overflow, or combination flag integer to enable signed integer overflow, unsigned integer overflow, integer divide by zero, shift base, and shift exponent to enable these behaviors. In the blueprint file, Misc_undefined is set to the desired flag to enable these behaviors. These UBSan targets, especially unsigned integer overflow, are widely used in mediaserver components to eliminate any potential integer overflow vulnerabilities

In Android, when undefined behavior occurs, the default is to abort the program. However, from October 2016, UBSan in Android will provide an optional runtime library, which will report more detailed error information, including the occurrence of undefined behavior types, files and source line information

At Android.mk In the file, you can enable the library in the following ways:

LOCAL_SANITIZE:=unsigned-integer-overflow signed-integer-overflow
LOCAL_SANITIZE_DIAG:=unsigned-integer-overflow signed-integer-overflow

(﹏ ﹏), after reading it, I'm still a bit confused, but I still know a little more knowledge, OK, after recording~

Tags: Android Google shell Linux

Posted on Tue, 16 Jun 2020 22:35:13 -0400 by pointsplat