Home | History | Annotate | Download | only in bin
      1 #!/bin/bash
      2 #
      3 # android_perf: utility for running perf on an android device
      4 #
      5 # The basic usage sequence is to run...
      6 #  1) perf record [gm/tests/bench] # runs profiler on specified app
      7 #  2) perf report # prints profiler results
      8 #  3) perf clean # cleans the temporary directory used to store results
      9 #
     10 
     11 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
     12 source $SCRIPT_DIR/android_setup.sh
     13 source $SCRIPT_DIR/utils/setup_adb.sh
     14 
     15 if [ $(uname) == "Linux" ]; then
     16     PERFHOST=$SCRIPT_DIR/linux/perfhost
     17 elif [ $(uname) == "Darwin" ]; then
     18     PERFHOST=$SCRIPT_DIR/mac/perfhost
     19 else
     20     echo "Could not automatically determine OS!"
     21     exit 1;
     22 fi
     23 
     24 # grab and remove the perf command from the input args
     25 PERF_CMD=${APP_ARGS[0]}
     26 unset APP_ARGS[0]
     27 runVars=("${APP_ARGS[@]}")  # shift array indices
     28 
     29 # We need the debug symbols from these files
     30 PERF_TMP_DIR=$(pwd)/android_perf_tmp
     31 
     32 TMP_SYS_BIN=$PERF_TMP_DIR/system/bin
     33 TMP_SYS_LIB=$PERF_TMP_DIR/system/lib
     34 TMP_APP_LOC=$PERF_TMP_DIR/data/local/tmp
     35 
     36 perf_setup() {
     37 
     38     mkdir -p $TMP_SYS_BIN
     39     mkdir -p $TMP_SYS_LIB
     40     mkdir -p $TMP_APP_LOC
     41 
     42     echo "Copying symbol files"
     43     adb_pull_if_needed /system/lib/libc.so $TMP_SYS_LIB
     44     adb_pull_if_needed /system/lib/libstlport.so $TMP_SYS_LIB
     45     adb_pull_if_needed /system/lib/libGLESv2.so $TMP_SYS_LIB
     46     adb_pull_if_needed /system/lib/libandroid.so $TMP_SYS_LIB
     47     adb_pull_if_needed /system/lib/libm.so $TMP_SYS_LIB
     48     adb_pull_if_needed /system/lib/libz.so $TMP_SYS_LIB
     49 
     50     # BUILDTYPE variable is set by android_setup.sh
     51     BUILDDIR="${SKIA_OUT}/${BUILDTYPE}"
     52     if [ ! -f "${BUILDDIR}/lib/lib${runVars[0]}.so" ];
     53     then
     54       echo "Unable to find the ${runVars[0]} library in ${BUILDDIR}/lib."
     55       exit 1
     56     fi
     57 
     58     echo "Pushing app..."
     59     for lib_file in \
     60         "${BUILDDIR}/skia_launcher" \
     61         "${BUILDDIR}/lib/libskia_android.so" \
     62         "${BUILDDIR}/lib/lib${runVars[0]}.so" \
     63       ; do
     64       adb_push_if_needed "$lib_file" /data/local/tmp
     65       cp "$lib_file" $TMP_APP_LOC
     66     done
     67 }
     68 
     69 perf_record() {
     70 
     71     echo "Killing any running Skia processes."
     72     $ADB shell ps | grep skia_launcher | awk '{print $2}' | xargs $ADB shell kill
     73 
     74     echo "Starting application"
     75     $ADB shell /data/local/tmp/skia_launcher ${runVars[@]} &
     76 
     77     # WE REALLY REALLY WANT TO BE ABLE TO PASS THE SKIA_LAUNCHER APP DIRECTLY TO
     78     # PERF, BUT AT THIS POINT THE DATA FILE WE GET WHEN GOING THAT ROUTE IS UNABLE
     79     # TO BE READ BY THE REPORTING TOOL
     80     echo "Starting profiler"
     81     APP_PID=$($ADB shell ps | grep skia_launcher | awk '{print $2}')
     82     $ADB shell perf record -p ${APP_PID} sleep 70
     83 
     84     $ADB pull /data/perf.data $PERF_TMP_DIR/perf.data
     85 
     86     exit 0;
     87 }
     88 
     89 perf_report() {
     90     adb_pull_if_needed /data/perf.data $PERF_TMP_DIR/perf.data
     91     $PERFHOST report -i $PERF_TMP_DIR/perf.data --symfs=$PERF_TMP_DIR ${runVars[@]}
     92 }
     93 
     94 # Clean up
     95 perf_clean() {
     96     rm -rf $PERF_TMP_DIR
     97 }
     98 
     99 case $PERF_CMD in
    100   setup)
    101       perf_setup ${runVars[@]}
    102       ;;
    103   record)
    104       perf_setup ${runVars[@]}
    105       perf_record ${runVars[@]}
    106       ;;
    107   report)
    108       perf_report
    109       ;;
    110   clean)
    111       perf_clean
    112       ;;
    113     *)
    114       echo -n "ERROR: unknown perf command ($PERF_CMD), valid values: "
    115       echo "setup, record, report, clean"
    116       exit 1;
    117       ;;
    118 esac
    119 
    120 exit 0;
    121