1cb93a386Sopenharmony_ci# Copyright 2016 Google Inc.
2cb93a386Sopenharmony_ci#
3cb93a386Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
4cb93a386Sopenharmony_ci# found in the LICENSE file.
5cb93a386Sopenharmony_ci
6cb93a386Sopenharmony_cifrom _hardware import HardwareException, Expectation
7cb93a386Sopenharmony_cifrom _hardware_android import HardwareAndroid
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ciCPU_CLOCK_RATE = 1326000
10cb93a386Sopenharmony_ci# If you run adb cat /sys/devices/57000000.gpu/pstate it shows all
11cb93a386Sopenharmony_ci# possible configurations, with a * next to the current one.
12cb93a386Sopenharmony_ciGPU_EMC_PROFILE = '04: core 307 MHz emc 1065 MHz a A d D *'
13cb93a386Sopenharmony_ciGPU_EMC_PROFILE_ID = '04'
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ciclass HardwarePixelC(HardwareAndroid):
16cb93a386Sopenharmony_ci  def __init__(self, adb):
17cb93a386Sopenharmony_ci    HardwareAndroid.__init__(self, adb)
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ci  def __enter__(self):
20cb93a386Sopenharmony_ci    HardwareAndroid.__enter__(self)
21cb93a386Sopenharmony_ci    if not self._adb.is_root():
22cb93a386Sopenharmony_ci      return self
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    self._adb.shell('\n'.join([
25cb93a386Sopenharmony_ci      # pylint: disable=line-too-long
26cb93a386Sopenharmony_ci      # Based on https://android.googlesource.com/platform/frameworks/base/+/master/libs/hwui/tests/scripts/prep_ryu.sh
27cb93a386Sopenharmony_ci      # All CPUs have the same scaling settings, so we only need to set it once
28cb93a386Sopenharmony_ci      '''
29cb93a386Sopenharmony_ci      stop thermal-engine
30cb93a386Sopenharmony_ci      stop perfd
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci      echo userspace > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
33cb93a386Sopenharmony_ci      echo %i > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
34cb93a386Sopenharmony_ci      echo %i > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
35cb93a386Sopenharmony_ci      echo %i > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
36cb93a386Sopenharmony_ci      ''' % tuple(CPU_CLOCK_RATE for _ in range(3)),
37cb93a386Sopenharmony_ci      # turn off the fourth core. This will hopefully produce less heat, allowing
38cb93a386Sopenharmony_ci      # for more consistent results.  3 cores should be enough to run Ganesh,
39cb93a386Sopenharmony_ci      # the graphics driver, and the OS.
40cb93a386Sopenharmony_ci      '''
41cb93a386Sopenharmony_ci      echo 0 > /sys/devices/system/cpu/cpu3/online''',
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_ci      # lock gpu/emc clocks.
44cb93a386Sopenharmony_ci      '''
45cb93a386Sopenharmony_ci      chown root:root /sys/devices/57000000.gpu/pstate
46cb93a386Sopenharmony_ci      echo %s > /sys/devices/57000000.gpu/pstate''' % GPU_EMC_PROFILE_ID]))
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    return self
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ci  def filter_line(self, line):
51cb93a386Sopenharmony_ci    JUNK = ['NvRmPrivGetChipPlatform: Could not read platform information',
52cb93a386Sopenharmony_ci            'Expected on kernels without fuse support, using silicon']
53cb93a386Sopenharmony_ci    return False if line in JUNK else HardwareAndroid.filter_line(self, line)
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci  def sanity_check(self):
56cb93a386Sopenharmony_ci    HardwareAndroid.sanity_check(self)
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    if not self._adb.is_root():
59cb93a386Sopenharmony_ci      return
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ci    # only issue one shell command in an attempt to minimize interference.
62cb93a386Sopenharmony_ci    result = self._adb.check('''\
63cb93a386Sopenharmony_ci      cat /sys/class/power_supply/bq27742-0/capacity \
64cb93a386Sopenharmony_ci          /sys/devices/system/cpu/online \
65cb93a386Sopenharmony_ci          /sys/class/thermal/thermal_zone7/temp \
66cb93a386Sopenharmony_ci          /sys/class/thermal/thermal_zone0/temp \
67cb93a386Sopenharmony_ci          /sys/class/thermal/thermal_zone1/temp \
68cb93a386Sopenharmony_ci          /sys/class/thermal/thermal_zone7/cdev1/cur_state \
69cb93a386Sopenharmony_ci          /sys/class/thermal/thermal_zone7/cdev0/cur_state
70cb93a386Sopenharmony_ci      for N in 0 1 2; do
71cb93a386Sopenharmony_ci        cat /sys/devices/system/cpu/cpu$N/cpufreq/scaling_cur_freq
72cb93a386Sopenharmony_ci      done
73cb93a386Sopenharmony_ci      cat /sys/devices/57000000.gpu/pstate | grep \*$''')
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci    expectations = \
76cb93a386Sopenharmony_ci      [Expectation(int, min_value=30, name='battery', sleeptime=30*60),
77cb93a386Sopenharmony_ci       Expectation(str, exact_value='0-2', name='online cpus'),
78cb93a386Sopenharmony_ci       Expectation(int, max_value=40000, name='skin temperature'),
79cb93a386Sopenharmony_ci       Expectation(int, max_value=86000, name='cpu temperature'),
80cb93a386Sopenharmony_ci       Expectation(int, max_value=87000, name='gpu temperature'),
81cb93a386Sopenharmony_ci       Expectation(int, exact_value=0, name='cpu throttle'),
82cb93a386Sopenharmony_ci       Expectation(int, exact_value=0, name='gpu throttle')] + \
83cb93a386Sopenharmony_ci      [Expectation(int, exact_value=CPU_CLOCK_RATE,
84cb93a386Sopenharmony_ci                   name='cpu_%i clock rate' % i, sleeptime=30)
85cb93a386Sopenharmony_ci       for i in (0, 1, 2)] + \
86cb93a386Sopenharmony_ci      [Expectation(str, exact_value=GPU_EMC_PROFILE, name='gpu/emc profile')]
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci    Expectation.check_all(expectations, result.splitlines())
89