aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/compilation_stats_target.sh
blob: 6bef47950b0e42506e900bd38982fe535d83ff9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
#
# Copyright (c) 2021, Linaro Ltd.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

readonly local_path=$(dirname "$0")
source "${local_path}/../utils/utils.sh"
source "${local_path}/../utils/utils_test.sh"
source "${local_path}/../utils/utils_android.sh"
source "${local_path}/../utils/utils_android_root.sh"
source "${local_path}/../utils/utils_benchmarks.sh"
source "${local_path}/../devices/cpu_freq_utils.sh"

readonly LOG_DIRECTORY="$(get_workspace)"

readonly default_iterations=10

# shellcheck disable=SC2034
declare -A options_format=(
  ["help"]="p:usage()"
  ["h"]="r:&help"
  ["verbose"]="p:enable_verbose()"
  ["v"]="r:&verbose"
  ["iterations"]="${default_iterations}"
  ["mode"]="all"
  ["linux"]="false"
  ["x86"]="false"
  ["sudo"]="false"
  ["cpu"]="all"
  ["skip-build"]="false"
  ["skip-run"]="false"
  ["list-devices"]="p:list_devices()"
  ["target-device"]=""
  ["dump-cfg"]="false"
  ["boot-oat"]="false"
)
declare -A options=()
declare -a benchmarks=()

validate_apk_names() {
  local apk
  for apk in "${benchmarks[@]}"; do
    if [[ ${apk} == "boot.oat" ]]; then
      continue
    fi
    if [[ ! -f "${apk}" ]]; then
      log E "APK file ${apk} does not exist"
      exit 1
    fi
  done
}

usage() {
  log I "Usage: $0 [OPTION]... [APK]... [boot.oat]"
  log I ""
  log I "This script should be used for getting compile stats for give APK(s) or"
  log I "boot.oat (as a special case) on a target device"
  log I "The script expects a device to be connected. Use --list-devices to show supported"
  log I "devices."
  log I ""
  log I "-------------------------------------------"
  log I " -h, --help                - help"
  log I " -v, --verbose             - verbose"
  log I " --list-devices            - List the devices supported by this script."
  log I " --mode <all|32|64>        - Get compile stats for the specified mode(s)."
  log I "                             (default: all)"
  log I " --cpu <all|big|little|default> - CPU mode."
  log I "                             \"big\": Run with only big cores and pin their frequency"
  log I "                             \"little\": Run with only little cores and pin their"
  log I "                             frequency"
  log I "                             \"all\": With big.LITTLE devices:"
  log I "                             Run consecutively with only little cores enabled and"
  log I "                             pinned,"
  log I "                             and then with only big cores enabled and pinned."
  log I "                             For devices without big.LITTLE, all cores are enabled and"
  log I "                             pinned"
  log I "                             \"default\": Run with unaltered default CPU configuration"
  log I "                             (no pinning)."
  log I "                             (default: all)"
  log I " --iterations <n>          - The number of compilations for each APK."
  log I "                             (default: $default_iterations)"
  log I " --target-device <device>  - Use specific lunch target and configuration for Arm "
  log I "                             target platform".
  log I "                             (default: \"\" - which will cause a default target arch"
  log I "                             to be used."
  log I " --skip-build <false|true> - Skips the build step and compiles APKs with prebuilt"
  log I "                             artifacts from \$OUT directory."
  log I "                             This option is mainly used for automation."
  log I "                             Make sure \`adb\` and \`dx\` are in your PATH when using"
  log I "                             this option."
  log I "                             (default: false)"
  log I " --skip-run <false|true>   - Skips the running benchmark but just builds the artifacts"
  log I "                             (default: false)"
  log I " --dump-cfg                - Dump control-flow graphs. Depending on the mode they will be"
  log I "                             either in bench.arm64.cfg or bench.arm32.cfg."
  log I "-------------------------------------------"
  log I "Default Configuration:"
  log I " --default                 - Default benchmark configuration, equivalent to"
  log I "                             \`--mode all --cpu all --iterations $default_iterations\`."
  log I "-------------------------------------------"
  exit 0
}

list_devices() {
  list_devices_from_config "${local_path}/../devices/config/*"
  exit 0
}

# Arguments:
#   ${1}: isa
#   ${2}: APK name
create_apk_target_cmdline_sh() {
  local isa="${1}"

  apk="${2}"
  apk_basename=$(basename "${apk}" ".apk")

  local dump_cfg=""
  if ${options["dump-cfg"]}; then
    target_cfg_file="/data/local/tmp/${apk_basename}.${1}.cfg"
    dump_cfg="--dump-cfg=${target_cfg_file}"
  fi

  # use --no-watch-dog option to bypass 9.5 minutes timeout in dex2oat
  read -r -d '' target_script << EOM
    $(get_target_art_test_env_vars_exports)
    rm -rf /data/local/tmp/oat/${isa}
    mkdir -p /data/local/tmp/oat/${isa}

    before=\$EPOCHREALTIME
    $(get_target_dex2oat_cmd "${isa}" "/data/local/tmp/${apk_basename}.apk") \
      $(get_target_art_test_bootclasspath_for_dex2oat) -j1 ${dump_cfg} --no-watch-dog
    after=\$EPOCHREALTIME

    echo "Before: "\$before
    echo "After: "\$after
    \$@
EOM

  safe adb_shell "mkdir -p ${ART_TEST_CHROOT}/data/local/tmp/"
  safe adb_shell "echo '${target_script}' > ${ART_TEST_CHROOT}/data/local/tmp/cmdline.sh"
}

