xref: /aosp_15_r20/test/dittosuite/abench.sh (revision 6fa2df46f119dce7527f5beb2814eca0e6f886ac)
1#!/bin/bash
2#
3# Copyright (C) 2024 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
17set -e
18
19unset TRACE_FILE
20unset QUERY_FILE
21unset WORKLOAD
22unset SET_ROOT
23unset PERFETTO_CONFIG_FILE
24unset PERFETTO_PID
25unset SERIAL
26
27
28function help {
29  echo "Usage: $0 [OPTION]..."
30  echo "Options: "
31  echo "  -h, --help          Print this message"
32  echo "  -r, --root          Switch the target device to root"
33  echo "  -c, --config PERFETTO_CONFIG Path to file containing the Perfetto"
34  echo "                      configuration"
35  echo "  -q, --query QUERY   Path to file containing the SQL query to run on"
36  echo "                      the Perfetto trace with trace_processor"
37  echo "  -t, --trace TRACE   Path to which the Perfetto trace will be written to"
38  echo "  -w, --workload WORKLOAD Benchmark to run Dittobench with"
39  echo "  -s, --serial SERIAL Serial address for ADB"
40}
41
42
43while [ "$#" -gt "0" ]; do
44  case $1 in
45    -w|--workload)
46      WORKLOAD="$2"
47      shift
48      shift
49      ;;
50    -q|--query)
51      QUERY_FILE="$2"
52      shift
53      shift
54      ;;
55    -t|--trace)
56      TRACE_FILE="$2"
57      shift
58      shift
59      ;;
60    -c|--config)
61      PERFETTO_CONFIG_FILE="$2"
62      shift
63      shift
64      ;;
65    -s|--serial)
66      SERIAL="-s $2"
67      shift
68      shift
69      ;;
70    -r|--root)
71      SET_ROOT=1
72      shift
73      ;;
74    -h|--help)
75      help
76      exit 0
77      shift
78      ;;
79    -*|--*)
80      echo "Unknown option $1"
81      help
82      exit 1
83      ;;
84  esac
85done
86
87
88if [ -z "${ANDROID_HOST_OUT}" ] ; then
89  echo "This script requires an Android environment. It needs to be run from an"
90  echo "Android repository after the initialization scripts:"
91  echo "$ source build/envsetup.sh"
92  echo "$ lunch aosp_cf_x86_64_phone-trunk_staging-userdebug"
93  exit 1
94fi
95
96
97# If the user specified a WORKLOAD, make sure that Dittobench is built, better
98# before enabling tracing.
99if [ ! -z "${WORKLOAD}" ] ; then
100  if [ ! -z "${SET_ROOT}" ] ; then
101    adb ${SERIAL} shell whoami | grep root || adb ${SERIAL} root
102    sleep 2
103  fi
104
105  m dittobench
106  adb ${SERIAL} push ${ANDROID_PRODUCT_OUT}/system/bin/dittobench /data/local/tmp/
107fi
108
109
110if [ ! -z "${PERFETTO_CONFIG_FILE}" ] ; then
111  if [ -z "${TRACE_FILE}" ] ; then
112    TRACE_FILE=$(mktemp)
113    echo "Using temporary trace file path: \"${TRACE_FILE}\""
114  fi
115  ${ANDROID_BUILD_TOP}/external/perfetto/tools/record_android_trace ${SERIAL} --no-open \
116    -c ${PERFETTO_CONFIG_FILE} \
117    -o ${TRACE_FILE} &
118  PERFETTO_PID=$!
119
120  echo "Perfetto started (${PERFETTO_PID}), cooling down..."
121  sleep 5
122fi
123
124
125if [ ! -z "${WORKLOAD}" ] ; then
126  adb ${SERIAL} shell /data/local/tmp/dittobench -w ${WORKLOAD} -f csv | column -t -s ","
127  echo "Cooldown..."
128  sleep 5
129fi
130
131
132# If there is an instance of record_android_trace, kill it gracefully, so that
133# the trace file is correctly pulled from the device.
134if [ ! -z "${PERFETTO_PID}" ] ; then
135  kill -s 15 $PERFETTO_PID # SIGTERM
136  wait $PERFETTO_PID
137fi
138
139
140if [ ! -z "${QUERY_FILE}" ] ; then
141  if [ -z "${TRACE_FILE}" ] ; then
142    echo "A TRACE file needs to be specified to run a query, unless Perfetto is"
143    echo "run as a result of setting the PERFETTO_CONFIG parameter"
144    exit 1
145  fi
146  [ ! which trace_processor_shell ] && m trace_processor_shell
147  trace_processor_shell -q ${QUERY_FILE} ${TRACE_FILE} | column -t -s ','
148fi
149
150