1 # Copyright 2015 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import os 6 import re 7 import time 8 9 from devil.android import flag_changer 10 from devil.android.perf import cache_control 11 from devil.android.sdk import intent 12 13 from profile_chrome import controllers 14 15 class ChromeStartupTracingController(controllers.BaseController): 16 def __init__(self, device, package_info, cold, url): 17 self._device = device 18 self._package_info = package_info 19 self._cold = cold 20 self._logcat_monitor = self._device.GetLogcatMonitor() 21 self._url = url 22 self._trace_file = None 23 self._trace_finish_re = re.compile(r' Completed startup tracing to (.*)') 24 self._flag_changer = flag_changer.FlagChanger( 25 self._device, self._package_info.cmdline_file) 26 27 def __repr__(self): 28 return 'Browser Startup Trace' 29 30 def _SetupTracing(self): 31 # TODO(lizeb): Figure out how to clean up the command-line file when 32 # _TearDownTracing() is not executed in StopTracing(). 33 self._flag_changer.AddFlags(['--trace-startup']) 34 self._device.ForceStop(self._package_info.package) 35 if self._cold: 36 self._device.EnableRoot() 37 cache_control.CacheControl(self._device).DropRamCaches() 38 launch_intent = None 39 if self._url == '': 40 launch_intent = intent.Intent( 41 action='android.intent.action.MAIN', 42 package=self._package_info.package, 43 activity=self._package_info.activity) 44 else: 45 launch_intent = intent.Intent( 46 package=self._package_info.package, 47 activity=self._package_info.activity, 48 data=self._url, 49 extras={'create_new_tab': True}) 50 self._device.StartActivity(launch_intent, blocking=True) 51 52 def _TearDownTracing(self): 53 self._flag_changer.Restore() 54 55 def StartTracing(self, interval): # pylint: disable=unused-argument 56 self._SetupTracing() 57 self._logcat_monitor.Start() 58 59 def StopTracing(self): 60 try: 61 self._trace_file = self._logcat_monitor.WaitFor( 62 self._trace_finish_re).group(1) 63 finally: 64 self._TearDownTracing() 65 66 def PullTrace(self): 67 # Wait a bit for the browser to finish writing the trace file. 68 time.sleep(3) 69 trace_file = self._trace_file.replace('/storage/emulated/0/', '/sdcard/') 70 host_file = os.path.join(os.path.curdir, os.path.basename(trace_file)) 71 self._device.PullFile(trace_file, host_file) 72 return host_file 73