Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / tools / profile_chrome / main.py
blob1dbf08a60d9e98cf67ca6f21cebc369988aaf689
1 #!/usr/bin/env python
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.
7 import logging
8 import optparse
9 import os
10 import sys
11 import webbrowser
13 from profile_chrome import chrome_controller
14 from profile_chrome import perf_controller
15 from profile_chrome import profiler
16 from profile_chrome import systrace_controller
17 from profile_chrome import ui
19 from pylib import android_commands
20 from pylib.device import device_utils
23 _DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES'
26 def _ComputeChromeCategories(options):
27 categories = []
28 if options.trace_frame_viewer:
29 categories.append('disabled-by-default-cc.debug')
30 if options.trace_ubercompositor:
31 categories.append('disabled-by-default-cc.debug*')
32 if options.trace_gpu:
33 categories.append('disabled-by-default-gpu.debug*')
34 if options.trace_flow:
35 categories.append('disabled-by-default-toplevel.flow')
36 if options.trace_memory:
37 categories.append('disabled-by-default-memory')
38 if options.trace_scheduler:
39 categories.append('disabled-by-default-blink.scheduler')
40 categories.append('disabled-by-default-cc.debug.scheduler')
41 categories.append('disabled-by-default-renderer.scheduler')
42 if options.chrome_categories:
43 categories += options.chrome_categories.split(',')
44 return categories
47 def _ComputeSystraceCategories(options):
48 if not options.systrace_categories:
49 return []
50 return options.systrace_categories.split(',')
53 def _ComputePerfCategories(options):
54 if not perf_controller.PerfProfilerController.IsSupported():
55 return []
56 if not options.perf_categories:
57 return []
58 return options.perf_categories.split(',')
61 def _OptionalValueCallback(default_value):
62 def callback(option, _, __, parser):
63 value = default_value
64 if parser.rargs and not parser.rargs[0].startswith('-'):
65 value = parser.rargs.pop(0)
66 setattr(parser.values, option.dest, value)
67 return callback
70 def _CreateOptionParser():
71 parser = optparse.OptionParser(description='Record about://tracing profiles '
72 'from Android browsers. See http://dev.'
73 'chromium.org/developers/how-tos/trace-event-'
74 'profiling-tool for detailed instructions for '
75 'profiling.')
77 timed_options = optparse.OptionGroup(parser, 'Timed tracing')
78 timed_options.add_option('-t', '--time', help='Profile for N seconds and '
79 'download the resulting trace.', metavar='N',
80 type='float')
81 parser.add_option_group(timed_options)
83 cont_options = optparse.OptionGroup(parser, 'Continuous tracing')
84 cont_options.add_option('--continuous', help='Profile continuously until '
85 'stopped.', action='store_true')
86 cont_options.add_option('--ring-buffer', help='Use the trace buffer as a '
87 'ring buffer and save its contents when stopping '
88 'instead of appending events into one long trace.',
89 action='store_true')
90 parser.add_option_group(cont_options)
92 chrome_opts = optparse.OptionGroup(parser, 'Chrome tracing options')
93 chrome_opts.add_option('-c', '--categories', help='Select Chrome tracing '
94 'categories with comma-delimited wildcards, '
95 'e.g., "*", "cat1*,-cat1a". Omit this option to trace '
96 'Chrome\'s default categories. Chrome tracing can be '
97 'disabled with "--categories=\'\'". Use "list" to '
98 'see the available categories.',
99 metavar='CHROME_CATEGORIES', dest='chrome_categories',
100 default=_DEFAULT_CHROME_CATEGORIES)
101 chrome_opts.add_option('--trace-cc',
102 help='Deprecated, use --trace-frame-viewer.',
103 action='store_true')
104 chrome_opts.add_option('--trace-frame-viewer',
105 help='Enable enough trace categories for '
106 'compositor frame viewing.', action='store_true')
107 chrome_opts.add_option('--trace-ubercompositor',
108 help='Enable enough trace categories for '
109 'ubercompositor frame data.', action='store_true')
110 chrome_opts.add_option('--trace-gpu', help='Enable extra trace categories '
111 'for GPU data.', action='store_true')
112 chrome_opts.add_option('--trace-flow', help='Enable extra trace categories '
113 'for IPC message flows.', action='store_true')
114 chrome_opts.add_option('--trace-memory', help='Enable extra trace categories '
115 'for memory profile. (tcmalloc required)',
116 action='store_true')
117 chrome_opts.add_option('--trace-scheduler', help='Enable extra trace '
118 'categories for scheduler state',
119 action='store_true')
120 parser.add_option_group(chrome_opts)
122 systrace_opts = optparse.OptionGroup(parser, 'Systrace tracing options')
123 systrace_opts.add_option('-s', '--systrace', help='Capture a systrace with '
124 'the chosen comma-delimited systrace categories. You '
125 'can also capture a combined Chrome + systrace by '
126 'enable both types of categories. Use "list" to see '
127 'the available categories. Systrace is disabled by '
128 'default.', metavar='SYS_CATEGORIES',
129 dest='systrace_categories', default='')
130 parser.add_option_group(systrace_opts)
132 if perf_controller.PerfProfilerController.IsSupported():
133 perf_opts = optparse.OptionGroup(parser, 'Perf profiling options')
134 perf_opts.add_option('-p', '--perf', help='Capture a perf profile with '
135 'the chosen comma-delimited event categories. '
136 'Samples CPU cycles by default. Use "list" to see '
137 'the available sample types.', action='callback',
138 default='', callback=_OptionalValueCallback('cycles'),
139 metavar='PERF_CATEGORIES', dest='perf_categories')
140 parser.add_option_group(perf_opts)
142 output_options = optparse.OptionGroup(parser, 'Output options')
143 output_options.add_option('-o', '--output', help='Save trace output to file.')
144 output_options.add_option('--json', help='Save trace as raw JSON instead of '
145 'HTML.', action='store_true')
146 output_options.add_option('--view', help='Open resulting trace file in a '
147 'browser.', action='store_true')
148 parser.add_option_group(output_options)
150 browsers = sorted(profiler.GetSupportedBrowsers().keys())
151 parser.add_option('-b', '--browser', help='Select among installed browsers. '
152 'One of ' + ', '.join(browsers) + ', "stable" is used by '
153 'default.', type='choice', choices=browsers,
154 default='stable')
155 parser.add_option('-v', '--verbose', help='Verbose logging.',
156 action='store_true')
157 parser.add_option('-z', '--compress', help='Compress the resulting trace '
158 'with gzip. ', action='store_true')
159 return parser
162 def main():
163 parser = _CreateOptionParser()
164 options, _args = parser.parse_args()
165 if options.trace_cc:
166 parser.parse_error("""--trace-cc is deprecated.
168 For basic jank busting uses, use --trace-frame-viewer
169 For detailed study of ubercompositor, pass --trace-ubercompositor.
171 When in doubt, just try out --trace-frame-viewer.
172 """)
174 if options.verbose:
175 logging.getLogger().setLevel(logging.DEBUG)
177 devices = android_commands.GetAttachedDevices()
178 if len(devices) != 1:
179 parser.error('Exactly 1 device must be attached.')
180 device = device_utils.DeviceUtils(devices[0])
181 package_info = profiler.GetSupportedBrowsers()[options.browser]
183 if options.chrome_categories in ['list', 'help']:
184 ui.PrintMessage('Collecting record categories list...', eol='')
185 record_categories = []
186 disabled_by_default_categories = []
187 record_categories, disabled_by_default_categories = \
188 chrome_controller.ChromeTracingController.GetCategories(
189 device, package_info)
191 ui.PrintMessage('done')
192 ui.PrintMessage('Record Categories:')
193 ui.PrintMessage('\n'.join('\t%s' % item \
194 for item in sorted(record_categories)))
196 ui.PrintMessage('\nDisabled by Default Categories:')
197 ui.PrintMessage('\n'.join('\t%s' % item \
198 for item in sorted(disabled_by_default_categories)))
200 return 0
202 if options.systrace_categories in ['list', 'help']:
203 ui.PrintMessage('\n'.join(
204 systrace_controller.SystraceController.GetCategories(device)))
205 return 0
207 if (perf_controller.PerfProfilerController.IsSupported() and
208 options.perf_categories in ['list', 'help']):
209 ui.PrintMessage('\n'.join(
210 perf_controller.PerfProfilerController.GetCategories(device)))
211 return 0
213 if not options.time and not options.continuous:
214 ui.PrintMessage('Time interval or continuous tracing should be specified.')
215 return 1
217 chrome_categories = _ComputeChromeCategories(options)
218 systrace_categories = _ComputeSystraceCategories(options)
219 perf_categories = _ComputePerfCategories(options)
221 if chrome_categories and 'webview' in systrace_categories:
222 logging.warning('Using the "webview" category in systrace together with '
223 'Chrome tracing results in duplicate trace events.')
225 enabled_controllers = []
226 if chrome_categories:
227 enabled_controllers.append(
228 chrome_controller.ChromeTracingController(device,
229 package_info,
230 chrome_categories,
231 options.ring_buffer,
232 options.trace_memory))
233 if systrace_categories:
234 enabled_controllers.append(
235 systrace_controller.SystraceController(device,
236 systrace_categories,
237 options.ring_buffer))
239 if perf_categories:
240 enabled_controllers.append(
241 perf_controller.PerfProfilerController(device,
242 perf_categories))
244 if not enabled_controllers:
245 ui.PrintMessage('No trace categories enabled.')
246 return 1
248 if options.output:
249 options.output = os.path.expanduser(options.output)
250 result = profiler.CaptureProfile(
251 enabled_controllers,
252 options.time if not options.continuous else 0,
253 output=options.output,
254 compress=options.compress,
255 write_json=options.json)
256 if options.view:
257 if sys.platform == 'darwin':
258 os.system('/usr/bin/open %s' % os.path.abspath(result))
259 else:
260 webbrowser.open(result)