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 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
):
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*')
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-cc.debug.scheduler')
40 if options
.chrome_categories
:
41 categories
+= options
.chrome_categories
.split(',')
45 def _ComputeSystraceCategories(options
):
46 if not options
.systrace_categories
:
48 return options
.systrace_categories
.split(',')
51 def _ComputePerfCategories(options
):
52 if not perf_controller
.PerfProfilerController
.IsSupported():
54 if not options
.perf_categories
:
56 return options
.perf_categories
.split(',')
59 def _OptionalValueCallback(default_value
):
60 def callback(option
, _
, __
, parser
):
62 if parser
.rargs
and not parser
.rargs
[0].startswith('-'):
63 value
= parser
.rargs
.pop(0)
64 setattr(parser
.values
, option
.dest
, value
)
68 def _CreateOptionParser():
69 parser
= optparse
.OptionParser(description
='Record about://tracing profiles '
70 'from Android browsers. See http://dev.'
71 'chromium.org/developers/how-tos/trace-event-'
72 'profiling-tool for detailed instructions for '
75 timed_options
= optparse
.OptionGroup(parser
, 'Timed tracing')
76 timed_options
.add_option('-t', '--time', help='Profile for N seconds and '
77 'download the resulting trace.', metavar
='N',
79 parser
.add_option_group(timed_options
)
81 cont_options
= optparse
.OptionGroup(parser
, 'Continuous tracing')
82 cont_options
.add_option('--continuous', help='Profile continuously until '
83 'stopped.', action
='store_true')
84 cont_options
.add_option('--ring-buffer', help='Use the trace buffer as a '
85 'ring buffer and save its contents when stopping '
86 'instead of appending events into one long trace.',
88 parser
.add_option_group(cont_options
)
90 chrome_opts
= optparse
.OptionGroup(parser
, 'Chrome tracing options')
91 chrome_opts
.add_option('-c', '--categories', help='Select Chrome tracing '
92 'categories with comma-delimited wildcards, '
93 'e.g., "*", "cat1*,-cat1a". Omit this option to trace '
94 'Chrome\'s default categories. Chrome tracing can be '
95 'disabled with "--categories=\'\'". Use "list" to '
96 'see the available categories.',
97 metavar
='CHROME_CATEGORIES', dest
='chrome_categories',
98 default
=_DEFAULT_CHROME_CATEGORIES
)
99 chrome_opts
.add_option('--trace-cc',
100 help='Deprecated, use --trace-frame-viewer.',
102 chrome_opts
.add_option('--trace-frame-viewer',
103 help='Enable enough trace categories for '
104 'compositor frame viewing.', action
='store_true')
105 chrome_opts
.add_option('--trace-ubercompositor',
106 help='Enable enough trace categories for '
107 'ubercompositor frame data.', action
='store_true')
108 chrome_opts
.add_option('--trace-gpu', help='Enable extra trace categories '
109 'for GPU data.', action
='store_true')
110 chrome_opts
.add_option('--trace-flow', help='Enable extra trace categories '
111 'for IPC message flows.', action
='store_true')
112 chrome_opts
.add_option('--trace-memory', help='Enable extra trace categories '
113 'for memory profile. (tcmalloc required)',
115 chrome_opts
.add_option('--trace-scheduler', help='Enable extra trace '
116 'categories for scheduler state',
118 parser
.add_option_group(chrome_opts
)
120 systrace_opts
= optparse
.OptionGroup(parser
, 'Systrace tracing options')
121 systrace_opts
.add_option('-s', '--systrace', help='Capture a systrace with '
122 'the chosen comma-delimited systrace categories. You '
123 'can also capture a combined Chrome + systrace by '
124 'enable both types of categories. Use "list" to see '
125 'the available categories. Systrace is disabled by '
126 'default.', metavar
='SYS_CATEGORIES',
127 dest
='systrace_categories', default
='')
128 parser
.add_option_group(systrace_opts
)
130 if perf_controller
.PerfProfilerController
.IsSupported():
131 perf_opts
= optparse
.OptionGroup(parser
, 'Perf profiling options')
132 perf_opts
.add_option('-p', '--perf', help='Capture a perf profile with '
133 'the chosen comma-delimited event categories. '
134 'Samples CPU cycles by default. Use "list" to see '
135 'the available sample types.', action
='callback',
136 default
='', callback
=_OptionalValueCallback('cycles'),
137 metavar
='PERF_CATEGORIES', dest
='perf_categories')
138 parser
.add_option_group(perf_opts
)
140 output_options
= optparse
.OptionGroup(parser
, 'Output options')
141 output_options
.add_option('-o', '--output', help='Save trace output to file.')
142 output_options
.add_option('--json', help='Save trace as raw JSON instead of '
143 'HTML.', action
='store_true')
144 output_options
.add_option('--view', help='Open resulting trace file in a '
145 'browser.', action
='store_true')
146 parser
.add_option_group(output_options
)
148 browsers
= sorted(profiler
.GetSupportedBrowsers().keys())
149 parser
.add_option('-b', '--browser', help='Select among installed browsers. '
150 'One of ' + ', '.join(browsers
) + ', "stable" is used by '
151 'default.', type='choice', choices
=browsers
,
153 parser
.add_option('-v', '--verbose', help='Verbose logging.',
155 parser
.add_option('-z', '--compress', help='Compress the resulting trace '
156 'with gzip. ', action
='store_true')
161 parser
= _CreateOptionParser()
162 options
, _args
= parser
.parse_args()
164 parser
.parse_error("""--trace-cc is deprecated.
166 For basic jank busting uses, use --trace-frame-viewer
167 For detailed study of ubercompositor, pass --trace-ubercompositor.
169 When in doubt, just try out --trace-frame-viewer.
173 logging
.getLogger().setLevel(logging
.DEBUG
)
175 devices
= android_commands
.GetAttachedDevices()
176 if len(devices
) != 1:
177 parser
.error('Exactly 1 device must be attached.')
178 device
= device_utils
.DeviceUtils(devices
[0])
179 package_info
= profiler
.GetSupportedBrowsers()[options
.browser
]
181 if options
.chrome_categories
in ['list', 'help']:
182 ui
.PrintMessage('Collecting record categories list...', eol
='')
183 record_categories
= []
184 disabled_by_default_categories
= []
185 record_categories
, disabled_by_default_categories
= \
186 chrome_controller
.ChromeTracingController
.GetCategories(
187 device
, package_info
)
189 ui
.PrintMessage('done')
190 ui
.PrintMessage('Record Categories:')
191 ui
.PrintMessage('\n'.join('\t%s' % item \
192 for item
in sorted(record_categories
)))
194 ui
.PrintMessage('\nDisabled by Default Categories:')
195 ui
.PrintMessage('\n'.join('\t%s' % item \
196 for item
in sorted(disabled_by_default_categories
)))
200 if options
.systrace_categories
in ['list', 'help']:
201 ui
.PrintMessage('\n'.join(
202 systrace_controller
.SystraceController
.GetCategories(device
)))
205 if (perf_controller
.PerfProfilerController
.IsSupported() and
206 options
.perf_categories
in ['list', 'help']):
207 ui
.PrintMessage('\n'.join(
208 perf_controller
.PerfProfilerController
.GetCategories(device
)))
211 if not options
.time
and not options
.continuous
:
212 ui
.PrintMessage('Time interval or continuous tracing should be specified.')
215 chrome_categories
= _ComputeChromeCategories(options
)
216 systrace_categories
= _ComputeSystraceCategories(options
)
217 perf_categories
= _ComputePerfCategories(options
)
219 if chrome_categories
and 'webview' in systrace_categories
:
220 logging
.warning('Using the "webview" category in systrace together with '
221 'Chrome tracing results in duplicate trace events.')
223 enabled_controllers
= []
224 if chrome_categories
:
225 enabled_controllers
.append(
226 chrome_controller
.ChromeTracingController(device
,
230 options
.trace_memory
))
231 if systrace_categories
:
232 enabled_controllers
.append(
233 systrace_controller
.SystraceController(device
,
235 options
.ring_buffer
))
238 enabled_controllers
.append(
239 perf_controller
.PerfProfilerController(device
,
242 if not enabled_controllers
:
243 ui
.PrintMessage('No trace categories enabled.')
247 options
.output
= os
.path
.expanduser(options
.output
)
248 result
= profiler
.CaptureProfile(
250 options
.time
if not options
.continuous
else 0,
251 output
=options
.output
,
252 compress
=options
.compress
,
253 write_json
=options
.json
)
255 if sys
.platform
== 'darwin':
256 os
.system('/usr/bin/open %s' % os
.path
.abspath(result
))
258 webbrowser
.open(result
)