| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 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 import logging | |
| 6 import os | |
| 7 import signal | |
| 8 import subprocess | |
| 9 import sys | |
| 10 import tempfile | |
| 11 | |
| 12 from devil.android import device_temp_file | |
| 13 from devil.android.perf import perf_control | |
| 14 | |
| 15 from profile_chrome import controllers | |
| 16 from profile_chrome import ui | |
| 17 | |
| 18 _SRC_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) | |
| 19 sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'telemetry')) | |
| 20 try: | |
| 21 # pylint: disable=F0401 | |
| 22 from telemetry.internal.platform.profiler import android_profiling_helper | |
| 23 from telemetry.internal.util import binary_manager | |
| 24 except ImportError: | |
| 25 android_profiling_helper = None | |
| 26 binary_manager = None | |
| 27 | |
| 28 | |
| 29 _PERF_OPTIONS = [ | |
| 30 # Sample across all processes and CPUs to so that the current CPU gets | |
| 31 # recorded to each sample. | |
| 32 '--all-cpus', | |
| 33 # In perf 3.13 --call-graph requires an argument, so use the -g short-hand | |
| 34 # which does not. | |
| 35 '-g', | |
| 36 # Increase priority to avoid dropping samples. Requires root. | |
| 37 '--realtime', '80', | |
| 38 # Record raw samples to get CPU information. | |
| 39 '--raw-samples', | |
| 40 # Increase sampling frequency for better coverage. | |
| 41 '--freq', '2000', | |
| 42 ] | |
| 43 | |
| 44 | |
| 45 class _PerfProfiler(object): | |
| 46 def __init__(self, device, perf_binary, categories): | |
| 47 self._device = device | |
| 48 self._output_file = device_temp_file.DeviceTempFile( | |
| 49 self._device.adb, prefix='perf_output') | |
| 50 self._log_file = tempfile.TemporaryFile() | |
| 51 | |
| 52 # TODO(jbudorick) Look at providing a way to unhandroll this once the | |
| 53 # adb rewrite has fully landed. | |
| 54 device_param = (['-s', str(self._device)] if str(self._device) else []) | |
| 55 cmd = ['adb'] + device_param + \ | |
| 56 ['shell', perf_binary, 'record', | |
| 57 '--output', self._output_file.name] + _PERF_OPTIONS | |
| 58 if categories: | |
| 59 cmd += ['--event', ','.join(categories)] | |
| 60 self._perf_control = perf_control.PerfControl(self._device) | |
| 61 self._perf_control.SetPerfProfilingMode() | |
| 62 self._perf_process = subprocess.Popen(cmd, | |
| 63 stdout=self._log_file, | |
| 64 stderr=subprocess.STDOUT) | |
| 65 | |
| 66 def SignalAndWait(self): | |
| 67 self._device.KillAll('perf', signum=signal.SIGINT) | |
| 68 self._perf_process.wait() | |
| 69 self._perf_control.SetDefaultPerfMode() | |
| 70 | |
| 71 def _FailWithLog(self, msg): | |
| 72 self._log_file.seek(0) | |
| 73 log = self._log_file.read() | |
| 74 raise RuntimeError('%s. Log output:\n%s' % (msg, log)) | |
| 75 | |
| 76 def PullResult(self, output_path): | |
| 77 if not self._device.FileExists(self._output_file.name): | |
| 78 self._FailWithLog('Perf recorded no data') | |
| 79 | |
| 80 perf_profile = os.path.join(output_path, | |
| 81 os.path.basename(self._output_file.name)) | |
| 82 self._device.PullFile(self._output_file.name, perf_profile) | |
| 83 if not os.stat(perf_profile).st_size: | |
| 84 os.remove(perf_profile) | |
| 85 self._FailWithLog('Perf recorded a zero-sized file') | |
| 86 | |
| 87 self._log_file.close() | |
| 88 self._output_file.close() | |
| 89 return perf_profile | |
| 90 | |
| 91 | |
| 92 class PerfProfilerController(controllers.BaseController): | |
| 93 def __init__(self, device, categories): | |
| 94 controllers.BaseController.__init__(self) | |
| 95 self._device = device | |
| 96 self._categories = categories | |
| 97 self._perf_binary = self._PrepareDevice(device) | |
| 98 self._perf_instance = None | |
| 99 | |
| 100 def __repr__(self): | |
| 101 return 'perf profile' | |
| 102 | |
| 103 @staticmethod | |
| 104 def IsSupported(): | |
| 105 return bool(android_profiling_helper) | |
| 106 | |
| 107 @staticmethod | |
| 108 def _PrepareDevice(device): | |
| 109 if not 'BUILDTYPE' in os.environ: | |
| 110 os.environ['BUILDTYPE'] = 'Release' | |
| 111 if binary_manager.NeedsInit(): | |
| 112 binary_manager.InitDependencyManager(None) | |
| 113 return android_profiling_helper.PrepareDeviceForPerf(device) | |
| 114 | |
| 115 @classmethod | |
| 116 def GetCategories(cls, device): | |
| 117 perf_binary = cls._PrepareDevice(device) | |
| 118 return device.RunShellCommand('%s list' % perf_binary) | |
| 119 | |
| 120 def StartTracing(self, _): | |
| 121 self._perf_instance = _PerfProfiler(self._device, | |
| 122 self._perf_binary, | |
| 123 self._categories) | |
| 124 | |
| 125 def StopTracing(self): | |
| 126 if not self._perf_instance: | |
| 127 return | |
| 128 self._perf_instance.SignalAndWait() | |
| 129 | |
| 130 @staticmethod | |
| 131 def _GetInteractivePerfCommand(perfhost_path, perf_profile, symfs_dir, | |
| 132 required_libs, kallsyms): | |
| 133 cmd = '%s report -n -i %s --symfs %s --kallsyms %s' % ( | |
| 134 os.path.relpath(perfhost_path, '.'), perf_profile, symfs_dir, kallsyms) | |
| 135 for lib in required_libs: | |
| 136 lib = os.path.join(symfs_dir, lib[1:]) | |
| 137 if not os.path.exists(lib): | |
| 138 continue | |
| 139 objdump_path = android_profiling_helper.GetToolchainBinaryPath( | |
| 140 lib, 'objdump') | |
| 141 if objdump_path: | |
| 142 cmd += ' --objdump %s' % os.path.relpath(objdump_path, '.') | |
| 143 break | |
| 144 return cmd | |
| 145 | |
| 146 def PullTrace(self): | |
| 147 symfs_dir = os.path.join(tempfile.gettempdir(), | |
| 148 os.path.expandvars('$USER-perf-symfs')) | |
| 149 if not os.path.exists(symfs_dir): | |
| 150 os.makedirs(symfs_dir) | |
| 151 required_libs = set() | |
| 152 | |
| 153 # Download the recorded perf profile. | |
| 154 perf_profile = self._perf_instance.PullResult(symfs_dir) | |
| 155 required_libs = \ | |
| 156 android_profiling_helper.GetRequiredLibrariesForPerfProfile( | |
| 157 perf_profile) | |
| 158 if not required_libs: | |
| 159 logging.warning('No libraries required by perf trace. Most likely there ' | |
| 160 'are no samples in the trace.') | |
| 161 | |
| 162 # Build a symfs with all the necessary libraries. | |
| 163 kallsyms = android_profiling_helper.CreateSymFs(self._device, | |
| 164 symfs_dir, | |
| 165 required_libs, | |
| 166 use_symlinks=False) | |
| 167 perfhost_path = binary_manager.FetchPath( | |
| 168 android_profiling_helper.GetPerfhostName(), 'x86_64', 'linux') | |
| 169 | |
| 170 ui.PrintMessage('\nNote: to view the profile in perf, run:') | |
| 171 ui.PrintMessage(' ' + self._GetInteractivePerfCommand(perfhost_path, | |
| 172 perf_profile, symfs_dir, required_libs, kallsyms)) | |
| 173 | |
| 174 # Convert the perf profile into JSON. | |
| 175 perf_script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), | |
| 176 'third_party', 'perf_to_tracing.py') | |
| 177 json_file_name = os.path.basename(perf_profile) | |
| 178 with open(os.devnull, 'w') as dev_null, \ | |
| 179 open(json_file_name, 'w') as json_file: | |
| 180 cmd = [perfhost_path, 'script', '-s', perf_script_path, '-i', | |
| 181 perf_profile, '--symfs', symfs_dir, '--kallsyms', kallsyms] | |
| 182 if subprocess.call(cmd, stdout=json_file, stderr=dev_null): | |
| 183 logging.warning('Perf data to JSON conversion failed. The result will ' | |
| 184 'not contain any perf samples. You can still view the ' | |
| 185 'perf data manually as shown above.') | |
| 186 return None | |
| 187 | |
| 188 return json_file_name | |
| OLD | NEW |