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
.device
import device_utils
24 _DEFAULT_CHROME_CATEGORIES
= '_DEFAULT_CHROME_CATEGORIES'
27 def _ComputeChromeCategories(options
):
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*')
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(',')
48 def _ComputeSystraceCategories(options
):
49 if not options
.systrace_categories
:
51 return options
.systrace_categories
.split(',')
54 def _ComputePerfCategories(options
):
55 if not perf_controller
.PerfProfilerController
.IsSupported():
57 if not options
.perf_categories
:
59 return options
.perf_categories
.split(',')
62 def _OptionalValueCallback(default_value
):
63 def callback(option
, _
, __
, parser
):
65 if parser
.rargs
and not parser
.rargs
[0].startswith('-'):
66 value
= parser
.rargs
.pop(0)
67 setattr(parser
.values
, option
.dest
, value
)
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 '
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',
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.',
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.',
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)',
118 chrome_opts
.add_option('--trace-scheduler', help='Enable extra trace '
119 'categories for scheduler state',
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
,
147 parser
.add_option('-v', '--verbose', help='Verbose logging.',
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 'If not specified, only 0 or 1 connected devices are '
153 'supported.', default
=None)
158 parser
= _CreateOptionParser()
159 options
, _args
= parser
.parse_args()
161 parser
.parse_error("""--trace-cc is deprecated.
163 For basic jank busting uses, use --trace-frame-viewer
164 For detailed study of ubercompositor, pass --trace-ubercompositor.
166 When in doubt, just try out --trace-frame-viewer.
170 logging
.getLogger().setLevel(logging
.DEBUG
)
172 devices
= device_utils
.DeviceUtils
.HealthyDevices()
175 device
= next((d
for d
in devices
if d
== options
.device
), None)
176 elif len(devices
) == 1:
180 parser
.error('Use -d/--device to select a device:\n' + '\n'.join(devices
))
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
)))
202 if options
.systrace_categories
in ['list', 'help']:
203 ui
.PrintMessage('\n'.join(
204 systrace_controller
.SystraceController
.GetCategories(device
)))
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
)))
213 if not options
.time
and not options
.continuous
:
214 ui
.PrintMessage('Time interval or continuous tracing should be specified.')
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
,
232 options
.trace_memory
))
233 if systrace_categories
:
234 enabled_controllers
.append(
235 systrace_controller
.SystraceController(device
,
237 options
.ring_buffer
))
240 enabled_controllers
.append(
241 perf_controller
.PerfProfilerController(device
,
245 enabled_controllers
.append(
246 ddms_controller
.DdmsController(device
,
249 if not enabled_controllers
:
250 ui
.PrintMessage('No trace categories enabled.')
254 options
.output
= os
.path
.expanduser(options
.output
)
255 result
= profiler
.CaptureProfile(
257 options
.time
if not options
.continuous
else 0,
258 output
=options
.output
,
259 compress
=options
.compress
,
260 write_json
=options
.json
)
262 if sys
.platform
== 'darwin':
263 os
.system('/usr/bin/open %s' % os
.path
.abspath(result
))
265 webbrowser
.open(result
)