• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright 2015 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.
6
7import logging
8import optparse
9import os
10import sys
11import webbrowser
12
13_SYSTRACE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
14sys.path.append(_SYSTRACE_DIR)
15
16from profile_chrome import chrome_startup_tracing_agent
17from profile_chrome import flags
18from profile_chrome import profiler
19from profile_chrome import ui
20from systrace import util
21from systrace.tracing_agents import atrace_agent
22
23_CATAPULT_DIR = os.path.join(
24    os.path.dirname(os.path.abspath(__file__)), '..', '..')
25sys.path.append(os.path.join(_CATAPULT_DIR, 'devil'))
26
27from devil.android import device_utils
28from devil.android.sdk import adb_wrapper
29
30
31_CHROME_STARTUP_MODULES = [atrace_agent, chrome_startup_tracing_agent]
32_DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES'
33
34
35def _CreateOptionParser():
36  parser = optparse.OptionParser(description='Record about://tracing profiles '
37                                 'from Android browsers startup, combined with '
38                                 'Android systrace. See http://dev.chromium.org'
39                                 '/developers/how-tos/trace-event-profiling-'
40                                 'tool for detailed instructions for '
41                                 'profiling.', conflict_handler='resolve')
42  parser = util.get_main_options(parser)
43
44  browsers = sorted(util.get_supported_browsers().keys())
45  parser.add_option('-b', '--browser', help='Select among installed browsers. '
46                    'One of ' + ', '.join(browsers) + ', "stable" is used by '
47                    'default.', type='choice', choices=browsers,
48                    default='stable')
49  parser.add_option('-v', '--verbose', help='Verbose logging.',
50                    action='store_true')
51  parser.add_option('-z', '--compress', help='Compress the resulting trace '
52                    'with gzip. ', action='store_true')
53  parser.add_option('-t', '--time', help='Stops tracing after N seconds, 0 to '
54                    'manually stop (startup trace ends after at most 5s).',
55                    default=5, metavar='N', type='int', dest='trace_time')
56  parser.add_option('-c', '--chrome_categories', help='Chrome tracing '
57                    'categories to record.', default=_DEFAULT_CHROME_CATEGORIES,
58                    type='string')
59  parser.add_option('-u', '--atrace-buffer-size', help='Number of bytes to'
60                    ' be used for capturing atrace data', type='int',
61                    default=None, dest='trace_buf_size')
62
63  parser.add_option_group(chrome_startup_tracing_agent.add_options(parser))
64  parser.add_option_group(atrace_agent.add_options(parser))
65  parser.add_option_group(flags.OutputOptions(parser))
66
67  return parser
68
69
70def main():
71  parser = _CreateOptionParser()
72  options, _ = parser.parse_args()
73
74  if not options.device_serial_number:
75    devices = [a.GetDeviceSerial() for a in adb_wrapper.AdbWrapper.Devices()]
76    if len(devices) == 0:
77      raise RuntimeError('No ADB devices connected.')
78    elif len(devices) >= 2:
79      raise RuntimeError('Multiple devices connected, serial number required')
80    options.device_serial_number = devices[0]
81
82  if options.verbose:
83    logging.getLogger().setLevel(logging.DEBUG)
84
85  devices = device_utils.DeviceUtils.HealthyDevices()
86  if len(devices) != 1:
87    logging.error('Exactly 1 device must be attached.')
88    return 1
89  device = devices[0]
90  package_info = util.get_supported_browsers()[options.browser]
91
92  options.device = device
93  options.package_info = package_info
94
95  # TODO(washingtonp): Once Systrace uses all of the profile_chrome agents,
96  # manually setting these options will no longer be necessary and should be
97  # removed.
98  options.ring_buffer = False
99  options.trace_memory = False
100
101  if options.atrace_categories in ['list', 'help']:
102    atrace_agent.list_categories(atrace_agent.get_config(options))
103    print '\n'
104    return 0
105  result = profiler.CaptureProfile(options,
106                                   options.trace_time,
107                                   _CHROME_STARTUP_MODULES,
108                                   output=options.output_file,
109                                   compress=options.compress,
110                                   write_json=options.write_json)
111  if options.view:
112    if sys.platform == 'darwin':
113      os.system('/usr/bin/open %s' % os.path.abspath(result))
114    else:
115      webbrowser.open(result)
116
117
118if __name__ == '__main__':
119  sys.exit(main())
120