• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 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
5import time
6
7from devil.android.constants import chrome
8from profile_chrome import chrome_startup_tracing_agent
9from profile_chrome import chrome_tracing_agent
10from profile_chrome import ui
11from profile_chrome import util
12from systrace import output_generator
13from systrace import tracing_controller
14
15
16def _GetResults(trace_results, controller, output, compress, write_json,
17                interval):
18  ui.PrintMessage('Downloading...', eol='')
19
20  # Wait for the trace file to get written.
21  time.sleep(1)
22
23  for agent in controller.get_child_agents:
24    if isinstance(agent, chrome_tracing_agent.ChromeTracingAgent):
25      time.sleep(interval / 4)
26
27  # Ignore the systraceController because it will not contain any results,
28  # instead being in charge of collecting results.
29  trace_results = [x for x in controller.all_results if not (x.source_name ==
30      'systraceController')]
31
32  if not trace_results:
33    ui.PrintMessage('No results')
34    return ''
35
36  result = None
37  trace_results = output_generator.MergeTraceResultsIfNeeded(trace_results)
38  if not write_json:
39    print 'Writing trace HTML'
40    html_file = trace_results[0].source_name + '.html'
41    result = output_generator.GenerateHTMLOutput(trace_results, html_file)
42    print '\nWrote file://%s\n' % result
43  elif compress and len(trace_results) == 1:
44    result = output or trace_results[0].source_name + '.gz'
45    util.WriteDataToCompressedFile(trace_results[0].raw_data, result)
46  elif len(trace_results) > 1:
47    result = (output or 'chrome-combined-trace-%s.zip' %
48              util.GetTraceTimestamp())
49    util.ArchiveData(trace_results, result)
50  elif output:
51    result = output
52    with open(result, 'wb') as f:
53      f.write(trace_results[0].raw_data)
54  else:
55    result = trace_results[0].source_name
56    with open(result, 'wb') as f:
57      f.write(trace_results[0].raw_data)
58
59  return result
60
61
62def GetSupportedBrowsers():
63  """Returns the package names of all supported browsers."""
64  # Add aliases for backwards compatibility.
65  supported_browsers = {
66    'stable': chrome.PACKAGE_INFO['chrome_stable'],
67    'beta': chrome.PACKAGE_INFO['chrome_beta'],
68    'dev': chrome.PACKAGE_INFO['chrome_dev'],
69    'build': chrome.PACKAGE_INFO['chrome'],
70  }
71  supported_browsers.update(chrome.PACKAGE_INFO)
72  return supported_browsers
73
74
75def CaptureProfile(options, interval, modules, output=None,
76                   compress=False, write_json=False):
77  """Records a profiling trace saves the result to a file.
78
79  Args:
80    options: Command line options.
81    interval: Time interval to capture in seconds. An interval of None (or 0)
82        continues tracing until stopped by the user.
83    modules: The list of modules to initialize the tracing controller with.
84    output: Output file name or None to use an automatically generated name.
85    compress: If True, the result will be compressed either with gzip or zip
86        depending on the number of captured subtraces.
87    write_json: If True, prefer JSON output over HTML.
88
89  Returns:
90    Path to saved profile.
91  """
92  agents_with_config = tracing_controller.CreateAgentsWithConfig(options,
93                                                                 modules)
94  if chrome_startup_tracing_agent in modules:
95    controller_config = tracing_controller.GetChromeStartupControllerConfig(
96        options)
97  else:
98    controller_config = tracing_controller.GetControllerConfig(options)
99  controller = tracing_controller.TracingController(agents_with_config,
100                                                    controller_config)
101  try:
102    result = controller.StartTracing()
103    trace_type = controller.GetTraceType()
104    if not result:
105      print 'Trace starting failed.'
106    if interval:
107      ui.PrintMessage(('Capturing %d-second %s. Press Enter to stop early...' %
108                     (interval, trace_type)), eol='')
109      ui.WaitForEnter(interval)
110    else:
111      ui.PrintMessage('Capturing %s. Press Enter to stop...' % trace_type,
112                     eol='')
113      raw_input()
114    all_results = controller.StopTracing()
115  finally:
116    if interval:
117      ui.PrintMessage('done')
118
119  return _GetResults(all_results, controller, output, compress, write_json,
120                     interval)
121