• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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
6from __future__ import print_function
7from _hardware import Hardware
8import sys
9import time
10
11class HardwareAndroid(Hardware):
12  def __init__(self, adb):
13    Hardware.__init__(self)
14    self.warmup_time = 5
15    self._adb = adb
16    self.desiredClock = 0.66
17
18    if self._adb.root():
19      self._adb.remount()
20
21  def __enter__(self):
22    Hardware.__enter__(self)
23    if not self._adb.is_root() and self._adb.root():
24      self._adb.remount()
25
26    self._adb.shell('\n'.join([
27      # turn on airplane mode.
28      '''
29      settings put global airplane_mode_on 1''',
30
31      # disable GPS.
32      '''
33      settings put secure location_providers_allowed -gps
34      settings put secure location_providers_allowed -wifi
35      settings put secure location_providers_allowed -network''']))
36
37    if self._adb.is_root():
38
39      # For explanation of variance reducing steps, see
40      # https://g3doc.corp.google.com/engedu/portal/android/g3doc/learn/develop/performance/content/best/reliable-startup-latency.md?cl=head
41
42      self._adb.shell('\n'.join([
43        # disable bluetooth, wifi, and mobile data.
44        '''
45        service call bluetooth_manager 8
46        svc wifi disable
47        svc data disable''',
48
49        # kill the gui.
50        '''
51        setprop ctl.stop media
52        setprop ctl.stop zygote
53        setprop ctl.stop surfaceflinger
54        setprop ctl.stop drm''',
55
56        # disable ASLR
57        '''
58        echo 0 > /proc/sys/kernel/randomize_va_space''',
59        ]))
60
61      self.lock_top_three_cores()
62
63      self.lock_adreno_gpu()
64
65    else:
66      print("WARNING: no adb root access; results may be unreliable.",
67            file=sys.stderr)
68
69    return self
70
71  def __exit__(self, exception_type, exception_value, traceback):
72    Hardware.__exit__(self, exception_type, exception_value, traceback)
73    self._adb.reboot() # some devices struggle waking up; just hard reboot.
74
75  def sanity_check(self):
76    Hardware.sanity_check(self)
77
78  def print_debug_diagnostics(self):
79    # search for and print thermal trip points that may have been exceeded.
80    self._adb.shell('''\
81      THERMALDIR=/sys/class/thermal
82      if [ ! -d $THERMALDIR ]; then
83        exit
84      fi
85      for ZONE in $(cd $THERMALDIR; echo thermal_zone*); do
86        cd $THERMALDIR/$ZONE
87        if [ ! -e mode ] || grep -Fxqv enabled mode || [ ! -e trip_point_0_temp ]; then
88          continue
89        fi
90        TEMP=$(cat temp)
91        TRIPPOINT=trip_point_0_temp
92        if [ $TEMP -le $(cat $TRIPPOINT) ]; then
93          echo "$ZONE ($(cat type)): temp=$TEMP <= $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
94        else
95          let i=1
96          while [ -e trip_point_${i}_temp ] &&
97                [ $TEMP -gt $(cat trip_point_${i}_temp) ]; do
98            TRIPPOINT=trip_point_${i}_temp
99            let i=i+1
100          done
101          echo "$ZONE ($(cat type)): temp=$TEMP > $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
102        fi
103      done''')
104
105    Hardware.print_debug_diagnostics(self)
106
107  # expects a float between 0 and 100 representing where along the list of freqs to choose a value.
108  def setDesiredClock(self, c):
109    self.desiredClock = c / 100
110
111  def lock_top_three_cores(self):
112    # Lock the clocks of the fastest three cores and disable others.
113    # Assumes root privlidges
114    core_count = int(self._adb.check('cat /proc/cpuinfo | grep processor | wc -l'))
115    max_speeds = []
116    for i in range(core_count):
117      khz = int(self._adb.check('cat /sys/devices/system/cpu/cpu%i/cpufreq/cpuinfo_max_freq' % i))
118      max_speeds.append((khz, i)) # the tuple's first position and it will be the sort key
119    cores_in_desc_order_of_max_speed = [a[1] for a in sorted(max_speeds, reverse=True)]
120    top_cores = cores_in_desc_order_of_max_speed[:3]
121    disable_cores = cores_in_desc_order_of_max_speed[3:]
122    if disable_cores:
123      self._adb.shell('\n'.join([('echo 0 > /sys/devices/system/cpu/cpu%i/online' % i) for i in disable_cores]))
124    # since thermal-engine will be disabled, don't pick the max freq to lock these at,
125    # pick something lower, so it doesn't get too hot (it'd reboot)
126    # get a list of available scaling frequencies and pick one 2/3 of the way up.
127    for i in top_cores:
128      freqs = self._adb.check('cat /sys/devices/system/cpu/cpu%i/cpufreq/scaling_available_frequencies' % i).split()
129      speed = freqs[int((len(freqs)-1) * self.desiredClock)]
130      self._adb.shell('''echo 1 > /sys/devices/system/cpu/cpu{id}/online
131      echo userspace > /sys/devices/system/cpu/cpu{id}/cpufreq/scaling_governor
132      echo {speed} > /sys/devices/system/cpu/cpu{id}/cpufreq/scaling_max_freq
133      echo {speed} > /sys/devices/system/cpu/cpu{id}/cpufreq/scaling_min_freq
134      echo {speed} > /sys/devices/system/cpu/cpu{id}/cpufreq/scaling_setspeed'''.format(id=i, speed=speed))
135
136  def lock_adreno_gpu(self):
137    # Use presence of /sys/class/kgsl to indicate Adreno GPU
138    exists = self._adb.check('test -d /sys/class/kgsl && echo y')
139    if (exists.strip() != 'y'):
140      print('Not attempting Adreno GPU clock locking steps')
141      return
142
143    # variance reducing changes
144    self._adb.shell('''
145      echo 0 > /sys/class/kgsl/kgsl-3d0/bus_split
146      echo 1 > /sys/class/kgsl/kgsl-3d0/force_clk_on
147      echo 10000 > /sys/class/kgsl/kgsl-3d0/idle_timer''')
148
149    freqs = self._adb.check('cat /sys/class/kgsl/kgsl-3d0/devfreq/available_frequencies').split()
150    speed = freqs[int((len(freqs)-1) * self.desiredClock)]
151
152    # Set GPU to performance mode and lock clock
153    self._adb.shell('''
154      echo performance > /sys/class/kgsl/kgsl-3d0/devfreq/governor
155      echo {speed} > /sys/class/kgsl/kgsl-3d0/devfreq/max_freq
156      echo {speed} > /sys/class/kgsl/kgsl-3d0/devfreq/min_freq'''.format(speed=speed))
157
158    # Set GPU power level
159    self._adb.shell('''
160      echo 1 > /sys/class/kgsl/kgsl-3d0/max_pwrlevel
161      echo 1 > /sys/class/kgsl/kgsl-3d0/min_pwrlevel''')
162