blob: 9f00b5f82b2c04c5d377895c8e2f7abaea300c0e [file] [log] [blame]
Primiano Tuccib7cca202018-01-29 16:30:47 +00001#!/bin/bash
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15set -e
Joel Fernandes16e86102019-03-19 13:42:48 -040016CUR_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
17
18export PATH="$PATH:$CUR_DIR"
Primiano Tuccib7cca202018-01-29 16:30:47 +000019
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010020if [ "$TMPDIR" == "" ]; then
21 TMPDIR=/tmp
22fi
23
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010024function get_gn_value() {
Hector Dearmanc9c183a2018-02-08 12:01:12 +000025 local out=$1
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010026 local key=$2
27 gn args "$out" --list --short | sed -n -e "s/$key = \"\(.*\)\"/\1/p"
Hector Dearmanc067d412018-03-16 17:01:41 +000028}
29
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010030function is_monolithic() {
Hector Dearmanc067d412018-03-16 17:01:41 +000031 local out=$1
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010032 value=$(get_gn_value "$out" "monolithic_binaries")
33 test "$value" == "true"
Hector Dearmanc9c183a2018-02-08 12:01:12 +000034}
35
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010036function is_android() {
37 local out=$1
38 value=$(get_gn_value "$out" "target_os")
39 test "$value" == "android"
40}
41
42function is_gn_target_and_host_value_equal() {
43 local out=$1
44 local key=$2
45 local host
46 local target
47 host=$(get_gn_value "$out" "host_$key")
48 target=$(get_gn_value "$out" "target_$key")
49 [ -z "$target" ] && target="$host"
50 test "$target" == "$host"
51}
52
53function is_cross_compilation() {
54 local out=$1
55 if is_gn_target_and_host_value_equal "$out" cpu &&
56 is_gn_target_and_host_value_equal "$out" os; then
57 false
58 else
59 true
60 fi
61}
62
63function is_mac() {
Primiano Tucci676f0cc2018-12-03 20:03:26 +010064 ! test -d /proc
65 return $?
66}
67
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010068function tmux_ensure_bash() {
Hector Dearman2407cdd2019-06-04 13:21:34 +010069 if [[ $SHELL == *"fish" ]]; then
70 tmux send-keys "bash" Enter
71 fi
72}
73
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010074function reset_tracing() {
75 if is_android "$OUT"; then
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010076 adb shell 'echo 0 > /d/tracing/tracing_on'
Primiano Tucci676f0cc2018-12-03 20:03:26 +010077 elif ! is_mac; then
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010078 # shellcheck disable=SC2016
79 local script='
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010080 if [ ! -w /sys/kernel/debug ]; then
81 echo "debugfs not accessible, try sudo chown -R $USER /sys/kernel/debug"
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010082 sudo chown -R "$USER" /sys/kernel/debug
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010083 fi
84
85 echo 0 > /sys/kernel/debug/tracing/tracing_on
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010086 '
87
88 if is_cross_compilation "$OUT"; then
89 # shellcheck disable=SC2029
90 ssh "$TARGET" "sh -c '$script'"
91 else
92 sh -c "$script"
93 fi
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010094 fi
Hector Dearman0ff07c72018-03-15 09:54:46 +000095}
96
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +010097function adb_supports_push_sync() {
98 adb --help 2>&1 | grep 'push.*\[--sync\]' >/dev/null 2>&1
Hector Dearmanc067d412018-03-16 17:01:41 +000099}
100
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100101function push() {
102 if is_android "$OUT"; then
Hector Dearmanc067d412018-03-16 17:01:41 +0000103 local maybe_sync=''
104 if adb_supports_push_sync; then
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100105 maybe_sync='--sync'
Hector Dearmanc067d412018-03-16 17:01:41 +0000106 fi
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100107 echo adb push $maybe_sync "$1" "$DIR"
108 adb push $maybe_sync "$1" "$DIR"
109 elif is_cross_compilation "$OUT"; then
110 echo scp "$1" "$TARGET:$DIR"
111 scp "$1" "$TARGET:$DIR"
Hector Dearmanc067d412018-03-16 17:01:41 +0000112 else
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100113 echo cp "$1" "$DIR"
114 cp "$1" "$DIR"
Hector Dearman9b89d472018-02-28 12:07:21 +0000115 fi
Hector Dearmanc067d412018-03-16 17:01:41 +0000116}
117
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100118function pull() {
119 if is_android "$OUT"; then
120 echo adb pull "$DIR/$1" "$2"
121 adb pull "$DIR/$1" "$2"
122 elif is_cross_compilation "$OUT"; then
123 echo scp "$TARGET:$DIR/$1" "$2"
124 scp "$TARGET:$DIR/$1" "$2"
Hector Dearmanc067d412018-03-16 17:01:41 +0000125 else
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100126 echo mv "$DIR/$1" "$2"
127 mv "$DIR/$1" "$2"
Hector Dearmanc067d412018-03-16 17:01:41 +0000128 fi
Hector Dearman9b89d472018-02-28 12:07:21 +0000129}
130
Ryan Savitskibf67d302019-07-29 14:00:43 +0100131BACKGROUND=0
132SKIP_CONVERTERS=0
133TMUX_LAYOUT="even-vertical"
Florian Mayerb8433222018-05-15 11:29:18 +0100134
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100135while getopts "bl:nt:c:C:" o; do
Florian Mayerb8433222018-05-15 11:29:18 +0100136 case "$o" in
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100137 b) BACKGROUND=1 ;;
138 l) TMUX_LAYOUT=${OPTARG} ;;
139 n) SKIP_CONVERTERS=1 ;;
140 t) TARGET=${OPTARG} ;;
141 c) CONFIG=${OPTARG} ;;
142 C) OUT=${OPTARG} ;;
143 *)
144 echo "Invalid option $o"
145 exit
146 ;;
Florian Mayerb8433222018-05-15 11:29:18 +0100147 esac
148done
149
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100150# Allow out to be passed as argument
151shift $((OPTIND - 1))
152OUT="${OUT:-$1}"
153
154# Warn about invalid output directories
155if [ -z "$OUT" ]; then
156 echo "Usage: $0 [OPTION]... [OUTPUT]"
157 echo ""
158 echo "Options:"
159 echo " -b run in the background"
160 echo " -l tmux pane layout"
161 echo " -n skip post-trace convertors"
162 echo " -t TARGET SSH device target"
163 echo " -c CONFIG trace configuration file"
164 echo " -C OUTPUT output directory"
165 echo ""
166 echo "Environment variables:"
167 echo " TARGET SSH device target"
168 echo " CONFIG trace configuration file"
169 echo " OUTPUT output directory"
170 exit 1
171fi
172
173# Warn about invalid output directories
Primiano Tuccib7cca202018-01-29 16:30:47 +0000174if [ ! -f "$OUT/args.gn" ]; then
175 echo "OUT=$OUT doesn't look like an output directory."
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100176 echo "Please specify a directory by doing:"
177 echo " export OUT=out/xxx $0"
178 exit 1
179fi
180
181# If we are cross-compiling we need to know the SSH target
182if is_cross_compilation "$OUT" && ! ssh -q "$TARGET" exit; then
183 echo "TARGET=$TARGET doesn't look like a valid SSH target."
184 echo "Please specify a SSH cross-compilation target by doing:"
185 echo " export TARGET=<user>@<host> $0"
Primiano Tuccib7cca202018-01-29 16:30:47 +0000186 exit 1
187fi
188
Hector Dearmanc88a66d2018-02-01 14:07:14 +0000189# You can set the config to one of the files under test/configs e.g.
190# CONFIG=ftrace.cfg or to :test. Defaults to :test.
191CONFIG="${CONFIG:-:test}"
192
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100193if is_android "$OUT"; then
Hector Dearmanc067d412018-03-16 17:01:41 +0000194 DIR=/data/local/tmp
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100195elif is_cross_compilation "$OUT"; then
196 DIR=$(ssh "$TARGET" mktemp -d $TMPDIR/perfetto.XXXXXX)
Primiano Tucci676f0cc2018-12-03 20:03:26 +0100197elif is_mac; then
198 DIR=$(mktemp -d $TMPDIR/perfetto.XXXXXX)
Hector Dearmanc067d412018-03-16 17:01:41 +0000199else
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100200 DIR=$(mktemp -p $TMPDIR -d perfetto.XXXXXX)
Hector Dearmanc067d412018-03-16 17:01:41 +0000201fi
202
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100203tools/ninja -C "$OUT" traced traced_probes perfetto trace_to_text test/configs
Primiano Tuccib7cca202018-01-29 16:30:47 +0000204
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100205push "$OUT/traced"
206push "$OUT/traced_probes"
207push "$OUT/perfetto"
Hector Dearman0ff07c72018-03-15 09:54:46 +0000208reset_tracing
Primiano Tuccib7cca202018-01-29 16:30:47 +0000209
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100210if is_android "$OUT"; then
Primiano Tucci301175d2019-01-11 10:10:21 +0000211 PREFIX="PERFETTO_CONSUMER_SOCK_NAME=@perfetto_test_consumer PERFETTO_PRODUCER_SOCK_NAME=@perfetto_test_producer"
Florian Mayerb0f00712018-04-04 16:35:45 +0100212else
213 PREFIX=""
214fi
215
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100216if ! is_monolithic "$OUT"; then
Florian Mayerb0f00712018-04-04 16:35:45 +0100217 PREFIX="$PREFIX LD_LIBRARY_PATH=$DIR"
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100218 push "$OUT/libperfetto.so"
Hector Dearmanc9c183a2018-02-08 12:01:12 +0000219fi
220
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100221CONFIG_DEVICE_PATH="$CONFIG"
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000222CMD_OPTS=""
223if [[ "$CONFIG" == *.protobuf ]]; then
Hector Dearman696ff772019-04-23 18:38:53 +0100224 CONFIG_DEVICE_PATH="$CONFIG"
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100225 CONFIG_PATH=$OUT/$CONFIG
Hector Dearmanc88a66d2018-02-01 14:07:14 +0000226 if [[ ! -f $CONFIG_PATH ]]; then
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100227 echo "Config \"$CONFIG_PATH\" not known."
Hector Dearmanc88a66d2018-02-01 14:07:14 +0000228 exit 1
229 fi
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100230 push "$CONFIG_PATH"
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000231elif [[ "$CONFIG" != ":test" ]]; then
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100232 CONFIG_DEVICE_PATH="$(basename "$CONFIG")"
Ioannis Ilkos482264e2019-01-15 10:46:11 +0000233 CONFIG_PATH=test/configs/$CONFIG
234 # Check if this is a valid absolute path
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000235 if [[ ! -f $CONFIG_PATH ]]; then
Ioannis Ilkos482264e2019-01-15 10:46:11 +0000236 CONFIG_PATH=$CONFIG
237 if [[ ! -f $CONFIG_PATH ]]; then
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100238 echo "Config \"$CONFIG\" not known."
Ioannis Ilkos482264e2019-01-15 10:46:11 +0000239 exit 1
240 fi
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000241 fi
242 CMD_OPTS="--txt $CMD_OPTS"
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100243 push "$CONFIG_PATH"
Hector Dearmanc88a66d2018-02-01 14:07:14 +0000244fi
245
Florian Mayerb8433222018-05-15 11:29:18 +0100246POSTFIX=""
247
Ryan Savitskibf67d302019-07-29 14:00:43 +0100248if [[ BACKGROUND -eq 1 ]]; then
Florian Mayerb8433222018-05-15 11:29:18 +0100249 PREFIX="$PREFIX nohup"
250 POSTFIX=" &> /dev/null &"
251fi
252
Primiano Tuccib7cca202018-01-29 16:30:47 +0000253if tmux has-session -t demo; then
254 tmux kill-session -t demo
255fi
256tmux -2 new-session -d -s demo
257
258if tmux -V | awk '{split($2, ver, "."); if (ver[1] < 2) exit 1 ; else if (ver[1] == 2 && ver[2] < 1) exit 1 }'; then
259 tmux set-option -g mouse on
260else
261 tmux set-option -g mode-mouse on
262 tmux set-option -g mouse-resize-pane on
263 tmux set-option -g mouse-select-pane on
264 tmux set-option -g mouse-select-window on
265fi
266
267tmux split-window -v
268tmux split-window -v
269
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100270tmux select-layout "${TMUX_LAYOUT}"
Primiano Tuccib7cca202018-01-29 16:30:47 +0000271
272tmux select-pane -t 0
273tmux send-keys "clear" C-m
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100274if is_android "$OUT"; then
Hector Dearmanc067d412018-03-16 17:01:41 +0000275 tmux send-keys "adb shell" C-m
276fi
Primiano Tuccib7cca202018-01-29 16:30:47 +0000277
278tmux select-pane -t 1
279tmux send-keys "clear" C-m
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100280if is_android "$OUT"; then
Hector Dearmanc067d412018-03-16 17:01:41 +0000281 tmux send-keys "adb shell" C-m
282fi
Primiano Tuccib7cca202018-01-29 16:30:47 +0000283
284tmux select-pane -t 2
285tmux send-keys "clear" C-m
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100286if is_android "$OUT"; then
Hector Dearmanc067d412018-03-16 17:01:41 +0000287 tmux send-keys "adb shell" C-m
288fi
Primiano Tuccib7cca202018-01-29 16:30:47 +0000289
Hector Dearmanc88a66d2018-02-01 14:07:14 +0000290sleep 2
Primiano Tuccib7cca202018-01-29 16:30:47 +0000291
292tmux select-pane -t 1
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100293if is_cross_compilation "$OUT"; then
294 tmux send-keys "ssh $TARGET" Enter
295fi
Hector Dearman2407cdd2019-06-04 13:21:34 +0100296tmux_ensure_bash
Ryan Savitskibf67d302019-07-29 14:00:43 +0100297tmux send-keys "PS1='[traced]$ '" Enter
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100298tmux send-keys "cd $DIR" Enter
Ryan Savitskibf67d302019-07-29 14:00:43 +0100299tmux send-keys "$PREFIX ./traced 2>&1 | tee traced.log $POSTFIX" Enter
Primiano Tuccib7cca202018-01-29 16:30:47 +0000300
Primiano Tuccib7cca202018-01-29 16:30:47 +0000301tmux select-pane -t 0
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100302if is_cross_compilation "$OUT"; then
303 tmux send-keys "ssh $TARGET" Enter
304fi
Hector Dearman2407cdd2019-06-04 13:21:34 +0100305tmux_ensure_bash
Ryan Savitskibf67d302019-07-29 14:00:43 +0100306tmux send-keys "PS1='[traced_probes]$ '" Enter
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100307tmux send-keys "cd $DIR" Enter
Ryan Savitskibf67d302019-07-29 14:00:43 +0100308tmux send-keys "$PREFIX ./traced_probes 2>&1 | tee traced_probes.log $POSTFIX" Enter
Primiano Tuccib7cca202018-01-29 16:30:47 +0000309
310tmux select-pane -t 2
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100311if is_cross_compilation "$OUT"; then
312 tmux send-keys "ssh $TARGET" Enter
313fi
Hector Dearman2407cdd2019-06-04 13:21:34 +0100314tmux_ensure_bash
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100315tmux send-keys "PS1='[consumer]$ '" Enter
316tmux send-keys "cd $DIR" Enter
Ryan Savitskibf67d302019-07-29 14:00:43 +0100317tmux send-keys "$PREFIX ./perfetto $CMD_OPTS -c $CONFIG_DEVICE_PATH -o trace 2>&1 | tee perfetto.log $POSTFIX"
Primiano Tuccib7cca202018-01-29 16:30:47 +0000318
319# Select consumer pane.
320tmux select-pane -t 2
321
322tmux -2 attach-session -t demo
Ryan Savitskibf67d302019-07-29 14:00:43 +0100323if [[ BACKGROUND -eq 1 ]]; then
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100324 exit 0
Florian Mayerb8433222018-05-15 11:29:18 +0100325fi
Hector Dearmana7c04f82018-03-29 11:31:24 +0100326
Hector Dearman3c4e5c22018-03-29 11:31:55 +0100327reset_tracing
Hector Dearmana7c04f82018-03-29 11:31:24 +0100328
329TRACE=$HOME/Downloads/trace
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100330echo -e "\n\x1b[32mPulling trace into $TRACE.protobuf\x1b[0m"
331pull trace "$TRACE.protobuf"
Ryan Savitskibf67d302019-07-29 14:00:43 +0100332
333if [[ SKIP_CONVERTERS -eq 0 ]]; then
334 echo -e "\n\x1b[32mPulling trace into $TRACE.pbtext\x1b[0m"
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100335 "$OUT/trace_to_text" text <"$TRACE.protobuf" >"$TRACE.pbtext"
Ryan Savitskibf67d302019-07-29 14:00:43 +0100336 echo -e "\n\x1b[32mPulling trace into $TRACE.json\x1b[0m"
Matthew Clarkson7a2a3bd2019-09-04 12:27:17 +0100337 "$OUT/trace_to_text" systrace <"$TRACE.protobuf" >"$TRACE.json"
Ryan Savitskibf67d302019-07-29 14:00:43 +0100338 # Keep this last so it can fail.
339fi