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 
     17 if [ "$TMPDIR" == "" ]; then
     18   TMPDIR=/tmp
     19 fi
     20 
     21 function is_monolithic {
     22   local out=$1
     23   gn args $out --list --short | grep 'monolithic_binaries = true' 2>&1 >/dev/null
     24   return $?
     25 }
     26 
     27 function is_android {
     28   local out=$1
     29   gn args $out --list --short | grep 'target_os = "android"' 2>&1 >/dev/null
     30   return $?
     31 }
     32 
     33 function reset_tracing {
     34   if is_android $OUT; then
     35     adb shell 'echo 0 > /d/tracing/tracing_on'
     36   else
     37     if [ ! -w /sys/kernel/debug ]; then
     38       echo "debugfs not accessible, try sudo chown -R $USER /sys/kernel/debug"
     39       sudo chown -R $USER /sys/kernel/debug
     40     fi
     41 
     42     echo 0 > /sys/kernel/debug/tracing/tracing_on
     43   fi
     44 }
     45 
     46 function adb_supports_push_sync {
     47   adb --help | grep 'push.*\[--sync\]' 2>&1 >/dev/null
     48 }
     49 
     50 function push {
     51   if is_android $OUT; then
     52     local maybe_sync=''
     53     if adb_supports_push_sync; then
     54       maybe_sync='--sync '
     55     fi
     56     echo adb push $maybe_sync $1 $DIR
     57     adb push $maybe_sync $1 $DIR
     58   else
     59     echo cp $1 $DIR
     60     cp $1 $DIR
     61   fi
     62 }
     63 
     64 function pull {
     65   if is_android $OUT; then
     66     echo adb pull $DIR/$1 $2
     67     adb pull $DIR/$1 $2
     68   else
     69     echo mv $DIR/$1 $2
     70     mv $DIR/$1 $2
     71   fi
     72 }
     73 
     74 # If not set guess the OUT dir using the latest directory.
     75 if [ ! -f "$OUT/args.gn" ]; then
     76   echo "OUT=$OUT doesn't look like an output directory."
     77   echo "Please specify a directory by doing: export OUT=out/xxx"
     78   exit 1
     79 fi
     80 
     81 # You can set the config to one of the files under test/configs e.g.
     82 # CONFIG=ftrace.cfg or to :test. Defaults to :test.
     83 CONFIG="${CONFIG:-:test}"
     84 
     85 if is_android $OUT ; then
     86   DIR=/data/local/tmp
     87 else
     88   DIR=$(mktemp -p $TMPDIR -d perfetto.XXXXXX)
     89 fi
     90 
     91 tools/ninja -C $OUT traced traced_probes perfetto trace_to_text test/configs
     92 
     93 push $OUT/traced
     94 push $OUT/traced_probes
     95 push $OUT/perfetto
     96 reset_tracing
     97 
     98 if is_android $OUT; then
     99   PREFIX="
    100 PERFETTO_CONSUMER_SOCK_NAME=/data/misc/perfetto-traces/test_consumer_socket
    101 PERFETTO_PRODUCER_SOCK_NAME=/data/misc/perfetto-traces/test_producer_socket"
    102 else
    103   PREFIX=""
    104 fi
    105 
    106 if ! is_monolithic $OUT; then
    107   PREFIX="$PREFIX LD_LIBRARY_PATH=$DIR"
    108   push $OUT/libtraced_shared.so
    109 fi
    110 
    111 CONFIG_DEVICE_PATH=$CONFIG
    112 if [[ "$CONFIG" != ":test" ]]; then
    113   CONFIG_DEVICE_PATH=$DIR/$CONFIG.protobuf
    114   CONFIG_PATH=$OUT/$CONFIG.protobuf;
    115   if [[ ! -f $CONFIG_PATH ]]; then
    116     echo 'Config "'$CONFIG_PATH'" not known.'
    117     exit 1
    118   fi
    119   push $CONFIG_PATH
    120 fi
    121 
    122 if tmux has-session -t demo; then
    123   tmux kill-session -t demo
    124 fi
    125 tmux -2 new-session -d -s demo
    126 
    127 if tmux -V | awk '{split($2, ver, "."); if (ver[1] < 2) exit 1 ; else if (ver[1] == 2 && ver[2] < 1) exit 1 }'; then
    128   tmux set-option -g mouse on
    129 else
    130   tmux set-option -g mode-mouse on
    131   tmux set-option -g mouse-resize-pane on
    132   tmux set-option -g mouse-select-pane on
    133   tmux set-option -g mouse-select-window on
    134 fi
    135 
    136 tmux split-window -v
    137 tmux split-window -v
    138 
    139 tmux select-layout even-vertical
    140 
    141 tmux select-pane -t 0
    142 tmux send-keys "clear" C-m
    143 if is_android $OUT; then
    144   tmux send-keys "adb shell" C-m
    145 fi
    146 
    147 tmux select-pane -t 1
    148 tmux send-keys "clear" C-m
    149 if is_android $OUT; then
    150   tmux send-keys "adb shell" C-m
    151 fi
    152 
    153 tmux select-pane -t 2
    154 tmux send-keys "clear" C-m
    155 if is_android $OUT; then
    156   tmux send-keys "adb shell" C-m
    157 fi
    158 
    159 sleep 2
    160 
    161 tmux select-pane -t 1
    162 tmux send-keys "$PREFIX $DIR/traced" Enter
    163 
    164 tmux select-pane -t 0
    165 tmux send-keys "$PREFIX $DIR/traced_probes" Enter
    166 
    167 tmux select-pane -t 2
    168 tmux send-keys "$PREFIX $DIR/perfetto -c $CONFIG_DEVICE_PATH -o $DIR/trace"
    169 
    170 # Select consumer pane.
    171 tmux select-pane -t 2
    172 
    173 tmux -2 attach-session -t demo
    174 
    175 reset_tracing
    176 
    177 TRACE=$HOME/Downloads/trace
    178 pull trace /tmp/trace.protobuf
    179 echo -e "\n\x1b[32mPulling trace into $TRACE.pbtext\x1b[0m"
    180 $OUT/trace_to_text text < /tmp/trace.protobuf > $TRACE.pbtext
    181 echo -e "\n\x1b[32mPulling trace into $TRACE.json\x1b[0m"
    182 $OUT/trace_to_text systrace < /tmp/trace.protobuf > $TRACE.json
    183