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.
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
import android_commands
22 from pylib
.device
import device_utils
25 _DEFAULT_CHROME_CATEGORIES
= '_DEFAULT_CHROME_CATEGORIES'
28 def _ComputeChromeCategories(options
):
30 if options
.trace_frame_viewer
:
31 categories
.append('disabled-by-default-cc.debug')
32 if options
.trace_ubercompositor
:
33 categories
.append('disabled-by-default-cc.debug*')
35 categories
.append('disabled-by-default-gpu.debug*')
36 if options
.trace_flow
:
37 categories
.append('disabled-by-default-toplevel.flow')
38 if options
.trace_memory
:
39 categories
.append('disabled-by-default-memory')
40 if options
.trace_scheduler
:
41 categories
.append('disabled-by-default-blink.scheduler')
42 categories
.append('disabled-by-default-cc.debug.scheduler')
43 categories
.append('disabled-by-default-renderer.scheduler')
44 if options
.chrome_categories
:
45 categories
+= options
.chrome_categories
.split(',')
49 def _ComputeSystraceCategories(options
):
50 if not options
.systrace_categories
:
52 return options
.systrace_categories
.split(',')
55 def _ComputePerfCategories(options
):
56 if not perf_controller
.PerfProfilerController
.IsSupported():
58 if not options
.perf_categories
:
60 return options
.perf_categories
.split(',')
63 def _OptionalValueCallback(default_value
):
64 def callback(option
, _
, __
, parser
):
66 if parser
.rargs
and not parser
.rargs
[0].startswith('-'):
67 value
= parser
.rargs
.pop(0)
68 setattr(parser
.values
, option
.dest
, value
)
72 def _CreateOptionParser():
73 parser
= optparse
.OptionParser(description
='Record about://tracing profiles '
74 'from Android browsers. See http://dev.'
75 'chromium.org/developers/how-tos/trace-event-'
76 'profiling-tool for detailed instructions for '
79 timed_options
= optparse
.OptionGroup(parser
, 'Timed tracing')
80 timed_options
.add_option('-t', '--time', help='Profile for N seconds and '
81 'download the resulting trace.', metavar
='N',
83 parser
.add_option_group(timed_options
)
85 cont_options
= optparse
.OptionGroup(parser
, 'Continuous tracing')
86 cont_options
.add_option('--continuous', help='Profile continuously until '
87 'stopped.', action
='store_true')
88 cont_options
.add_option('--ring-buffer', help='Use the trace buffer as a '
89 'ring buffer and save its contents when stopping '
90 'instead of appending events into one long trace.',
92 parser
.add_option_group(cont_options
)
94 chrome_opts
= optparse
.OptionGroup(parser
, 'Chrome tracing options')
95 chrome_opts
.add_option('-c', '--categories', help='Select Chrome tracing '
96 'categories with comma-delimited wildcards, '
97 'e.g., "*", "cat1*,-cat1a". Omit this option to trace '
98 'Chrome\'s default categories. Chrome tracing can be '
99 'disabled with "--categories=\'\'". Use "list" to '
100 'see the available categories.',
101 metavar
='CHROME_CATEGORIES', dest
='chrome_categories',
102 default
=_DEFAULT_CHROME_CATEGORIES
)
103 chrome_opts
.add_option('--trace-cc',
104 help='Deprecated, use --trace-frame-viewer.',
106 chrome_opts
.add_option('--trace-frame-viewer',
107 help='Enable enough trace categories for '
108 'compositor frame viewing.', action
='store_true')
109 chrome_opts
.add_option('--trace-ubercompositor',
110 help='Enable enough trace categories for '
111 'ubercompositor frame data.', action
='store_true')
112 chrome_opts
.add_option('--trace-gpu', help='Enable extra trace categories '
113 'for GPU data.', action
='store_true')
114 chrome_opts
.add_option('--trace-flow', help='Enable extra trace categories '
115 'for IPC message flows.', action
='store_true')
116 chrome_opts
.add_option('--trace-memory', help='Enable extra trace categories '
117 'for memory profile. (tcmalloc required)',
119 chrome_opts
.add_option('--trace-scheduler', help='Enable extra trace '
120 'categories for scheduler state',
122 parser
.add_option_group(chrome_opts
)
124 parser
.add_option_group(flags
.SystraceOptions(parser
))
126 if perf_controller
.PerfProfilerController
.IsSupported():
127 perf_opts
= optparse
.OptionGroup(parser
, 'Perf profiling options')
128 perf_opts
.add_option('-p', '--perf', help='Capture a perf profile with '
129 'the chosen comma-delimited event categories. '
130 'Samples CPU cycles by default. Use "list" to see '
131 'the available sample types.', action
='callback',
132 default
='', callback
=_OptionalValueCallback('cycles'),
133 metavar
='PERF_CATEGORIES', dest
='perf_categories')
134 parser
.add_option_group(perf_opts
)
136 ddms_options
= optparse
.OptionGroup(parser
, 'Java tracing')
137 ddms_options
.add_option('--ddms', help='Trace Java execution using DDMS '
138 'sampling.', action
='store_true')
139 parser
.add_option_group(ddms_options
)
141 parser
.add_option_group(flags
.OutputOptions(parser
))
143 browsers
= sorted(profiler
.GetSupportedBrowsers().keys())
144 parser
.add_option('-b', '--browser', help='Select among installed browsers. '
145 'One of ' + ', '.join(browsers
) + ', "stable" is used by '
146 'default.', type='choice', choices
=browsers
,
148 parser
.add_option('-v', '--verbose', help='Verbose logging.',
150 parser
.add_option('-z', '--compress', help='Compress the resulting trace '
151 'with gzip. ', action
='store_true')
152 parser
.add_option('-d', '--device', help='The Android device ID to use.'
153 'If not specified, only 0 or 1 connected devices are '
154 'supported.', default
=None)
159 parser
= _CreateOptionParser()
160 options
, _args
= parser
.parse_args()
162 parser
.parse_error("""--trace-cc is deprecated.
164 For basic jank busting uses, use --trace-frame-viewer
165 For detailed study of ubercompositor, pass --trace-ubercompositor.
167 When in doubt, just try out --trace-frame-viewer.
171 logging
.getLogger().setLevel(logging
.DEBUG
)
173 devices
= android_commands
.GetAttachedDevices()
175 if options
.device
in devices
:
176 device
= options
.device
177 elif not options
.device
and len(devices
) == 1:
180 parser
.error('Use -d/--device to select a device:\n' + '\n'.join(devices
))
181 device
= device_utils
.DeviceUtils(device
)
182 package_info
= profiler
.GetSupportedBrowsers()[options
.browser
]
184 if options
.chrome_categories
in ['list', 'help']:
185 ui
.PrintMessage('Collecting record categories list...', eol
='')
186 record_categories
= []
187 disabled_by_default_categories
= []
188 record_categories
, disabled_by_default_categories
= \
189 chrome_controller
.ChromeTracingController
.GetCategories(
190 device
, package_info
)
192 ui
.PrintMessage('done')
193 ui
.PrintMessage('Record Categories:')
194 ui
.PrintMessage('\n'.join('\t%s' % item \
195 for item
in sorted(record_categories
)))
197 ui
.PrintMessage('\nDisabled by Default Categories:')
198 ui
.PrintMessage('\n'.join('\t%s' % item \
199 for item
in sorted(disabled_by_default_categories
)))
203 if options
.systrace_categories
in ['list', 'help']:
204 ui
.PrintMessage('\n'.join(
205 systrace_controller
.SystraceController
.GetCategories(device
)))
208 if (perf_controller
.PerfProfilerController
.IsSupported() and
209 options
.perf_categories
in ['list', 'help']):
210 ui
.PrintMessage('\n'.join(
211 perf_controller
.PerfProfilerController
.GetCategories(device
)))
214 if not options
.time
and not options
.continuous
:
215 ui
.PrintMessage('Time interval or continuous tracing should be specified.')
218 chrome_categories
= _ComputeChromeCategories(options
)
219 systrace_categories
= _ComputeSystraceCategories(options
)
220 perf_categories
= _ComputePerfCategories(options
)
222 if chrome_categories
and 'webview' in systrace_categories
:
223 logging
.warning('Using the "webview" category in systrace together with '
224 'Chrome tracing results in duplicate trace events.')
226 enabled_controllers
= []
227 if chrome_categories
:
228 enabled_controllers
.append(
229 chrome_controller
.ChromeTracingController(device
,
233 options
.trace_memory
))
234 if systrace_categories
:
235 enabled_controllers
.append(
236 systrace_controller
.SystraceController(device
,
238 options
.ring_buffer
))
241 enabled_controllers
.append(
242 perf_controller
.PerfProfilerController(device
,
246 enabled_controllers
.append(
247 ddms_controller
.DdmsController(device
,
250 if not enabled_controllers
:
251 ui
.PrintMessage('No trace categories enabled.')
255 options
.output
= os
.path
.expanduser(options
.output
)
256 result
= profiler
.CaptureProfile(
258 options
.time
if not options
.continuous
else 0,
259 output
=options
.output
,
260 compress
=options
.compress
,
261 write_json
=options
.json
)
263 if sys
.platform
== 'darwin':
264 os
.system('/usr/bin/open %s' % os
.path
.abspath(result
))
266 webbrowser
.open(result
)