Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1597)

Unified Diff: systrace/systrace/systrace_tracing_controller.py

Issue 1776013005: [DO NOT COMMIT] Refactor systrace to support new clock sync design (Closed) Base URL: git@github.com:catapult-project/catapult@master
Patch Set: changes from code review Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: systrace/systrace/systrace_tracing_controller.py
diff --git a/systrace/systrace/systrace_tracing_controller.py b/systrace/systrace/systrace_tracing_controller.py
new file mode 100644
index 0000000000000000000000000000000000000000..bf37f676379a693ef601377855db91a11477a11f
--- /dev/null
+++ b/systrace/systrace/systrace_tracing_controller.py
@@ -0,0 +1,148 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+'''Implementation of tracing controller for systrace. This class creates the
+necessary tracing agents for systrace, runs them, and outputs the results
+as an HTML file.'''
+
+import imp
+import os
+from systrace import tracing_controller
+
+# The default agent directory.
+DEFAULT_AGENT_DIR = 'agents'
+
+
+class SystraceTracingController(tracing_controller.TracingController):
+ def __init__(self, script_dir, options, categories):
+ """Set up the SystraceTracingController.
+
+ Args:
+ script_dir: Directory containing the trace viewer script
+ (systrace_trace_viewer.html)
+ options: List of command line options.
+ categories: List of trace categories to capture.
+ """
+ # Parse command line arguments and create agents
+ self._script_dir = script_dir
+ self._html_filename = options.output_file
+ 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
+ agents = CreateAgents(options, categories)
+
+ # Error if no agents are available
+ if not agents:
+ dirs = DEFAULT_AGENT_DIR
+ if options.agent_dirs:
+ dirs += ',' + options.agent_dirs
+ raise RuntimeError('No systrace agent is available'
+ 'in directories |%s|.\n' % dirs)
+
+ # Update trace viewer if necessary
+ try:
+ update_systrace_trace_viewer = __import__('update_systrace_trace_viewer')
+ except ImportError:
+ pass
+ else:
+ update_systrace_trace_viewer.update()
+
+ super(SystraceTracingController, self).__init__(agents)
+
+ #override
+ def SupportsExplicitClockSync(self):
+ return False
+
+ #override
+ def RecordClockSyncMarker(self, sync_id, callback):
+ raise NotImplementedError
+
+ def OutputSystraceResults(self, json=False):
+ """Output the results of systrace to a file if
+ output is necessary.
+
+ Args:
+ json: Whether to output to a json file (if false, use HTML file)
+ """
+ if all([x.should_output_trace for x in self._all_results]):
+ print 'Tracing complete, writing results'
+ if json:
+ self._WriteTraceJSON(self._all_results)
+ else:
+ self._WriteTraceHTML(self._all_results)
+ else:
+ print 'Tracing complete, results not required'
+
+ def _WriteTraceJSON(self, results):
+ raise NotImplementedError # TODO: write this function
+
+ def _WriteTraceHTML(self, results):
+ """Write the results of systrace to an HTML file."""
+ def _read_asset(src_dir, filename):
+ return open(os.path.join(src_dir, filename)).read()
+
+ print 'Writing trace HTML'
+ systrace_dir = os.path.abspath(os.path.dirname(__file__))
+ html_prefix = _read_asset(systrace_dir, 'prefix.html')
+ html_suffix = _read_asset(systrace_dir, 'suffix.html')
+ trace_viewer_html = _read_asset(self._script_dir,
+ 'systrace_trace_viewer.html')
+
+ # Open the file in binary mode to prevent python from changing the
+ # line endings.
+ html_file = open(self._html_filename, 'wb')
+ html_file.write(html_prefix.replace('{{SYSTRACE_TRACE_VIEWER_HTML}}',
+ trace_viewer_html))
+
+ html_file.write('<!-- BEGIN TRACE -->\n')
+ for a in results:
+ if a.trace_data:
+ html_file.write(' <script class="')
+ html_file.write('trace-data')
+ html_file.write('" type="application/text">\n')
+ html_file.write(a.trace_data)
+ html_file.write(' </script>\n')
+ html_file.write('<!-- END TRACE -->\n')
+
+ html_file.write(html_suffix)
+ html_file.close()
+ print '\n wrote file://%s\n' % os.path.abspath(self._html_filename)
+
+def CreateAgents(options, categories):
+ """Create systrace agents.
+
+ This function will search systrace agent modules in agent directories and
+ create the corresponding systrace agents.
+ Args:
+ options: The command-line options.
+ categories: The trace categories to capture.
+ Returns:
+ The list of systrace agents.
+ """
+ agent_dirs = [os.path.join(os.path.dirname(__file__),
+ DEFAULT_AGENT_DIR)]
+ if options.agent_dirs:
+ agent_dirs.extend(options.agent_dirs.split(','))
+
+ agents = []
+ for agent_dir in agent_dirs:
+ if not agent_dir:
+ continue
+ for filename in os.listdir(agent_dir):
+ (module_name, ext) = os.path.splitext(filename)
+ if (ext != '.py' or module_name == '__init__'
+ or module_name.endswith('_unittest')):
+ continue
+ (f, pathname, data) = imp.find_module(module_name, [agent_dir])
+ try:
+ module = imp.load_module(module_name, f, pathname, data)
+ finally:
+ if f:
+ f.close()
+ if module:
+ agent = module.try_create_agent(options, categories)
+ if not agent:
+ continue
+ agents.append(agent)
+ return agents

Powered by Google App Engine
This is Rietveld 408576698