main.py revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
18dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org#!/usr/bin/env python
28dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org#
38dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org# Copyright 2014 The Chromium Authors. All rights reserved.
48dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org# Use of this source code is governed by a BSD-style license that can be
58dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org# found in the LICENSE file.
68dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
78dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgimport logging
88dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgimport optparse
90a98d870448f66ea0df7c37a47b38cf2d3b734e5commit-bot@chromium.orgimport os
108dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgimport sys
118dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgimport webbrowser
128dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
138dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgfrom profile_chrome import chrome_controller
148dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgfrom profile_chrome import perf_controller
15f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.orgfrom profile_chrome import profiler
16f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.orgfrom profile_chrome import systrace_controller
178dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgfrom profile_chrome import ui
188dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
198dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgfrom pylib import android_commands
208dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgfrom pylib.device import device_utils
218dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
228dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
238dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org_DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES'
248dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
258dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
268dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgdef _ComputeChromeCategories(options):
278dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  categories = []
288dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  if options.trace_frame_viewer:
298dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org    categories.append('disabled-by-default-cc.debug')
308dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  if options.trace_ubercompositor:
318dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org    categories.append('disabled-by-default-cc.debug*')
328dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  if options.trace_gpu:
338dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org    categories.append('disabled-by-default-gpu.debug*')
348dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  if options.trace_flow:
357066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org    categories.append('disabled-by-default-toplevel.flow')
367066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  if options.trace_memory:
378dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org    categories.append('disabled-by-default-memory')
388dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  if options.chrome_categories:
391e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org    categories += options.chrome_categories.split(',')
401e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  return categories
411e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org
421e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org
431e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.orgdef _ComputeSystraceCategories(options):
441e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  if not options.systrace_categories:
451e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org    return []
461e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  return options.systrace_categories.split(',')
471e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org
481e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org
491e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.orgdef _ComputePerfCategories(options):
501e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  if not perf_controller.PerfProfilerController.IsSupported():
511e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org    return []
521e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  if not options.perf_categories:
531e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org    return []
541e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  return options.perf_categories.split(',')
551e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org
561e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org
571e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.orgdef _OptionalValueCallback(default_value):
581e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  def callback(option, _, __, parser):
591e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org    value = default_value
601e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org    if parser.rargs and not parser.rargs[0].startswith('-'):
611e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org      value = parser.rargs.pop(0)
621e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org    setattr(parser.values, option.dest, value)
631e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  return callback
641e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org
651e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org
661e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.orgdef _CreateOptionParser():
671e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org  parser = optparse.OptionParser(description='Record about://tracing profiles '
688dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                                 'from Android browsers. See http://dev.'
698dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                                 'chromium.org/developers/how-tos/trace-event-'
708dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                                 'profiling-tool for detailed instructions for '
718dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                                 'profiling.')
728dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
738dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  timed_options = optparse.OptionGroup(parser, 'Timed tracing')
748dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  timed_options.add_option('-t', '--time', help='Profile for N seconds and '
758dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                          'download the resulting trace.', metavar='N',
768dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                           type='float')
778dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  parser.add_option_group(timed_options)
788dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
798dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  cont_options = optparse.OptionGroup(parser, 'Continuous tracing')
808dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  cont_options.add_option('--continuous', help='Profile continuously until '
818dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                          'stopped.', action='store_true')
828dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  cont_options.add_option('--ring-buffer', help='Use the trace buffer as a '
838dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                          'ring buffer and save its contents when stopping '
848dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                          'instead of appending events into one long trace.',
858dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                          action='store_true')
868dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  parser.add_option_group(cont_options)
878dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
887066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  chrome_opts = optparse.OptionGroup(parser, 'Chrome tracing options')
897066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  chrome_opts.add_option('-c', '--categories', help='Select Chrome tracing '
908dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'categories with comma-delimited wildcards, '
918dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'e.g., "*", "cat1*,-cat1a". Omit this option to trace '
928dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'Chrome\'s default categories. Chrome tracing can be '
938dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'disabled with "--categories=\'\'". Use "list" to '
948dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'see the available categories.',
958dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         metavar='CHROME_CATEGORIES', dest='chrome_categories',
968dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         default=_DEFAULT_CHROME_CATEGORIES)
978dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  chrome_opts.add_option('--trace-cc',
988dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         help='Deprecated, use --trace-frame-viewer.',
998dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         action='store_true')
1008dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  chrome_opts.add_option('--trace-frame-viewer',
1017066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org                         help='Enable enough trace categories for '
1028dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'compositor frame viewing.', action='store_true')
1038dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  chrome_opts.add_option('--trace-ubercompositor',
1048dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         help='Enable enough trace categories for '
1058dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'ubercompositor frame data.', action='store_true')
1068dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  chrome_opts.add_option('--trace-gpu', help='Enable extra trace categories '
1077066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org                         'for GPU data.', action='store_true')
1088dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  chrome_opts.add_option('--trace-flow', help='Enable extra trace categories '
1098dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'for IPC message flows.', action='store_true')
1108dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  chrome_opts.add_option('--trace-memory', help='Enable extra trace categories '
1118dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         'for memory profile. (tcmalloc required)',
1128dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                         action='store_true')
1138dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  parser.add_option_group(chrome_opts)
1148dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
115467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org  systrace_opts = optparse.OptionGroup(parser, 'Systrace tracing options')
116467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org  systrace_opts.add_option('-s', '--systrace', help='Capture a systrace with '
117467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                        'the chosen comma-delimited systrace categories. You '
118467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                        'can also capture a combined Chrome + systrace by '
119467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                        'enable both types of categories. Use "list" to see '
120467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                        'the available categories. Systrace is disabled by '
121467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                        'default.', metavar='SYS_CATEGORIES',
122467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                        dest='systrace_categories', default='')
123467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org  parser.add_option_group(systrace_opts)
124467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org
125467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org  if perf_controller.PerfProfilerController.IsSupported():
126467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org    perf_opts = optparse.OptionGroup(parser, 'Perf profiling options')
127467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org    perf_opts.add_option('-p', '--perf', help='Capture a perf profile with '
128467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                         'the chosen comma-delimited event categories. '
129467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                         'Samples CPU cycles by default. Use "list" to see '
130467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                         'the available sample types.', action='callback',
131467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                         default='', callback=_OptionalValueCallback('cycles'),
132467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org                         metavar='PERF_CATEGORIES', dest='perf_categories')
133467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org    parser.add_option_group(perf_opts)
134467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org
135467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org  output_options = optparse.OptionGroup(parser, 'Output options')
136467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org  output_options.add_option('-o', '--output', help='Save trace output to file.')
1377066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  output_options.add_option('--json', help='Save trace as raw JSON instead of '
1387066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org                            'HTML.', action='store_true')
1397066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  output_options.add_option('--view', help='Open resulting trace file in a '
1407066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org                            'browser.', action='store_true')
1417066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  parser.add_option_group(output_options)
1427066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org
1437066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  browsers = sorted(profiler.GetSupportedBrowsers().keys())
1447066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  parser.add_option('-b', '--browser', help='Select among installed browsers. '
1457066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org                    'One of ' + ', '.join(browsers) + ', "stable" is used by '
1467066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org                    'default.', type='choice', choices=browsers,
1477066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org                    default='stable')
1487066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  parser.add_option('-v', '--verbose', help='Verbose logging.',
1497066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org                    action='store_true')
1507066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  parser.add_option('-z', '--compress', help='Compress the resulting trace '
1518dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org                    'with gzip. ', action='store_true')
1528dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  return parser
1538dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
1548dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
1558dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgdef main():
1568dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  parser = _CreateOptionParser()
1578dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  options, _args = parser.parse_args()
1588dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  if options.trace_cc:
1598dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org    parser.parse_error("""--trace-cc is deprecated.
1608dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
1618dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgFor basic jank busting uses, use  --trace-frame-viewer
1628dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgFor detailed study of ubercompositor, pass --trace-ubercompositor.
1638dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
1648dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.orgWhen in doubt, just try out --trace-frame-viewer.
1658dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org""")
1668dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
1678dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  if options.verbose:
1688dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org    logging.getLogger().setLevel(logging.DEBUG)
1698dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org
1707066bf3fbff7a54d3692414ec11ca419f3fd0ad5commit-bot@chromium.org  devices = android_commands.GetAttachedDevices()
1718dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org  if len(devices) != 1:
1728dac8b18eea8a729062440b85285d19fc890bb1acommit-bot@chromium.org    parser.error('Exactly 1 device must be attached.')
173f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  device = device_utils.DeviceUtils(devices[0])
174f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  package_info = profiler.GetSupportedBrowsers()[options.browser]
175f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
176f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if options.chrome_categories in ['list', 'help']:
177f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('Collecting record categories list...', eol='')
178f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    record_categories = []
179f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    disabled_by_default_categories = []
180f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    record_categories, disabled_by_default_categories = \
181f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org        chrome_controller.ChromeTracingController.GetCategories(
182f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org            device, package_info)
183f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
184f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('done')
185f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('Record Categories:')
186f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('\n'.join('\t%s' % item \
187f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org        for item in sorted(record_categories)))
188f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
189f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('\nDisabled by Default Categories:')
190f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('\n'.join('\t%s' % item \
191f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org        for item in sorted(disabled_by_default_categories)))
192f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
193f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    return 0
194f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
195f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if options.systrace_categories in ['list', 'help']:
196f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('\n'.join(
197f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org        systrace_controller.SystraceController.GetCategories(device)))
198f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    return 0
199f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
200f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if (perf_controller.PerfProfilerController.IsSupported() and
201f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org      options.perf_categories in ['list', 'help']):
202f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('\n'.join(
203f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org        perf_controller.PerfProfilerController.GetCategories(device)))
204f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    return 0
205f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
206f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if not options.time and not options.continuous:
207f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('Time interval or continuous tracing should be specified.')
208f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    return 1
209f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
210f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  chrome_categories = _ComputeChromeCategories(options)
211f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  systrace_categories = _ComputeSystraceCategories(options)
212f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  perf_categories = _ComputePerfCategories(options)
213f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
214f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if chrome_categories and 'webview' in systrace_categories:
215f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    logging.warning('Using the "webview" category in systrace together with '
216f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org                    'Chrome tracing results in duplicate trace events.')
217f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
218f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  enabled_controllers = []
219f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if chrome_categories:
220f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    enabled_controllers.append(
221f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org        chrome_controller.ChromeTracingController(device,
222f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org                                                  package_info,
223f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org                                                  chrome_categories,
224f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org                                                  options.ring_buffer,
225f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org                                                  options.trace_memory))
226f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if systrace_categories:
227f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    enabled_controllers.append(
228f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org        systrace_controller.SystraceController(device,
229f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org                                               systrace_categories,
230f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org                                               options.ring_buffer))
231f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
232f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if perf_categories:
233f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    enabled_controllers.append(
234f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org        perf_controller.PerfProfilerController(device,
235f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org                                               perf_categories))
236f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
237f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if not enabled_controllers:
238f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    ui.PrintMessage('No trace categories enabled.')
239f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    return 1
240f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org
241f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  if options.output:
242f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org    options.output = os.path.expanduser(options.output)
243f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org  result = profiler.CaptureProfile(
244f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org      enabled_controllers,
245f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org      options.time if not options.continuous else 0,
246      output=options.output,
247      compress=options.compress,
248      write_json=options.json)
249  if options.view:
250    if sys.platform == 'darwin':
251      os.system('/usr/bin/open %s' % os.path.abspath(result))
252    else:
253      webbrowser.open(result)
254