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 5import os 6 7from profile_chrome import trace_packager 8from profile_chrome import ui 9 10from pylib import constants 11 12 13def _StartTracing(controllers, interval): 14 for controller in controllers: 15 controller.StartTracing(interval) 16 17 18def _StopTracing(controllers): 19 for controller in controllers: 20 controller.StopTracing() 21 22 23def _PullTraces(controllers, output, compress, write_json): 24 ui.PrintMessage('Downloading...', eol='') 25 trace_files = [controller.PullTrace() for controller in controllers] 26 trace_files = [trace for trace in trace_files if trace] 27 if not trace_files: 28 ui.PrintMessage('No results') 29 return [] 30 result = trace_packager.PackageTraces(trace_files, 31 output=output, 32 compress=compress, 33 write_json=write_json) 34 ui.PrintMessage('done') 35 ui.PrintMessage('Trace written to file://%s' % os.path.abspath(result)) 36 return result 37 38 39def GetSupportedBrowsers(): 40 """Returns the package names of all supported browsers.""" 41 # Add aliases for backwards compatibility. 42 supported_browsers = { 43 'stable': constants.PACKAGE_INFO['chrome_stable'], 44 'beta': constants.PACKAGE_INFO['chrome_beta'], 45 'dev': constants.PACKAGE_INFO['chrome_dev'], 46 'build': constants.PACKAGE_INFO['chrome'], 47 } 48 supported_browsers.update(constants.PACKAGE_INFO) 49 unsupported_browsers = ['content_browsertests', 'gtest', 'legacy_browser'] 50 for browser in unsupported_browsers: 51 del supported_browsers[browser] 52 return supported_browsers 53 54 55def CaptureProfile(controllers, interval, output=None, compress=False, 56 write_json=False): 57 """Records a profiling trace saves the result to a file. 58 59 Args: 60 controllers: List of tracing controllers. 61 interval: Time interval to capture in seconds. An interval of None (or 0) 62 continues tracing until stopped by the user. 63 output: Output file name or None to use an automatically generated name. 64 compress: If True, the result will be compressed either with gzip or zip 65 depending on the number of captured subtraces. 66 write_json: If True, prefer JSON output over HTML. 67 68 Returns: 69 Path to saved profile. 70 """ 71 trace_type = ' + '.join(map(str, controllers)) 72 try: 73 _StartTracing(controllers, interval) 74 if interval: 75 ui.PrintMessage('Capturing %d-second %s. Press Enter to stop early...' % \ 76 (interval, trace_type), eol='') 77 ui.WaitForEnter(interval) 78 else: 79 ui.PrintMessage('Capturing %s. Press Enter to stop...' % \ 80 trace_type, eol='') 81 raw_input() 82 finally: 83 _StopTracing(controllers) 84 if interval: 85 ui.PrintMessage('done') 86 87 return _PullTraces(controllers, output, compress, write_json) 88