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