adb_profile_chrome_startup.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright 2021 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """
  5. This script runs Android perfetto tracing, located in the
  6. third_party/catapult folder. It is consumed as a library by the
  7. executable profile_chrome_startup script.
  8. """
  9. import os
  10. import sys
  11. _CATAPULT_DIR = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
  12. 'third_party', 'catapult')
  13. sys.path.insert(0, os.path.join(_CATAPULT_DIR, 'systrace'))
  14. sys.path.insert(0, os.path.join(_CATAPULT_DIR, 'devil'))
  15. from profile_chrome import chrome_startup_tracing_agent
  16. from profile_chrome import profiler
  17. from profile_chrome import flags
  18. from systrace import util
  19. from devil.android import device_utils
  20. from devil.android.sdk import adb_wrapper
  21. _CHROME_STARTUP_MODULES = [chrome_startup_tracing_agent]
  22. def ProfileChrome(options):
  23. """Profiles chrome on Android and saves the trace file to output file.
  24. Args:
  25. options: Command line flags with their specified values as
  26. returned by optparse.
  27. Returns:
  28. Path to Android profetto trace file
  29. """
  30. if not options.device_serial_number:
  31. # Find the serial number of the connected device.
  32. devices = [a.GetDeviceSerial() for a in adb_wrapper.AdbWrapper.Devices()]
  33. if len(devices) == 0:
  34. raise RuntimeError('No ADB devices connected.')
  35. if len(devices) >= 2:
  36. raise RuntimeError('Multiple devices connected, serial number ' +
  37. 'required. Specify the -e flag.')
  38. options.device_serial_number = devices[0]
  39. # Check if the device is healthy.
  40. devices = device_utils.DeviceUtils.HealthyDevices()
  41. device = None
  42. for d in devices:
  43. if d.serial == options.device_serial_number:
  44. device = d
  45. break
  46. if device is None:
  47. raise RuntimeError('No valid connected devices. Check the device ' +
  48. 'serial number.')
  49. package_info = util.get_supported_browsers()[options.browser]
  50. options.device = device
  51. options.package_info = package_info
  52. # Ensure compatibility between trace_format and write_json flags.
  53. # trace_format is preferred. write_json is supported for backward
  54. # compatibility reasons.
  55. flags.ParseFormatFlags(options)
  56. # Set to root permissions since CaptureProfile() reads app data
  57. # to pull the trace.
  58. adb_wrapper.AdbWrapper(options.device).Root()
  59. trace_file = profiler.CaptureProfile(options,
  60. options.trace_time,
  61. _CHROME_STARTUP_MODULES,
  62. output=options.output_file,
  63. compress=options.compress,
  64. trace_format=options.trace_format)
  65. return trace_file