Home | History | Annotate | Download | only in android
      1 #!/bin/bash
      2 #
      3 # Copyright 2013 The Chromium Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 #
      7 # Start / stop profiling in chrome.
      8 
      9 # The profiling data is saved to directory /sdcard/Download. The files
     10 # are named beginning chrome-profile-results-
     11 #
     12 # Assumes you have sourced the android build environment script
     13 # (e.g. 'adb' is on your path).
     14 set -e
     15 
     16 usage() {
     17   echo "adb_profile_chrome [--start [-o file] [-c C]|--stop|-d|-t N] [-v N]"
     18   echo "See http://dev.chromium.org/developers/how-tos/trace-event-profiling-tool for detailed instructions on profiling."
     19   echo ""
     20   echo "  --start            Start profiling."
     21   echo "  --output|-o file   Save profile output to file. "
     22   echo "                     (Default is /sdcard/Download/chrome-profile-results-*)"
     23   echo "  --categories|-c C  Select categories to trace with comma-delimited wildcards."
     24   echo "                     e.g. '*', 'cat1*,-cat1a'. Default is '*'."
     25   echo "  --stop             Stop profiling."
     26   echo "  --download|-d      Download latest trace."
     27   echo "  --time|-t N        Profile for N seconds and download the resulting trace."
     28   echo "  --version|v N      Select among installed browsers."
     29   echo "                     One of stable (default), beta, dev, build"
     30   echo ""
     31   echo "Profiling data is saved to the device."
     32   exit 0
     33 }
     34 
     35 send_intent() {
     36   local PACKAGE=$1
     37   local INTENT=$2
     38   shift
     39   shift
     40   adb shell am broadcast -a $PACKAGE.$INTENT $*
     41 }
     42 
     43 download_latest_trace() {
     44   TRACE_FILE=$(adb logcat -d | \
     45       grep "Logging performance trace to file: " | \
     46       tail -1 | \
     47       perl -pi -e "s/.*\/storage\/emulated\/.+\/([^\r]+).*/\/sdcard\/Download\/\\1/g")
     48   if [ -z "$TRACE_FILE" ]; then
     49     echo "Unable to determine trace file name"
     50     exit 1
     51   fi
     52 
     53   adb pull $TRACE_FILE 2> /dev/null
     54   LOCAL_TRACE_FILE=$(basename $TRACE_FILE)
     55   if [ ! -f "$LOCAL_TRACE_FILE" ]; then
     56     echo "Unable to download trace file"
     57     exit 1
     58   fi
     59 }
     60 
     61 do_timed_capture() {
     62   local PACKAGE=$1
     63   local INTERVAL=$2
     64   shift
     65   shift
     66   echo -n "Capturing trace..."
     67   send_intent ${PACKAGE} "GPU_PROFILER_START" $* > /dev/null
     68   sleep ${INTERVAL}
     69   send_intent ${PACKAGE} "GPU_PROFILER_STOP" > /dev/null
     70   echo "done"
     71 
     72   echo -n "Downloading trace..."
     73   sleep $[${INTERVAL} / 4 + 1]
     74   download_latest_trace
     75   echo "done"
     76 
     77   echo "Trace written to ${PWD}/${LOCAL_TRACE_FILE}"
     78 }
     79 
     80 PACKAGE=${DEFAULT_PACKAGE:-com.android.chrome}
     81 
     82 while test -n "$1"; do
     83   case "$1" in
     84     -v|--version)
     85       if [[ -z "$2" ]] ; then
     86         usage
     87       fi
     88       shift
     89       case "$1" in
     90         stable) PACKAGE="com.android.chrome" ;;
     91         beta) PACKAGE="com.chrome.beta" ;;
     92         dev) PACKAGE="com.google.android.apps.chrome_dev" ;;
     93         build) PACKAGE="com.google.android.apps.chrome" ;;
     94         *) usage ;;
     95       esac
     96       ;;
     97     --start) FUNCTION="GPU_PROFILER_START" ;;
     98     --stop) FUNCTION="GPU_PROFILER_STOP" ;;
     99     -o|--output)
    100       if [ -z "$2" ] ; then
    101         usage
    102       fi
    103       OUTPUT="-e file '$2'"
    104       shift
    105       ;;
    106     -c|--categories)
    107       if [ -z "$2" ]; then
    108         usage
    109       fi
    110       CATEGORIES="-e categories '$2'"
    111       shift
    112       ;;
    113     -t|--time)
    114       shift
    115       if [ -z "$1" ] ; then
    116         usage
    117       fi
    118       INTERVAL="$1"
    119       ;;
    120     -d|--download)
    121       shift
    122       download_latest_trace
    123       echo "Trace written to ${PWD}/${LOCAL_TRACE_FILE}"
    124       ;;
    125     *) usage ;;
    126   esac
    127   shift
    128 done
    129 
    130 if [ -z "${INTERVAL}" ] ; then
    131   if [ -z "${FUNCTION}" ] ; then
    132     usage
    133   else
    134     send_intent ${PACKAGE} ${FUNCTION} ${OUTPUT} ${CATEGORIES}
    135   fi
    136 else
    137   do_timed_capture ${PACKAGE} ${INTERVAL} ${CATEGORIES}
    138 fi
    139 exit 0
    140