summaryrefslogtreecommitdiffstats
path: root/build/android/pylib/perf/perf_control.py
blob: 70389071a6b46037f23e7c891af7045486e0ef57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import atexit
import logging

from pylib import android_commands
from pylib.device import device_utils

class PerfControl(object):
  """Provides methods for setting the performance mode of a device."""
  _SCALING_GOVERNOR_FMT = (
      '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor')
  _CPU_ONLINE_FMT = '/sys/devices/system/cpu/cpu%d/online'
  _KERNEL_MAX = '/sys/devices/system/cpu/kernel_max'

  def __init__(self, device):
    # TODO(jbudorick) Remove once telemetry gets switched over.
    if isinstance(device, android_commands.AndroidCommands):
      device = device_utils.DeviceUtils(device)
    self._device = device
    cpu_files = self._device.RunShellCommand(
      'ls -d /sys/devices/system/cpu/cpu[0-9]*')
    self._num_cpu_cores = len(cpu_files)
    assert self._num_cpu_cores > 0, 'Failed to detect CPUs.'
    logging.info('Number of CPUs: %d', self._num_cpu_cores)
    self._have_mpdecision = self._device.FileExists('/system/bin/mpdecision')

  def SetHighPerfMode(self):
    """Sets the highest possible performance mode for the device."""
    if not self._device.old_interface.IsRootEnabled():
      message = 'Need root for performance mode. Results may be NOISY!!'
      logging.warning(message)
      # Add an additional warning at exit, such that it's clear that any results
      # may be different/noisy (due to the lack of intended performance mode).
      atexit.register(logging.warning, message)
      return
    # TODO(epenner): Enable on all devices (http://crbug.com/383566)
    if 'Nexus 4' == self._device.old_interface.GetProductModel():
      self._ForceAllCpusOnline(True)
      if not self._AllCpusAreOnline():
        logging.warning('Failed to force CPUs online. Results may be NOISY!')
    self._SetScalingGovernorInternal('performance')

  def SetPerfProfilingMode(self):
    """Enables all cores for reliable perf profiling."""
    self._ForceAllCpusOnline(True)
    self._SetScalingGovernorInternal('performance')
    if not self._AllCpusAreOnline():
      if not self._device.old_interface.IsRootEnabled():
        raise RuntimeError('Need root to force CPUs online.')
      raise RuntimeError('Failed to force CPUs online.')

  def SetDefaultPerfMode(self):
    """Sets the performance mode for the device to its default mode."""
    if not self._device.old_interface.IsRootEnabled():
      return
    product_model = self._device.GetProp('ro.product.model')
    governor_mode = {
        'GT-I9300': 'pegasusq',
        'Galaxy Nexus': 'interactive',
        'Nexus 4': 'ondemand',
        'Nexus 7': 'interactive',
        'Nexus 10': 'interactive'
    }.get(product_model, 'ondemand')
    self._SetScalingGovernorInternal(governor_mode)
    self._ForceAllCpusOnline(False)

  def _SetScalingGovernorInternal(self, value):
    for cpu in range(self._num_cpu_cores):
      scaling_governor_file = PerfControl._SCALING_GOVERNOR_FMT % cpu
      if self._device.FileExists(scaling_governor_file):
        logging.info('Writing scaling governor mode \'%s\' -> %s',
                     value, scaling_governor_file)
        self._device.WriteFile(scaling_governor_file, value, as_root=True)

  def _AllCpusAreOnline(self):
    for cpu in range(self._num_cpu_cores):
      online_path = PerfControl._CPU_ONLINE_FMT % cpu
      # TODO(epenner): Investigate why file may be missing
      # (http://crbug.com/397118)
      if not self._device.FileExists(online_path) or \
            self._device.ReadFile(online_path)[0] == '0':
        return False
    return True

  def _ForceAllCpusOnline(self, force_online):
    """Enable all CPUs on a device.

    Some vendors (or only Qualcomm?) hot-plug their CPUs, which can add noise
    to measurements:
    - In perf, samples are only taken for the CPUs that are online when the
      measurement is started.
    - The scaling governor can't be set for an offline CPU and frequency scaling
      on newly enabled CPUs adds noise to both perf and tracing measurements.

    It appears Qualcomm is the only vendor that hot-plugs CPUs, and on Qualcomm
    this is done by "mpdecision".

    """
    if self._have_mpdecision:
      script = 'stop mpdecision' if force_online else 'start mpdecision'
      self._device.RunShellCommand(script, as_root=True)

    if not self._have_mpdecision and not self._AllCpusAreOnline():
      logging.warning('Unexpected cpu hot plugging detected.')

    if not force_online:
      return

    for cpu in range(self._num_cpu_cores):
      online_path = PerfControl._CPU_ONLINE_FMT % cpu
      self._device.WriteFile(online_path, '1', as_root=True)