| 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 import atexit | 
| 5 import logging | 6 import logging | 
| 6 | 7 | 
| 7 from pylib import android_commands | 8 from pylib import android_commands | 
| 8 from pylib.device import device_utils | 9 from pylib.device import device_utils | 
| 9 | 10 | 
| 10 |  | 
| 11 class PerfControl(object): | 11 class PerfControl(object): | 
| 12   """Provides methods for setting the performance mode of a device.""" | 12   """Provides methods for setting the performance mode of a device.""" | 
| 13   _SCALING_GOVERNOR_FMT = ( | 13   _SCALING_GOVERNOR_FMT = ( | 
| 14       '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor') | 14       '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor') | 
| 15   _CPU_ONLINE_FMT = '/sys/devices/system/cpu/cpu%d/online' | 15   _CPU_ONLINE_FMT = '/sys/devices/system/cpu/cpu%d/online' | 
| 16   _KERNEL_MAX = '/sys/devices/system/cpu/kernel_max' | 16   _KERNEL_MAX = '/sys/devices/system/cpu/kernel_max' | 
| 17 | 17 | 
| 18   def __init__(self, device): | 18   def __init__(self, device): | 
| 19     # TODO(jbudorick) Remove once telemetry gets switched over. | 19     # TODO(jbudorick) Remove once telemetry gets switched over. | 
| 20     if isinstance(device, android_commands.AndroidCommands): | 20     if isinstance(device, android_commands.AndroidCommands): | 
| 21       device = device_utils.DeviceUtils(device) | 21       device = device_utils.DeviceUtils(device) | 
| 22     self._device = device | 22     self._device = device | 
| 23     cpu_files = self._device.RunShellCommand( | 23     cpu_files = self._device.RunShellCommand( | 
| 24       'ls -d /sys/devices/system/cpu/cpu[0-9]*') | 24       'ls -d /sys/devices/system/cpu/cpu[0-9]*') | 
| 25     self._num_cpu_cores = len(cpu_files) | 25     self._num_cpu_cores = len(cpu_files) | 
| 26     assert self._num_cpu_cores > 0, 'Failed to detect CPUs.' | 26     assert self._num_cpu_cores > 0, 'Failed to detect CPUs.' | 
| 27     logging.info('Number of CPUs: %d', self._num_cpu_cores) | 27     logging.info('Number of CPUs: %d', self._num_cpu_cores) | 
| 28     self._have_mpdecision = self._device.FileExists('/system/bin/mpdecision') | 28     self._have_mpdecision = self._device.FileExists('/system/bin/mpdecision') | 
| 29 | 29 | 
| 30   def SetHighPerfMode(self): | 30   def SetHighPerfMode(self): | 
|  | 31     """Sets the highest possible performance mode for the device.""" | 
|  | 32     if not self._device.old_interface.IsRootEnabled(): | 
|  | 33       message = 'Need root for performance mode. Results may be NOISY!!' | 
|  | 34       logging.warning(message) | 
|  | 35       # Add an additional warning at exit, such that it's clear that any results | 
|  | 36       # may be different/noisy (due to the lack of intended performance mode). | 
|  | 37       atexit.register(logging.warning, message) | 
|  | 38       return | 
| 31     # TODO(epenner): Enable on all devices (http://crbug.com/383566) | 39     # TODO(epenner): Enable on all devices (http://crbug.com/383566) | 
| 32     if 'Nexus 4' == self._device.old_interface.GetProductModel(): | 40     if 'Nexus 4' == self._device.old_interface.GetProductModel(): | 
| 33       self._ForceAllCpusOnline(True) | 41       self._ForceAllCpusOnline(True) | 
| 34       if not self._AllCpusAreOnline(): | 42       if not self._AllCpusAreOnline(): | 
| 35         logging.warning('Failed to force CPUs online. Results may be noisy!') | 43         logging.warning('Failed to force CPUs online. Results may be NOISY!') | 
| 36     self._SetScalingGovernorInternal('performance') | 44     self._SetScalingGovernorInternal('performance') | 
| 37 | 45 | 
| 38   def SetPerfProfilingMode(self): | 46   def SetPerfProfilingMode(self): | 
| 39     """Sets the highest possible performance mode for the device.""" | 47     """Enables all cores for reliable perf profiling.""" | 
| 40     self._ForceAllCpusOnline(True) | 48     self._ForceAllCpusOnline(True) | 
| 41     self._SetScalingGovernorInternal('performance') | 49     self._SetScalingGovernorInternal('performance') | 
| 42     if not self._AllCpusAreOnline(): | 50     if not self._AllCpusAreOnline(): | 
| 43       if not self._device.old_interface.IsRootEnabled(): | 51       if not self._device.old_interface.IsRootEnabled(): | 
| 44         raise RuntimeError('Need root to force CPUs online.') | 52         raise RuntimeError('Need root to force CPUs online.') | 
| 45       raise RuntimeError('Failed to force CPUs online.') | 53       raise RuntimeError('Failed to force CPUs online.') | 
| 46 | 54 | 
| 47   def SetDefaultPerfMode(self): | 55   def SetDefaultPerfMode(self): | 
| 48     """Sets the performance mode for the device to its default mode.""" | 56     """Sets the performance mode for the device to its default mode.""" | 
|  | 57     if not self._device.old_interface.IsRootEnabled(): | 
|  | 58       return | 
| 49     product_model = self._device.GetProp('ro.product.model') | 59     product_model = self._device.GetProp('ro.product.model') | 
| 50     governor_mode = { | 60     governor_mode = { | 
| 51         'GT-I9300': 'pegasusq', | 61         'GT-I9300': 'pegasusq', | 
| 52         'Galaxy Nexus': 'interactive', | 62         'Galaxy Nexus': 'interactive', | 
| 53         'Nexus 4': 'ondemand', | 63         'Nexus 4': 'ondemand', | 
| 54         'Nexus 7': 'interactive', | 64         'Nexus 7': 'interactive', | 
| 55         'Nexus 10': 'interactive' | 65         'Nexus 10': 'interactive' | 
| 56     }.get(product_model, 'ondemand') | 66     }.get(product_model, 'ondemand') | 
| 57     self._SetScalingGovernorInternal(governor_mode) | 67     self._SetScalingGovernorInternal(governor_mode) | 
| 58     self._ForceAllCpusOnline(False) | 68     self._ForceAllCpusOnline(False) | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 95 | 105 | 
| 96     if not self._have_mpdecision and not self._AllCpusAreOnline(): | 106     if not self._have_mpdecision and not self._AllCpusAreOnline(): | 
| 97       logging.warning('Unexpected cpu hot plugging detected.') | 107       logging.warning('Unexpected cpu hot plugging detected.') | 
| 98 | 108 | 
| 99     if not force_online: | 109     if not force_online: | 
| 100       return | 110       return | 
| 101 | 111 | 
| 102     for cpu in range(self._num_cpu_cores): | 112     for cpu in range(self._num_cpu_cores): | 
| 103       online_path = PerfControl._CPU_ONLINE_FMT % cpu | 113       online_path = PerfControl._CPU_ONLINE_FMT % cpu | 
| 104       self._device.WriteFile(online_path, '1', as_root=True) | 114       self._device.WriteFile(online_path, '1', as_root=True) | 
| OLD | NEW | 
|---|