Home | History | Annotate | Download | only in perf
      1 # Copyright 2013 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 
      6 import logging
      7 
      8 
      9 class PerfControl(object):
     10   """Provides methods for setting the performance mode of a device."""
     11   _SCALING_GOVERNOR_FMT = (
     12       '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor')
     13   _KERNEL_MAX = '/sys/devices/system/cpu/kernel_max'
     14 
     15   def __init__(self, adb):
     16     self._adb = adb
     17     kernel_max = self._adb.GetFileContents(PerfControl._KERNEL_MAX,
     18                                            log_result=False)
     19     assert kernel_max, 'Unable to find %s' % PerfControl._KERNEL_MAX
     20     self._kernel_max = int(kernel_max[0])
     21     logging.info('Maximum CPU index: %d', self._kernel_max)
     22     self._original_scaling_governor = self._adb.GetFileContents(
     23         PerfControl._SCALING_GOVERNOR_FMT % 0,
     24         log_result=False)[0]
     25 
     26   def SetHighPerfMode(self):
     27     """Sets the highest possible performance mode for the device."""
     28     self._SetScalingGovernorInternal('performance')
     29 
     30   def SetDefaultPerfMode(self):
     31     """Sets the performance mode for the device to its default mode."""
     32     product_model = self._adb.GetProductModel()
     33     governor_mode = {
     34         'GT-I9300': 'pegasusq',
     35         'Galaxy Nexus': 'interactive',
     36         'Nexus 4': 'ondemand',
     37         'Nexus 7': 'interactive',
     38         'Nexus 10': 'interactive'
     39     }.get(product_model, 'ondemand')
     40     self._SetScalingGovernorInternal(governor_mode)
     41 
     42   def RestoreOriginalPerfMode(self):
     43     """Resets the original performance mode of the device."""
     44     self._SetScalingGovernorInternal(self._original_scaling_governor)
     45 
     46   def _SetScalingGovernorInternal(self, value):
     47     for cpu in range(self._kernel_max + 1):
     48       scaling_governor_file = PerfControl._SCALING_GOVERNOR_FMT % cpu
     49       if self._adb.FileExistsOnDevice(scaling_governor_file):
     50         logging.info('Writing scaling governor mode \'%s\' -> %s',
     51                      value, scaling_governor_file)
     52         self._adb.SetProtectedFileContents(scaling_governor_file, value)
     53