flag_utils.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. """This script provides the necessary flags to symbolize proto traces.
  5. """
  6. import logging
  7. import optparse
  8. import os
  9. import sys
  10. sys.path.insert(
  11. 0,
  12. os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'third_party',
  13. 'catapult', 'systrace'))
  14. from systrace import util
  15. TRACING_LOGGER_NAME = 'chromium:tools/tracing'
  16. def GeneralOptions(parser):
  17. """Build option group for general options.
  18. Args:
  19. parser: OptionParser object for parsing the command-line.
  20. Returns:
  21. Option group that contains general options.
  22. """
  23. general_options = optparse.OptionGroup(parser, 'General Options')
  24. general_options.add_option('-o',
  25. '--output',
  26. help=('Save trace output to file. '
  27. 'Defaults to trace_file + '
  28. '"_symbolized_trace."'),
  29. dest='output_file')
  30. general_options.add_option('-v',
  31. '--verbose',
  32. help='Increase output verbosity.',
  33. action='count',
  34. dest='verbosity',
  35. default=0)
  36. general_options.add_option('--view',
  37. help='Open resulting trace file in a '
  38. 'browser.',
  39. action='store_true',
  40. dest='view')
  41. return general_options
  42. def ProfileOptions(parser):
  43. """Build option group for profiling chrome.
  44. Args:
  45. parser: OptionParser object for parsing the command-line.
  46. Returns:
  47. Option group that contains profiling chrome options.
  48. """
  49. profile_options = optparse.OptionGroup(parser, 'Profile Chrome Options')
  50. browsers = sorted(util.get_supported_browsers().keys())
  51. profile_options.add_option('-b',
  52. '--browser',
  53. help='Select among installed browsers. '
  54. 'One of ' + ', '.join(browsers) +
  55. '. "stable" is used by '
  56. 'default.',
  57. type='choice',
  58. choices=browsers,
  59. default='stable')
  60. profile_options.add_option('-t',
  61. '--time',
  62. help=('Stops tracing after N seconds. '
  63. 'Default is 5 seconds'),
  64. default=5,
  65. metavar='N',
  66. type='int',
  67. dest='trace_time')
  68. profile_options.add_option('-e',
  69. '--serial',
  70. help='adb device serial number.',
  71. type='string',
  72. default=util.get_default_serial(),
  73. dest='device_serial_number')
  74. profile_options.add_option('-f',
  75. '--trace_format',
  76. help='Format of saved trace: proto, json, html.'
  77. ' Default is proto.',
  78. default='proto',
  79. dest='trace_format')
  80. profile_options.add_option('-p',
  81. '--platform',
  82. help='Device platform. Only Android is supported.',
  83. default='android',
  84. dest='platform')
  85. profile_options.add_option('--buf-size',
  86. help='Use a trace buffer size '
  87. ' of N KB.',
  88. type='int',
  89. metavar='N',
  90. dest='trace_buf_size')
  91. profile_options.add_option(
  92. '--enable_profiler',
  93. help='Comma-separated string of '
  94. 'profiling options to use. Supports options for memory or '
  95. 'cpu or both. Ex: --enable_profiler=memory '
  96. 'or --enable_profiler=memory,cpu. ',
  97. dest='enable_profiler')
  98. profile_options.add_option('--chrome_categories',
  99. help='Chrome tracing '
  100. 'categories to record.',
  101. type='string')
  102. profile_options.add_option(
  103. '--skip_symbolize',
  104. help='Skips symbolization after recording trace profile, if specified.',
  105. action='store_true',
  106. dest='skip_symbolize')
  107. profile_options.add_option('--compress',
  108. help='Compress the resulting trace '
  109. 'with gzip. ',
  110. action='store_true')
  111. # This is kept for backwards compatibility. Help is suppressed because this
  112. # should be specified through the newer |trace_format| flag.
  113. profile_options.add_option('--json',
  114. help=optparse.SUPPRESS_HELP,
  115. dest='write_json')
  116. return profile_options
  117. def SymbolizeOptions(parser):
  118. """Build option group for proto trace symbolization.
  119. Args:
  120. parser: OptionParser object for parsing the command-line.
  121. Returns:
  122. Option group that contains symbolization options.
  123. """
  124. symbolization_options = optparse.OptionGroup(parser, 'Symbolization Options')
  125. symbolization_options.add_option(
  126. '--breakpad_output_dir',
  127. help='Optional path to empty directory to store fetched trace symbols '
  128. 'and extracted breakpad symbol files for official builds. Useful '
  129. 'for symbolizing multiple traces from the same Chrome build. Use '
  130. '--local_breakpad_dir when you already have breakpad files fetched. '
  131. 'Automatically uses temporary directory if flag not specified.',
  132. dest='breakpad_output_dir')
  133. symbolization_options.add_option(
  134. '--local_breakpad_dir',
  135. help='Optional path to local directory containing breakpad symbol files '
  136. 'used to symbolize the given trace. Breakpad files should be named '
  137. 'with module id in upper case hexadecimal and ".breakpad" '
  138. 'suffix. Ex: 12EFA32BE.breakpad',
  139. dest='local_breakpad_dir')
  140. symbolization_options.add_option(
  141. '--local_build_dir',
  142. help='Optional path to a local build directory containing symbol files.',
  143. dest='local_build_dir')
  144. symbolization_options.add_option(
  145. '--dump_syms',
  146. help=('Path to a dump_syms binary. Required when symbolizing an official '
  147. 'build. Optional on local builds as we can use --local_build_dir '
  148. 'to locate a binary. If built locally with '
  149. 'autoninja -C out/Release dump_syms then the binary path is '
  150. 'out/Release/dump_syms'),
  151. dest='dump_syms_path')
  152. symbolization_options.add_option(
  153. '--symbolizer',
  154. help=('Path to the trace_to_text binary for symbolization. Defaults to '
  155. '"third_party/perfetto/tools/traceconv". traceconv is the same as '
  156. 'trace_to_text, except that tracevonv finds a prebuilt '
  157. 'trace_to_text binary to use.'),
  158. default='third_party/perfetto/tools/traceconv',
  159. dest='symbolizer_path')
  160. symbolization_options.add_option(
  161. '--trace_processor',
  162. help=('Optional path to trace processor binary. '
  163. 'Automatically downloads binary if flag not specified.'),
  164. dest='trace_processor_path')
  165. symbolization_options.add_option(
  166. '--cloud_storage_bucket',
  167. help=('Optional cloud storage bucket to where symbol files reside. '
  168. 'Defaults to "chrome-unsigned".'),
  169. default='chrome-unsigned',
  170. dest='cloud_storage_bucket')
  171. return symbolization_options
  172. def SetupProfilingCategories(options):
  173. """Sets --chrome_categories flag.
  174. Uses the --enable_profile flag to modify the --chrome_categories flag for
  175. specific profiling options.
  176. Args:
  177. options: The command-line options given.
  178. """
  179. if options.enable_profiler is None:
  180. if not options.chrome_categories:
  181. GetTracingLogger().warning(
  182. 'No trace category or profiler is enabled using --enable_profiler '
  183. 'and/or --chrome_categories, enabling all default categories.')
  184. if not options.skip_symbolize:
  185. GetTracingLogger().warning(
  186. 'No profiler is enabled, symbolization might fail without any '
  187. 'frames. Use --skip_symbolize to disable symbolization.')
  188. else:
  189. GetTracingLogger().info(
  190. 'No profiler is enabled, the trace will only have trace events.')
  191. return
  192. if options.chrome_categories is None:
  193. options.chrome_categories = ''
  194. profile_options = options.enable_profiler.split(',')
  195. # Add to the --chrome_categories flag.
  196. if 'cpu' in profile_options:
  197. if options.chrome_categories:
  198. options.chrome_categories += ','
  199. options.chrome_categories += 'disabled-by-default-cpu_profiler'
  200. if 'memory' in profile_options:
  201. if options.chrome_categories:
  202. options.chrome_categories += ',disabled-by-default-memory-infra'
  203. else:
  204. # If heap profiling is enabled and no categories are provided, it is
  205. # usually not helpful to include other information in traces. '-*'
  206. # disables other default categories so that only memory data is recorded.
  207. options.chrome_categories += 'disabled-by-default-memory-infra,-*'
  208. def GetTracingLogger():
  209. """Returns the logger for tools/tracing."""
  210. return logging.getLogger(TRACING_LOGGER_NAME)
  211. def SetupLogging(verbosity):
  212. """Setup logging levels.
  213. Sets default level to warning.
  214. Args:
  215. verbosity: None or integer type that specifies the logging level.
  216. (options.verbosity)
  217. """
  218. logger = GetTracingLogger()
  219. if verbosity == 1:
  220. logging.basicConfig(level=logging.INFO)
  221. logger.setLevel(level=logging.INFO)
  222. elif verbosity >= 2:
  223. logging.basicConfig(level=logging.DEBUG)
  224. logger.setLevel(level=logging.DEBUG)
  225. else:
  226. logging.basicConfig(level=logging.WARNING)
  227. logger.setLevel(level=logging.WARNING)