Home | History | Annotate | Download | only in tools
      1 #!/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.
     15 set -e
     16 CUR_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
     17 
     18 export PATH="$PATH:$CUR_DIR"
     19 
     20 if [ "$TMPDIR" == "" ]; then
     21   TMPDIR=/tmp
     22 fi
     23 
     24 function is_monolithic {
     25   local out=$1
     26   gn args $out --list --short | grep 'monolithic_binaries = true' 2>&1 >/dev/null
     27   return $?
     28 }
     29 
     30 function is_android {
     31   local out=$1
     32   gn args $out --list --short | grep 'target_os = "android"' 2>&1 >/dev/null
     33   return $?
     34 }
     35 
     36 function is_mac {
     37   ! test -d /proc
     38   return $?
     39 }
     40 
     41 function reset_tracing {
     42   if is_android $OUT; then
     43     adb shell 'echo 0 > /d/tracing/tracing_on'
     44   elif ! is_mac; then
     45     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
     52 }
     53 
     54 function adb_supports_push_sync {
     55   adb --help 2>&1 | grep 'push.*\[--sync\]' 2>&1 >/dev/null
     56 }
     57 
     58 function push {
     59   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
     69   fi
     70 }
     71 
     72 function pull {
     73   if is_android $OUT; then
     74     echo adb pull $DIR/$1 $2
     75     adb pull $DIR/$1 $2
     76   else
     77     echo mv $DIR/$1 $2
     78     mv $DIR/$1 $2
     79   fi
     80 }
     81 
     82 background=0
     83 
     84 while getopts b o; do
     85   case "$o" in
     86     b) background=1;;
     87     *) echo "Invalid option $o"; exit;;
     88   esac
     89 done
     90 
     91 # If not set guess the OUT dir using the latest directory.
     92 if [ ! -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
     96 fi
     97 
     98 # 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.
    100 CONFIG="${CONFIG:-:test}"
    101 
    102 if is_android $OUT ; then
    103   DIR=/data/local/tmp
    104 elif is_mac; then
    105   DIR=$(mktemp -d $TMPDIR/perfetto.XXXXXX)
    106 else
    107   DIR=$(mktemp -p $TMPDIR -d perfetto.XXXXXX)
    108 fi
    109 
    110 tools/ninja -C $OUT traced traced_probes perfetto trace_to_text test/configs
    111 
    112 push $OUT/traced
    113 push $OUT/traced_probes
    114 push $OUT/perfetto
    115 reset_tracing
    116 
    117 if is_android $OUT; then
    118   PREFIX="PERFETTO_CONSUMER_SOCK_NAME=@perfetto_test_consumer PERFETTO_PRODUCER_SOCK_NAME=@perfetto_test_producer"
    119 else
    120   PREFIX=""
    121 fi
    122 
    123 if ! is_monolithic $OUT; then
    124   PREFIX="$PREFIX LD_LIBRARY_PATH=$DIR"
    125   push $OUT/libperfetto.so
    126 fi
    127 
    128 CONFIG_DEVICE_PATH="$CONFIG"
    129 CMD_OPTS=""
    130 if [[ "$CONFIG" == *.protobuf ]]; then
    131   CONFIG_DEVICE_PATH="$CONFIG"
    132   CONFIG_PATH=$OUT/$CONFIG;
    133   if [[ ! -f $CONFIG_PATH ]]; then
    134     echo 'Config "'$CONFIG_PATH'" not known.'
    135     exit 1
    136   fi
    137   push $CONFIG_PATH
    138 elif [[ "$CONFIG" != ":test" ]]; then
    139   CONFIG_DEVICE_PATH="$(basename $CONFIG)"
    140   CONFIG_PATH=test/configs/$CONFIG
    141   # Check if this is a valid absolute path
    142   if [[ ! -f $CONFIG_PATH ]]; then
    143     CONFIG_PATH=$CONFIG
    144     if [[ ! -f $CONFIG_PATH ]]; then
    145       echo 'Config "'$CONFIG'" not known.'
    146       exit 1
    147     fi
    148   fi
    149   CMD_OPTS="--txt $CMD_OPTS"
    150   push $CONFIG_PATH
    151 fi
    152 
    153 POSTFIX=""
    154 
    155 if [[ background -eq 1 ]]; then
    156   PREFIX="$PREFIX nohup"
    157   POSTFIX=" &> /dev/null &"
    158 fi
    159 
    160 if tmux has-session -t demo; then
    161   tmux kill-session -t demo
    162 fi
    163 tmux -2 new-session -d -s demo
    164 
    165 if 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
    167 else
    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
    172 fi
    173 
    174 tmux split-window -v
    175 tmux split-window -v
    176 
    177 tmux select-layout even-vertical
    178 
    179 tmux select-pane -t 0
    180 tmux send-keys "clear" C-m
    181 if is_android $OUT; then
    182   tmux send-keys "adb shell" C-m
    183 fi
    184 
    185 tmux select-pane -t 1
    186 tmux send-keys "clear" C-m
    187 if is_android $OUT; then
    188   tmux send-keys "adb shell" C-m
    189 fi
    190 
    191 tmux select-pane -t 2
    192 tmux send-keys "clear" C-m
    193 if is_android $OUT; then
    194   tmux send-keys "adb shell" C-m
    195 fi
    196 
    197 sleep 2
    198 
    199 tmux select-pane -t 1
    200 tmux send-keys "PS1='[traced_probes]$ '" Enter
    201 tmux send-keys "cd $DIR" Enter
    202 tmux send-keys "$PREFIX ./traced $POSTFIX" Enter
    203 
    204 tmux select-pane -t 0
    205 tmux send-keys "PS1='[traced]$ '" Enter
    206 tmux send-keys "cd $DIR" Enter
    207 tmux send-keys "$PREFIX PERFETTO_METATRACE_FILE=mtrace ./traced_probes $POSTFIX" Enter
    208 
    209 tmux select-pane -t 2
    210 tmux send-keys "PS1='[consumer]$ '" Enter
    211 tmux send-keys "cd $DIR" Enter
    212 tmux send-keys "$PREFIX ./perfetto $CMD_OPTS -c $CONFIG_DEVICE_PATH -o trace $POSTFIX"
    213 
    214 # Select consumer pane.
    215 tmux select-pane -t 2
    216 
    217 tmux -2 attach-session -t demo
    218 if [[ background -eq 1 ]]; then
    219   exit 0;
    220 fi
    221 
    222 reset_tracing
    223 
    224 TRACE=$HOME/Downloads/trace
    225 pull trace /tmp/trace.protobuf
    226 echo -e "\n\x1b[32mPulling trace into $TRACE.pbtext\x1b[0m"
    227 $OUT/trace_to_text text < /tmp/trace.protobuf > $TRACE.pbtext
    228 echo -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.
    231 pull mtrace /tmp/mtrace.json
    232 # Add [ to beginning of file and replace trailing , with ] to turn into valid
    233 # JSON array.
    234 sed -i -e '$ s/.$/]/' /tmp/mtrace.json
    235 sed -i -e '1s/^/[/' /tmp/mtrace.json
    236