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 import json | |
| 14 | |
| 15 from systrace import tracing_controller | |
| 16 | |
| 17 # The default agent directory. | |
| 18 DEFAULT_AGENT_DIR = 'tracing_agents' | |
| 19 | |
| 20 | |
| 21 class SystraceTracingController(tracing_controller.TracingController): | |
| 22 def __init__(self, script_dir, options, categories): | |
| 23 """Set up the SystraceTracingController. | |
| 24 | |
| 25 Args: | |
| 26 script_dir: Directory containing the trace viewer script | |
| 27 (systrace_trace_viewer.html) | |
| 28 options: List of command line options. | |
| 29 categories: List of trace categories to capture. | |
| 30 """ | |
| 31 # Parse command line arguments and create agents | |
| 32 self._script_dir = script_dir | |
| 33 self._out_filename = options.output_file | |
| 34 agents = CreateAgents(options, categories) | |
| 35 | |
| 36 # Error if no agents are available | |
| 37 if not agents: | |
| 38 dirs = DEFAULT_AGENT_DIR | |
| 39 if options.agent_dirs: | |
| 40 dirs += ',' + options.agent_dirs | |
| 41 raise RuntimeError('No systrace agent is available' | |
| 42 'in directories |%s|.\n' % dirs) | |
| 43 | |
| 44 # Update trace viewer if necessary | |
| 45 try: | |
| 46 update_systrace_trace_viewer = __import__('update_systrace_trace_viewer') | |
| 47 except ImportError: | |
| 48 pass | |
| 49 else: | |
| 50 update_systrace_trace_viewer.update() | |
|
Zhen Wang
2016/03/29 18:53:39
Pass script_dir to update_systrace_trace_viewer an
alexandermont
2016/03/30 01:04:24
Done
| |
| 51 | |
| 52 super(SystraceTracingController, self).__init__(agents) | |
| 53 | |
| 54 def SupportsExplicitClockSync(self): | |
| 55 return False | |
| 56 | |
| 57 def RecordClockSyncMarker(self, sync_id, callback): | |
| 58 raise NotImplementedError | |
| 59 | |
| 60 def OutputSystraceResults(self, write_json=False): | |
| 61 """Output the results of systrace to a file. | |
| 62 | |
| 63 If output is necessary, then write the results of systrace to either (a) | |
| 64 a standalone HTML file, or (b) a json file which can be read by the | |
| 65 trace viewer. | |
| 66 | |
| 67 Args: | |
| 68 json: Whether to output to a json file (if false, use HTML file) | |
| 69 """ | |
| 70 print 'Tracing complete, writing results' | |
| 71 if write_json: | |
| 72 self._WriteTraceJSON(self._all_results) | |
| 73 else: | |
| 74 self._WriteTraceHTML(self._all_results) | |
| 75 | |
| 76 def _WriteTraceJSON(self, results): | |
| 77 print 'Writing trace JSON' | |
| 78 results['controllerTraceDataKey'] = 'traceEvents' | |
| 79 with open(self._out_filename, 'w') as json_file: | |
| 80 json.dump(results, json_file) | |
| 81 print '\n wrote file://%s\n' % os.path.abspath(self._out_filename) | |
| 82 | |
| 83 def _WriteTraceHTML(self, results): | |
| 84 """Write the results of systrace to an HTML file.""" | |
| 85 def _read_asset(src_dir, filename): | |
| 86 return open(os.path.join(src_dir, filename)).read() | |
| 87 | |
| 88 print 'Writing trace HTML' | |
| 89 systrace_dir = os.path.abspath(os.path.dirname(__file__)) | |
| 90 html_prefix = _read_asset(systrace_dir, 'prefix.html') | |
| 91 html_suffix = _read_asset(systrace_dir, 'suffix.html') | |
| 92 trace_viewer_html = _read_asset(self._script_dir, | |
| 93 'systrace_trace_viewer.html') | |
| 94 | |
| 95 # Open the file in binary mode to prevent python from changing the | |
| 96 # line endings. | |
| 97 html_file = open(self._out_filename, 'wb') | |
| 98 html_file.write(html_prefix.replace('{{SYSTRACE_TRACE_VIEWER_HTML}}', | |
| 99 trace_viewer_html)) | |
| 100 | |
| 101 html_file.write('<!-- BEGIN TRACE -->\n') | |
| 102 for (_, data) in results.iteritems(): | |
| 103 html_file.write(' <script class="') | |
| 104 html_file.write('trace-data') | |
| 105 html_file.write('" type="application/text">\n') | |
| 106 html_file.write(convert(data)) | |
| 107 html_file.write(' </script>\n') | |
| 108 html_file.write('<!-- END TRACE -->\n') | |
| 109 | |
| 110 html_file.write(html_suffix) | |
| 111 html_file.close() | |
| 112 print '\n wrote file://%s\n' % os.path.abspath(self._out_filename) | |
| 113 | |
| 114 def convert(data): | |
| 115 """Convert a data element to the format to be output into HTML. | |
| 116 | |
| 117 If the data element is a dictionary or list, JSON-encode it. | |
| 118 If the data element is a string, leave it unchanged. | |
| 119 | |
| 120 Returns: | |
| 121 String to be output into the HTML file. | |
| 122 """ | |
| 123 if isinstance(data, dict) or isinstance(data, list): | |
| 124 return json.dumps(data) | |
| 125 elif isinstance(data, str): | |
| 126 return data | |
| 127 else: | |
| 128 raise ValueError('Invalid data format for HTML output') | |
| 129 | |
| 130 def CreateAgents(options, categories): | |
| 131 """Create systrace agents. | |
| 132 | |
| 133 This function will search systrace agent modules in agent directories and | |
| 134 create the corresponding systrace agents. | |
| 135 Args: | |
| 136 options: The command-line options. | |
| 137 categories: The trace categories to capture. | |
| 138 Returns: | |
| 139 The list of systrace agents. | |
| 140 """ | |
| 141 agent_dirs = [os.path.join(os.path.dirname(__file__), | |
| 142 DEFAULT_AGENT_DIR)] | |
| 143 if options.agent_dirs: | |
| 144 agent_dirs.extend(options.agent_dirs.split(',')) | |
| 145 | |
| 146 agents = [] | |
| 147 for agent_dir in agent_dirs: | |
| 148 if not agent_dir: | |
| 149 continue | |
| 150 for filename in os.listdir(agent_dir): | |
| 151 (module_name, ext) = os.path.splitext(filename) | |
| 152 if (ext != '.py' or module_name == '__init__' | |
| 153 or module_name.endswith('_unittest')): | |
| 154 continue | |
| 155 (f, pathname, data) = imp.find_module(module_name, [agent_dir]) | |
| 156 try: | |
| 157 module = imp.load_module(module_name, f, pathname, data) | |
| 158 finally: | |
| 159 if f: | |
| 160 f.close() | |
| 161 if module: | |
| 162 agent = module.try_create_agent(options, categories) | |
| 163 if not agent: | |
| 164 continue | |
| 165 agents.append(agent) | |
| 166 return agents | |
| OLD | NEW |