1 # Copyright 2013 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.
7 from telemetry.core import util
8 from telemetry.core.backends.chrome import android_browser_finder
9 from telemetry.core.platform import profiler
10 from telemetry.util import support_binaries
12 util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android')
13 from pylib.device import intent # pylint: disable=F0401
15 class UnableToFindApplicationException(Exception):
16 """Exception when unable to find a launched application"""
18 def __init__(self, application):
19 super(UnableToFindApplicationException, self).__init__()
20 self.application = application
23 return repr(self.application)
26 class OOMKillerProfiler(profiler.Profiler):
27 """Android-specific, Launch the music application and check it is still alive
28 at the end of the run."""
30 def __init__(self, browser_backend, platform_backend, output_path, state):
31 super(OOMKillerProfiler, self).__init__(
32 browser_backend, platform_backend, output_path, state)
33 if not 'mem_consumer_launched' in state:
34 state['mem_consumer_launched'] = True
35 mem_consumer_path = support_binaries.FindPath(
36 os.path.join('apks', 'MemConsumer.apk'), 'android')
37 assert mem_consumer_path, ('Could not find memconsumer app. Please build '
38 'memconsumer target.')
39 if not self._platform_backend.CanLaunchApplication(
40 'org.chromium.memconsumerg'):
41 self._platform_backend.InstallApplication(mem_consumer_path)
42 self._browser_backend.adb.device().GoHome()
43 self._platform_backend.LaunchApplication(
44 'org.chromium.memconsumer/.MemConsumer',
46 # Bring the browser to the foreground after launching the mem consumer
47 self._browser_backend.adb.device().StartActivity(
48 intent.Intent(package=browser_backend.package,
49 activity=browser_backend.activity),
57 def is_supported(cls, browser_type):
58 if browser_type == 'any':
59 return android_browser_finder.CanFindAvailableBrowsers()
60 return browser_type.startswith('android')
63 def WillCloseBrowser(cls, browser_backend, platform_backend):
64 browser_backend.adb.device().ForceStop('org.chromium.memconsumer')
66 def CollectProfile(self):
67 missing_applications = self._MissingApplications()
68 if not len(missing_applications):
70 raise UnableToFindApplicationException(', '.join(missing_applications))
72 def _MissingApplications(self):
73 # TODO(qsr): Add com.android.launcher to the list, when the reason why the
74 # launcher is often killed is understood.
76 'org.chromium.memconsumer',
78 return [app for app in must_have_apps if
79 not self._platform_backend.IsApplicationRunning(app)]