1#!/usr/bin/env python 2 3# Copyright 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 8necessary tracing agents for systrace, runs them, and outputs the results 9as an HTML or JSON file.''' 10 11from systrace import output_generator 12from systrace import tracing_controller 13from systrace.tracing_agents import android_process_data_agent 14from systrace.tracing_agents import atrace_agent 15from systrace.tracing_agents import atrace_from_file_agent 16from systrace.tracing_agents import atrace_process_dump 17from systrace.tracing_agents import ftrace_agent 18from systrace.tracing_agents import walt_agent 19 20AGENT_MODULES = [android_process_data_agent, atrace_agent, 21 atrace_from_file_agent, atrace_process_dump, 22 ftrace_agent, walt_agent] 23 24class SystraceRunner(object): 25 def __init__(self, script_dir, options): 26 """Constructor. 27 28 Args: 29 script_dir: Directory containing the trace viewer script 30 (systrace_trace_viewer.html) 31 options: Object containing command line options. 32 """ 33 # Parse command line arguments and create agents. 34 self._script_dir = script_dir 35 self._out_filename = options.output_file 36 agents_with_config = tracing_controller.CreateAgentsWithConfig( 37 options, AGENT_MODULES) 38 controller_config = tracing_controller.GetControllerConfig(options) 39 40 # Set up tracing controller. 41 self._tracing_controller = tracing_controller.TracingController( 42 agents_with_config, controller_config) 43 44 def StartTracing(self): 45 self._tracing_controller.StartTracing() 46 47 def StopTracing(self): 48 self._tracing_controller.StopTracing() 49 50 def OutputSystraceResults(self, write_json=False): 51 """Output the results of systrace to a file. 52 53 If output is necessary, then write the results of systrace to either (a) 54 a standalone HTML file, or (b) a json file which can be read by the 55 trace viewer. 56 57 Args: 58 write_json: Whether to output to a json file (if false, use HTML file) 59 """ 60 print 'Tracing complete, writing results' 61 if write_json: 62 result = output_generator.GenerateJSONOutput( 63 self._tracing_controller.all_results, 64 self._out_filename) 65 else: 66 result = output_generator.GenerateHTMLOutput( 67 self._tracing_controller.all_results, 68 self._out_filename) 69 print '\nWrote trace %s file: file://%s\n' % (('JSON' if write_json 70 else 'HTML'), result) 71