Igor Murashkin | 45087de | 2018-09-13 13:56:58 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2018, The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # |
| 18 | # Forces an application APK to be compiled (by ART's dex2oat) |
| 19 | # with a specific compiler filter. |
| 20 | # |
| 21 | # Example usage: |
| 22 | # $> ./force_compiler_filter -p com.google.android.apps.maps -c speed-profile |
| 23 | # |
| 24 | # (The application may be started/stopped as a side effect) |
| 25 | # |
| 26 | |
| 27 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 28 | source "$DIR/lib/common" |
| 29 | |
| 30 | usage() { |
| 31 | cat <<EOF |
| 32 | Usage: $(basename $0) [OPTION]... |
| 33 | |
| 34 | Required: |
| 35 | -p, --package package of the app to recompile |
| 36 | -c, --compiler-filter override the compiler filter if set (default none) |
| 37 | valid options are listed by: adb shell cmd package, under compile -m |
| 38 | |
| 39 | Optional: |
| 40 | -a, --activity activity of the app to recompile |
| 41 | -h, --help usage information (this) |
| 42 | -v, --verbose enable extra verbose printing |
| 43 | -w, --wait_time how long to wait for app startup (default 10) in seconds |
| 44 | EOF |
| 45 | } |
| 46 | |
| 47 | wait_time="10" # seconds |
| 48 | |
| 49 | parse_arguments() { |
| 50 | while [[ $# -gt 0 ]]; do |
| 51 | case "$1" in |
| 52 | -a|--activity) |
| 53 | activity="$2" |
| 54 | shift |
| 55 | ;; |
| 56 | -h|--help) |
| 57 | usage |
| 58 | exit 0 |
| 59 | ;; |
| 60 | -p|--package) |
| 61 | package="$2" |
| 62 | shift |
| 63 | ;; |
| 64 | -w|--wait_time) |
| 65 | wait_time="$2" |
| 66 | shift |
| 67 | ;; |
| 68 | -c|--compiler-filter) |
| 69 | compiler_filter="$2" |
| 70 | shift |
| 71 | ;; |
| 72 | -v|--verbose) |
| 73 | verbose="y" |
| 74 | ;; |
| 75 | esac |
| 76 | shift |
| 77 | done |
| 78 | |
| 79 | if [[ -z "$compiler_filter" ]]; then |
| 80 | echo "Missing required --compiler-filter" >&2 |
| 81 | echo "" |
| 82 | usage |
| 83 | exit 1 |
| 84 | fi |
| 85 | if [[ -z "$package" ]]; then |
| 86 | echo "Missing required --package" >&2 |
| 87 | echo "" |
| 88 | usage |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | if [[ "$activity" == "" ]]; then |
| 93 | activity="$(get_activity_name "$package")" |
| 94 | if [[ "$activity" == "" ]]; then |
| 95 | echo "Activity name could not be found, invalid package name?" 1>&2 |
| 96 | exit 1 |
| 97 | else |
| 98 | verbose_print "Activity name inferred: " "$activity" |
| 99 | fi |
| 100 | fi |
| 101 | } |
| 102 | |
Igor Murashkin | 45087de | 2018-09-13 13:56:58 -0700 | [diff] [blame] | 103 | force_package_compilation() { |
| 104 | local arg_compiler_filter="$1" |
| 105 | local arg_package="$2" |
| 106 | |
| 107 | if [[ $arg_compiler_filter == speed-profile ]]; then |
| 108 | # Force the running app to dump its profiles to disk. |
| 109 | remote_pkill "$arg_package" -SIGUSR1 |
| 110 | sleep 1 # give some time for above to complete. |
| 111 | fi |
| 112 | |
| 113 | adb shell cmd package compile -m "$arg_compiler_filter" -f "$arg_package" |
| 114 | } |
| 115 | |
| 116 | main() { |
| 117 | parse_arguments "$@" |
| 118 | |
| 119 | if [[ $compiler_filter == speed-profile ]]; then |
| 120 | # screen needs to be unlocked in order to run an app |
| 121 | "$DIR"/unlock_screen |
| 122 | |
Igor Murashkin | 973a2dc | 2018-09-14 16:27:53 -0700 | [diff] [blame] | 123 | local output=$("$DIR"/launch_application "$package" "$activity") |
Igor Murashkin | 45087de | 2018-09-13 13:56:58 -0700 | [diff] [blame] | 124 | if [[ $? -ne 0 ]]; then |
Igor Murashkin | 973a2dc | 2018-09-14 16:27:53 -0700 | [diff] [blame] | 125 | echo "launching application failed" >&2 |
Igor Murashkin | 45087de | 2018-09-13 13:56:58 -0700 | [diff] [blame] | 126 | exit 1 |
| 127 | fi |
| 128 | |
Igor Murashkin | 973a2dc | 2018-09-14 16:27:53 -0700 | [diff] [blame] | 129 | verbose_print "$output" |
Igor Murashkin | 45087de | 2018-09-13 13:56:58 -0700 | [diff] [blame] | 130 | # give some time for app startup to complete. |
| 131 | # this is supposed to be an upper bound for measuring startup time. |
| 132 | sleep "$wait_time" |
| 133 | fi |
| 134 | |
| 135 | force_package_compilation "$compiler_filter" "$package" |
| 136 | |
| 137 | # kill the application to ensure next time it's started, |
| 138 | # it picks up the correct compilation filter. |
| 139 | adb shell am force-stop "$package" |
| 140 | remote_pkill "$package" |
| 141 | } |
| 142 | |
| 143 | main "$@" |