Home | History | Annotate | Download | only in skpbench
      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 
      6 from __future__ import print_function
      7 from _hardware import Hardware
      8 import sys
      9 import time
     10 
     11 class HardwareAndroid(Hardware):
     12   def __init__(self, adb):
     13     Hardware.__init__(self)
     14     self.warmup_time = 5
     15     self._adb = adb
     16 
     17     if self._adb.root():
     18       self._adb.remount()
     19 
     20   def __enter__(self):
     21     Hardware.__enter__(self)
     22     if not self._adb.is_root() and self._adb.root():
     23       self._adb.remount()
     24 
     25     self._adb.shell('\n'.join([
     26       # turn on airplane mode.
     27       '''
     28       settings put global airplane_mode_on 1''',
     29 
     30       # disable GPS.
     31       '''
     32       for MODE in gps wifi network; do
     33         settings put secure location_providers_allowed -$MODE
     34       done''']))
     35 
     36     if self._adb.is_root():
     37       self._adb.shell('\n'.join([
     38         # disable bluetooth, wifi, and mobile data.
     39         '''
     40         service call bluetooth_manager 8
     41         svc wifi disable
     42         svc data disable''',
     43 
     44         # kill the gui.
     45         '''
     46         setprop ctl.stop media
     47         setprop ctl.stop zygote
     48         setprop ctl.stop surfaceflinger
     49         setprop ctl.stop drm''',
     50 
     51         # disable ASLR
     52         '''
     53         echo 0 > /proc/sys/kernel/randomize_va_space''']))
     54     else:
     55       print("WARNING: no adb root access; results may be unreliable.",
     56             file=sys.stderr)
     57 
     58     return self
     59 
     60   def __exit__(self, exception_type, exception_value, traceback):
     61     Hardware.__exit__(self, exception_type, exception_value, traceback)
     62     self._adb.reboot() # some devices struggle waking up; just hard reboot.
     63 
     64   def sanity_check(self):
     65     Hardware.sanity_check(self)
     66 
     67   def print_debug_diagnostics(self):
     68     # search for and print thermal trip points that may have been exceeded.
     69     self._adb.shell('''\
     70       THERMALDIR=/sys/class/thermal
     71       if [ ! -d $THERMALDIR ]; then
     72         exit
     73       fi
     74       for ZONE in $(cd $THERMALDIR; echo thermal_zone*); do
     75         cd $THERMALDIR/$ZONE
     76         if [ ! -e mode ] || grep -Fxqv enabled mode || [ ! -e trip_point_0_temp ]; then
     77           continue
     78         fi
     79         TEMP=$(cat temp)
     80         TRIPPOINT=trip_point_0_temp
     81         if [ $TEMP -le $(cat $TRIPPOINT) ]; then
     82           echo "$ZONE ($(cat type)): temp=$TEMP <= $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
     83         else
     84           let i=1
     85           while [ -e trip_point_${i}_temp ] &&
     86                 [ $TEMP -gt $(cat trip_point_${i}_temp) ]; do
     87             TRIPPOINT=trip_point_${i}_temp
     88             let i=i+1
     89           done
     90           echo "$ZONE ($(cat type)): temp=$TEMP > $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
     91         fi
     92       done''')
     93 
     94     Hardware.print_debug_diagnostics(self)
     95