aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/calibrate_benchmark_target.sh
blob: 50f2c57365aa6e96518ec45e9d8301bf4325b6bf (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
#!/bin/bash
#
# Copyright (c) 2020, 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.

# Get a number of iterations needed for benchmark methods to have the specified running time
# on a target.

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 art="$(get_art_dir)"
readonly art_tools="$(get_art_tools_dir)"

readonly target_virtual_work_dir="$(get_target_virtual_work_dir)"

# It is initialized in the set_environment_for_run function.
target_physical_work_dir=""

readonly target_cmdline="${target_virtual_work_dir}/cmdline.sh"

# The default running time in milliseconds for benchmark the calibration will target.
# It is chosen to be 1 second (1000 ms).
readonly default_target_runnig_time="1000"

declare -A options

init_options() {
  set_default_options
}

set_default_options() {
  options["cpu"]=""
  options["classpath"]=""
  options["target_running_time"]="${default_target_runnig_time}"
  options["benchmark"]=""
}

validate_options() {
  validate_cluster "${options["cpu"]}"
  validate_benchmarks_names "${options["benchmark"]}"
}

usage() {
  log I "Usage: $0 [OPTION]... BENCHMARK"
  log I ""
  log I "This script can be used to get a number of iterations needed for benchmark methods to"
  log I "have the specified running time on a target device."
  log I ""
  log I "-------------------------------------------"
  log I " -h, --help              - help"
  log I " --cpu <cluster_name>    - Cluster name."
  log I "                             Run dalvikvm on the cluster specified by"
  log I "                             cluster_name."
  log I "                             Note: The frequency of the specified cores will be pinned."
  log I " --classpath <classpath> - Classpath for dalvikvm on a target device."
  log I " --target_running_time <time in ms>"
  log I "                         - The target running time for benchmark methods."
  log I "                           The default value is 1000 ms."
  log I "-------------------------------------------"
}

arguments_parser() {
  init_options

  while [[ $# -gt 0 ]]; do
    local option=$1
    case "${option}" in
      -h|--help)
        usage
        exit 0
        ;;
      --cpu|--classpath|--target_running_time)
        shift
        set_option "${option}" "$1"
        ;;
      --*)
        log E "Invalid option: $1"
        exit 1
        ;;
      *)
        break
        ;;
    esac
    shift
  done

  options["benchmark"]="$1"

  readonly options
  validate_options
}

set_environment_for_run() {
  set_environment_target

  log I "Retrieve the target device using adb getprop..."
  prepare_adb
  local target_device="$(retrieve_target_product_name)"
  log I "Target device to be used: ${target_device}."
  select_android_target "arm" "${target_device}"
  target_physical_work_dir="$(get_target_physical_dir "${target_virtual_work_dir}")"
  readonly target_physical_work_dir
  safe adb_shell "mkdir -p ${target_physical_work_dir}"
}

run_target_cmdline() {
  local -r sh="chroot ${ART_TEST_CHROOT} sh"
  local -r cpu="${options["cpu"]}"

  safe "${local_path}/../devices/set_cpu_freq.sh" --cpu "${cpu}" --pin-freq

  echo -n "CALIBRATION_RESULT:"
  adb_shell "${sh}" "${target_virtual_work_dir}/cmdline.sh"
  echo ""

  safe "${local_path}/../devices/set_cpu_freq.sh" --default
}

generate_target_cmdline() {
  local -r work_dir="${target_virtual_work_dir}"
  local -r target_device=$(retrieve_target_product_name)
  local -r cluster_name="${options["cpu"]}"
  local -r classpath="${options["classpath"]}"
  local -r target_running_time="${options["target_running_time"]}"
  local -r benchmark="${options["benchmark"]}"

  exit_on_failure get_device_settings "${target_device}" "${local_path}/../devices/config"

  local -r -a cluster=(${!CPUS[${cluster_name}]})
  local -r -a cpu_ids=($(get_ids_from_cluster "${cluster[*]}"))
  local -r dalvikvm_cpus="$(get_cpu_affinity_mask "${cpu_ids[@]}")"

  local target_script
  read -r -d '' target_script << EOM
$(get_target_art_test_env_vars_exports)

cd ${work_dir}

taskset ${dalvikvm_cpus} $(get_target_art_test_dalvikvm_cmd "${work_dir}" "${classpath}") \
  org.linaro.bench.CalibrateBench --target_running_time ${target_running_time} ${benchmark}
EOM

  safe adb_shell "echo '${target_script}' > ${target_physical_work_dir}/cmdline.sh"
}

main() {
  arguments_parser "$@"
  dump_options
  set_environment_for_run
  generate_target_cmdline
  run_target_cmdline
}

main "$@"