OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 import logging | 7 import logging |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 import webbrowser | 11 import webbrowser |
12 | 12 |
13 from chrome_profiler import chrome_controller | 13 from chrome_profiler import chrome_controller |
14 from chrome_profiler import perf_controller | |
14 from chrome_profiler import profiler | 15 from chrome_profiler import profiler |
15 from chrome_profiler import systrace_controller | 16 from chrome_profiler import systrace_controller |
16 from chrome_profiler import ui | 17 from chrome_profiler import ui |
17 | 18 |
18 from pylib import android_commands | 19 from pylib import android_commands |
19 from pylib.device import device_utils | 20 from pylib.device import device_utils |
20 | 21 |
21 | 22 |
22 _DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES' | 23 _DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES' |
23 | 24 |
(...skipping 12 matching lines...) Expand all Loading... | |
36 categories += options.chrome_categories.split(',') | 37 categories += options.chrome_categories.split(',') |
37 return categories | 38 return categories |
38 | 39 |
39 | 40 |
40 def _ComputeSystraceCategories(options): | 41 def _ComputeSystraceCategories(options): |
41 if not options.systrace_categories: | 42 if not options.systrace_categories: |
42 return [] | 43 return [] |
43 return options.systrace_categories.split(',') | 44 return options.systrace_categories.split(',') |
44 | 45 |
45 | 46 |
47 def _ComputePerfCategories(options): | |
48 if not options.perf_categories: | |
49 return [] | |
50 return options.perf_categories.split(',') | |
51 | |
52 | |
53 def _OptionalValueCallback(default_value): | |
54 def callback(option, _, __, parser): | |
55 value = default_value | |
56 if parser.rargs and not parser.rargs[0].startswith('-'): | |
57 value = parser.rargs.pop(0) | |
58 setattr(parser.values, option.dest, value) | |
59 return callback | |
60 | |
61 | |
46 def _CreateOptionParser(): | 62 def _CreateOptionParser(): |
47 parser = optparse.OptionParser(description='Record about://tracing profiles ' | 63 parser = optparse.OptionParser(description='Record about://tracing profiles ' |
48 'from Android browsers. See http://dev.' | 64 'from Android browsers. See http://dev.' |
49 'chromium.org/developers/how-tos/trace-event-' | 65 'chromium.org/developers/how-tos/trace-event-' |
50 'profiling-tool for detailed instructions for ' | 66 'profiling-tool for detailed instructions for ' |
51 'profiling.') | 67 'profiling.') |
52 | 68 |
53 timed_options = optparse.OptionGroup(parser, 'Timed tracing') | 69 timed_options = optparse.OptionGroup(parser, 'Timed tracing') |
54 timed_options.add_option('-t', '--time', help='Profile for N seconds and ' | 70 timed_options.add_option('-t', '--time', help='Profile for N seconds and ' |
55 'download the resulting trace.', metavar='N', | 71 'download the resulting trace.', metavar='N', |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 systrace_opts = optparse.OptionGroup(parser, 'Systrace tracing options') | 108 systrace_opts = optparse.OptionGroup(parser, 'Systrace tracing options') |
93 systrace_opts.add_option('-s', '--systrace', help='Capture a systrace with ' | 109 systrace_opts.add_option('-s', '--systrace', help='Capture a systrace with ' |
94 'the chosen comma-delimited systrace categories. You ' | 110 'the chosen comma-delimited systrace categories. You ' |
95 'can also capture a combined Chrome + systrace by ' | 111 'can also capture a combined Chrome + systrace by ' |
96 'enable both types of categories. Use "list" to see ' | 112 'enable both types of categories. Use "list" to see ' |
97 'the available categories. Systrace is disabled by ' | 113 'the available categories. Systrace is disabled by ' |
98 'default.', metavar='SYS_CATEGORIES', | 114 'default.', metavar='SYS_CATEGORIES', |
99 dest='systrace_categories', default='') | 115 dest='systrace_categories', default='') |
100 parser.add_option_group(systrace_opts) | 116 parser.add_option_group(systrace_opts) |
101 | 117 |
118 perf_opts = optparse.OptionGroup(parser, 'Perf profiling options') | |
119 perf_opts.add_option('-p', '--perf', help='Capture a perf profile with the ' | |
120 'chosen comma-delimited event categories. Samples CPU ' | |
121 ' cycles by default. Use "list" to see the available ' | |
Dominik Grewe
2014/05/28 17:09:43
Nit: remove preceding whitespace
Sami
2014/06/02 17:56:38
Done.
| |
122 'sample types.', action='callback', default='', | |
123 callback=_OptionalValueCallback('cycles'), | |
124 metavar='PERF_CATEGORIES', dest='perf_categories') | |
125 parser.add_option_group(perf_opts) | |
126 | |
102 output_options = optparse.OptionGroup(parser, 'Output options') | 127 output_options = optparse.OptionGroup(parser, 'Output options') |
103 output_options.add_option('-o', '--output', help='Save trace output to file.') | 128 output_options.add_option('-o', '--output', help='Save trace output to file.') |
104 output_options.add_option('--json', help='Save trace as raw JSON instead of ' | 129 output_options.add_option('--json', help='Save trace as raw JSON instead of ' |
105 'HTML.', action='store_true') | 130 'HTML.', action='store_true') |
106 output_options.add_option('--view', help='Open resulting trace file in a ' | 131 output_options.add_option('--view', help='Open resulting trace file in a ' |
107 'browser.', action='store_true') | 132 'browser.', action='store_true') |
108 parser.add_option_group(output_options) | 133 parser.add_option_group(output_options) |
109 | 134 |
110 browsers = sorted(profiler.GetSupportedBrowsers().keys()) | 135 browsers = sorted(profiler.GetSupportedBrowsers().keys()) |
111 parser.add_option('-b', '--browser', help='Select among installed browsers. ' | 136 parser.add_option('-b', '--browser', help='Select among installed browsers. ' |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 ui.PrintMessage('\n'.join('\t%s' % item \ | 182 ui.PrintMessage('\n'.join('\t%s' % item \ |
158 for item in sorted(disabled_by_default_categories))) | 183 for item in sorted(disabled_by_default_categories))) |
159 | 184 |
160 return 0 | 185 return 0 |
161 | 186 |
162 if options.systrace_categories in ['list', 'help']: | 187 if options.systrace_categories in ['list', 'help']: |
163 ui.PrintMessage('\n'.join( | 188 ui.PrintMessage('\n'.join( |
164 systrace_controller.SystraceController.GetCategories(device))) | 189 systrace_controller.SystraceController.GetCategories(device))) |
165 return 0 | 190 return 0 |
166 | 191 |
192 if options.perf_categories in ['list', 'help']: | |
193 ui.PrintMessage('\n'.join( | |
194 perf_controller.PerfProfilerController.GetCategories(device))) | |
195 return 0 | |
196 | |
167 if not options.time and not options.continuous: | 197 if not options.time and not options.continuous: |
168 ui.PrintMessage('Time interval or continuous tracing should be specified.') | 198 ui.PrintMessage('Time interval or continuous tracing should be specified.') |
169 return 1 | 199 return 1 |
170 | 200 |
171 chrome_categories = _ComputeChromeCategories(options) | 201 chrome_categories = _ComputeChromeCategories(options) |
172 systrace_categories = _ComputeSystraceCategories(options) | 202 systrace_categories = _ComputeSystraceCategories(options) |
203 perf_categories = _ComputePerfCategories(options) | |
173 | 204 |
174 if chrome_categories and 'webview' in systrace_categories: | 205 if chrome_categories and 'webview' in systrace_categories: |
175 logging.warning('Using the "webview" category in systrace together with ' | 206 logging.warning('Using the "webview" category in systrace together with ' |
176 'Chrome tracing results in duplicate trace events.') | 207 'Chrome tracing results in duplicate trace events.') |
177 | 208 |
178 enabled_controllers = [] | 209 enabled_controllers = [] |
179 if chrome_categories: | 210 if chrome_categories: |
180 enabled_controllers.append( | 211 enabled_controllers.append( |
181 chrome_controller.ChromeTracingController(device, | 212 chrome_controller.ChromeTracingController(device, |
182 package_info, | 213 package_info, |
183 chrome_categories, | 214 chrome_categories, |
184 options.ring_buffer)) | 215 options.ring_buffer)) |
185 if systrace_categories: | 216 if systrace_categories: |
186 enabled_controllers.append( | 217 enabled_controllers.append( |
187 systrace_controller.SystraceController(device, | 218 systrace_controller.SystraceController(device, |
188 systrace_categories, | 219 systrace_categories, |
189 options.ring_buffer)) | 220 options.ring_buffer)) |
190 | 221 |
222 if perf_categories: | |
223 enabled_controllers.append( | |
224 perf_controller.PerfProfilerController(device, | |
225 perf_categories)) | |
226 | |
191 if not enabled_controllers: | 227 if not enabled_controllers: |
192 ui.PrintMessage('No trace categories enabled.') | 228 ui.PrintMessage('No trace categories enabled.') |
193 return 1 | 229 return 1 |
194 | 230 |
195 if options.output: | 231 if options.output: |
196 options.output = os.path.expanduser(options.output) | 232 options.output = os.path.expanduser(options.output) |
197 result = profiler.CaptureProfile( | 233 result = profiler.CaptureProfile( |
198 enabled_controllers, | 234 enabled_controllers, |
199 options.time if not options.continuous else 0, | 235 options.time if not options.continuous else 0, |
200 output=options.output, | 236 output=options.output, |
201 compress=options.compress, | 237 compress=options.compress, |
202 write_json=options.json) | 238 write_json=options.json) |
203 if options.view: | 239 if options.view: |
204 if sys.platform == 'darwin': | 240 if sys.platform == 'darwin': |
205 os.system('/usr/bin/open %s' % os.path.abspath(result)) | 241 os.system('/usr/bin/open %s' % os.path.abspath(result)) |
206 else: | 242 else: |
207 webbrowser.open(result) | 243 webbrowser.open(result) |
208 | 244 |
209 | 245 |
210 if __name__ == '__main__': | 246 if __name__ == '__main__': |
211 sys.exit(main()) | 247 sys.exit(main()) |
OLD | NEW |