blob: c10327ef09c13bf8268d857a6ed07ad47b7a4efb [file] [log] [blame]
Igor Murashkin7ab5c4d2019-04-17 12:28:19 -07001#!/bin/bash
2#
3# Copyright 2019, 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
17DIR_IORAP_COMMON="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
18APP_STARTUP_DIR="$DIR/../app_startup/"
19source "$APP_STARTUP_DIR/lib/common"
20
21IORAPD_DATA_PATH="/data/misc/iorapd"
22
23iorapd_start() {
24 verbose_print 'iorapd_start'
25 adb shell start iorapd
26 sleep 1
27 # TODO: block until logcat prints successfully connecting
28}
29
30iorapd_stop() {
31 verbose_print 'iorapd_stop'
32 adb shell stop iorapd
33}
34
35iorapd_reset() {
36 iorapd_stop
37 iorapd_start
38}
39
40# Enable perfetto tracing.
41# Subsequent launches of an application will record a perfetto trace protobuf.
42iorapd_perfetto_enable() {
43 verbose_print 'enable perfetto'
44 adb shell setprop iorapd.perfetto.enable true
45 iorapd_reset # iorapd only reads this flag when initializing
46}
47
48# Enable perfetto tracing.
49# Subsequent launches of applications will no longer record perfetto trace protobufs.
50iorapd_perfetto_disable() {
51 verbose_print 'disable perfetto'
52 adb shell setprop iorapd.perfetto.enable false
53 iorapd_reset # iorapd only reads this flag when initializing
54}
55
56_iorapd_path_to_data_file() {
57 local package="$1"
58 local activity="$2"
59 local suffix="$3"
60
61 # Match logic of 'AppComponentName' in iorap::compiler C++ code.
62 echo "${IORAPD_DATA_PATH}/${package}%2F${activity}.${suffix}"
63}
64
65iorapd_perfetto_wait_for_app_trace() {
66 local package="$1"
67 local activity="$2"
68 local timeout="$3"
69 local timestamp="$4"
70
71 local remote_path="$(_iorapd_path_to_data_file "$package" "$activity" "perfetto_trace.pb")"
72
73 verbose_print "iorapd_perfetto_wait_for_app_trace on file '$remote_path'"
74
75 # see event_manager.cc
76 local pattern="Perfetto TraceBuffer saved to file: $remote_path"
77 logcat_wait_for_pattern "$timeout" "$timestamp" "$pattern"
78}
79
80# Purge all perfetto traces for a given application.
81iorapd_perfetto_purge_app_trace() {
82 local package="$1"
83 local activity="$2"
84
85 local remote_path="$(_iorapd_path_to_data_file "$package" "$activity" "perfetto_trace.pb")"
86
87 verbose_print 'iorapd-perfetto: purge app trace in ' "$remote_path"
88 adb shell "[[ -f '$remote_path' ]] && rm -f '$remote_path' || exit 0"
89}
90
Igor Murashkinab37e6e2019-05-13 16:31:25 -070091# Pull the remote perfetto trace file into a local file.
92iorapd_perfetto_pull_trace_file() {
93 local package="$1"
94 local activity="$2"
95 local output_file="$3" # local path
96
97 local compiled_path="$(_iorapd_path_to_data_file "$package" "$activity" "perfetto_trace.pb")"
98
99 if ! adb shell "[[ -f '$compiled_path' ]]"; then
100 echo "Error: Remote path '$compiled_path' invalid" >&2
101 return 1
102 fi
103 if ! mkdir -p "$(dirname "$output_file")"; then
104 echo "Error: Fail to make output directory for '$output_file'" >&2
105 return 1
106 fi
107 verbose_print adb pull "$compiled_path" "$output_file"
108 adb pull "$compiled_path" "$output_file"
109}
110
Igor Murashkin7ab5c4d2019-04-17 12:28:19 -0700111# Compile a perfetto trace for a given application.
112# This requires the app has run at least once with perfetto tracing enabled.
113iorapd_compiler_for_app_trace() {
114 local package="$1"
115 local activity="$2"
116 local inodes="$3" # local path
117
118 # remote path calculations
119 local input_path="$(_iorapd_path_to_data_file "$package" "$activity" "perfetto_trace.pb")"
120 local compiled_path="$(_iorapd_path_to_data_file "$package" "$activity" "compiled_trace.tmp.pb")"
121 local compiled_path_final="$(_iorapd_path_to_data_file "$package" "$activity" "compiled_trace.pb")"
122
123 if ! adb shell "[[ -f '$input_path' ]]"; then
124 echo "Error: Missing perfetto traces; nothing to compile. Expected: '$input_path'" >&2
125 return 1
126 fi
127
128 if ! [[ -f $inodes ]]; then
129 # We could compile using 'diskscan' but it's non-deterministic, so refuse instead.
130 echo "Error: Missing inodes textcache at '$inodes'; refusing to compile." >&2
131 return 1
132 fi
133
134 # inodes file needs to be on the device for iorap.cmd.compiler to access it
135 local remote_inodes=/data/local/tmp/prefetch/inodes.txt
136 adb shell "mkdir -p \"$(dirname "$remote_inodes")\"" || return 1
Igor Murashkinab37e6e2019-05-13 16:31:25 -0700137 verbose_print adb push "$inodes" "$remote_inodes"
Igor Murashkin7ab5c4d2019-04-17 12:28:19 -0700138 adb push "$inodes" "$remote_inodes"
139
140 verbose_print 'iorapd-compiler: compile app trace in ' "$input_path"
141 verbose_print adb shell "iorap.cmd.compiler '$input_path' --inode-textcache '$remote_inodes' --output-proto '$compiled_path'"
142 adb shell "iorap.cmd.compiler '$input_path' --inode-textcache '$remote_inodes' --output-proto '$compiled_path'"
143 retcode=$?
144
145 # Don't overwrite the true 'compiled_trace.pb' unless the compiler completed without error.
146 # TODO: The native compiler code should be handling its own transaction-safety.
147 if [[ $retcode -eq 0 ]]; then
148 adb shell "mv '$compiled_path' '$compiled_path_final'"
149 else
150 adb shell "[[ -f '$compiled_path' ]] && rm -f '$compiled_path'"
151 fi
152
153 # Clean up inodes file we just pushed.
Igor Murashkinab37e6e2019-05-13 16:31:25 -0700154# adb shell "[[ -f '$remote_inodes' ]] && rm -f '$remote_inodes'"
Igor Murashkin7ab5c4d2019-04-17 12:28:19 -0700155
156 return $retcode
157}
158
159# Pull the remote compiled trace file into a local file.
160iorapd_compiler_pull_trace_file() {
161 local package="$1"
162 local activity="$2"
163 local output_file="$3" # local path
164
165 local compiled_path="$(_iorapd_path_to_data_file "$package" "$activity" "compiled_trace.pb")"
166
167 if ! adb shell "[[ -f '$compiled_path' ]]"; then
168 echo "Error: Remote path '$compiled_path' invalid" >&2
169 return 1
170 fi
171 if ! mkdir -p "$(dirname "$output_file")"; then
172 echo "Error: Fail to make output directory for '$output_file'" >&2
173 return 1
174 fi
175 verbose_print adb pull "$compiled_path" "$output_file"
176 adb pull "$compiled_path" "$output_file"
177}
178
179# Install a compiled trace file.
180iorapd_compiler_install_trace_file() {
181 local package="$1"
182 local activity="$2"
183 local input_file="$3" # local path
184
185 # remote path calculations
186 local compiled_path="$(_iorapd_path_to_data_file "$package" "$activity" "compiled_trace.pb")"
187
188 if ! [[ -f $input_file ]]; then
189 echo "Error: File '$input_file' does not exist." >&2
190 return 1
191 fi
192
193 adb shell "mkdir -p \"$(dirname "$compiled_path")\"" || return 1
194
195 verbose_print adb push "$input_file" "$compiled_path"
196 adb push "$input_file" "$compiled_path"
197}
198
199iorapd_compiler_purge_trace_file() {
200 local package="$1"
201 local activity="$2"
202 local input_file="$3" # local path
203
204 local remote_path="$(_iorapd_path_to_data_file "$package" "$activity" "compiled_trace.pb")"
205
206 adb shell "[[ -f '$remote_path' ]] && rm -f '$remote_path' || exit 0"
207}
208
209# Blocks until the readahead for the requested package/activity has finished.
210# This assumes that the trace file was already installed, and also that
211# the application launched but not completed yet.
212iorapd_readahead_wait_until_finished() {
213 local package="$1"
214 local activity="$2"
215 local timestamp="$3"
216 local timeout="$4"
217
218 if [[ $# -lt 4 ]]; then
219 echo "FATAL: Expected 4 arguments (actual $# $@)" >&2
220 exit 1
221 fi
222
223 local remote_path="$(_iorapd_path_to_data_file "$package" "$activity" "compiled_trace.pb")"
224
225 # See 'read_ahead.cc' LOG(INFO).
226 local pattern="ReadAhead completed ($remote_path)"
227 logcat_wait_for_pattern "$timeout" "$timestamp" "$pattern"
228}