aboutsummaryrefslogtreecommitdiff
path: root/devices/set_cpu_freq.sh
blob: 1c7249c16ddb2265bde7a729f9ab08972a27d1af (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
#!/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.

# Used for setting the freq of Nexus devices.

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

declare -A options
target_device=

init_options() {
  options["cpus"]="all"
  options["pin-freq"]="false"
  options["default"]="false"
}

validate_options() {
  if ${options["default"]} && ( ${options["pin-freq"]} || [[ "${options["cpus"]}" != "all" ]] ); then
    log E "The option \`--default\` is incompatible with \`--pin-freq\`, \`--big\`, and \`--little\`  options."
    exit 1
  fi
}

usage() {
  log I "$0"
  log I "This script should be used for frequency scaling your device."
  log I " -h|--help       - help"
  log I " -v|--verbose    - verbose"
  log I "-------------------------------------------"
  log I "Cluster:    (does not pin the frequency by default)."
  log I " --all           - Enable all CPUs (default)."
  log I " --big           - Enable only the big cluster in a big.LITTLE SoC."
  log I " --little        - Enable only the little cluster in a big.LITTLE SoC."
  log I ""
  log I " --pin-freq      - Pin the frequency of enabled cores."
  log I " --default       - Restore the CPUs to the default configuration."
  log I ""
  log I "Example Usage: ./set_cpu_freq.sh --big --pin-freq"
  log I "-------------------------------------------"
}

arguments_parser() {
  init_options
  while [[ $# -gt 0 ]]; do
    case "$1" in
      -v|--verbose)
        enable_verbose
        ;;
      -h|--help)
        usage
        exit 0
        ;;
      --all|--big|--little)
        set_option "cpus" "${1#--}"
        ;;
      --pin-freq|--default)
        set_option "$1"
        ;;
      *)
        log E "Invalid option: $1"
        exit 1
        ;;
    esac
    shift
  done
  readonly options
  validate_options
}

print_freq() {
  printf "%'u Hz" "${TARGET_FREQ}"
}

set_device_freq() {
  # We need root for writing to /sys files.
  adb_root

  if ! ${DEVICE_IS_BIG_LITTLE} && [[ ${options["cpus"]} != "all" ]]; then
    log E "Device ${target_device} is single-cluster, do not use \`--big\` or \`little\` options."
  fi

  if ${options["default"]}; then
    log I "Restoring CPUs configuration to defaults."
    set_default_cpu_config
    exit 0
  fi

  case ${options["cpus"]} in
    all)
      log I "Enabling all CPUs."
      safe enable_cpus "${CPUS[@]}"
      ;;
    big)
      log I "Enabling only the big cluster."
      safe enable_cpus "${BIG_CPUS[@]}"
      safe disable_cpus "${LITTLE_CPUS[@]}"
      ;;
    little)
      log I "Enabling only the little cluster."
      safe enable_cpus "${LITTLE_CPUS[@]}"
      safe disable_cpus "${BIG_CPUS[@]}"
      ;;
  esac

  if ${options["pin-freq"]}; then
    if ! ${DEVICE_IS_BIG_LITTLE}; then
      log I "Pinning the frequency on all cores."
      safe set_freq_cpus "${TARGET_FREQ}" "${CPUS[@]}"
    else
      case ${options["cpus"]} in
        big|all)
          log I "Pinning the frequency on the big cores."
          safe set_freq_cpus "${TARGET_FREQ_BIG:-${TARGET_FREQ}}" "${BIG_CPUS[@]}"
          ;;&
        little|all)
          log I "Pinning the frequency on the little cores."
          safe set_freq_cpus "${TARGET_FREQ_LITTLE:-${TARGET_FREQ}}" "${LITTLE_CPUS[@]}"
          ;;
      esac
    fi
  fi
}

main() {
  arguments_parser "$@"

  # Make sure that adb is started before checking the board name.
  safe adb start-server

  readonly target_device=$(safe adb_shell getprop ro.product.device)
  log I "Detected device ${target_device}"

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

  set_device_freq
}

main "$@"