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 devil
.android
.perf
import cache_control
10 from devil
.android
.sdk
import intent
11 from pylib
import flag_changer
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()
40 launch_intent
= intent
.Intent(
41 action
='android.intent.action.MAIN',
42 package
=self
._package
_info
.package
,
43 activity
=self
._package
_info
.activity
)
45 launch_intent
= intent
.Intent(
46 package
=self
._package
_info
.package
,
47 activity
=self
._package
_info
.activity
,
49 extras
={'create_new_tab': True})
50 self
._device
.StartActivity(launch_intent
, blocking
=True)
52 def _TearDownTracing(self
):
53 changer
= flag_changer
.FlagChanger(
54 self
._device
, self
._package
_info
.cmdline_file
)
55 changer
.RemoveFlags(['--trace-startup'])
57 def StartTracing(self
, interval
):
59 self
._logcat
_monitor
.Start()
61 def StopTracing(self
):
63 self
._trace
_file
= self
._logcat
_monitor
.WaitFor(
64 self
._trace
_finish
_re
).group(1)
66 self
._TearDownTracing
()
69 # Wait a bit for the browser to finish writing the trace file.
71 trace_file
= self
._trace
_file
.replace('/storage/emulated/0/', '/sdcard/')
72 host_file
= os
.path
.join(os
.path
.curdir
, os
.path
.basename(trace_file
))
73 self
._device
.PullFile(trace_file
, host_file
)