profile_chrome_startup 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2021 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. This executable script runs perfetto traces on the specified device.
  8. See the README.md file for more information.
  9. """
  10. import os
  11. import sys
  12. import optparse
  13. import adb_profile_chrome_startup
  14. import symbolize_trace
  15. import flag_utils
  16. import display_in_browser
  17. sys.path.insert(0, os.path.join(os.path.dirname(__file__),
  18. os.pardir, os.pardir, 'third_party',
  19. 'catapult', 'systrace'))
  20. from systrace import util
  21. from profile_chrome import chrome_startup_tracing_agent
  22. def _CreateOptionParser():
  23. parser = optparse.OptionParser(description='Record Perfetto tracing profiles '
  24. 'from Android browsers startup. See '
  25. 'http://dev.chromium.org'
  26. '/developers/how-tos/trace-event-profiling-'
  27. 'tool for detailed instructions for '
  28. 'profiling.', usage='%prog [options]',
  29. prog='tools/tracing/profile_chrome_startup',
  30. conflict_handler='resolve')
  31. parser.add_option_group(flag_utils.GeneralOptions(parser))
  32. parser.add_option_group(flag_utils.ProfileOptions(parser))
  33. parser.add_option_group(chrome_startup_tracing_agent.add_options(parser))
  34. parser.add_option_group(flag_utils.SymbolizeOptions(parser))
  35. return parser
  36. def _SetupFlags(options):
  37. flag_utils.SetupLogging(options.verbosity)
  38. flag_utils.SetupProfilingCategories(options)
  39. def main():
  40. parser = _CreateOptionParser()
  41. options, _ = parser.parse_args()
  42. _SetupFlags(options)
  43. # Run Tracing
  44. trace_file = None
  45. if options.platform.lower() == 'android':
  46. # TODO(crbug/1239748): Fix manual tracing. Setting flag --time=0 stalls and fails
  47. # to download the collected trace.
  48. trace_file = adb_profile_chrome_startup.ProfileChrome(options)
  49. else:
  50. raise Exception('Platform "%s" is not supported. '
  51. 'Specify platform with the --platform flag.'
  52. % (options.platform))
  53. print('Wrote unsymbolized trace to {path}'.format(
  54. path=os.path.abspath(trace_file)))
  55. # Symbolize Trace
  56. if not options.skip_symbolize:
  57. if options.trace_format is None or options.trace_format.lower() != 'proto':
  58. raise Exception('Symbolization is currently only supported for protos.')
  59. symbolize_trace.SymbolizeTrace(trace_file=trace_file, options=options)
  60. if options.view:
  61. trace = options.output_file
  62. if not trace or not os.path.isfile(trace):
  63. trace = trace_file
  64. if trace and os.path.isfile(trace):
  65. display_in_browser.DisplayInBrowser(trace, options.trace_format)
  66. else:
  67. flag_utils.GetTraceLogger().warning(
  68. 'Trace is not found, so not opening in browser.')
  69. if __name__ == '__main__':
  70. sys.exit(main())