blob: 4f626b747fec7d776b305cced4dc5fd7a77c6aed [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
Hector Dearman9b89d472018-02-28 12:07:21 +000024function is_monolithic {
Hector Dearmanc9c183a2018-02-08 12:01:12 +000025 local out=$1
Hector Dearmanc067d412018-03-16 17:01:41 +000026 gn args $out --list --short | grep 'monolithic_binaries = true' 2>&1 >/dev/null
27 return $?
28}
29
30function is_android {
31 local out=$1
32 gn args $out --list --short | grep 'target_os = "android"' 2>&1 >/dev/null
Hector Dearmanc9c183a2018-02-08 12:01:12 +000033 return $?
34}
35
Primiano Tucci676f0cc2018-12-03 20:03:26 +010036function is_mac {
37 ! test -d /proc
38 return $?
39}
40
Hector Dearman0ff07c72018-03-15 09:54:46 +000041function reset_tracing {
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010042 if is_android $OUT; then
43 adb shell 'echo 0 > /d/tracing/tracing_on'
Primiano Tucci676f0cc2018-12-03 20:03:26 +010044 elif ! is_mac; then
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010045 if [ ! -w /sys/kernel/debug ]; then
46 echo "debugfs not accessible, try sudo chown -R $USER /sys/kernel/debug"
47 sudo chown -R $USER /sys/kernel/debug
48 fi
49
50 echo 0 > /sys/kernel/debug/tracing/tracing_on
51 fi
Hector Dearman0ff07c72018-03-15 09:54:46 +000052}
53
Hector Dearman9b89d472018-02-28 12:07:21 +000054function adb_supports_push_sync {
Primiano Tuccifd8240d2018-08-01 09:34:54 +010055 adb --help 2>&1 | grep 'push.*\[--sync\]' 2>&1 >/dev/null
Hector Dearmanc067d412018-03-16 17:01:41 +000056}
57
Hector Dearman9b89d472018-02-28 12:07:21 +000058function push {
Hector Dearmanc067d412018-03-16 17:01:41 +000059 if is_android $OUT; then
60 local maybe_sync=''
61 if adb_supports_push_sync; then
62 maybe_sync='--sync '
63 fi
64 echo adb push $maybe_sync $1 $DIR
65 adb push $maybe_sync $1 $DIR
66 else
67 echo cp $1 $DIR
68 cp $1 $DIR
Hector Dearman9b89d472018-02-28 12:07:21 +000069 fi
Hector Dearmanc067d412018-03-16 17:01:41 +000070}
71
72function pull {
73 if is_android $OUT; then
74 echo adb pull $DIR/$1 $2
75 adb pull $DIR/$1 $2
76 else
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010077 echo mv $DIR/$1 $2
78 mv $DIR/$1 $2
Hector Dearmanc067d412018-03-16 17:01:41 +000079 fi
Hector Dearman9b89d472018-02-28 12:07:21 +000080}
81
Florian Mayerb8433222018-05-15 11:29:18 +010082background=0
83
84while getopts b o; do
85 case "$o" in
86 b) background=1;;
87 *) echo "Invalid option $o"; exit;;
88 esac
89done
90
Primiano Tuccib7cca202018-01-29 16:30:47 +000091# If not set guess the OUT dir using the latest directory.
92if [ ! -f "$OUT/args.gn" ]; then
93 echo "OUT=$OUT doesn't look like an output directory."
94 echo "Please specify a directory by doing: export OUT=out/xxx"
95 exit 1
96fi
97
Hector Dearmanc88a66d2018-02-01 14:07:14 +000098# You can set the config to one of the files under test/configs e.g.
99# CONFIG=ftrace.cfg or to :test. Defaults to :test.
100CONFIG="${CONFIG:-:test}"
101
Hector Dearmanc067d412018-03-16 17:01:41 +0000102if is_android $OUT ; then
103 DIR=/data/local/tmp
Primiano Tucci676f0cc2018-12-03 20:03:26 +0100104elif is_mac; then
105 DIR=$(mktemp -d $TMPDIR/perfetto.XXXXXX)
Hector Dearmanc067d412018-03-16 17:01:41 +0000106else
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100107 DIR=$(mktemp -p $TMPDIR -d perfetto.XXXXXX)
Hector Dearmanc067d412018-03-16 17:01:41 +0000108fi
109
Lalit Magantic27975c2018-04-10 21:42:18 +0100110tools/ninja -C $OUT traced traced_probes perfetto trace_to_text test/configs
Primiano Tuccib7cca202018-01-29 16:30:47 +0000111
Hector Dearmanc067d412018-03-16 17:01:41 +0000112push $OUT/traced
113push $OUT/traced_probes
114push $OUT/perfetto
Hector Dearman0ff07c72018-03-15 09:54:46 +0000115reset_tracing
Primiano Tuccib7cca202018-01-29 16:30:47 +0000116
Florian Mayerb0f00712018-04-04 16:35:45 +0100117if is_android $OUT; then
Primiano Tucci301175d2019-01-11 10:10:21 +0000118 PREFIX="PERFETTO_CONSUMER_SOCK_NAME=@perfetto_test_consumer PERFETTO_PRODUCER_SOCK_NAME=@perfetto_test_producer"
Florian Mayerb0f00712018-04-04 16:35:45 +0100119else
120 PREFIX=""
121fi
122
Hector Dearmanc067d412018-03-16 17:01:41 +0000123if ! is_monolithic $OUT; then
Florian Mayerb0f00712018-04-04 16:35:45 +0100124 PREFIX="$PREFIX LD_LIBRARY_PATH=$DIR"
Primiano Tuccibdb2a592018-10-11 15:59:29 +0100125 push $OUT/libperfetto.so
Hector Dearmanc9c183a2018-02-08 12:01:12 +0000126fi
127
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100128CONFIG_DEVICE_PATH="$CONFIG"
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000129CMD_OPTS=""
130if [[ "$CONFIG" == *.protobuf ]]; then
Hector Dearman696ff772019-04-23 18:38:53 +0100131 CONFIG_DEVICE_PATH="$CONFIG"
132 CONFIG_PATH=$OUT/$CONFIG;
Hector Dearmanc88a66d2018-02-01 14:07:14 +0000133 if [[ ! -f $CONFIG_PATH ]]; then
134 echo 'Config "'$CONFIG_PATH'" not known.'
135 exit 1
136 fi
Hector Dearmanc067d412018-03-16 17:01:41 +0000137 push $CONFIG_PATH
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000138elif [[ "$CONFIG" != ":test" ]]; then
Ioannis Ilkos482264e2019-01-15 10:46:11 +0000139 CONFIG_DEVICE_PATH="$(basename $CONFIG)"
140 CONFIG_PATH=test/configs/$CONFIG
141 # Check if this is a valid absolute path
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000142 if [[ ! -f $CONFIG_PATH ]]; then
Ioannis Ilkos482264e2019-01-15 10:46:11 +0000143 CONFIG_PATH=$CONFIG
144 if [[ ! -f $CONFIG_PATH ]]; then
145 echo 'Config "'$CONFIG'" not known.'
146 exit 1
147 fi
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000148 fi
149 CMD_OPTS="--txt $CMD_OPTS"
150 push $CONFIG_PATH
Hector Dearmanc88a66d2018-02-01 14:07:14 +0000151fi
152
Florian Mayerb8433222018-05-15 11:29:18 +0100153POSTFIX=""
154
155if [[ background -eq 1 ]]; then
156 PREFIX="$PREFIX nohup"
157 POSTFIX=" &> /dev/null &"
158fi
159
Primiano Tuccib7cca202018-01-29 16:30:47 +0000160if tmux has-session -t demo; then
161 tmux kill-session -t demo
162fi
163tmux -2 new-session -d -s demo
164
165if tmux -V | awk '{split($2, ver, "."); if (ver[1] < 2) exit 1 ; else if (ver[1] == 2 && ver[2] < 1) exit 1 }'; then
166 tmux set-option -g mouse on
167else
168 tmux set-option -g mode-mouse on
169 tmux set-option -g mouse-resize-pane on
170 tmux set-option -g mouse-select-pane on
171 tmux set-option -g mouse-select-window on
172fi
173
174tmux split-window -v
175tmux split-window -v
176
177tmux select-layout even-vertical
178
179tmux select-pane -t 0
180tmux send-keys "clear" C-m
Hector Dearmanc067d412018-03-16 17:01:41 +0000181if is_android $OUT; then
182 tmux send-keys "adb shell" C-m
183fi
Primiano Tuccib7cca202018-01-29 16:30:47 +0000184
185tmux select-pane -t 1
186tmux send-keys "clear" C-m
Hector Dearmanc067d412018-03-16 17:01:41 +0000187if is_android $OUT; then
188 tmux send-keys "adb shell" C-m
189fi
Primiano Tuccib7cca202018-01-29 16:30:47 +0000190
191tmux select-pane -t 2
192tmux send-keys "clear" C-m
Hector Dearmanc067d412018-03-16 17:01:41 +0000193if is_android $OUT; then
194 tmux send-keys "adb shell" C-m
195fi
Primiano Tuccib7cca202018-01-29 16:30:47 +0000196
Hector Dearmanc88a66d2018-02-01 14:07:14 +0000197sleep 2
Primiano Tuccib7cca202018-01-29 16:30:47 +0000198
199tmux select-pane -t 1
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100200tmux send-keys "PS1='[traced_probes]$ '" Enter
201tmux send-keys "cd $DIR" Enter
202tmux send-keys "$PREFIX ./traced $POSTFIX" Enter
Primiano Tuccib7cca202018-01-29 16:30:47 +0000203
Primiano Tuccib7cca202018-01-29 16:30:47 +0000204tmux select-pane -t 0
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100205tmux send-keys "PS1='[traced]$ '" Enter
206tmux send-keys "cd $DIR" Enter
207tmux send-keys "$PREFIX PERFETTO_METATRACE_FILE=mtrace ./traced_probes $POSTFIX" Enter
Primiano Tuccib7cca202018-01-29 16:30:47 +0000208
209tmux select-pane -t 2
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100210tmux send-keys "PS1='[consumer]$ '" Enter
211tmux send-keys "cd $DIR" Enter
212tmux send-keys "$PREFIX ./perfetto $CMD_OPTS -c $CONFIG_DEVICE_PATH -o trace $POSTFIX"
Primiano Tuccib7cca202018-01-29 16:30:47 +0000213
214# Select consumer pane.
215tmux select-pane -t 2
216
217tmux -2 attach-session -t demo
Florian Mayerb8433222018-05-15 11:29:18 +0100218if [[ background -eq 1 ]]; then
219 exit 0;
220fi
Hector Dearmana7c04f82018-03-29 11:31:24 +0100221
Hector Dearman3c4e5c22018-03-29 11:31:55 +0100222reset_tracing
Hector Dearmana7c04f82018-03-29 11:31:24 +0100223
224TRACE=$HOME/Downloads/trace
225pull trace /tmp/trace.protobuf
Florian Mayerb8433222018-05-15 11:29:18 +0100226echo -e "\n\x1b[32mPulling trace into $TRACE.pbtext\x1b[0m"
227$OUT/trace_to_text text < /tmp/trace.protobuf > $TRACE.pbtext
228echo -e "\n\x1b[32mPulling trace into $TRACE.json\x1b[0m"
229$OUT/trace_to_text systrace < /tmp/trace.protobuf > $TRACE.json
230# Keep this last so it can fail.
Florian Mayer236ba472018-05-09 15:47:53 +0100231pull mtrace /tmp/mtrace.json
232# Add [ to beginning of file and replace trailing , with ] to turn into valid
233# JSON array.
Primiano Tucci89d1ad22018-10-31 21:42:36 -0700234sed -i -e '$ s/.$/]/' /tmp/mtrace.json
235sed -i -e '1s/^/[/' /tmp/mtrace.json