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