create_boot_oat_cmdline_sh() {
  local isa="${1}"

  # use --no-watch-dog option to bypass 9.5 minutes timeout in dex2oat
  read -r -d '' target_script << EOM
    $(get_target_art_test_env_vars_exports)
    rm -rf /data/local/tmp/boot-oat/${isa}
    mkdir -p /data/local/tmp/boot-oat/${isa}

    cd /data/local/tmp/boot-oat/${isa}
    mkdir symbols

    before=\$EPOCHREALTIME
    $(get_target_dex2oat_boot_oat_cmd "${isa}" ".") --no-watch-dog -j1
    after=\$EPOCHREALTIME

    echo "Before: "\$before
    echo "After: "\$after
    \$@
EOM

  safe adb_shell "mkdir -p ${ART_TEST_CHROOT}/data/local/tmp/"
  safe adb_shell "echo '${target_script}' > ${ART_TEST_CHROOT}/data/local/tmp/cmdline.sh"
}

# Arguments:
#   ${1}: bitness
#   ${2}: device
#   ${3}: CPU mode
run_apks() {
  local compilation_stats_file="${LOG_DIRECTORY}/${2}_${1}_${3}_compilation_stats.json"
  if [[ -f "${compilation_stats_file}" ]]; then
    safe rm "${compilation_stats_file}"
  fi

  local isa="arm"
  if [[ "${1}" == "64" ]]; then
    isa+="64"
  fi

  start_adb_section "run_benchmarks_${2}_${1}_${3}"
  for benchmark in "${benchmarks[@]}"; do
    log I "Processing ${benchmark}"

    if [[ ${benchmark} != "boot.oat" ]]; then
      adb push "${benchmark}" "${ART_TEST_CHROOT}/data/local/tmp"
      apk_name=$(basename "${benchmark}")
      apk_basename=$(basename "${apk_name}" ".apk")
      output_oats=("${ART_TEST_CHROOT}/data/local/tmp/oat/${isa}/${apk_basename}.odex")
      create_apk_target_cmdline_sh "${isa}" "${apk_name}"
    else
      apk_name="boot.oat"
      local -r boot_oat_path="${ART_TEST_CHROOT}/data/local/tmp/boot-oat/${isa}"
      output_oats=()
      output_oats+=("${boot_oat_path}/boot.oat")
      output_oats+=("${boot_oat_path}/boot-core-libart.oat")
      output_oats+=("${boot_oat_path}/boot-okhttp.oat")
      output_oats+=("${boot_oat_path}/boot-apache-xml.oat")
      output_oats+=("${boot_oat_path}/boot-bouncycastle.oat")
      create_boot_oat_cmdline_sh "${isa}"
    fi
    export ART_COMMAND="chroot ${ART_TEST_CHROOT} sh /data/local/tmp/cmdline.sh "

    local run_cmd_options=()
    run_cmd_options+=("--target")
    run_cmd_options+=("--iterations" "${options["iterations"]}")
    run_cmd_options+=("--mode" "${1}")
    run_cmd_options+=("--output-json" "${compilation_stats_file}")
    run_cmd_options+=("--output-oat" "${output_oats[@]}")
    run_cmd_options+=("--add-pathname" "${apk_name}")
    run_cmd_options+=("--target-copy-path" "${ART_TEST_CHROOT}/data/local/tmp")
    ./benchmarks/compilation_stats.py "${run_cmd_options[@]}"
  done
  end_adb_section "run_benchmarks_${2}_${1}_${3}" "$?"
}

# Arguments:
#   ${1}: bitness
run_all_apks() {
  local -r bitness="${1}"
  local -r target_device=$(safe adb_shell getprop ro.product.device)
  local -r cpu="${options["cpu"]}"
  local -r path_to_devices="${local_path}/../devices"
  set_freq_and_run run_apks "${bitness}" "${cpu}" "${target_device}" "${path_to_devices}"
}

main() {
  exit_on_failure arguments_parser options_format options benchmarks -- "$@"

  if [[ -z "${benchmarks[@]}" ]]; then
    log E "Empty list of APKs to compile"
    exit 1
  fi

  validate_apk_names

  readonly options
  dump_options

  local -r mode="${options["mode"]}"
  local -r skip_build="${options["skip-build"]}"

  if android_build_already_setup; then
    log E "This test does not support environment targets. Please re-run in a clean environment."
    exit 1
  fi

  for bits in 32 64; do
    if [[ "$mode" != "all" && "$mode" != "$bits" ]]; then
      log I "Skipping ${bits}bit benchmarks."
      continue
    fi
    log I "Starting ${bits}bit benchmarks."

    # Set environment variables.
    set_environment_for_benchmark_run options
    if [[ "${skip_build}" == "false" ]]; then
      # Build target.
      build_target "${bits}"
    else
      setup_android_target_from_bits "${bits}"
    fi

    buildbot_device_prepare "${bits}"
    run_all_apks "${bits}"
  done
}

main "$@"