Update To 11.40.268.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       arch_name = self._browser_backend.adb.device().GetABI()
36       mem_consumer_path = support_binaries.FindPath(
37           os.path.join('apks', 'MemConsumer.apk'), arch_name, 'android')
38       assert mem_consumer_path, ('Could not find memconsumer app. Please build '
39                                  'memconsumer target.')
40       if not self._platform_backend.CanLaunchApplication(
41           'org.chromium.memconsumerg'):
42         self._platform_backend.InstallApplication(mem_consumer_path)
43       self._browser_backend.adb.device().GoHome()
44       self._platform_backend.LaunchApplication(
45           'org.chromium.memconsumer/.MemConsumer',
46           '--ei memory 20')
47       # Bring the browser to the foreground after launching the mem consumer
48       self._browser_backend.adb.device().StartActivity(
49           intent.Intent(package=browser_backend.package,
50                         activity=browser_backend.activity),
51           blocking=True)
52
53   @classmethod
54   def name(cls):
55     return 'oomkiller'
56
57   @classmethod
58   def is_supported(cls, browser_type):
59     if browser_type == 'any':
60       return android_browser_finder.CanFindAvailableBrowsers()
61     return browser_type.startswith('android')
62
63   @classmethod
64   def WillCloseBrowser(cls, browser_backend, platform_backend):
65     browser_backend.adb.device().ForceStop('org.chromium.memconsumer')
66
67   def CollectProfile(self):
68     missing_applications = self._MissingApplications()
69     if not len(missing_applications):
70       return []
71     raise UnableToFindApplicationException(', '.join(missing_applications))
72
73   def _MissingApplications(self):
74     # TODO(qsr): Add com.android.launcher to the list, when the reason why the
75     # launcher is often killed is understood.
76     must_have_apps = [
77         'org.chromium.memconsumer',
78     ]
79     return [app for app in must_have_apps if
80             not self._platform_backend.IsApplicationRunning(app)]