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.
9 from pylib
import flag_changer
10 from pylib
.device
import intent
11 from pylib
.perf
import cache_control
13 from profile_chrome
import controllers
15 class ChromeStartupTracingController(controllers
.BaseController
):
16 def __init__(self
, device
, package_info
, cold
, url
):
18 self
._package
_info
= package_info
20 self
._logcat
_monitor
= self
._device
.GetLogcatMonitor()
22 self
._trace
_file
= None
23 self
._trace
_finish
_re
= re
.compile(r
' Completed startup tracing to (.*)')
26 return 'Browser Startup Trace'
28 def _SetupTracing(self
):
29 # TODO(lizeb): Figure out how to clean up the command-line file when
30 # _TearDownTracing() is not executed in StopTracing().
31 changer
= flag_changer
.FlagChanger(
32 self
._device
, self
._package
_info
.cmdline_file
)
33 changer
.AddFlags(['--trace-startup'])
34 self
._device
.ForceStop(self
._package
_info
.package
)
36 self
._device
.EnableRoot()
37 cache_control
.CacheControl(self
._device
).DropRamCaches()
38 self
._device
.StartActivity(
40 package
=self
._package
_info
.package
,
41 activity
=self
._package
_info
.activity
,
43 extras
={'create_new_tab' : True}), blocking
=True)
45 def _TearDownTracing(self
):
46 changer
= flag_changer
.FlagChanger(
47 self
._device
, self
._package
_info
.cmdline_file
)
48 changer
.RemoveFlags(['--trace-startup'])
50 def StartTracing(self
, interval
):
52 self
._logcat
_monitor
.Start()
54 def StopTracing(self
):
56 self
._trace
_file
= self
._logcat
_monitor
.WaitFor(
57 self
._trace
_finish
_re
).group(1)
59 self
._TearDownTracing
()
62 # Wait a bit for the browser to finish writing the trace file.
64 trace_file
= self
._trace
_file
.replace('/storage/emulated/0/', '/sdcard/')
65 host_file
= os
.path
.join(os
.path
.curdir
, os
.path
.basename(trace_file
))
66 self
._device
.PullFile(trace_file
, host_file
)