Home | History | Annotate | Download | only in tools
      1 # Copyright (C) 2011 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 # This script is used on host and device. It uses a common subset
     16 # shell dialect that should work on the host (e.g. bash), and
     17 # Android (e.g. mksh).
     18 
     19 function follow_links() {
     20   if [ z"$BASH_SOURCE" != z ]; then
     21     file="$BASH_SOURCE"
     22   else
     23     file="$0"
     24   fi
     25   while [ -h "$file" ]; do
     26     # On Mac OS, readlink -f doesn't work.
     27     file="$(readlink "$file")"
     28   done
     29   echo "$file"
     30 }
     31 
     32 function find_libdir() {
     33   # Use realpath instead of readlink because Android does not have a readlink.
     34   if [ "$(realpath "$ANDROID_ROOT/bin/$DALVIKVM")" = "$(realpath "$ANDROID_ROOT/bin/dalvikvm64")" ]; then
     35     echo "lib64"
     36   else
     37     echo "lib"
     38   fi
     39 }
     40 
     41 invoke_with=
     42 DALVIKVM=dalvikvm
     43 LIBART=libart.so
     44 
     45 while true; do
     46   if [ "$1" = "--invoke-with" ]; then
     47     shift
     48     invoke_with="$invoke_with $1"
     49     shift
     50   elif [ "$1" = "-d" ]; then
     51     LIBART="libartd.so"
     52     shift
     53   elif [ "$1" = "--32" ]; then
     54     DALVIKVM=dalvikvm32
     55     shift
     56   elif [ "$1" = "--64" ]; then
     57     DALVIKVM=dalvikvm64
     58     shift
     59   elif [ "$1" = "--perf" ]; then
     60     PERF="record"
     61     shift
     62   elif [ "$1" = "--perf-report" ]; then
     63     PERF="report"
     64     shift
     65   elif expr "$1" : "--" >/dev/null 2>&1; then
     66     echo "unknown option: $1" 1>&2
     67     exit 1
     68   else
     69     break
     70   fi
     71 done
     72 
     73 PROG_NAME="$(follow_links)"
     74 PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
     75 ANDROID_ROOT=$PROG_DIR/..
     76 LIBDIR=$(find_libdir)
     77 LD_LIBRARY_PATH=$ANDROID_ROOT/$LIBDIR
     78 DEBUG_OPTION=""
     79 
     80 DELETE_ANDROID_DATA=false
     81 # If ANDROID_DATA is the system ANDROID_DATA or is not set, use our own,
     82 # and ensure we delete it at the end.
     83 if [ "$ANDROID_DATA" = "/data" ] || [ "$ANDROID_DATA" = "" ]; then
     84   ANDROID_DATA=$PWD/android-data$$
     85   mkdir -p $ANDROID_DATA/dalvik-cache/{arm,arm64,x86,x86_64}
     86   DELETE_ANDROID_DATA=true
     87 fi
     88 
     89 if [ z"$PERF" != z ]; then
     90   invoke_with="perf record -o $ANDROID_DATA/perf.data -e cycles:u $invoke_with"
     91   DEBUG_OPTION="-Xcompiler-option --generate-debug-info"
     92 fi
     93 
     94 # We use the PIC core image to work with perf.
     95 ANDROID_DATA=$ANDROID_DATA \
     96   ANDROID_ROOT=$ANDROID_ROOT \
     97   LD_LIBRARY_PATH=$LD_LIBRARY_PATH \
     98   PATH=$ANDROID_ROOT/bin:$PATH \
     99   LD_USE_LOAD_BIAS=1 \
    100   $invoke_with $ANDROID_ROOT/bin/$DALVIKVM $lib \
    101     -XXlib:$LIBART \
    102     -Xnorelocate \
    103     -Ximage:$ANDROID_ROOT/framework/core-optimizing-pic.art \
    104     $DEBUG_OPTION \
    105     "$@"
    106 
    107 EXIT_STATUS=$?
    108 
    109 if [ z"$PERF" != z ]; then
    110   if [ z"$PERF" = zreport ]; then
    111     perf report -i $ANDROID_DATA/perf.data
    112   fi
    113   echo "Perf data saved in: $ANDROID_DATA/perf.data"
    114 else
    115   if [ "$DELETE_ANDROID_DATA" = "true" ]; then
    116     rm -rf $ANDROID_DATA
    117   fi
    118 fi
    119 
    120 exit $EXIT_STATUS
    121