xref: /aosp_15_r20/external/chromium-trace/catapult/systrace/profile_chrome/profiler.py (revision 1fa4b3da657c0e9ad43c0220bacf9731820715a5)
1*1fa4b3daSHector Dearman# Copyright 2014 The Chromium Authors. All rights reserved.
2*1fa4b3daSHector Dearman# Use of this source code is governed by a BSD-style license that can be
3*1fa4b3daSHector Dearman# found in the LICENSE file.
4*1fa4b3daSHector Dearman
5*1fa4b3daSHector Dearmanimport time
6*1fa4b3daSHector Dearman
7*1fa4b3daSHector Dearmanfrom profile_chrome import chrome_startup_tracing_agent
8*1fa4b3daSHector Dearmanfrom profile_chrome import chrome_tracing_agent
9*1fa4b3daSHector Dearmanfrom profile_chrome import ui
10*1fa4b3daSHector Dearmanfrom profile_chrome import util
11*1fa4b3daSHector Dearmanfrom systrace import output_generator
12*1fa4b3daSHector Dearmanfrom systrace import tracing_controller
13*1fa4b3daSHector Dearman
14*1fa4b3daSHector Dearman
15*1fa4b3daSHector Dearmandef _GetResults(trace_results, controller, output, compress, write_json,
16*1fa4b3daSHector Dearman                interval):
17*1fa4b3daSHector Dearman  ui.PrintMessage('Downloading...')
18*1fa4b3daSHector Dearman
19*1fa4b3daSHector Dearman  # Wait for the trace file to get written.
20*1fa4b3daSHector Dearman  time.sleep(1)
21*1fa4b3daSHector Dearman
22*1fa4b3daSHector Dearman  for agent in controller.get_child_agents:
23*1fa4b3daSHector Dearman    if isinstance(agent, chrome_tracing_agent.ChromeTracingAgent):
24*1fa4b3daSHector Dearman      time.sleep(interval / 4)
25*1fa4b3daSHector Dearman
26*1fa4b3daSHector Dearman  # Ignore the systraceController because it will not contain any results,
27*1fa4b3daSHector Dearman  # instead being in charge of collecting results.
28*1fa4b3daSHector Dearman  trace_results = [x for x in controller.all_results if not (x.source_name ==
29*1fa4b3daSHector Dearman      'systraceController')]
30*1fa4b3daSHector Dearman
31*1fa4b3daSHector Dearman  if not trace_results:
32*1fa4b3daSHector Dearman    ui.PrintMessage('No results')
33*1fa4b3daSHector Dearman    return ''
34*1fa4b3daSHector Dearman
35*1fa4b3daSHector Dearman  result = None
36*1fa4b3daSHector Dearman  trace_results = output_generator.MergeTraceResultsIfNeeded(trace_results)
37*1fa4b3daSHector Dearman  if not write_json:
38*1fa4b3daSHector Dearman    ui.PrintMessage('Writing trace HTML...')
39*1fa4b3daSHector Dearman    html_file = output or trace_results[0].source_name + '.html'
40*1fa4b3daSHector Dearman    result = output_generator.GenerateHTMLOutput(trace_results, html_file)
41*1fa4b3daSHector Dearman    ui.PrintMessage('\nWrote file://%s' % result)
42*1fa4b3daSHector Dearman  elif compress and len(trace_results) == 1:
43*1fa4b3daSHector Dearman    result = output or trace_results[0].source_name + '.gz'
44*1fa4b3daSHector Dearman    util.WriteDataToCompressedFile(trace_results[0].raw_data, result)
45*1fa4b3daSHector Dearman  elif len(trace_results) > 1:
46*1fa4b3daSHector Dearman    result = (output or 'chrome-combined-trace-%s.zip' %
47*1fa4b3daSHector Dearman              util.GetTraceTimestamp())
48*1fa4b3daSHector Dearman    util.ArchiveData(trace_results, result)
49*1fa4b3daSHector Dearman  elif output:
50*1fa4b3daSHector Dearman    result = output
51*1fa4b3daSHector Dearman    with open(result, 'wb') as f:
52*1fa4b3daSHector Dearman      f.write(trace_results[0].raw_data)
53*1fa4b3daSHector Dearman  else:
54*1fa4b3daSHector Dearman    result = trace_results[0].source_name
55*1fa4b3daSHector Dearman    with open(result, 'wb') as f:
56*1fa4b3daSHector Dearman      f.write(trace_results[0].raw_data)
57*1fa4b3daSHector Dearman
58*1fa4b3daSHector Dearman  return result
59*1fa4b3daSHector Dearman
60*1fa4b3daSHector Dearman
61*1fa4b3daSHector Dearmandef CaptureProfile(options, interval, modules, output=None,
62*1fa4b3daSHector Dearman                   compress=False, write_json=False):
63*1fa4b3daSHector Dearman  """Records a profiling trace saves the result to a file.
64*1fa4b3daSHector Dearman
65*1fa4b3daSHector Dearman  Args:
66*1fa4b3daSHector Dearman    options: Command line options.
67*1fa4b3daSHector Dearman    interval: Time interval to capture in seconds. An interval of None (or 0)
68*1fa4b3daSHector Dearman        continues tracing until stopped by the user.
69*1fa4b3daSHector Dearman    modules: The list of modules to initialize the tracing controller with.
70*1fa4b3daSHector Dearman    output: Output file name or None to use an automatically generated name.
71*1fa4b3daSHector Dearman    compress: If True, the result will be compressed either with gzip or zip
72*1fa4b3daSHector Dearman        depending on the number of captured subtraces.
73*1fa4b3daSHector Dearman    write_json: If True, prefer JSON output over HTML.
74*1fa4b3daSHector Dearman
75*1fa4b3daSHector Dearman  Returns:
76*1fa4b3daSHector Dearman    Path to saved profile.
77*1fa4b3daSHector Dearman  """
78*1fa4b3daSHector Dearman  agents_with_config = tracing_controller.CreateAgentsWithConfig(options,
79*1fa4b3daSHector Dearman                                                                 modules)
80*1fa4b3daSHector Dearman  if chrome_startup_tracing_agent in modules:
81*1fa4b3daSHector Dearman    controller_config = tracing_controller.GetChromeStartupControllerConfig(
82*1fa4b3daSHector Dearman        options)
83*1fa4b3daSHector Dearman  else:
84*1fa4b3daSHector Dearman    controller_config = tracing_controller.GetControllerConfig(options)
85*1fa4b3daSHector Dearman  controller = tracing_controller.TracingController(agents_with_config,
86*1fa4b3daSHector Dearman                                                    controller_config)
87*1fa4b3daSHector Dearman  try:
88*1fa4b3daSHector Dearman    result = controller.StartTracing()
89*1fa4b3daSHector Dearman    trace_type = controller.GetTraceType()
90*1fa4b3daSHector Dearman    if not result:
91*1fa4b3daSHector Dearman      ui.PrintMessage('Trace starting failed.')
92*1fa4b3daSHector Dearman    if interval:
93*1fa4b3daSHector Dearman      ui.PrintMessage(('Capturing %d-second %s. Press Enter to stop early...' %
94*1fa4b3daSHector Dearman                     (interval, trace_type)), eol='')
95*1fa4b3daSHector Dearman      ui.WaitForEnter(interval)
96*1fa4b3daSHector Dearman    else:
97*1fa4b3daSHector Dearman      ui.PrintMessage('Capturing %s. Press Enter to stop...' % trace_type,
98*1fa4b3daSHector Dearman                      eol='')
99*1fa4b3daSHector Dearman      raw_input()
100*1fa4b3daSHector Dearman
101*1fa4b3daSHector Dearman    ui.PrintMessage('Stopping...')
102*1fa4b3daSHector Dearman    all_results = controller.StopTracing()
103*1fa4b3daSHector Dearman  finally:
104*1fa4b3daSHector Dearman    if interval:
105*1fa4b3daSHector Dearman      ui.PrintMessage('done')
106*1fa4b3daSHector Dearman
107*1fa4b3daSHector Dearman  return _GetResults(all_results, controller, output, compress, write_json,
108*1fa4b3daSHector Dearman                     interval)
109