Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 '''Implementation of tracing controller for systrace. This class creates the | |
| 8 necessary tracing agents for systrace, runs them, and outputs the results | |
| 9 as an HTML file.''' | |
| 10 | |
| 11 import imp | |
| 12 import os | |
| 13 from systrace import tracing_controller | |
| 14 | |
| 15 # The default agent directory. | |
| 16 DEFAULT_AGENT_DIR = 'agents' | |
| 17 | |
| 18 | |
| 19 class SystraceTracingController(tracing_controller.TracingController): | |
| 20 def __init__(self, script_dir, options, categories): | |
| 21 """Set up the SystraceTracingController. | |
| 22 | |
| 23 Args: | |
| 24 script_dir: Directory containing the trace viewer script | |
| 25 (systrace_trace_viewer.html) | |
| 26 options: List of command line options. | |
| 27 categories: List of trace categories to capture. | |
| 28 """ | |
| 29 # Parse command line arguments and create agents | |
| 30 self._script_dir = script_dir | |
| 31 self._html_filename = options.output_file | |
| 32 self._all_results = None | |
|
Zhen Wang
2016/03/14 17:11:27
I think you don't want to override self._all_resul
alexandermont
2016/03/14 22:19:28
Fixed
| |
| 33 agents = CreateAgents(options, categories) | |
| 34 | |
| 35 # Error if no agents are available | |
| 36 if not agents: | |
| 37 dirs = DEFAULT_AGENT_DIR | |
| 38 if options.agent_dirs: | |
| 39 dirs += ',' + options.agent_dirs | |
| 40 raise RuntimeError('No systrace agent is available' | |
| 41 'in directories |%s|.\n' % dirs) | |
| 42 | |
| 43 # Update trace viewer if necessary | |
| 44 try: | |
| 45 update_systrace_trace_viewer = __import__('update_systrace_trace_viewer') | |
| 46 except ImportError: | |
| 47 pass | |
| 48 else: | |
| 49 update_systrace_trace_viewer.update() | |
| 50 | |
| 51 super(SystraceTracingController, self).__init__(agents) | |
| 52 | |
| 53 #override | |
| 54 def SupportsExplicitClockSync(self): | |
| 55 return False | |
| 56 | |
| 57 #override | |
| 58 def RecordClockSyncMarker(self, sync_id, callback): | |
| 59 raise NotImplementedError | |
| 60 | |
| 61 def OutputSystraceResults(self, json=False): | |
| 62 """Output the results of systrace to a file if | |
| 63 output is necessary. | |
| 64 | |
| 65 Args: | |
| 66 json: Whether to output to a json file (if false, use HTML file) | |
| 67 """ | |
| 68 if all([x.should_output_trace for x in self._all_results]): | |
| 69 print 'Tracing complete, writing results' | |
| 70 if json: | |
| 71 self._WriteTraceJSON(self._all_results) | |
| 72 else: | |
| 73 self._WriteTraceHTML(self._all_results) | |
| 74 else: | |
| 75 print 'Tracing complete, results not required' | |
| 76 | |
| 77 def _WriteTraceJSON(self, results): | |
| 78 raise NotImplementedError # TODO: write this function | |
| 79 | |
| 80 def _WriteTraceHTML(self, results): | |
| 81 """Write the results of systrace to an HTML file.""" | |
| 82 def _read_asset(src_dir, filename): | |
| 83 return open(os.path.join(src_dir, filename)).read() | |
| 84 | |
| 85 print 'Writing trace HTML' | |
| 86 systrace_dir = os.path.abspath(os.path.dirname(__file__)) | |
| 87 html_prefix = _read_asset(systrace_dir, 'prefix.html') | |
| 88 html_suffix = _read_asset(systrace_dir, 'suffix.html') | |
| 89 trace_viewer_html = _read_asset(self._script_dir, | |
| 90 'systrace_trace_viewer.html') | |
| 91 | |
| 92 # Open the file in binary mode to prevent python from changing the | |
| 93 # line endings. | |
| 94 html_file = open(self._html_filename, 'wb') | |
| 95 html_file.write(html_prefix.replace('{{SYSTRACE_TRACE_VIEWER_HTML}}', | |
| 96 trace_viewer_html)) | |
| 97 | |
| 98 html_file.write('<!-- BEGIN TRACE -->\n') | |
| 99 for a in results: | |
| 100 if a.trace_data: | |
| 101 html_file.write(' <script class="') | |
| 102 html_file.write('trace-data') | |
| 103 html_file.write('" type="application/text">\n') | |
| 104 html_file.write(a.trace_data) | |
| 105 html_file.write(' </script>\n') | |
| 106 html_file.write('<!-- END TRACE -->\n') | |
| 107 | |
| 108 html_file.write(html_suffix) | |
| 109 html_file.close() | |
| 110 print '\n wrote file://%s\n' % os.path.abspath(self._html_filename) | |
| 111 | |
| 112 def CreateAgents(options, categories): | |
| 113 """Create systrace agents. | |
| 114 | |
| 115 This function will search systrace agent modules in agent directories and | |
| 116 create the corresponding systrace agents. | |
| 117 Args: | |
| 118 options: The command-line options. | |
| 119 categories: The trace categories to capture. | |
| 120 Returns: | |
| 121 The list of systrace agents. | |
| 122 """ | |
| 123 agent_dirs = [os.path.join(os.path.dirname(__file__), | |
| 124 DEFAULT_AGENT_DIR)] | |
| 125 if options.agent_dirs: | |
| 126 agent_dirs.extend(options.agent_dirs.split(',')) | |
| 127 | |
| 128 agents = [] | |
| 129 for agent_dir in agent_dirs: | |
| 130 if not agent_dir: | |
| 131 continue | |
| 132 for filename in os.listdir(agent_dir): | |
| 133 (module_name, ext) = os.path.splitext(filename) | |
| 134 if (ext != '.py' or module_name == '__init__' | |
| 135 or module_name.endswith('_unittest')): | |
| 136 continue | |
| 137 (f, pathname, data) = imp.find_module(module_name, [agent_dir]) | |
| 138 try: | |
| 139 module = imp.load_module(module_name, f, pathname, data) | |
| 140 finally: | |
| 141 if f: | |
| 142 f.close() | |
| 143 if module: | |
| 144 agent = module.try_create_agent(options, categories) | |
| 145 if not agent: | |
| 146 continue | |
| 147 agents.append(agent) | |
| 148 return agents | |
| OLD | NEW |