OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 import logging |
| 8 import optparse |
| 9 import os |
| 10 import sys |
| 11 import webbrowser |
| 12 |
| 13 from chrome_profiler import chrome_controller |
| 14 from chrome_profiler import profiler |
| 15 from chrome_profiler import systrace_controller |
| 16 from chrome_profiler import ui |
| 17 |
| 18 from pylib import android_commands |
| 19 from pylib.device import device_utils |
| 20 |
| 21 |
| 22 _DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES' |
| 23 |
| 24 |
| 25 def _ComputeChromeCategories(options): |
| 26 categories = [] |
| 27 if options.trace_frame_viewer: |
| 28 categories.append('disabled-by-default-cc.debug') |
| 29 if options.trace_ubercompositor: |
| 30 categories.append('disabled-by-default-cc.debug*') |
| 31 if options.trace_gpu: |
| 32 categories.append('disabled-by-default-gpu.debug*') |
| 33 if options.trace_flow: |
| 34 categories.append('disabled-by-default-toplevel.flow') |
| 35 if options.chrome_categories: |
| 36 categories += options.chrome_categories.split(',') |
| 37 return categories |
| 38 |
| 39 |
| 40 def _ComputeSystraceCategories(options): |
| 41 if not options.systrace_categories: |
| 42 return [] |
| 43 return options.systrace_categories.split(',') |
| 44 |
| 45 |
| 46 def _CreateOptionParser(): |
| 47 parser = optparse.OptionParser(description='Record about://tracing profiles ' |
| 48 'from Android browsers. See http://dev.' |
| 49 'chromium.org/developers/how-tos/trace-event-' |
| 50 'profiling-tool for detailed instructions for ' |
| 51 'profiling.') |
| 52 |
| 53 timed_options = optparse.OptionGroup(parser, 'Timed tracing') |
| 54 timed_options.add_option('-t', '--time', help='Profile for N seconds and ' |
| 55 'download the resulting trace.', metavar='N', |
| 56 type='float') |
| 57 parser.add_option_group(timed_options) |
| 58 |
| 59 cont_options = optparse.OptionGroup(parser, 'Continuous tracing') |
| 60 cont_options.add_option('--continuous', help='Profile continuously until ' |
| 61 'stopped.', action='store_true') |
| 62 cont_options.add_option('--ring-buffer', help='Use the trace buffer as a ' |
| 63 'ring buffer and save its contents when stopping ' |
| 64 'instead of appending events into one long trace.', |
| 65 action='store_true') |
| 66 parser.add_option_group(cont_options) |
| 67 |
| 68 chrome_opts = optparse.OptionGroup(parser, 'Chrome tracing options') |
| 69 chrome_opts.add_option('-c', '--categories', help='Select Chrome tracing ' |
| 70 'categories with comma-delimited wildcards, ' |
| 71 'e.g., "*", "cat1*,-cat1a". Omit this option to trace ' |
| 72 'Chrome\'s default categories. Chrome tracing can be ' |
| 73 'disabled with "--categories=\'\'". Use "list" to ' |
| 74 'see the available categories.', |
| 75 metavar='CHROME_CATEGORIES', dest='chrome_categories', |
| 76 default=_DEFAULT_CHROME_CATEGORIES) |
| 77 chrome_opts.add_option('--trace-cc', |
| 78 help='Deprecated, use --trace-frame-viewer.', |
| 79 action='store_true') |
| 80 chrome_opts.add_option('--trace-frame-viewer', |
| 81 help='Enable enough trace categories for ' |
| 82 'compositor frame viewing.', action='store_true') |
| 83 chrome_opts.add_option('--trace-ubercompositor', |
| 84 help='Enable enough trace categories for ' |
| 85 'ubercompositor frame data.', action='store_true') |
| 86 chrome_opts.add_option('--trace-gpu', help='Enable extra trace categories ' |
| 87 'for GPU data.', action='store_true') |
| 88 chrome_opts.add_option('--trace-flow', help='Enable extra trace categories ' |
| 89 'for IPC message flows.', action='store_true') |
| 90 parser.add_option_group(chrome_opts) |
| 91 |
| 92 systrace_opts = optparse.OptionGroup(parser, 'Systrace tracing options') |
| 93 systrace_opts.add_option('-s', '--systrace', help='Capture a systrace with ' |
| 94 'the chosen comma-delimited systrace categories. You ' |
| 95 'can also capture a combined Chrome + systrace by ' |
| 96 'enable both types of categories. Use "list" to see ' |
| 97 'the available categories. Systrace is disabled by ' |
| 98 'default.', metavar='SYS_CATEGORIES', |
| 99 dest='systrace_categories', default='') |
| 100 parser.add_option_group(systrace_opts) |
| 101 |
| 102 output_options = optparse.OptionGroup(parser, 'Output options') |
| 103 output_options.add_option('-o', '--output', help='Save trace output to file.') |
| 104 output_options.add_option('--json', help='Save trace as raw JSON instead of ' |
| 105 'HTML.', action='store_true') |
| 106 output_options.add_option('--view', help='Open resulting trace file in a ' |
| 107 'browser.', action='store_true') |
| 108 parser.add_option_group(output_options) |
| 109 |
| 110 browsers = sorted(profiler.GetSupportedBrowsers().keys()) |
| 111 parser.add_option('-b', '--browser', help='Select among installed browsers. ' |
| 112 'One of ' + ', '.join(browsers) + ', "stable" is used by ' |
| 113 'default.', type='choice', choices=browsers, |
| 114 default='stable') |
| 115 parser.add_option('-v', '--verbose', help='Verbose logging.', |
| 116 action='store_true') |
| 117 parser.add_option('-z', '--compress', help='Compress the resulting trace ' |
| 118 'with gzip. ', action='store_true') |
| 119 return parser |
| 120 |
| 121 |
| 122 def main(): |
| 123 parser = _CreateOptionParser() |
| 124 options, _args = parser.parse_args() |
| 125 if options.trace_cc: |
| 126 parser.parse_error("""--trace-cc is deprecated. |
| 127 |
| 128 For basic jank busting uses, use --trace-frame-viewer |
| 129 For detailed study of ubercompositor, pass --trace-ubercompositor. |
| 130 |
| 131 When in doubt, just try out --trace-frame-viewer. |
| 132 """) |
| 133 |
| 134 if options.verbose: |
| 135 logging.getLogger().setLevel(logging.DEBUG) |
| 136 |
| 137 devices = android_commands.GetAttachedDevices() |
| 138 if len(devices) != 1: |
| 139 parser.error('Exactly 1 device must be attached.') |
| 140 device = device_utils.DeviceUtils(devices[0]) |
| 141 package_info = profiler.GetSupportedBrowsers()[options.browser] |
| 142 |
| 143 if options.chrome_categories in ['list', 'help']: |
| 144 ui.PrintMessage('Collecting record categories list...', eol='') |
| 145 record_categories = [] |
| 146 disabled_by_default_categories = [] |
| 147 record_categories, disabled_by_default_categories = \ |
| 148 chrome_controller.ChromeTracingController.GetCategories( |
| 149 device, package_info) |
| 150 |
| 151 ui.PrintMessage('done') |
| 152 ui.PrintMessage('Record Categories:') |
| 153 ui.PrintMessage('\n'.join('\t%s' % item \ |
| 154 for item in sorted(record_categories))) |
| 155 |
| 156 ui.PrintMessage('\nDisabled by Default Categories:') |
| 157 ui.PrintMessage('\n'.join('\t%s' % item \ |
| 158 for item in sorted(disabled_by_default_categories))) |
| 159 |
| 160 return 0 |
| 161 |
| 162 if options.systrace_categories in ['list', 'help']: |
| 163 ui.PrintMessage('\n'.join( |
| 164 systrace_controller.SystraceController.GetCategories(device))) |
| 165 return 0 |
| 166 |
| 167 if not options.time and not options.continuous: |
| 168 ui.PrintMessage('Time interval or continuous tracing should be specified.') |
| 169 return 1 |
| 170 |
| 171 chrome_categories = _ComputeChromeCategories(options) |
| 172 systrace_categories = _ComputeSystraceCategories(options) |
| 173 |
| 174 if chrome_categories and 'webview' in systrace_categories: |
| 175 logging.warning('Using the "webview" category in systrace together with ' |
| 176 'Chrome tracing results in duplicate trace events.') |
| 177 |
| 178 enabled_controllers = [] |
| 179 if chrome_categories: |
| 180 enabled_controllers.append( |
| 181 chrome_controller.ChromeTracingController(device, |
| 182 package_info, |
| 183 chrome_categories, |
| 184 options.ring_buffer)) |
| 185 if systrace_categories: |
| 186 enabled_controllers.append( |
| 187 systrace_controller.SystraceController(device, |
| 188 systrace_categories, |
| 189 options.ring_buffer)) |
| 190 |
| 191 if not enabled_controllers: |
| 192 ui.PrintMessage('No trace categories enabled.') |
| 193 return 1 |
| 194 |
| 195 if options.output: |
| 196 options.output = os.path.expanduser(options.output) |
| 197 result = profiler.CaptureProfile( |
| 198 enabled_controllers, |
| 199 options.time if not options.continuous else 0, |
| 200 output=options.output, |
| 201 compress=options.compress, |
| 202 write_json=options.json) |
| 203 if options.view: |
| 204 if sys.platform == 'darwin': |
| 205 os.system('/usr/bin/open %s' % os.path.abspath(result)) |
| 206 else: |
| 207 webbrowser.open(result) |
| 208 |
| 209 |
| 210 if __name__ == '__main__': |
| 211 sys.exit(main()) |
OLD | NEW |