aboutsummaryrefslogtreecommitdiff
path: root/devices/cpu_freq_utils.sh
blob: 0d75c5ac8884c3eca00f562e604b4b93db18c3e9 (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
#!/bin/bash
#
# Copyright (c) 2016-2017, 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 sys_cpu_path="/sys/devices/system/cpu"

# Check that the device has given CPUs.
# Arguments:
#   ${*} - IDs of the CPUs to check.
validate_cpus() {
  local -r present_cpus_range=$(safe adb_shell "cat ${sys_cpu_path}/present")
  local -r int_regex='^[0-9]+$'
  local -r range_regex='^0-([0-9]+)$'
  if [[ ${present_cpus_range} =~ ${range_regex} ]]; then
    # Max CPU ID, captured by the regex.
    local -r max_cpu=${BASH_REMATCH[1]}
    local cpu
    for cpu; do
      if ! [[ ${cpu} =~ ${int_regex} && ${cpu} -le ${max_cpu} ]]; then
        log E "Bad CPU: ${cpu} (max ${max_cpu})"
        return 1
      fi
    done
  else
    log E "Unexpected present CPUs: ${present_cpus_range}"
    return 1
  fi
  return 0
}

# Turn CPUs on or off through hotplug.
# Arguments:
#   ${1}   - 1 to enable, 0 to disable.
#   ${2-#} - IDs of the CPU.
set_state_cpus() {
  local -r state=$1
  shift
  validate_cpus "$@" || return 1

  local -r max_trials=10
  for cpu; do
    local online_file="${sys_cpu_path}/cpu${cpu}/online"
    # Check whether the CPU has an associated `online` file.
    if [[ $(safe adb_shell "[[ -f ${online_file} ]] && echo y || echo n") == "n" ]]; then
      # Only raise an error if we were trying to disable CPU.
      if [[ $state -eq 0 ]]; then
        log E "Could not write to $online_file"
      fi
    else
      local new_state trial
      adb_shell "echo ${state} > $online_file"
      new_state=$(safe adb_shell "cat $online_file")
      for ((trial=1; trial <= max_trials && new_state != state; trial++)); do
        # There might be a kthread hotplugging CPUs on and off (for example the
        # thermal governor if the temperature is high), so let us wait for the
        # device to cool down and retry.
        log W "Failed to set CPU ${cpu} to state ${state}, retrying ${trial}..."
        safe sleep 100s
        adb_shell "echo ${state} > $online_file"
        new_state=$(safe adb_shell "cat $online_file")
      done
      # If after 10 trials there is still a failure, abort with an error.
      if [[ "${new_state}" != "${state}" ]]; then
        log E "Failed to set CPU ${cpu} to state ${state}, after ${trial} trials"
        return 1
      fi
    fi
  done

  return 0
}

# Turn on CPUs through hotplug.
# Arguments:
#   ${*} - IDs of the CPU to enable.
enable_cpus() {
  set_state_cpus 1 "$@"
}

# Turn off CPUs through hotplug.
# Arguments:
#   ${*} - IDs of the CPU to disable.
disable_cpus() {
  set_state_cpus 0 "$@"
}

# Set CPU frequency through cpufreq.
# Arguments:
#   ${1}   - frequency (Hz).
#   ${2-#} - IDs of the CPU.
set_freq_cpus() {
  local -r freq=$1
  shift
  validate_cpus "$@" || return 1

  # Ensure boost is disabled on supported platforms
  local -r boost_file="${sys_cpu_path}/cpufreq/boost"
  safe adb_shell "if [[ -f ${boost_file} ]]; then echo 0 > ${boost_file}; fi"
  # Check the frequency is available on all given CPUs.
  local avail_freq
  # Available frequencies are a space-separated list, use \b to ensure word
  # boundaries.
  local -r freq_regex="\b${freq}\b"
  for cpu; do
    # The file might not exist depending on the exact cpufreq driver, in this
    # case we just skip the check.
    avail_freq=$(adb_shell "cat \
      $sys_cpu_path/cpu$cpu/cpufreq/scaling_available_frequencies 2> /dev/null")

    if ! [[ ${avail_freq} ]]; then
      log W "Skipping frequency check"
      break
    elif ! [[ ${avail_freq} =~ ${freq_regex} ]]; then
      log E "CPU ${cpu} does not support frequency ${freq} (available: ${avail_freq})"
      return 1
    fi
  done

  # Set the governor to userspace (fixed frequency) and the frequency.
  local output=
  for cpu; do
    # Like in set_state_cpus, we cannot rely on adb shell to forward return
    # status, so let's check that we don't get any unexpected message.
    output+=$(safe adb_shell "echo userspace > \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/scaling_governor")
    output+=$(safe adb_shell "echo $freq > \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/scaling_setspeed")
    output+=$(safe adb_shell "echo $freq > \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/scaling_max_freq")
    output+=$(safe adb_shell "echo $freq > \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/scaling_min_freq")
  done

  if [[ ${output} ]]; then
    log E "${output}"
    return 1
  else
    return 0
  fi
}

# Define environment variables describing the device.
# Arguments:
#   ${1} - name of the device.
#   ${2} - path to the directory where device scripts are stored.
get_device_settings() {
  local -r cpu_freq_script="${2}/$1.sh"

  if [[ ! -f ${cpu_freq_script} ]]; then
    log E "Device $1 is not supported."
    log E "Please check that ${cpu_freq_script} exists."
    return 1
  fi

  safe source "${cpu_freq_script}"
}

# Set back the cpu(s) to interactive governor using cpus min and
# max frequency read from the cpufreq interface.
#   ${1-#} - CPU indexes
configure_cpus_to_default_governor() {
  local output=
  for cpu; do
    local min_freq=$(safe adb_shell "cat \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/cpuinfo_min_freq 2> /dev/null")
    local max_freq=$(safe adb_shell "cat \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/cpuinfo_max_freq 2> /dev/null")
    output+=$(safe adb_shell "echo ${DEFAULT_GOVERNOR} > \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/scaling_governor")
    # `scaling_max_freq` must be set before `scaling_min_freq`. See
    # https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt.
    output+=$(safe adb_shell "echo $max_freq > \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/scaling_max_freq")
    output+=$(safe adb_shell "echo $min_freq > \
      ${sys_cpu_path}/cpu${cpu}/cpufreq/scaling_min_freq")
  done

  if [[ ${output} ]]; then
    log E "${output}"
    return 1
  else
    return 0
  fi
}

set_default_cpu_config() {
  validate_cpus "$@" || return 1

  if ${DEVICE_IS_BIG_LITTLE}; then
    safe enable_cpus "${BIG_CPUS[@]}"
    safe enable_cpus "${LITTLE_CPUS[@]}"
    safe configure_cpus_to_default_governor "${BIG_CPUS[@]}"
    safe configure_cpus_to_default_governor "${LITTLE_CPUS[@]}"
  else
    safe enable_cpus "${CPUS[@]}"
    safe configure_cpus_to_default_governor "${CPUS[@]}"
  fi
}

# Sets the CPU frequency and calls the function passed as an argument
# Arguments:
#   ${1} - function to run
#   ${2} - CPU mode (bitness)
#   ${3} - target device
#   ${4} - path to devices directory
set_freq_and_run() {
  local -r function_to_run="$1"
  local -r mode="$2"
  local -r cpu_freq="$3"
  local -r target_device="$4"
  local -r path_to_devices="$5"

  if [[ "$cpu" == "default" ]]; then
    "${function_to_run}" "${mode}" "${target_device}" "default-cpu"
    return
  fi

  exit_on_failure get_device_settings "${target_device}" "${path_to_devices}/config"

  if ${DEVICE_IS_BIG_LITTLE}; then
    if [[ "$cpu_freq" == "little" || "$cpu_freq" == "all" ]]; then
      safe "${path_to_devices}/set_cpu_freq.sh" --little --pin-freq
      "${function_to_run}" "${mode}" "${target_device}" "${LITTLE_CPUS_NAME}"
    fi
    if [[ "$cpu_freq" == "big" || "$cpu_freq" == "all" ]]; then
      safe "${path_to_devices}/set_cpu_freq.sh" --big --pin-freq
      "${function_to_run}" "${mode}" "${target_device}" "${BIG_CPUS_NAME}"
    fi
  else
    if [[ "$cpu_freq" == "big" || "$cpu_freq" == "little" ]]; then
      log E "Options \`--big\` and \`--little\` are only valid for big.LITTLE devices."
      exit 1
    fi
    safe "${path_to_devices}/set_cpu_freq.sh" --all --pin-freq
    "${function_to_run}" "${mode}" "${target_device}" "${CPUS_NAME}"
  fi
  safe "${path_to_devices}/set_cpu_freq.sh" --default
}