Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / build / android / pylib / perf / perf_control.py
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 from pylib import android_commands
8 from pylib.device import device_utils
9
10
11 class PerfControl(object):
12   """Provides methods for setting the performance mode of a device."""
13   _SCALING_GOVERNOR_FMT = (
14       '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor')
15   _KERNEL_MAX = '/sys/devices/system/cpu/kernel_max'
16
17   def __init__(self, device):
18     # TODO(jbudorick) Remove once telemetry gets switched over.
19     if isinstance(device, android_commands.AndroidCommands):
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)
24     assert kernel_max, 'Unable to find %s' % PerfControl._KERNEL_MAX
25     self._kernel_max = int(kernel_max[0])
26     logging.info('Maximum CPU index: %d', self._kernel_max)
27     self._original_scaling_governor = \
28         self._device.old_interface.GetFileContents(
29             PerfControl._SCALING_GOVERNOR_FMT % 0,
30             log_result=False)[0]
31
32   def SetHighPerfMode(self):
33     """Sets the highest possible performance mode for the device."""
34     self._SetScalingGovernorInternal('performance')
35
36   def SetDefaultPerfMode(self):
37     """Sets the performance mode for the device to its default mode."""
38     product_model = self._device.old_interface.GetProductModel()
39     governor_mode = {
40         'GT-I9300': 'pegasusq',
41         'Galaxy Nexus': 'interactive',
42         'Nexus 4': 'ondemand',
43         'Nexus 7': 'interactive',
44         'Nexus 10': 'interactive'
45     }.get(product_model, 'ondemand')
46     self._SetScalingGovernorInternal(governor_mode)
47
48   def RestoreOriginalPerfMode(self):
49     """Resets the original performance mode of the device."""
50     self._SetScalingGovernorInternal(self._original_scaling_governor)
51
52   def _SetScalingGovernorInternal(self, value):
53     for cpu in range(self._kernel_max + 1):
54       scaling_governor_file = PerfControl._SCALING_GOVERNOR_FMT % cpu
55       if self._device.old_interface.FileExistsOnDevice(scaling_governor_file):
56         logging.info('Writing scaling governor mode \'%s\' -> %s',
57                      value, scaling_governor_file)
58         self._device.old_interface.SetProtectedFileContents(
59             scaling_governor_file, value)
60