| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import time | 5 import time |
| 6 | 6 |
| 7 from devil.android.constants import chrome | 7 from devil.android.constants import chrome |
| 8 from profile_chrome import atrace_tracing_agent | |
| 9 from profile_chrome import chrome_startup_tracing_agent | 8 from profile_chrome import chrome_startup_tracing_agent |
| 10 from profile_chrome import chrome_tracing_agent | 9 from profile_chrome import chrome_tracing_agent |
| 11 from profile_chrome import ddms_tracing_agent | |
| 12 from profile_chrome import perf_tracing_agent | |
| 13 from profile_chrome import ui | 10 from profile_chrome import ui |
| 14 from profile_chrome import util | 11 from profile_chrome import util |
| 15 from systrace import output_generator | 12 from systrace import output_generator |
| 13 from systrace import tracing_controller |
| 16 | 14 |
| 17 | 15 |
| 18 # TODO(washingtonp): This mapping is temporarily in place because | 16 def _GetResults(trace_results, controller, output, compress, write_json, |
| 19 # profile_chrome does not currently follow Systrace's controller API. This | 17 interval): |
| 20 # mapping will be removed in the CL that makes profile_chrome follow the | |
| 21 # Systrace API. | |
| 22 AGENT_TYPE_TO_MODULE = {'chrome trace': chrome_tracing_agent, | |
| 23 'Browser Startup Trace': chrome_startup_tracing_agent, | |
| 24 'ddms profile': ddms_tracing_agent, | |
| 25 'perf profile': perf_tracing_agent, | |
| 26 'atrace': atrace_tracing_agent} | |
| 27 | |
| 28 | |
| 29 def _StartTracing(agents, options): | |
| 30 for agent in agents: | |
| 31 if repr(agent) not in AGENT_TYPE_TO_MODULE: | |
| 32 continue | |
| 33 agent.StartAgentTracing(AGENT_TYPE_TO_MODULE[repr(agent)]. | |
| 34 get_config(options)) | |
| 35 | |
| 36 | |
| 37 def _StopTracing(agents): | |
| 38 for agent in agents: | |
| 39 agent.StopAgentTracing() | |
| 40 | |
| 41 | |
| 42 def _GetResults(agents, output, compress, write_json, interval): | |
| 43 ui.PrintMessage('Downloading...', eol='') | 18 ui.PrintMessage('Downloading...', eol='') |
| 44 | 19 |
| 45 # Wait for the trace file to get written. | 20 # Wait for the trace file to get written. |
| 46 time.sleep(1) | 21 time.sleep(1) |
| 47 | 22 |
| 48 trace_results = [] | 23 for agent in controller.child_agents: |
| 49 for agent in agents: | |
| 50 if isinstance(agent, chrome_tracing_agent.ChromeTracingAgent): | 24 if isinstance(agent, chrome_tracing_agent.ChromeTracingAgent): |
| 51 time.sleep(interval / 4) | 25 time.sleep(interval / 4) |
| 52 trace_results.append(agent.GetResults()) | 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')] |
| 53 | 31 |
| 54 if not trace_results: | 32 if not trace_results: |
| 55 ui.PrintMessage('No results') | 33 ui.PrintMessage('No results') |
| 56 return '' | 34 return '' |
| 57 | 35 |
| 58 result = None | 36 result = None |
| 59 trace_results = output_generator.MergeTraceResultsIfNeeded(trace_results) | 37 trace_results = output_generator.MergeTraceResultsIfNeeded(trace_results) |
| 60 if not write_json: | 38 if not write_json: |
| 61 print 'Writing trace HTML' | 39 print 'Writing trace HTML' |
| 62 html_file = trace_results[0].source_name + '.html' | 40 html_file = trace_results[0].source_name + '.html' |
| (...skipping 24 matching lines...) Expand all Loading... |
| 87 supported_browsers = { | 65 supported_browsers = { |
| 88 'stable': chrome.PACKAGE_INFO['chrome_stable'], | 66 'stable': chrome.PACKAGE_INFO['chrome_stable'], |
| 89 'beta': chrome.PACKAGE_INFO['chrome_beta'], | 67 'beta': chrome.PACKAGE_INFO['chrome_beta'], |
| 90 'dev': chrome.PACKAGE_INFO['chrome_dev'], | 68 'dev': chrome.PACKAGE_INFO['chrome_dev'], |
| 91 'build': chrome.PACKAGE_INFO['chrome'], | 69 'build': chrome.PACKAGE_INFO['chrome'], |
| 92 } | 70 } |
| 93 supported_browsers.update(chrome.PACKAGE_INFO) | 71 supported_browsers.update(chrome.PACKAGE_INFO) |
| 94 return supported_browsers | 72 return supported_browsers |
| 95 | 73 |
| 96 | 74 |
| 97 def CaptureProfile(options, agents, interval, output=None, compress=False, | 75 def CaptureProfile(options, interval, modules, output=None, |
| 98 write_json=False): | 76 compress=False, write_json=False): |
| 99 """Records a profiling trace saves the result to a file. | 77 """Records a profiling trace saves the result to a file. |
| 100 | 78 |
| 101 Args: | 79 Args: |
| 102 options: Command line options. | 80 options: Command line options. |
| 103 agents: List of tracing agents. | |
| 104 interval: Time interval to capture in seconds. An interval of None (or 0) | 81 interval: Time interval to capture in seconds. An interval of None (or 0) |
| 105 continues tracing until stopped by the user. | 82 continues tracing until stopped by the user. |
| 83 modules: The list of modules to initialize the tracing controller with. |
| 106 output: Output file name or None to use an automatically generated name. | 84 output: Output file name or None to use an automatically generated name. |
| 107 compress: If True, the result will be compressed either with gzip or zip | 85 compress: If True, the result will be compressed either with gzip or zip |
| 108 depending on the number of captured subtraces. | 86 depending on the number of captured subtraces. |
| 109 write_json: If True, prefer JSON output over HTML. | 87 write_json: If True, prefer JSON output over HTML. |
| 110 | 88 |
| 111 Returns: | 89 Returns: |
| 112 Path to saved profile. | 90 Path to saved profile. |
| 113 """ | 91 """ |
| 114 trace_type = ' + '.join(map(str, agents)) | 92 agents_with_config = tracing_controller.CreateAgentsWithConfig(options, |
| 93 modules) |
| 94 if chrome_startup_tracing_agent in modules: |
| 95 controller_config = tracing_controller.GetStartupControllerConfig(options) |
| 96 else: |
| 97 controller_config = tracing_controller.GetControllerConfig(options) |
| 98 controller = tracing_controller.TracingController(agents_with_config, |
| 99 controller_config) |
| 115 try: | 100 try: |
| 116 _StartTracing(agents, options) | 101 result = controller.StartTracing() |
| 102 trace_type = controller.GetTraceType() |
| 103 if not result: |
| 104 print 'Trace starting failed.' |
| 117 if interval: | 105 if interval: |
| 118 ui.PrintMessage(('Capturing %d-second %s. Press Enter to stop early...' % | 106 ui.PrintMessage(('Capturing %d-second %s. Press Enter to stop early...' % |
| 119 (interval, trace_type)), eol='') | 107 (interval, trace_type)), eol='') |
| 120 ui.WaitForEnter(interval) | 108 ui.WaitForEnter(interval) |
| 121 else: | 109 else: |
| 122 ui.PrintMessage('Capturing %s. Press Enter to stop...' % trace_type, | 110 ui.PrintMessage('Capturing %s. Press Enter to stop...' % trace_type, |
| 123 eol='') | 111 eol='') |
| 124 raw_input() | 112 raw_input() |
| 113 all_results = controller.StopTracing() |
| 125 finally: | 114 finally: |
| 126 _StopTracing(agents) | 115 if interval: |
| 127 if interval: | 116 ui.PrintMessage('done') |
| 128 ui.PrintMessage('done') | |
| 129 | 117 |
| 130 return _GetResults(agents, output, compress, write_json, interval) | 118 return _GetResults(all_results, controller, output, compress, write_json, |
| 119 interval) |
| OLD | NEW |