Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / profiler / oomkiller_profiler.py
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.
4
5 import os
6
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
11
12 util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android')
13 from pylib.device import intent  # pylint: disable=F0401
14
15 class UnableToFindApplicationException(Exception):
16   """Exception when unable to find a launched application"""
17
18   def __init__(self, application):
19     super(UnableToFindApplicationException, self).__init__()
20     self.application = application
21
22   def __str__(self):
23     return repr(self.application)
24
25
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."""
29
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',
45           '--ei memory 20')
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),
50           blocking=True)
51
52   @classmethod
53   def name(cls):
54     return 'oomkiller'
55
56   @classmethod
57   def is_supported(cls, browser_type):
58     if browser_type == 'any':
59       return android_browser_finder.CanFindAvailableBrowsers()
60     return browser_type.startswith('android')
61
62   @classmethod
63   def WillCloseBrowser(cls, browser_backend, platform_backend):
64     browser_backend.adb.device().ForceStop('org.chromium.memconsumer')
65
66   def CollectProfile(self):
67     missing_applications = self._MissingApplications()
68     if not len(missing_applications):
69       return []
70     raise UnableToFindApplicationException(', '.join(missing_applications))
71
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.
75     must_have_apps = [
76         'org.chromium.memconsumer',
77     ]
78     return [app for app in must_have_apps if
79             not self._platform_backend.IsApplicationRunning(app)]