Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame^] | 1 | # Copyright 2017 Google Inc. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | from _hardware import HardwareException, Expectation |
| 7 | from _hardware_android import HardwareAndroid |
| 8 | from collections import namedtuple |
| 9 | import itertools |
| 10 | |
| 11 | CPU_CLOCK_RATE = 1670400 |
| 12 | GPU_CLOCK_RATE = 510000000 |
| 13 | |
| 14 | DEVFREQ_DIRNAME = '/sys/class/devfreq' |
| 15 | DEVFREQ_THROTTLE = 0.74 |
| 16 | DEVFREQ_BLACKLIST = ('b00000.qcom,kgsl-3d0', 'soc:qcom,kgsl-busmon', |
| 17 | 'soc:qcom,m4m') |
| 18 | DevfreqKnobs = namedtuple('knobs', |
| 19 | ('available_governors', 'available_frequencies', |
| 20 | 'governor', 'min_freq', 'max_freq', 'cur_freq')) |
| 21 | |
| 22 | class HardwarePixel(HardwareAndroid): |
| 23 | def __init__(self, adb): |
| 24 | HardwareAndroid.__init__(self, adb) |
| 25 | self._discover_devfreqs() |
| 26 | |
| 27 | def __enter__(self): |
| 28 | HardwareAndroid.__enter__(self) |
| 29 | self._lock_clocks() |
| 30 | return self |
| 31 | |
| 32 | def __exit__(self, exception_type, exception_value, exception_traceback): |
| 33 | # pixel struggles waking up; just pull a hard reboot. |
| 34 | self._adb.reboot() |
| 35 | |
| 36 | def _lock_clocks(self): |
| 37 | if not self._adb.is_root(): |
| 38 | return |
| 39 | |
| 40 | self._adb.shell('\n'.join(['''\ |
| 41 | stop thermal-engine |
| 42 | stop thermald |
| 43 | stop perfd |
| 44 | stop mpdecision''', |
| 45 | |
| 46 | # enable and lock the two fast cores. |
| 47 | ''' |
| 48 | for N in 3 2; do |
| 49 | echo 1 > /sys/devices/system/cpu/cpu$N/online |
| 50 | echo userspace > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_governor |
| 51 | echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_max_freq |
| 52 | echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_min_freq |
| 53 | echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_setspeed |
| 54 | done''' % tuple(CPU_CLOCK_RATE for _ in range(3)), |
| 55 | |
| 56 | # turn off the two slow cores |
| 57 | ''' |
| 58 | for N in 1 0; do |
| 59 | echo 0 > /sys/devices/system/cpu/cpu$N/online |
| 60 | done''', |
| 61 | |
| 62 | # gpu perf commands from |
| 63 | # https://developer.qualcomm.com/qfile/28823/lm80-p0436-11_adb_commands.pdf |
| 64 | ''' |
| 65 | echo 0 > /sys/class/kgsl/kgsl-3d0/bus_split |
| 66 | echo 1 > /sys/class/kgsl/kgsl-3d0/force_bus_on |
| 67 | echo 1 > /sys/class/kgsl/kgsl-3d0/force_rail_on |
| 68 | echo 1 > /sys/class/kgsl/kgsl-3d0/force_clk_on |
| 69 | echo 1000000 > /sys/class/kgsl/kgsl-3d0/idle_timer |
| 70 | echo userspace > /sys/class/kgsl/kgsl-3d0/devfreq/governor |
| 71 | echo 2 > /sys/class/kgsl/kgsl-3d0/max_pwrlevel |
| 72 | echo 2 > /sys/class/kgsl/kgsl-3d0/min_pwrlevel |
| 73 | echo 2 > /sys/class/kgsl/kgsl-3d0/thermal_pwrlevel |
| 74 | echo %i > /sys/class/kgsl/kgsl-3d0/devfreq/max_freq |
| 75 | echo %i > /sys/class/kgsl/kgsl-3d0/devfreq/min_freq |
| 76 | echo %i > /sys/class/kgsl/kgsl-3d0/max_gpuclk |
| 77 | echo %i > /sys/class/kgsl/kgsl-3d0/gpuclk''' % |
| 78 | tuple(GPU_CLOCK_RATE for _ in range(4))] + \ |
| 79 | |
| 80 | self._devfreq_lock_cmds)) |
| 81 | |
| 82 | def _unlock_clocks(self): |
| 83 | if not self._adb.is_root(): |
| 84 | return |
| 85 | |
| 86 | self._adb.shell('\n'.join( |
| 87 | self._devfreq_unlock_cmds + [ |
| 88 | |
| 89 | # restore gpu settings to default. |
| 90 | ''' |
| 91 | echo 133000000 > /sys/class/kgsl/kgsl-3d0/devfreq/min_freq |
| 92 | echo 600000000 > /sys/class/kgsl/kgsl-3d0/devfreq/max_freq |
| 93 | echo 0 > /sys/class/kgsl/kgsl-3d0/gpuclk |
| 94 | echo msm-adreno-tz > /sys/class/kgsl/kgsl-3d0/devfreq/governor |
| 95 | echo 6 > /sys/class/kgsl/kgsl-3d0/min_pwrlevel |
| 96 | echo 0 > /sys/class/kgsl/kgsl-3d0/max_pwrlevel |
| 97 | echo 1 > /sys/class/kgsl/kgsl-3d0/thermal_pwrlevel |
| 98 | echo 0 > /sys/class/kgsl/kgsl-3d0/idle_timer |
| 99 | echo 0 > /sys/class/kgsl/kgsl-3d0/force_clk_on |
| 100 | echo 0 > /sys/class/kgsl/kgsl-3d0/force_rail_on |
| 101 | echo 0 > /sys/class/kgsl/kgsl-3d0/force_bus_on |
| 102 | echo 1 > /sys/class/kgsl/kgsl-3d0/bus_split''', |
| 103 | |
| 104 | # turn the disabled cores back on. |
| 105 | ''' |
| 106 | for N in 0 1; do |
| 107 | echo 1 > /sys/devices/system/cpu/cpu$N/online |
| 108 | done''', |
| 109 | |
| 110 | # unlock the 2 enabled big cores. |
| 111 | ''' |
| 112 | for N in 2 3; do |
| 113 | echo 307200 > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_min_freq |
| 114 | echo 2150400 > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_max_freq |
| 115 | echo 0 > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_setspeed |
| 116 | echo sched > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_governor |
| 117 | done''', |
| 118 | |
| 119 | ''' |
| 120 | start mpdecision |
| 121 | start perfd |
| 122 | start thermald |
| 123 | start thermal-engine'''])) |
| 124 | |
| 125 | def sanity_check(self): |
| 126 | HardwareAndroid.sanity_check(self) |
| 127 | |
| 128 | if not self._adb.is_root(): |
| 129 | return |
| 130 | |
| 131 | result = self._adb.check(' '.join( |
| 132 | ['cat', |
| 133 | '/sys/class/power_supply/battery/capacity', |
| 134 | '/sys/devices/system/cpu/online'] + \ |
| 135 | ['/sys/devices/system/cpu/cpu%i/cpufreq/scaling_cur_freq' % i |
| 136 | for i in range(2, 4)] + \ |
| 137 | ['/sys/class/kgsl/kgsl-3d0/thermal_pwrlevel', |
| 138 | '/sys/kernel/debug/clk/gpu_gx_gfx3d_clk/measure', |
| 139 | '/sys/kernel/debug/clk/bimc_clk/measure', |
| 140 | '/sys/class/thermal/thermal_zone22/temp', |
| 141 | '/sys/class/thermal/thermal_zone23/temp'] + \ |
| 142 | self._devfreq_sanity_knobs)) |
| 143 | |
| 144 | expectations = \ |
| 145 | [Expectation(int, min_value=30, name='battery', sleeptime=30*60), |
| 146 | Expectation(str, exact_value='2-3', name='online cpus')] + \ |
| 147 | [Expectation(int, exact_value=CPU_CLOCK_RATE, name='cpu_%i clock rate' %i) |
| 148 | for i in range(2, 4)] + \ |
| 149 | [Expectation(int, exact_value=2, name='gpu thermal power level'), |
| 150 | Expectation(long, min_value=(GPU_CLOCK_RATE - 5000), |
| 151 | max_value=(GPU_CLOCK_RATE + 5000), |
| 152 | name='measured gpu clock'), |
| 153 | Expectation(long, min_value=902390000, max_value=902409999, |
| 154 | name='measured ddr clock', sleeptime=10), |
| 155 | Expectation(int, max_value=41000, name='pm8994_tz temperature'), |
| 156 | Expectation(int, max_value=40, name='msm_therm temperature')] + \ |
| 157 | self._devfreq_sanity_expectations |
| 158 | |
| 159 | Expectation.check_all(expectations, result.splitlines()) |
| 160 | |
| 161 | def _discover_devfreqs(self): |
| 162 | self._devfreq_lock_cmds = list() |
| 163 | self._devfreq_unlock_cmds = list() |
| 164 | self._devfreq_sanity_knobs = list() |
| 165 | self._devfreq_sanity_expectations = list() |
| 166 | |
| 167 | results = iter(self._adb.check('''\ |
| 168 | KNOBS='%s' |
| 169 | for DEVICE in %s/*; do |
| 170 | if cd $DEVICE && ls $KNOBS >/dev/null; then |
| 171 | basename $DEVICE |
| 172 | cat $KNOBS |
| 173 | fi |
| 174 | done 2>/dev/null''' % |
| 175 | (' '.join(DevfreqKnobs._fields), DEVFREQ_DIRNAME)).splitlines()) |
| 176 | |
| 177 | while True: |
| 178 | batch = tuple(itertools.islice(results, 1 + len(DevfreqKnobs._fields))) |
| 179 | if not batch: |
| 180 | break |
| 181 | |
| 182 | devfreq = batch[0] |
| 183 | if devfreq in DEVFREQ_BLACKLIST: |
| 184 | continue |
| 185 | |
| 186 | path = '%s/%s' % (DEVFREQ_DIRNAME, devfreq) |
| 187 | |
| 188 | knobs = DevfreqKnobs(*batch[1:]) |
| 189 | if not 'performance' in knobs.available_governors.split(): |
| 190 | print('WARNING: devfreq %s does not have performance governor' % path) |
| 191 | continue |
| 192 | |
| 193 | self._devfreq_lock_cmds.append('echo performance > %s/governor' % path) |
| 194 | self._devfreq_unlock_cmds.append('echo %s > %s/governor' % |
| 195 | (knobs.governor, path)) |
| 196 | |
| 197 | frequencies = map(int, knobs.available_frequencies.split()) |
| 198 | if frequencies: |
| 199 | # choose the lowest frequency that is >= DEVFREQ_THROTTLE * max. |
| 200 | frequencies.sort() |
| 201 | target = DEVFREQ_THROTTLE * frequencies[-1] |
| 202 | idx = len(frequencies) - 1 |
| 203 | while idx > 0 and frequencies[idx - 1] >= target: |
| 204 | idx -= 1 |
| 205 | bench_frequency = frequencies[idx] |
| 206 | self._devfreq_lock_cmds.append('echo %i > %s/min_freq' % |
| 207 | (bench_frequency, path)) |
| 208 | self._devfreq_lock_cmds.append('echo %i > %s/max_freq' % |
| 209 | (bench_frequency, path)) |
| 210 | self._devfreq_unlock_cmds.append('echo %s > %s/min_freq' % |
| 211 | (knobs.min_freq, path)) |
| 212 | self._devfreq_unlock_cmds.append('echo %s > %s/max_freq' % |
| 213 | (knobs.max_freq, path)) |
| 214 | self._devfreq_sanity_knobs.append('%s/cur_freq' % path) |
| 215 | self._devfreq_sanity_expectations.append( |
| 216 | Expectation(int, exact_value=bench_frequency, |
| 217 | name='%s/cur_freq' % path)) |