- add sources.
[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
11
12 class UnableToFindApplicationException(Exception):
13   """Exception when unable to find a launched application"""
14
15   def __init__(self, application):
16     super(UnableToFindApplicationException, self).__init__()
17     self.application = application
18
19   def __str__(self):
20     return repr(self.application)
21
22
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."""
26
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'),
34           executable=False)
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',
41           '--ei memory 20')
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,
45                                               True)
46
47   @classmethod
48   def name(cls):
49     return 'oomkiller'
50
51   @classmethod
52   def is_supported(cls, browser_type):
53     if browser_type == 'any':
54       return android_browser_finder.CanFindAvailableBrowsers()
55     return browser_type.startswith('android')
56
57   @classmethod
58   def WillCloseBrowser(cls, browser_backend, platform_backend):
59     browser_backend.adb.CloseApplication('org.chromium.memconsumer')
60
61   def CollectProfile(self):
62     missing_applications = self._MissingApplications()
63     if not len(missing_applications):
64       return []
65     raise UnableToFindApplicationException(', '.join(missing_applications))
66
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.
70     must_have_apps = [
71         'org.chromium.memconsumer',
72     ]
73     return [app for app in must_have_apps if
74             not self._platform_backend.IsApplicationRunning(app)]