blob: fed3416992c5a860f128e6c0230f4414df61788a [file] [log] [blame]
#!/usr/bin/env python
#
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import optparse
import os
import sys
import webbrowser
_SYSTRACE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(_SYSTRACE_DIR)
from profile_chrome import atrace_tracing_agent
from profile_chrome import chrome_startup_tracing_agent
from profile_chrome import flags
from profile_chrome import profiler
from profile_chrome import ui
_CATAPULT_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), '..', '..')
sys.path.append(os.path.join(_CATAPULT_DIR, 'devil'))
from devil.android import device_utils
_CHROME_STARTUP_MODULES = [atrace_tracing_agent,
chrome_startup_tracing_agent]
_DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES'
def _CreateOptionParser():
parser = optparse.OptionParser(description='Record about://tracing profiles '
'from Android browsers startup, combined with '
'Android systrace. See http://dev.chromium.org'
'/developers/how-tos/trace-event-profiling-'
'tool for detailed instructions for '
'profiling.')
browsers = sorted(profiler.GetSupportedBrowsers().keys())
parser.add_option('-b', '--browser', help='Select among installed browsers. '
'One of ' + ', '.join(browsers) + ', "stable" is used by '
'default.', type='choice', choices=browsers,
default='stable')
parser.add_option('-v', '--verbose', help='Verbose logging.',
action='store_true')
parser.add_option('-z', '--compress', help='Compress the resulting trace '
'with gzip. ', action='store_true')
parser.add_option('-t', '--time', help='Stops tracing after N seconds, 0 to '
'manually stop (startup trace ends after at most 5s).',
default=5, metavar='N', type='int', dest='trace_time')
parser.add_option_group(chrome_startup_tracing_agent.add_options(parser))
parser.add_option_group(atrace_tracing_agent.add_options(parser))
parser.add_option_group(flags.OutputOptions(parser))
return parser
def main():
parser = _CreateOptionParser()
options, _ = parser.parse_args()
if options.verbose:
logging.getLogger().setLevel(logging.DEBUG)
devices = device_utils.DeviceUtils.HealthyDevices()
if len(devices) != 1:
logging.error('Exactly 1 device must be attached.')
return 1
device = devices[0]
package_info = profiler.GetSupportedBrowsers()[options.browser]
options.device = device
options.package_info = package_info
# TODO(washingtonp): Once Systrace uses all of the profile_chrome agents,
# manually setting these options will no longer be necessary and should be
# removed.
options.ring_buffer = False
options.trace_memory = False
options.chrome_categories = _DEFAULT_CHROME_CATEGORIES
if options.atrace_categories in ['list', 'help']:
ui.PrintMessage('\n'.join(
atrace_tracing_agent.AtraceAgent.GetCategories(device)))
return 0
result = profiler.CaptureProfile(options,
options.trace_time,
_CHROME_STARTUP_MODULES,
output=options.output_file,
compress=options.compress,
write_json=options.write_json)
if options.view:
if sys.platform == 'darwin':
os.system('/usr/bin/open %s' % os.path.abspath(result))
else:
webbrowser.open(result)
if __name__ == '__main__':
sys.exit(main())