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
12 class UnableToFindApplicationException(Exception):
13 """Exception when unable to find a launched application"""
15 def __init__(self, application):
16 super(UnableToFindApplicationException, self).__init__()
17 self.application = application
20 return repr(self.application)
23 class OOMKillerProfiler(profiler.Profiler):
24 """Android-specific, Launch the music application and check it is still alive
25 at the end of the run."""
27 def __init__(self, browser_backend, platform_backend, output_path, state):
28 super(OOMKillerProfiler, self).__init__(
29 browser_backend, platform_backend, output_path, state)
30 if not 'mem_consumer_launched' in state:
31 state['mem_consumer_launched'] = True
32 mem_consumer_path = util.FindSupportBinary(
33 os.path.join('apks', 'MemConsumer.apk'),
35 assert mem_consumer_path, ('Could not find memconsumer app. Please build '
36 'memconsumer target.')
37 self._browser_backend.adb.Install(mem_consumer_path)
38 self._browser_backend.adb.GoHome()
39 self._platform_backend.LaunchApplication(
40 'org.chromium.memconsumer/.MemConsumer',
42 # Bring the browser to the foreground after launching the mem consumer
43 self._browser_backend.adb.StartActivity(browser_backend.package,
44 browser_backend.activity,
52 def is_supported(cls, browser_type):
53 if browser_type == 'any':
54 return android_browser_finder.CanFindAvailableBrowsers()
55 return browser_type.startswith('android')
58 def WillCloseBrowser(cls, browser_backend, platform_backend):
59 browser_backend.adb.CloseApplication('org.chromium.memconsumer')
61 def CollectProfile(self):
62 missing_applications = self._MissingApplications()
63 if not len(missing_applications):
65 raise UnableToFindApplicationException(', '.join(missing_applications))
67 def _MissingApplications(self):
68 # TODO(qsr): Add com.android.launcher to the list, when the reason why the
69 # launcher is often killed is understood.
71 'org.chromium.memconsumer',
73 return [app for app in must_have_apps if
74 not self._platform_backend.IsApplicationRunning(app)]