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 os
6
7from devil.android.constants import chrome
8
9from profile_chrome import trace_packager
10from profile_chrome import ui
11
12
13def _StartTracing(controllers, interval):
14  for controller in controllers:
15    controller.StartTracing(interval)
16
17
18def _StopTracing(controllers):
19  for controller in controllers:
20    controller.StopTracing()
21
22
23def _PullTraces(controllers, output, compress, write_json):
24  ui.PrintMessage('Downloading...', eol='')
25  trace_files = [controller.PullTrace() for controller in controllers]
26  trace_files = [trace for trace in trace_files if trace]
27  if not trace_files:
28    ui.PrintMessage('No results')
29    return ''
30  result = trace_packager.PackageTraces(trace_files,
31                                        output=output,
32                                        compress=compress,
33                                        write_json=write_json)
34  ui.PrintMessage('done')
35  ui.PrintMessage('Trace written to file://%s' % os.path.abspath(result))
36  return result
37
38
39def GetSupportedBrowsers():
40  """Returns the package names of all supported browsers."""
41  # Add aliases for backwards compatibility.
42  supported_browsers = {
43    'stable': chrome.PACKAGE_INFO['chrome_stable'],
44    'beta': chrome.PACKAGE_INFO['chrome_beta'],
45    'dev': chrome.PACKAGE_INFO['chrome_dev'],
46    'build': chrome.PACKAGE_INFO['chrome'],
47  }
48  supported_browsers.update(chrome.PACKAGE_INFO)
49  unsupported_browsers = ['content_browsertests', 'gtest', 'legacy_browser']
50  for browser in unsupported_browsers:
51    if browser in supported_browsers:
52      del supported_browsers[browser]
53  return supported_browsers
54
55
56def CaptureProfile(controllers, interval, output=None, compress=False,
57                   write_json=False):
58  """Records a profiling trace saves the result to a file.
59
60  Args:
61    controllers: List of tracing controllers.
62    interval: Time interval to capture in seconds. An interval of None (or 0)
63        continues tracing until stopped by the user.
64    output: Output file name or None to use an automatically generated name.
65    compress: If True, the result will be compressed either with gzip or zip
66        depending on the number of captured subtraces.
67    write_json: If True, prefer JSON output over HTML.
68
69  Returns:
70    Path to saved profile.
71  """
72  trace_type = ' + '.join(map(str, controllers))
73  try:
74    _StartTracing(controllers, interval)
75    if interval:
76      ui.PrintMessage('Capturing %d-second %s. Press Enter to stop early...' % \
77          (interval, trace_type), eol='')
78      ui.WaitForEnter(interval)
79    else:
80      ui.PrintMessage('Capturing %s. Press Enter to stop...' % \
81          trace_type, eol='')
82      raw_input()
83  finally:
84    _StopTracing(controllers)
85  if interval:
86    ui.PrintMessage('done')
87
88  return _PullTraces(controllers, output, compress, write_json)
89