Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / tools / profile_chrome / main.py
blob8e61f2aa9bce7fd105331b23a98aec89d0b11859
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 ddms_controller
15 from profile_chrome import flags
16 from profile_chrome import perf_controller
17 from profile_chrome import profiler
18 from profile_chrome import systrace_controller
19 from profile_chrome import ui
21 from pylib.device import device_utils
24 _DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES'
27 def _ComputeChromeCategories(options):
28 categories = []
29 if options.trace_frame_viewer:
30 categories.append('disabled-by-default-cc.debug')
31 if options.trace_ubercompositor:
32 categories.append('disabled-by-default-cc.debug*')
33 if options.trace_gpu:
34 categories.append('disabled-by-default-gpu.debug*')
35 if options.trace_flow:
36 categories.append('disabled-by-default-toplevel.flow')
37 if options.trace_memory:
38 categories.append('disabled-by-default-memory')
39 if options.trace_scheduler:
40 categories.append('disabled-by-default-blink.scheduler')
41 categories.append('disabled-by-default-cc.debug.scheduler')
42 categories.append('disabled-by-default-renderer.scheduler')
43 if options.chrome_categories:
44 categories += options.chrome_categories.split(',')
45 return categories
48 def _ComputeSystraceCategories(options):
49 if not options.systrace_categories:
50 return []
51 return options.systrace_categories.split(',')
54 def _ComputePerfCategories(options):
55 if not perf_controller.PerfProfilerController.IsSupported():
56 return []
57 if not options.perf_categories:
58 return []
59 return options.perf_categories.split(',')
62 def _OptionalValueCallback(default_value):
63 def callback(option, _, __, parser):
64 value = default_value
65 if parser.rargs and not parser.rargs[0].startswith('-'):
66 value = parser.rargs.pop(0)
67 setattr(parser.values, option.dest, value)
68 return callback
71 def _CreateOptionParser():
72 parser = optparse.OptionParser(description='Record about://tracing profiles '
73 'from Android browsers. See http://dev.'
74 'chromium.org/developers/how-tos/trace-event-'
75 'profiling-tool for detailed instructions for '
76 'profiling.')
78 timed_options = optparse.OptionGroup(parser, 'Timed tracing')
79 timed_options.add_option('-t', '--time', help='Profile for N seconds and '
80 'download the resulting trace.', metavar='N',
81 type='float')
82 parser.add_option_group(timed_options)
84 cont_options = optparse.OptionGroup(parser, 'Continuous tracing')
85 cont_options.add_option('--continuous', help='Profile continuously until '
86 'stopped.', action='store_true')
87 cont_options.add_option('--ring-buffer', help='Use the trace buffer as a '
88 'ring buffer and save its contents when stopping '
89 'instead of appending events into one long trace.',
90 action='store_true')
91 parser.add_option_group(cont_options)
93 chrome_opts = optparse.OptionGroup(parser, 'Chrome tracing options')
94 chrome_opts.add_option('-c', '--categories', help='Select Chrome tracing '
95 'categories with comma-delimited wildcards, '
96 'e.g., "*", "cat1*,-cat1a". Omit this option to trace '
97 'Chrome\'s default categories. Chrome tracing can be '
98 'disabled with "--categories=\'\'". Use "list" to '
99 'see the available categories.',
100 metavar='CHROME_CATEGORIES', dest='chrome_categories',
101 default=_DEFAULT_CHROME_CATEGORIES)
102 chrome_opts.add_option('--trace-cc',
103 help='Deprecated, use --trace-frame-viewer.',
104 action='store_true')
105 chrome_opts.add_option('--trace-frame-viewer',
106 help='Enable enough trace categories for '
107 'compositor frame viewing.', action='store_true')
108 chrome_opts.add_option('--trace-ubercompositor',
109 help='Enable enough trace categories for '
110 'ubercompositor frame data.', action='store_true')
111 chrome_opts.add_option('--trace-gpu', help='Enable extra trace categories '
112 'for GPU data.', action='store_true')
113 chrome_opts.add_option('--trace-flow', help='Enable extra trace categories '
114 'for IPC message flows.', action='store_true')
115 chrome_opts.add_option('--trace-memory', help='Enable extra trace categories '
116 'for memory profile. (tcmalloc required)',
117 action='store_true')
118 chrome_opts.add_option('--trace-scheduler', help='Enable extra trace '
119 'categories for scheduler state',
120 action='store_true')
121 parser.add_option_group(chrome_opts)
123 parser.add_option_group(flags.SystraceOptions(parser))
125 if perf_controller.PerfProfilerController.IsSupported():
126 perf_opts = optparse.OptionGroup(parser, 'Perf profiling options')
127 perf_opts.add_option('-p', '--perf', help='Capture a perf profile with '
128 'the chosen comma-delimited event categories. '
129 'Samples CPU cycles by default. Use "list" to see '
130 'the available sample types.', action='callback',
131 default='', callback=_OptionalValueCallback('cycles'),
132 metavar='PERF_CATEGORIES', dest='perf_categories')
133 parser.add_option_group(perf_opts)
135 ddms_options = optparse.OptionGroup(parser, 'Java tracing')
136 ddms_options.add_option('--ddms', help='Trace Java execution using DDMS '
137 'sampling.', action='store_true')
138 parser.add_option_group(ddms_options)
140 parser.add_option_group(flags.OutputOptions(parser))
142 browsers = sorted(profiler.GetSupportedBrowsers().keys())
143 parser.add_option('-b', '--browser', help='Select among installed browsers. '
144 'One of ' + ', '.join(browsers) + ', "stable" is used by '
145 'default.', type='choice', choices=browsers,
146 default='stable')
147 parser.add_option('-v', '--verbose', help='Verbose logging.',
148 action='store_true')
149 parser.add_option('-z', '--compress', help='Compress the resulting trace '
150 'with gzip. ', action='store_true')
151 parser.add_option('-d', '--device', help='The Android device ID to use, '
152 'defaults to the value of ANDROID_SERIAL environment '
153 'variable. If not specified, only 0 or 1 connected '
154 'devices are supported.',
155 default=os.environ.get('ANDROID_SERIAL', None))
156 return parser
159 def main():
160 parser = _CreateOptionParser()
161 options, _args = parser.parse_args()
162 if options.trace_cc:
163 parser.parse_error("""--trace-cc is deprecated.
165 For basic jank busting uses, use --trace-frame-viewer
166 For detailed study of ubercompositor, pass --trace-ubercompositor.
168 When in doubt, just try out --trace-frame-viewer.
169 """)
171 if options.verbose:
172 logging.getLogger().setLevel(logging.DEBUG)
174 devices = device_utils.DeviceUtils.HealthyDevices()
175 device = None
176 if options.device:
177 device = next((d for d in devices if d == options.device), None)
178 elif len(devices) == 1:
179 device = devices[0]
181 if not device:
182 parser.error('Use -d/--device to select a device:\n' + '\n'.join(devices))
183 package_info = profiler.GetSupportedBrowsers()[options.browser]
185 if options.chrome_categories in ['list', 'help']:
186 ui.PrintMessage('Collecting record categories list...', eol='')
187 record_categories = []
188 disabled_by_default_categories = []
189 record_categories, disabled_by_default_categories = \
190 chrome_controller.ChromeTracingController.GetCategories(
191 device, package_info)
193 ui.PrintMessage('done')
194 ui.PrintMessage('Record Categories:')
195 ui.PrintMessage('\n'.join('\t%s' % item \
196 for item in sorted(record_categories)))
198 ui.PrintMessage('\nDisabled by Default Categories:')
199 ui.PrintMessage('\n'.join('\t%s' % item \
200 for item in sorted(disabled_by_default_categories)))
202 return 0
204 if options.systrace_categories in ['list', 'help']:
205 ui.PrintMessage('\n'.join(
206 systrace_controller.SystraceController.GetCategories(device)))
207 return 0
209 if (perf_controller.PerfProfilerController.IsSupported() and
210 options.perf_categories in ['list', 'help']):
211 ui.PrintMessage('\n'.join(
212 perf_controller.PerfProfilerController.GetCategories(device)))
213 return 0
215 if not options.time and not options.continuous:
216 ui.PrintMessage('Time interval or continuous tracing should be specified.')
217 return 1
219 chrome_categories = _ComputeChromeCategories(options)
220 systrace_categories = _ComputeSystraceCategories(options)
221 perf_categories = _ComputePerfCategories(options)
223 if chrome_categories and 'webview' in systrace_categories:
224 logging.warning('Using the "webview" category in systrace together with '
225 'Chrome tracing results in duplicate trace events.')
227 enabled_controllers = []
228 if chrome_categories:
229 enabled_controllers.append(
230 chrome_controller.ChromeTracingController(device,
231 package_info,
232 chrome_categories,
233 options.ring_buffer,
234 options.trace_memory))
235 if systrace_categories:
236 enabled_controllers.append(
237 systrace_controller.SystraceController(device,
238 systrace_categories,
239 options.ring_buffer))
241 if perf_categories:
242 enabled_controllers.append(
243 perf_controller.PerfProfilerController(device,
244 perf_categories))
246 if options.ddms:
247 enabled_controllers.append(
248 ddms_controller.DdmsController(device,
249 package_info))
251 if not enabled_controllers:
252 ui.PrintMessage('No trace categories enabled.')
253 return 1
255 if options.output:
256 options.output = os.path.expanduser(options.output)
257 result = profiler.CaptureProfile(
258 enabled_controllers,
259 options.time if not options.continuous else 0,
260 output=options.output,
261 compress=options.compress,
262 write_json=options.json)
263 if options.view:
264 if sys.platform == 'darwin':
265 os.system('/usr/bin/open %s' % os.path.abspath(result))
266 else:
267 webbrowser.open(result)