1#!/bin/bash
2
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17readme() {
18    echo '
19Analyze boot-time
20e.g.
21ANDROID_BUILD_TOP="$PWD" \
22CONFIG_YMAL="$ANDROID_BUILD_TOP/system/extras/boottime_tools/bootanalyze/config.yaml" \
23    LOOPS=3 \
24    RESULTS_DIR="$PWD/bootAnalyzeResults" \
25    $ANDROID_BUILD_TOP/system/extras/boottime_tools/bootanalyze/bootanalyze.sh
26
27Flags:
28-a : Uses "adb reboot" (instead of "adb shell su root svc power reboot") command to reboot
29-b : If set grabs bootchart
30-w : If set grabs carwatchdog perf stats
31'
32    exit
33}
34
35
36if [[ -z $ANDROID_BUILD_TOP ]]; then
37    echo 'Error: you need to specify ANDROID_BUILD_TOP'
38    readme
39fi
40echo "ANDROID_BUILD_TOP=$ANDROID_BUILD_TOP"
41SCRIPT_DIR="$ANDROID_BUILD_TOP/system/extras/boottime_tools/bootanalyze"
42
43
44if [[ -z $CONFIG_YMAL ]]; then
45	CONFIG_YMAL="$SCRIPT_DIR/config.yaml"
46fi
47echo "CONFIG_YMAL=$CONFIG_YMAL"
48
49
50if [[ -z $RESULTS_DIR ]]; then
51	RESULTS_DIR="$PWD/bootAnalyzeResults"
52fi
53echo "RESULTS_DIR=$RESULTS_DIR"
54mkdir -p $RESULTS_DIR
55
56ADB_REBOOT_FLAG=""
57BOOTCHART_FLAG=""
58CARWATCHDOG_FLAG=""
59
60while getopts 'abw' OPTION; do
61  case "$OPTION" in
62    a)
63      ADB_REBOOT_FLAG="-a"
64      ;;
65    b)
66      BOOTCHART_FLAG="-b"
67      ;;
68    w)
69      CARWATCHDOG_FLAG="-W"
70      ;;
71    ?)
72      echo 'Error: Invalid flag set'
73      readme
74      ;;
75  esac
76done
77shift "$(($OPTIND -1))"
78
79
80adb shell 'touch /data/bootchart/enabled'
81
82if [[ -z $LOOPS ]]; then
83	LOOPS=1
84fi
85echo "Analyzing boot-time for LOOPS=$LOOPS"
86BOOTCHART_TGZ="/tmp/android-bootchart/bootchart.tgz"
87START=1
88
89SLEEP_SEC=20
90for (( l=$START; l<=$LOOPS; l++ )); do
91    echo "Loop: $l"
92    SECONDS=0
93    mkdir $RESULTS_DIR/$l
94    $SCRIPT_DIR/bootanalyze.py -c $CONFIG_YMAL -G 4M -r \
95        $ADB_REBOOT_FLAG $BOOTCHART_FLAG $CARWATCHDOG_FLAG \
96        -o "$RESULTS_DIR/$l" 1> "$RESULTS_DIR/$l/boot.txt"
97    if [[ $? -ne 0 ]]; then
98        echo "bootanalyze.py failed"
99        exit 1
100    fi
101    echo "$SECONDS sec."
102    if [ -f "$BOOTCHART_TGZ" ]; then
103        cp $BOOTCHART_TGZ "$RESULTS_DIR/$l/bootchart.tgz"
104    fi
105    echo "Sleep for $SLEEP_SEC sec."
106    sleep $SLEEP_SEC
107done
108
109echo
110echo "Complete $LOOPS"