Roland Levillain | 0645bcb | 2019-02-06 12:00:01 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (C) 2019 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 | # Test Android Runtime (Boot) device configuration flags (living in namespace |
| 19 | # `runtime_native_boot`). |
| 20 | |
| 21 | me=$(basename $0) |
| 22 | |
| 23 | # Namespace containing the tested flag. |
| 24 | namespace=runtime_native_boot |
| 25 | # Default set of checked zygote processes. |
| 26 | zygotes= |
| 27 | |
| 28 | # Status of whole test script. |
| 29 | exit_status=0 |
| 30 | |
| 31 | function say { |
| 32 | echo "$me: $*" |
| 33 | } |
| 34 | |
| 35 | function banner { |
| 36 | local separator=$(echo "$@" | sed s/./=/g ) |
| 37 | say "$separator" |
| 38 | say "$@" |
| 39 | say "$separator" |
| 40 | } |
| 41 | |
| 42 | function fail { |
| 43 | say "FAILED: $@" |
| 44 | exit_status=1 |
| 45 | } |
| 46 | |
| 47 | function reboot_and_wait_for_device { |
| 48 | say "Rebooting device..." |
| 49 | adb reboot |
| 50 | adb wait-for-device >/dev/null |
| 51 | # Wait until the device has finished booting. Give the device 60 iterations |
| 52 | # (~60 seconds) to try and finish booting before declaring defeat. |
| 53 | local niters=60 |
| 54 | for i in $(seq $niters); do |
| 55 | [[ $(adb shell getprop sys.boot_completed) -eq 1 ]] && return 0 |
| 56 | sleep 1 |
| 57 | done |
| 58 | fail "Device did not finish booting before timeout (~$niters seconds)" |
| 59 | } |
| 60 | |
| 61 | # check_device_config_flag CONTEXT FLAG VALUE |
| 62 | # ------------------------------------------- |
| 63 | # Check that the device configuration flag FLAG is set to VALUE. Use CONTEXT in |
| 64 | # logging. |
| 65 | function check_device_config_flag { |
| 66 | local context=$1 |
| 67 | local flag=$2 |
| 68 | local value=$3 |
| 69 | |
| 70 | say "[$context] Check that the device configuration flag is set..." |
| 71 | local flag_value=$(adb shell device_config get "$namespace" "$flag") |
| 72 | [[ "$flag_value" = "$value" ]] \ |
| 73 | || fail "Device configuration flag \`$flag\` set to \`$flag_value\` (expected \`$value\`)" |
| 74 | } |
| 75 | |
| 76 | # check_no_device_config_flag CONTEXT FLAG |
| 77 | # ---------------------------------------- |
| 78 | # Check that the device configuration flag FLAG is not set. Use CONTEXT in |
| 79 | # logging. |
| 80 | function check_no_device_config_flag { |
| 81 | local context=$1 |
| 82 | local flag=$2 |
| 83 | |
| 84 | say "[$context] Check that the device configuration flag is not set..." |
| 85 | local flag_value=$(adb shell device_config get "$namespace" "$flag") |
| 86 | [[ "$flag_value" = null ]] \ |
| 87 | || fail "Device configuration flag \`$flag\` set to \`$flag_value\` (expected `null`)" |
| 88 | } |
| 89 | |
| 90 | # get_system_property PROP |
| 91 | # ------------------------ |
| 92 | # Get system property PROP associated with a device configuration flag. |
| 93 | function get_system_property { |
| 94 | local prop=$1 |
| 95 | |
| 96 | # Note that we need to be root to read that system property. |
| 97 | adb root >/dev/null |
| 98 | local prop_value=$(adb shell getprop "$prop") |
| 99 | adb unroot >/dev/null |
| 100 | echo "$prop_value" |
| 101 | } |
| 102 | |
| 103 | # check_system_property CONTEXT PROP VALUE |
| 104 | # ---------------------------------------- |
| 105 | # Check that the system property PROP associated with a device configuration |
| 106 | # flag is set to VALUE. Use CONTEXT in logging. |
| 107 | function check_system_property { |
| 108 | local context=$1 |
| 109 | local prop=$2 |
| 110 | local value=$3 |
| 111 | |
| 112 | say "[$context] Check that the persistent system property is set..." |
| 113 | local prop_value=$(get_system_property "$prop") |
| 114 | [[ "$prop_value" = "$value" ]] \ |
| 115 | || fail "System property \`$prop\` set to \`$prop_value\` (expected \`$value\`)" |
| 116 | } |
| 117 | |
| 118 | # check_no_system_property CONTEXT PROP |
| 119 | # ------------------------------------- |
| 120 | # Check that the system property PROP associated with a device configuration |
| 121 | # flag is not set. Use CONTEXT in logging. |
| 122 | function check_no_system_property { |
| 123 | local context=$1 |
| 124 | local prop=$2 |
| 125 | |
| 126 | say "[$context] Check that the persistent system property is not set..." |
| 127 | local prop_value=$(get_system_property "$prop") |
| 128 | [[ -z "$prop_value" ]] \ |
| 129 | || fail "System property \`$prop\` set to \`$prop_value\` (expected unset property)" |
| 130 | } |
| 131 | |
| 132 | # find_zygote_runtime_option ZYGOTE RUNTIME_OPTION |
| 133 | # ------------------------------------------------ |
| 134 | # Return whether ZYGOTE is passed RUNTIME_OPTION. |
| 135 | function find_zygote_runtime_option { |
| 136 | local zygote=$1 |
| 137 | local runtime_option=$2 |
| 138 | |
| 139 | adb logcat -d -s "$zygote" | grep -q -e "option\[[0-9]\+\]=$runtime_option" |
| 140 | } |
| 141 | |
| 142 | # check_zygote_gc_runtime_option CONTEXT VALUE |
| 143 | # -------------------------------------------- |
| 144 | # Check that all zygote processes are passed device configuration flag VALUE as |
| 145 | # GC runtime option. Use CONTEXT in logging. |
| 146 | function check_zygote_gc_runtime_option { |
| 147 | local context=$1 |
| 148 | local value=$2 |
| 149 | |
| 150 | say \ |
| 151 | "[$context] Check that all zygote processes are passed the flag value as a GC runtime option..." |
| 152 | local runtime_option="-Xgc:$value" |
| 153 | for zygote in $zygotes; do |
| 154 | find_zygote_runtime_option "$zygote" "$runtime_option"\ |
| 155 | || fail "Found no \`$runtime_option\` among runtime options passed to \`$zygote\`" |
| 156 | done |
| 157 | } |
| 158 | |
| 159 | # check_no_zygote_gc_runtime_option CONTEXT VALUE |
| 160 | # ----------------------------------------------- |
| 161 | # Check that no zygote process is passed device configuration flag VALUE as GC |
| 162 | # runtime option. Use CONTEXT in logging. |
| 163 | function check_no_zygote_gc_runtime_option { |
| 164 | local context=$1 |
| 165 | local value=$2 |
| 166 | |
| 167 | say "[$context] Check no zygote process is passed the flag value as a GC runtime option..." |
| 168 | local runtime_option="-Xgc:$value" |
| 169 | for zygote in $zygotes; do |
| 170 | find_zygote_runtime_option "$zygote" "$runtime_option"\ |
| 171 | && fail "Found \`$runtime_option\` among runtime options passed to \`$zygote\`" |
| 172 | done |
| 173 | } |
| 174 | |
Roland Levillain | 72b16f0 | 2019-02-19 18:09:58 +0000 | [diff] [blame] | 175 | # test_android_runtime_flag FLAG VALUE GC_RUNTIME_OPTION |
| 176 | # ------------------------------------------------------ |
| 177 | # Test device configuration FLAG with VALUE. Check that GC_RUNTIME_OPTION is |
| 178 | # passed as GC Runtime option by the zygote. |
Roland Levillain | 0645bcb | 2019-02-06 12:00:01 +0000 | [diff] [blame] | 179 | function test_android_runtime_flag { |
| 180 | local flag=$1 |
| 181 | local value=$2 |
Roland Levillain | 72b16f0 | 2019-02-19 18:09:58 +0000 | [diff] [blame] | 182 | local gc_runtime_option=$3 |
Roland Levillain | 0645bcb | 2019-02-06 12:00:01 +0000 | [diff] [blame] | 183 | |
| 184 | # Persistent system property (set after a reboot) associated with the device |
| 185 | # configuration flag. |
| 186 | local prop="persist.device_config.$namespace.$flag" |
| 187 | |
| 188 | banner "Testing \`$flag\` value \`$value\`." |
| 189 | |
| 190 | say "Setting device configuration flag..." |
| 191 | adb shell device_config put "$namespace" "$flag" "$value" |
| 192 | # Give some time to the device to digest this change before rebooting. |
| 193 | sleep 3 |
| 194 | |
| 195 | # Check that both the device configuration flag and the associated system |
| 196 | # property are set, but that the zygote hasn't had the flag passed to it as a |
| 197 | # GC runtime option (as we haven't rebooted yet). |
| 198 | local context="Flag set, before reboot" |
| 199 | check_device_config_flag "$context" "$flag" "$value" |
| 200 | check_system_property "$context" "$prop" "$value" |
Roland Levillain | 72b16f0 | 2019-02-19 18:09:58 +0000 | [diff] [blame] | 201 | check_no_zygote_gc_runtime_option "$context" "$gc_runtime_option" |
Roland Levillain | 0645bcb | 2019-02-06 12:00:01 +0000 | [diff] [blame] | 202 | |
| 203 | # Reboot device for the flag value to take effect. |
| 204 | reboot_and_wait_for_device |
| 205 | context="Flag set, after 1st reboot" |
| 206 | check_device_config_flag "$context" "$flag" "$value" |
| 207 | check_system_property "$context" "$prop" "$value" |
Roland Levillain | 72b16f0 | 2019-02-19 18:09:58 +0000 | [diff] [blame] | 208 | check_zygote_gc_runtime_option "$context" "$gc_runtime_option" |
Roland Levillain | 0645bcb | 2019-02-06 12:00:01 +0000 | [diff] [blame] | 209 | |
| 210 | # Reboot device a second time and check that the state has persisted. |
| 211 | reboot_and_wait_for_device |
| 212 | context="Flag set, after 2nd reboot" |
| 213 | check_device_config_flag "$context" "$flag" "$value" |
| 214 | check_system_property "$context" "$prop" "$value" |
Roland Levillain | 72b16f0 | 2019-02-19 18:09:58 +0000 | [diff] [blame] | 215 | check_zygote_gc_runtime_option "$context" "$gc_runtime_option" |
Roland Levillain | 0645bcb | 2019-02-06 12:00:01 +0000 | [diff] [blame] | 216 | |
| 217 | say "Unsetting device configuration flag..." |
| 218 | adb shell device_config delete "$namespace" "$flag" >/dev/null |
| 219 | # Give some time to the device to digest this change before rebooting. |
| 220 | sleep 3 |
| 221 | |
| 222 | # Reboot and check that the device is back to its default state. |
| 223 | reboot_and_wait_for_device |
| 224 | context="Flag unset, after 3rd reboot" |
| 225 | check_no_device_config_flag "$context" "$flag" |
| 226 | check_no_system_property "$context" "$prop" |
Roland Levillain | 72b16f0 | 2019-02-19 18:09:58 +0000 | [diff] [blame] | 227 | check_no_zygote_gc_runtime_option "$context" "$gc_runtime_option" |
Roland Levillain | 0645bcb | 2019-02-06 12:00:01 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | # Enumerate Zygote processes. |
| 231 | case $(adb shell getprop ro.zygote) in |
| 232 | (zygote32) zygotes="zygote";; |
| 233 | (zygote64) zygotes="zygote64";; |
| 234 | (zygote32_64|zygote64_32) zygotes="zygote zygote64";; |
| 235 | esac |
| 236 | |
Roland Levillain | 72b16f0 | 2019-02-19 18:09:58 +0000 | [diff] [blame] | 237 | # Test "enable_generational_cc" flag values. |
| 238 | test_android_runtime_flag enable_generational_cc false nogenerational_cc |
| 239 | test_android_runtime_flag enable_generational_cc true generational_cc |
Roland Levillain | 0645bcb | 2019-02-06 12:00:01 +0000 | [diff] [blame] | 240 | |
| 241 | if [[ "$exit_status" -eq 0 ]]; then |
| 242 | banner "All tests passed." |
| 243 | else |
| 244 | banner "Test(s) failed." |
| 245 | fi |
| 246 | exit $exit_status |