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 |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 12 | GPU_CLOCK_RATE = 315000000 |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 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) |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 29 | if not self._adb.is_root(): |
Chris Dalton | 49b7ed3 | 2017-10-23 17:19:37 -0600 | [diff] [blame] | 30 | return self |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 31 | |
Kevin Lubick | 4f16db6 | 2018-03-01 10:13:44 -0500 | [diff] [blame] | 32 | self._adb.shell('\n'.join([ |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 33 | # enable and lock the two fast cores. |
| 34 | ''' |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 35 | stop thermal-engine |
| 36 | stop perfd |
| 37 | |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 38 | for N in 3 2; do |
| 39 | echo 1 > /sys/devices/system/cpu/cpu$N/online |
| 40 | echo userspace > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_governor |
| 41 | echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_max_freq |
| 42 | echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_min_freq |
| 43 | echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_setspeed |
| 44 | done''' % tuple(CPU_CLOCK_RATE for _ in range(3)), |
| 45 | |
| 46 | # turn off the two slow cores |
| 47 | ''' |
| 48 | for N in 1 0; do |
| 49 | echo 0 > /sys/devices/system/cpu/cpu$N/online |
| 50 | done''', |
| 51 | |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 52 | # pylint: disable=line-too-long |
| 53 | |
| 54 | # Set GPU bus and idle timer |
| 55 | # Set DDR frequency to max |
| 56 | # Set GPU to performance mode, 315 MHZ |
| 57 | # See https://android.googlesource.com/platform/frameworks/base/+/master/libs/hwui/tests/scripts/prep_marlfish.sh |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 58 | ''' |
| 59 | echo 0 > /sys/class/kgsl/kgsl-3d0/bus_split |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 60 | echo 1 > /sys/class/kgsl/kgsl-3d0/force_clk_on |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 61 | echo 10000 > /sys/class/kgsl/kgsl-3d0/idle_timer |
| 62 | |
| 63 | echo 13763 > /sys/class/devfreq/soc:qcom,gpubw/min_freq |
| 64 | |
| 65 | echo performance > /sys/class/kgsl/kgsl-3d0/devfreq/governor |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 66 | echo %i > /sys/class/kgsl/kgsl-3d0/devfreq/max_freq |
| 67 | echo %i > /sys/class/kgsl/kgsl-3d0/devfreq/min_freq |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 68 | |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 69 | echo 4 > /sys/class/kgsl/kgsl-3d0/max_pwrlevel |
| 70 | echo 4 > /sys/class/kgsl/kgsl-3d0/min_pwrlevel''' % |
| 71 | tuple(GPU_CLOCK_RATE for _ in range(2))])) |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 72 | |
Chris Dalton | 49b7ed3 | 2017-10-23 17:19:37 -0600 | [diff] [blame] | 73 | return self |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 74 | |
| 75 | def sanity_check(self): |
| 76 | HardwareAndroid.sanity_check(self) |
| 77 | |
| 78 | if not self._adb.is_root(): |
| 79 | return |
| 80 | |
| 81 | result = self._adb.check(' '.join( |
| 82 | ['cat', |
| 83 | '/sys/class/power_supply/battery/capacity', |
| 84 | '/sys/devices/system/cpu/online'] + \ |
| 85 | ['/sys/devices/system/cpu/cpu%i/cpufreq/scaling_cur_freq' % i |
| 86 | for i in range(2, 4)] + \ |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 87 | ['/sys/kernel/debug/clk/bimc_clk/measure', |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 88 | '/sys/class/thermal/thermal_zone22/temp', |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 89 | '/sys/class/thermal/thermal_zone23/temp'])) |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 90 | |
| 91 | expectations = \ |
| 92 | [Expectation(int, min_value=30, name='battery', sleeptime=30*60), |
| 93 | Expectation(str, exact_value='2-3', name='online cpus')] + \ |
| 94 | [Expectation(int, exact_value=CPU_CLOCK_RATE, name='cpu_%i clock rate' %i) |
| 95 | for i in range(2, 4)] + \ |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 96 | [Expectation(long, min_value=902390000, max_value=902409999, |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 97 | name='measured ddr clock', sleeptime=10), |
| 98 | Expectation(int, max_value=41000, name='pm8994_tz temperature'), |
Kevin Lubick | 08b44fa | 2018-03-07 08:22:53 -0500 | [diff] [blame^] | 99 | Expectation(int, max_value=40, name='msm_therm temperature')] |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 100 | |
| 101 | Expectation.check_all(expectations, result.splitlines()) |
| 102 | |
| 103 | def _discover_devfreqs(self): |
| 104 | self._devfreq_lock_cmds = list() |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 105 | self._devfreq_sanity_knobs = list() |
| 106 | self._devfreq_sanity_expectations = list() |
| 107 | |
| 108 | results = iter(self._adb.check('''\ |
| 109 | KNOBS='%s' |
| 110 | for DEVICE in %s/*; do |
| 111 | if cd $DEVICE && ls $KNOBS >/dev/null; then |
| 112 | basename $DEVICE |
| 113 | cat $KNOBS |
| 114 | fi |
| 115 | done 2>/dev/null''' % |
| 116 | (' '.join(DevfreqKnobs._fields), DEVFREQ_DIRNAME)).splitlines()) |
| 117 | |
| 118 | while True: |
| 119 | batch = tuple(itertools.islice(results, 1 + len(DevfreqKnobs._fields))) |
| 120 | if not batch: |
| 121 | break |
| 122 | |
| 123 | devfreq = batch[0] |
| 124 | if devfreq in DEVFREQ_BLACKLIST: |
| 125 | continue |
| 126 | |
| 127 | path = '%s/%s' % (DEVFREQ_DIRNAME, devfreq) |
| 128 | |
| 129 | knobs = DevfreqKnobs(*batch[1:]) |
| 130 | if not 'performance' in knobs.available_governors.split(): |
| 131 | print('WARNING: devfreq %s does not have performance governor' % path) |
| 132 | continue |
| 133 | |
| 134 | self._devfreq_lock_cmds.append('echo performance > %s/governor' % path) |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 135 | |
| 136 | frequencies = map(int, knobs.available_frequencies.split()) |
| 137 | if frequencies: |
| 138 | # choose the lowest frequency that is >= DEVFREQ_THROTTLE * max. |
| 139 | frequencies.sort() |
| 140 | target = DEVFREQ_THROTTLE * frequencies[-1] |
| 141 | idx = len(frequencies) - 1 |
| 142 | while idx > 0 and frequencies[idx - 1] >= target: |
| 143 | idx -= 1 |
| 144 | bench_frequency = frequencies[idx] |
| 145 | self._devfreq_lock_cmds.append('echo %i > %s/min_freq' % |
| 146 | (bench_frequency, path)) |
| 147 | self._devfreq_lock_cmds.append('echo %i > %s/max_freq' % |
| 148 | (bench_frequency, path)) |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 149 | self._devfreq_sanity_knobs.append('%s/cur_freq' % path) |
| 150 | self._devfreq_sanity_expectations.append( |
| 151 | Expectation(int, exact_value=bench_frequency, |
| 152 | name='%s/cur_freq' % path)) |