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