Home | History | Annotate | Download | only in bin
      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 
      7 import logging
      8 import optparse
      9 import os
     10 import sys
     11 import webbrowser
     12 
     13 _SYSTRACE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
     14 sys.path.append(_SYSTRACE_DIR)
     15 
     16 from profile_chrome import atrace_tracing_agent
     17 from profile_chrome import chrome_startup_tracing_agent
     18 from profile_chrome import flags
     19 from profile_chrome import profiler
     20 from profile_chrome import ui
     21 
     22 _CATAPULT_DIR = os.path.join(
     23     os.path.dirname(os.path.abspath(__file__)), '..', '..')
     24 sys.path.append(os.path.join(_CATAPULT_DIR, 'devil'))
     25 
     26 from devil.android import device_utils
     27 
     28 
     29 _CHROME_STARTUP_MODULES = [atrace_tracing_agent,
     30                            chrome_startup_tracing_agent]
     31 _DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES'
     32 
     33 
     34 def _CreateOptionParser():
     35   parser = optparse.OptionParser(description='Record about://tracing profiles '
     36                                  'from Android browsers startup, combined with '
     37                                  'Android systrace. See http://dev.chromium.org'
     38                                  '/developers/how-tos/trace-event-profiling-'
     39                                  'tool for detailed instructions for '
     40                                  'profiling.')
     41 
     42   browsers = sorted(profiler.GetSupportedBrowsers().keys())
     43   parser.add_option('-b', '--browser', help='Select among installed browsers. '
     44                     'One of ' + ', '.join(browsers) + ', "stable" is used by '
     45                     'default.', type='choice', choices=browsers,
     46                     default='stable')
     47   parser.add_option('-v', '--verbose', help='Verbose logging.',
     48                     action='store_true')
     49   parser.add_option('-z', '--compress', help='Compress the resulting trace '
     50                     'with gzip. ', action='store_true')
     51   parser.add_option('-t', '--time', help='Stops tracing after N seconds, 0 to '
     52                     'manually stop (startup trace ends after at most 5s).',
     53                     default=5, metavar='N', type='int', dest='trace_time')
     54 
     55   parser.add_option_group(chrome_startup_tracing_agent.add_options(parser))
     56   parser.add_option_group(atrace_tracing_agent.add_options(parser))
     57   parser.add_option_group(flags.OutputOptions(parser))
     58 
     59   return parser
     60 
     61 
     62 def main():
     63   parser = _CreateOptionParser()
     64   options, _ = parser.parse_args()
     65 
     66   if options.verbose:
     67     logging.getLogger().setLevel(logging.DEBUG)
     68 
     69   devices = device_utils.DeviceUtils.HealthyDevices()
     70   if len(devices) != 1:
     71     logging.error('Exactly 1 device must be attached.')
     72     return 1
     73   device = devices[0]
     74   package_info = profiler.GetSupportedBrowsers()[options.browser]
     75 
     76   options.device = device
     77   options.package_info = package_info
     78 
     79   # TODO(washingtonp): Once Systrace uses all of the profile_chrome agents,
     80   # manually setting these options will no longer be necessary and should be
     81   # removed.
     82   options.ring_buffer = False
     83   options.trace_memory = False
     84   options.chrome_categories = _DEFAULT_CHROME_CATEGORIES
     85 
     86   if options.atrace_categories in ['list', 'help']:
     87     ui.PrintMessage('\n'.join(
     88         atrace_tracing_agent.AtraceAgent.GetCategories(device)))
     89     return 0
     90   result = profiler.CaptureProfile(options,
     91                                    options.trace_time,
     92                                    _CHROME_STARTUP_MODULES,
     93                                    output=options.output_file,
     94                                    compress=options.compress,
     95                                    write_json=options.write_json)
     96   if options.view:
     97     if sys.platform == 'darwin':
     98       os.system('/usr/bin/open %s' % os.path.abspath(result))
     99     else:
    100       webbrowser.open(result)
    101 
    102 
    103 if __name__ == '__main__':
    104   sys.exit(main())
    105