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