blob: dea7ebdc3c4c1cf28a72ec8857c8ece591f9145c [file] [log] [blame]
Elliott Hughes40ef99e2011-08-11 17:44:34 -07001# 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
Nicolas Geoffray70a998c2014-12-04 17:05:22 +000015# 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
Orion Hodson9763f2e2017-03-28 08:27:23 +010019# Globals
Orion Hodson9763f2e2017-03-28 08:27:23 +010020ART_BINARY=dalvikvm
21DELETE_ANDROID_DATA="no"
22LAUNCH_WRAPPER=
23LIBART=libart.so
24JIT_PROFILE="no"
25VERBOSE="no"
Vladimir Markoafd44ea2017-07-14 13:52:02 +010026EXTRA_OPTIONS=()
Orion Hodson9763f2e2017-03-28 08:27:23 +010027
Calin Juravle64f45cb2017-03-16 19:58:26 -070028# Follow all sym links to get the program name.
29if [ z"$BASH_SOURCE" != z ]; then
30 PROG_NAME="$BASH_SOURCE"
31else
32 PROG_NAME="$0"
33fi
34while [ -h "$PROG_NAME" ]; do
35 # On Mac OS, readlink -f doesn't work.
36 PROG_NAME="$(readlink "$PROG_NAME")"
37done
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000038
Nicolas Geoffrayfc3c67a2014-07-02 14:57:53 +010039function find_libdir() {
Orion Hodson9763f2e2017-03-28 08:27:23 +010040 # Get the actual file, $1 is the ART_BINARY_PATH and may be a symbolic link.
Nicolas Geoffray70a998c2014-12-04 17:05:22 +000041 # Use realpath instead of readlink because Android does not have a readlink.
Orion Hodson9763f2e2017-03-28 08:27:23 +010042 if [[ "$(realpath "$1")" == *dalvikvm64 ]]; then
Nicolas Geoffrayfc3c67a2014-07-02 14:57:53 +010043 echo "lib64"
44 else
45 echo "lib"
46 fi
47}
48
Nicolas Geoffray49cda062017-04-21 13:08:25 +010049function replace_compiler_filter_with_quicken() {
50 ARGS_WITH_QUICKEN=("$@")
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +000051
52 found="false"
53 ((index=0))
54 while ((index <= $#)); do
Nicolas Geoffray49cda062017-04-21 13:08:25 +010055 what="${ARGS_WITH_QUICKEN[$index]}"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +000056
57 case "$what" in
58 --compiler-filter=*)
Nicolas Geoffray49cda062017-04-21 13:08:25 +010059 ARGS_WITH_QUICKEN[$index]="--compiler-filter=quicken"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +000060 found="true"
61 ;;
62 esac
63
64 ((index++))
65 shift
66 done
67 if [ "$found" != "true" ]; then
Nicolas Geoffray49cda062017-04-21 13:08:25 +010068 ARGS_WITH_QUICKEN=(-Xcompiler-option --compiler-filter=quicken "${ARGS_WITH_QUICKEN[@]}")
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +000069 fi
70}
71
Orion Hodson9763f2e2017-03-28 08:27:23 +010072function usage() {
73 cat 1>&2 <<EOF
74Usage: art [OPTIONS] [--] [ART_OPTIONS] CLASS
Nicolas Geoffrayf63a0a52014-09-02 15:24:25 +010075
Orion Hodson9763f2e2017-03-28 08:27:23 +010076Supported OPTIONS include:
77 --32 Use the 32-bit Android Runtime.
78 --64 Use the 64-bit Android Runtime.
79 --callgrind Launch the Android Runtime in callgrind.
80 -d Use the debug ART library (libartd.so).
81 --debug Equivalent to -d.
82 --gdb Launch the Android Runtime in gdb.
83 --help Display usage message.
84 --invoke-with <program> Launch the Android Runtime in <program>.
85 --perf Launch the Android Runtime with perf recording.
86 --perf-report Launch the Android Runtime with perf recording with
87 report upon completion.
88 --profile Run with profiling, then run using profile data.
89 --verbose Run script verbosely.
90
91The ART_OPTIONS are passed directly to the Android Runtime.
92
93Example:
94 art --32 -cp my_classes.dex MainClass
95
96Common errors:
97 1) Not having core.art available (see $ANDROID_BUILD_TOP/art/Android.mk).
98 eg m -j32 build-art-host
99 2) Not having boot.art available (see $ANDROID_BUILD_TOP/build/make/core/dex_preopt_libart_boot.mk)
100 eg m -j32 out/target/product/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art
101EOF
102}
103
104function clean_android_data() {
105 if [ "$DELETE_ANDROID_DATA" = "yes" ]; then
106 rm -rf $ANDROID_DATA
Nicolas Geoffrayf63a0a52014-09-02 15:24:25 +0100107 fi
Orion Hodson9763f2e2017-03-28 08:27:23 +0100108}
109
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100110# Given 'VAR1=VAL VAR2=VAL2 ... cmd arg1 arg2 ... argN' run the 'cmd' with the args
111# with the modified environment {VAR1=VAL,VAL2=,...}.
112#
113# Also prints the command to be run if verbose mode is enabled.
Orion Hodson9763f2e2017-03-28 08:27:23 +0100114function verbose_run() {
115 if [ "$VERBOSE" = "yes" ]; then
116 echo "$@"
117 fi
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100118
119 env "$@"
Orion Hodson9763f2e2017-03-28 08:27:23 +0100120}
121
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000122# Parse a colon-separated list into an array (e.g. "foo.dex:bar.dex" -> (foo.dex bar.dex))
123PARSE_CLASSPATH_RESULT=() # Return value will be here due to shell limitations.
124parse_classpath() {
125 local cp="$1"
126 local oldifs=$IFS
127
128 local cp_array
129 cp_array=()
130
131 IFS=":"
132 for part in $cp; do
133 cp_array+=("$part")
134 done
135 IFS=$oldifs
136
137 PARSE_CLASSPATH_RESULT=("${cp_array[@]}")
138}
139
140# Sets 'PARSE_CLASSPATH_RESULT' to an array of class path dex files.
141# e.g. (-cp foo/classes.dex:bar/classes.dex) -> (foo/classes.dex bar/classes.dex)
142find_cp_in_args() {
143 local found="false"
144 local index=0
145 local what
146
147 while [[ $# -gt 0 ]]; do
148 case "$1" in
149 -cp|-classpath)
150 parse_classpath "$2"
151 # Sets 'PARSE_CLASSPATH_RESULT' to an array of class path dex files.
152 # Subsequent parses will overwrite the preceding.
153 shift
154 ;;
155 esac
156 shift
157 done
158}
159
160# Delete the 'oat' directories relative to the classpath's dex files.
161# e.g. (foo/classes.dex bar/classes.dex) would delete (foo/oat bar/oat) directories.
162cleanup_oat_directory() {
163 local classpath
164 classpath=("$@")
165
166 local dirpath
167
168 for path in "${classpath[@]}"; do
169 dirpath="$(dirname "$path")"
170 [[ -d "$dirpath" ]] && verbose_run rm -rf "$dirpath/oat"
171 done
172}
173
174# Parse -cp <CP>, -classpath <CP>, and $CLASSPATH to find the dex files.
175# Each dex file's directory will have an 'oat' file directory, delete it.
176# Input: Command line arguments to the art script.
177# e.g. -cp foo/classes.dex:bar/classes.dex would delete (foo/oat bar/oat) directories.
178cleanup_oat_directory_for_classpath() {
179 # First try: Use $CLASSPATH environment variable.
180 parse_classpath "$CLASSPATH"
181 # Second try: Look for latest -cp or -classpath arg which will take precedence.
182 find_cp_in_args "$@"
183
184 cleanup_oat_directory "${PARSE_CLASSPATH_RESULT[@]}"
185}
186
Igor Murashkind54ac262017-07-26 11:16:23 -0700187# Attempt to find $ANDROID_ROOT/framework/<isa>/core.art' without knowing what <isa> is.
188function check_if_boot_image_file_exists() {
189 local image_location_dir="$1"
190 local image_location_name="$2"
191
192 # Expand image_files to a list of existing image files on the disk.
193 # If no such files exist, it expands to single element 'dir/*/file' with a literal '*'.
194 local image_files
195 image_files=("$image_location_dir"/*/"$image_location_name") # avoid treating "*" as literal.
196
197 # Array always has at least 1 element. Test explicitly whether the file exists.
198 [[ -e "${image_files[0]}" ]]
199}
200
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700201# Automatically find the boot image location. It uses core.art by default.
202# On a real device, it might only have a boot.art, so use that instead when core.art does not exist.
203function detect_boot_image_location() {
204 local image_location_dir="$ANDROID_ROOT/framework"
205 local image_location_name="core.art"
206
Igor Murashkind54ac262017-07-26 11:16:23 -0700207 # If there are no existing core.art, try to find boot.art.
208 # If there is no boot.art then leave it as-is, assumes -Ximage is explicitly used.
209 # Otherwise let dalvikvm give the error message about an invalid image file.
210 if ! check_if_boot_image_file_exists "$image_location_dir" "core.art" && \
211 check_if_boot_image_file_exists "$image_location_dir" "boot.art"; then
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700212 image_location_name="boot.art"
213 fi
214
215 local image_location="$image_location_dir/$image_location_name"
216 echo "$image_location"
217}
218
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000219# Runs dalvikvm, returns its exit code.
220# (Oat directories are cleaned up in between runs)
Orion Hodson9763f2e2017-03-28 08:27:23 +0100221function run_art() {
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700222 local image_location="$(detect_boot_image_location)"
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000223 local ret
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700224
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000225 # First cleanup any left-over 'oat' files from the last time dalvikvm was run.
226 cleanup_oat_directory_for_classpath "$@"
227 # Run dalvikvm.
Orion Hodson9763f2e2017-03-28 08:27:23 +0100228 verbose_run ANDROID_DATA=$ANDROID_DATA \
229 ANDROID_ROOT=$ANDROID_ROOT \
230 LD_LIBRARY_PATH=$LD_LIBRARY_PATH \
231 PATH=$ANDROID_ROOT/bin:$PATH \
232 LD_USE_LOAD_BIAS=1 \
233 $LAUNCH_WRAPPER $ART_BINARY_PATH $lib \
234 -XXlib:$LIBART \
235 -Xnorelocate \
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700236 -Ximage:"$image_location" \
Orion Hodson9763f2e2017-03-28 08:27:23 +0100237 "$@"
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000238 ret=$?
239
240 # Avoid polluting disk with 'oat' files after dalvikvm has finished.
241 cleanup_oat_directory_for_classpath "$@"
242
243 # Forward exit code of dalvikvm.
244 return $ret
Orion Hodson9763f2e2017-03-28 08:27:23 +0100245}
246
247while [[ "$1" = "-"* ]]; do
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100248 case "$1" in
Orion Hodson9763f2e2017-03-28 08:27:23 +0100249 --)
250 # No more arguments for this script.
251 shift
252 break
253 ;;
254 --32)
255 ART_BINARY=dalvikvm32
256 ;;
257 --64)
258 ART_BINARY=dalvikvm64
259 ;;
260 --callgrind)
261 LAUNCH_WRAPPER="valgrind --tool=callgrind"
262 ;;
263 -d)
264 ;& # Fallthrough
265 --debug)
266 LIBART="libartd.so"
Andreas Gampe1c5b42f2017-06-15 18:20:45 -0700267 # Expect that debug mode wants all checks.
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100268 EXTRA_OPTIONS+=(-XX:SlowDebug=true)
Orion Hodson9763f2e2017-03-28 08:27:23 +0100269 ;;
270 --gdb)
271 LIBART="libartd.so"
272 LAUNCH_WRAPPER="gdb --args"
273 ;;
274 --help)
275 usage
276 exit 0
277 ;;
278 --invoke-with)
279 LAUNCH_WRAPPER=$2
280 shift
281 ;;
282 --perf)
283 PERF="record"
284 ;;
285 --perf-report)
286 PERF="report"
287 ;;
288 --profile)
289 JIT_PROFILE="yes"
290 ;;
291 --verbose)
292 VERBOSE="yes"
293 ;;
294 --*)
295 echo "unknown option: $1" 1>&2
296 usage
297 exit 1
298 ;;
299 *)
300 break
301 ;;
302 esac
303 shift
Nicolas Geoffrayf63a0a52014-09-02 15:24:25 +0100304done
305
Orion Hodson9763f2e2017-03-28 08:27:23 +0100306if [ $# -eq 0 ]; then
307 usage
308 exit 1
309fi
310
Brian Carlstrom87bb26f2014-09-08 11:13:47 -0700311PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
312ANDROID_ROOT=$PROG_DIR/..
Orion Hodson9763f2e2017-03-28 08:27:23 +0100313ART_BINARY_PATH=$ANDROID_ROOT/bin/$ART_BINARY
Brian Carlstrom87bb26f2014-09-08 11:13:47 -0700314
Orion Hodson9763f2e2017-03-28 08:27:23 +0100315if [ ! -x "$ART_BINARY_PATH" ]; then
316 cat 1>&2 <<EOF
317Android Runtime not found: $ART_BINARY_PATH
318This script should be in the same directory as the Android Runtime ($ART_BINARY).
319EOF
320 exit 1
321fi
322
323LIBDIR="$(find_libdir $ART_BINARY_PATH)"
324LD_LIBRARY_PATH=$ANDROID_ROOT/$LIBDIR
Orion Hodson9763f2e2017-03-28 08:27:23 +0100325
Nicolas Geoffray70a998c2014-12-04 17:05:22 +0000326# If ANDROID_DATA is the system ANDROID_DATA or is not set, use our own,
327# and ensure we delete it at the end.
328if [ "$ANDROID_DATA" = "/data" ] || [ "$ANDROID_DATA" = "" ]; then
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700329 if [[ $PWD != / ]]; then
330 ANDROID_DATA="$PWD/android-data$$"
331 else
332 # Use /data/local/tmp when running this from adb shell, since it starts out in /
333 # by default.
334 ANDROID_DATA="$ANDROID_DATA/local/tmp/android-data$$"
335 fi
Igor Murashkind54ac262017-07-26 11:16:23 -0700336 mkdir -p "$ANDROID_DATA"
Orion Hodson9763f2e2017-03-28 08:27:23 +0100337 DELETE_ANDROID_DATA="yes"
Nicolas Geoffray70a998c2014-12-04 17:05:22 +0000338fi
339
Orion Hodson9763f2e2017-03-28 08:27:23 +0100340if [ "$PERF" != "" ]; then
341 LAUNCH_WRAPPER="perf record -g -o $ANDROID_DATA/perf.data -e cycles:u $LAUNCH_WRAPPER"
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100342 EXTRA_OPTIONS+=(-Xcompiler-option --generate-debug-info)
Nicolas Geoffraye099a612014-12-12 13:52:00 +0000343fi
344
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000345if [ "$JIT_PROFILE" = "yes" ]; then
346 # Create the profile. The runtime expects profiles to be created before
347 # execution.
348 PROFILE_PATH="$ANDROID_DATA/primary.prof"
Igor Murashkind54ac262017-07-26 11:16:23 -0700349 touch "$PROFILE_PATH"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000350
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100351 # Replace the compiler filter with quicken so that we
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000352 # can capture the profile.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100353 ARGS_WITH_QUICKEN=
354 replace_compiler_filter_with_quicken "$@"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000355
356 run_art -Xjitsaveprofilinginfo \
357 -Xps-min-methods-to-save:1 \
358 -Xps-min-classes-to-save:1 \
359 -Xps-min-notification-before-wake:10 \
360 -Xps-profile-path:$PROFILE_PATH \
361 -Xusejit:true \
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100362 "${ARGS_WITH_QUICKEN[@]}" \
Igor Murashkin11942442017-07-20 11:08:34 -0700363 &> "$ANDROID_DATA/profile_gen.log"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000364 EXIT_STATUS=$?
365
366 if [ $EXIT_STATUS != 0 ]; then
Igor Murashkind54ac262017-07-26 11:16:23 -0700367 echo "Profile run failed: " >&2
368 cat "$ANDROID_DATA/profile_gen.log" >&2
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000369 clean_android_data
370 exit $EXIT_STATUS
371 fi
372
Igor Murashkind54ac262017-07-26 11:16:23 -0700373 # Wipe dalvik-cache so that a subsequent run_art must regenerate it.
374 # Leave $ANDROID_DATA intact since it contains our profile file.
375 rm -rf "$ANDROID_DATA/dalvik-cache"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000376
377 # Append arguments so next invocation of run_art uses the profile.
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100378 EXTRA_OPTIONS+=(-Xcompiler-option --profile-file="$PROFILE_PATH")
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000379fi
380
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100381# Protect additional arguments in quotes to preserve whitespaces (used by
382# run-jdwp-test.sh when running on device), '$' (may be used as part of
383# classpath) and other special characters when evaluated.
384EXTRA_OPTIONS+=("$@")
Calin Juravle64f45cb2017-03-16 19:58:26 -0700385
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100386run_art "${EXTRA_OPTIONS[@]}"
Orion Hodson9763f2e2017-03-28 08:27:23 +0100387EXIT_STATUS=$?
Calin Juravleaa980612014-10-20 15:58:57 +0100388
Orion Hodson9763f2e2017-03-28 08:27:23 +0100389if [ "$PERF" != "" ]; then
390 if [ "$PERF" = report ]; then
Calin Juravleaa980612014-10-20 15:58:57 +0100391 perf report -i $ANDROID_DATA/perf.data
392 fi
393 echo "Perf data saved in: $ANDROID_DATA/perf.data"
394else
Orion Hodson9763f2e2017-03-28 08:27:23 +0100395 # Perf output is placed under $ANDROID_DATA so not cleaned when perf options used.
396 clean_android_data
Calin Juravleaa980612014-10-20 15:58:57 +0100397fi
398
Nicolas Geoffray89c4e282014-03-24 09:33:30 +0000399exit $EXIT_STATUS