def GetTopSamples(cls, file_name, number): """Parses the perf generated profile in |file_name| and returns a {function: period} dict of the |number| hottests functions. """ assert os.path.exists(file_name) with open(os.devnull, 'w') as devnull: _InstallPerfHost() report = subprocess.Popen([ android_profiling_helper.GetPerfhostName(), 'report', '--show-total-period', '-U', '-t', '^', '-i', file_name ], stdout=subprocess.PIPE, stderr=devnull).communicate()[0] period_by_function = {} for line in report.split('\n'): if not line or line.startswith('#'): continue fields = line.split('^') if len(fields) != 5: continue period = int(fields[1]) function = fields[4].partition(' ')[2] function = re.sub('<.*>', '', function) # Strip template params. function = re.sub('[(].*[)]', '', function) # Strip function params. period_by_function[function] = period if len(period_by_function) == number: break return period_by_function
def PullTrace(self): symfs_dir = os.path.join(tempfile.gettempdir(), os.path.expandvars('$USER-perf-symfs')) if not os.path.exists(symfs_dir): os.makedirs(symfs_dir) required_libs = set() # Download the recorded perf profile. perf_profile = self._perf_instance.PullResult(symfs_dir) required_libs = \ android_profiling_helper.GetRequiredLibrariesForPerfProfile( perf_profile) if not required_libs: logging.warning( 'No libraries required by perf trace. Most likely there ' 'are no samples in the trace.') # Build a symfs with all the necessary libraries. kallsyms = android_profiling_helper.CreateSymFs(self._device, symfs_dir, required_libs, use_symlinks=False) perfhost_path = binary_manager.FetchPath( android_profiling_helper.GetPerfhostName(), 'x86_64', 'linux') ui.PrintMessage('\nNote: to view the profile in perf, run:') ui.PrintMessage(' ' + self._GetInteractivePerfCommand( perfhost_path, perf_profile, symfs_dir, required_libs, kallsyms)) # Convert the perf profile into JSON. perf_script_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'third_party', 'perf_to_tracing.py') json_file_name = os.path.basename(perf_profile) with open(os.devnull, 'w') as dev_null, \ open(json_file_name, 'w') as json_file: cmd = [ perfhost_path, 'script', '-s', perf_script_path, '-i', perf_profile, '--symfs', symfs_dir, '--kallsyms', kallsyms ] if subprocess.call(cmd, stdout=json_file, stderr=dev_null): logging.warning( 'Perf data to JSON conversion failed. The result will ' 'not contain any perf samples. You can still view the ' 'perf data manually as shown above.') return None return json_file_name
def _InstallPerfHost(): perfhost_name = android_profiling_helper.GetPerfhostName() host = platform.GetHostPlatform() if not host.CanLaunchApplication(perfhost_name): host.InstallApplication(perfhost_name) return binary_manager.FetchPath(perfhost_name, 'x86_64', 'linux')