Chromium Code Reviews| 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 gzip | |
| 6 import os | |
| 7 import shutil | |
| 8 import sys | |
| 9 import zipfile | |
| 10 | |
| 11 from chrome_profiler import ui | |
| 12 from chrome_profiler import util | |
| 13 | |
| 14 from pylib import constants | |
| 15 | |
| 16 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, | |
| 17 'third_party', | |
| 18 'trace-viewer')) | |
| 19 # pylint: disable=F0401 | |
| 20 from trace_viewer.build import trace2html | |
| 21 | |
| 22 | |
| 23 def _CompressFile(host_file, output): | |
| 24 with gzip.open(output, 'wb') as out: | |
| 25 with open(host_file, 'rb') as input_file: | |
| 26 out.write(input_file.read()) | |
| 27 os.unlink(host_file) | |
| 28 | |
| 29 | |
| 30 def _ArchiveFiles(host_files, output): | |
| 31 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as z: | |
| 32 for host_file in host_files: | |
| 33 z.write(host_file) | |
| 34 os.unlink(host_file) | |
| 35 | |
| 36 | |
| 37 def _PackageTracesAsHtml(trace_files, html_file): | |
| 38 with open(html_file, 'w') as f: | |
| 39 trace2html.WriteHTMLForTracesToFile(trace_files, f) | |
| 40 for trace_file in trace_files: | |
| 41 os.unlink(trace_file) | |
| 42 | |
| 43 | |
| 44 def _StartTracing(controllers, interval): | |
| 45 for controller in controllers: | |
| 46 controller.StartTracing(interval) | |
| 47 | |
| 48 | |
| 49 def _StopTracing(controllers): | |
| 50 for controller in controllers: | |
| 51 controller.StopTracing() | |
| 52 | |
| 53 | |
| 54 def _PullTraces(controllers, output, compress, write_json): | |
| 55 ui.PrintMessage('Downloading...', eol='') | |
| 56 trace_files = [] | |
| 57 for controller in controllers: | |
| 58 trace_files.append(controller.PullTrace()) | |
| 59 | |
| 60 if not write_json: | |
| 61 html_file = os.path.splitext(trace_files[0])[0] + '.html' | |
| 62 _PackageTracesAsHtml(trace_files, html_file) | |
| 63 trace_files = [html_file] | |
| 64 | |
| 65 if compress and len(trace_files) == 1: | |
| 66 result = output or trace_files[0] + '.gz' | |
| 67 _CompressFile(trace_files[0], result) | |
| 68 elif len(trace_files) > 1: | |
| 69 result = output or 'chrome-combined-trace-%s.zip' % util.GetTraceTimestamp() | |
| 70 _ArchiveFiles(trace_files, result) | |
| 71 elif output: | |
| 72 result = output | |
| 73 shutil.move(trace_files[0], result) | |
| 74 else: | |
| 75 result = trace_files[0] | |
| 76 | |
| 77 ui.PrintMessage('done') | |
| 78 ui.PrintMessage('Trace written to file://%s' % os.path.abspath(result)) | |
| 79 return result | |
| 80 | |
| 81 | |
| 82 def GetSupportedBrowsers(): | |
| 83 """Returns the package names of all supported browsers.""" | |
| 84 # Add aliases for backwards compatibility. | |
| 85 supported_browsers = { | |
| 86 'stable': constants.PACKAGE_INFO['chrome_stable'], | |
| 87 'beta': constants.PACKAGE_INFO['chrome_beta'], | |
| 88 'dev': constants.PACKAGE_INFO['chrome_dev'], | |
| 89 'build': constants.PACKAGE_INFO['chrome'], | |
| 90 } | |
| 91 supported_browsers.update(constants.PACKAGE_INFO) | |
| 92 unsupported_browsers = ['content_browsertests', 'gtest', 'legacy_browser'] | |
| 93 for browser in unsupported_browsers: | |
| 94 del supported_browsers[browser] | |
| 95 return supported_browsers | |
| 96 | |
| 97 | |
| 98 def CaptureProfile(controllers, interval, output=None, compress=False, | |
| 99 write_json=False): | |
| 100 """Records a profiling trace saves the result to a file. | |
| 101 | |
| 102 Args: | |
| 103 controllers: List of tracing controllers. | |
| 104 interval: Time interval to capture in seconds. | |
|
Dominik Grewe
2014/05/22 18:07:10
Add that passing 0 (or None) means it will run unt
Sami
2014/05/27 11:03:54
Done.
| |
| 105 output: Output file name or None to use an automatically generated name. | |
| 106 compress: If True, the result will be compressed either with gzip or zip | |
| 107 depending on the number of captured subtraces. | |
| 108 write_json: If True, prefer JSON output over HTML. | |
| 109 | |
| 110 Returns: | |
| 111 Path to saved profile. | |
| 112 """ | |
| 113 trace_type = ' + '.join(map(str, controllers)) | |
| 114 try: | |
| 115 _StartTracing(controllers, interval) | |
| 116 if interval: | |
| 117 ui.PrintMessage('Capturing %d-second %s. Press Enter to stop early...' % \ | |
| 118 (interval, trace_type), eol='') | |
| 119 ui.WaitForEnter(interval) | |
| 120 else: | |
| 121 ui.PrintMessage('Capturing %s. Press Enter to stop...' % \ | |
| 122 trace_type, eol='') | |
| 123 raw_input() | |
| 124 finally: | |
| 125 _StopTracing(controllers) | |
| 126 if interval: | |
| 127 ui.PrintMessage('done') | |
| 128 | |
| 129 return _PullTraces(controllers, output, compress, write_json) | |
| OLD | NEW |