- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / android_platform_backend.py
1 # Copyright (c) 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 logging
6
7 from telemetry.core import platform
8 from telemetry.core import util
9 from telemetry.core.platform import platform_backend
10 from telemetry.core.platform import proc_util
11
12 # Get build/android scripts into our path.
13 util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android')
14 from pylib.perf import cache_control  # pylint: disable=F0401
15 from pylib.perf import perf_control  # pylint: disable=F0401
16 from pylib.perf import thermal_throttle  # pylint: disable=F0401
17
18 try:
19   from pylib.perf import surface_stats_collector  # pylint: disable=F0401
20 except Exception:
21   surface_stats_collector = None
22
23
24 class AndroidPlatformBackend(platform_backend.PlatformBackend):
25   def __init__(self, adb, no_performance_mode):
26     super(AndroidPlatformBackend, self).__init__()
27     self._adb = adb
28     self._surface_stats_collector = None
29     self._perf_tests_setup = perf_control.PerfControl(self._adb)
30     self._thermal_throttle = thermal_throttle.ThermalThrottle(self._adb)
31     self._no_performance_mode = no_performance_mode
32     self._raw_display_frame_rate_measurements = []
33     if self._no_performance_mode:
34       logging.warning('CPU governor will not be set!')
35
36   def IsRawDisplayFrameRateSupported(self):
37     return True
38
39   def StartRawDisplayFrameRateMeasurement(self):
40     assert not self._surface_stats_collector
41     # Clear any leftover data from previous timed out tests
42     self._raw_display_frame_rate_measurements = []
43     self._surface_stats_collector = \
44         surface_stats_collector.SurfaceStatsCollector(self._adb)
45     self._surface_stats_collector.Start()
46
47   def StopRawDisplayFrameRateMeasurement(self):
48     self._surface_stats_collector.Stop()
49     for r in self._surface_stats_collector.GetResults():
50       self._raw_display_frame_rate_measurements.append(
51           platform.Platform.RawDisplayFrameRateMeasurement(
52               r.name, r.value, r.unit))
53
54     self._surface_stats_collector = None
55
56   def GetRawDisplayFrameRateMeasurements(self):
57     ret = self._raw_display_frame_rate_measurements
58     self._raw_display_frame_rate_measurements = []
59     return ret
60
61   def SetFullPerformanceModeEnabled(self, enabled):
62     if self._no_performance_mode:
63       return
64     if enabled:
65       self._perf_tests_setup.SetHighPerfMode()
66     else:
67       self._perf_tests_setup.SetDefaultPerfMode()
68
69   def CanMonitorThermalThrottling(self):
70     return True
71
72   def IsThermallyThrottled(self):
73     return self._thermal_throttle.IsThrottled()
74
75   def HasBeenThermallyThrottled(self):
76     return self._thermal_throttle.HasBeenThrottled()
77
78   def GetSystemCommitCharge(self):
79     for line in self._adb.RunShellCommand('dumpsys meminfo', log_result=False):
80       if line.startswith('Total PSS: '):
81         return int(line.split()[2]) * 1024
82     return 0
83
84   def GetCpuStats(self, pid):
85     if not self._adb.CanAccessProtectedFileContents():
86       logging.warning('CPU stats cannot be retrieved on non-rooted device.')
87       return {}
88     stats = self._adb.GetProtectedFileContents('/proc/%s/stat' % pid,
89                                                log_result=False)
90     if not stats:
91       logging.warning('Unable to get /proc/%s/stat, process gone?', pid)
92       return {}
93     return proc_util.GetCpuStats(stats[0].split())
94
95   def GetCpuTimestamp(self):
96     if not self._adb.CanAccessProtectedFileContents():
97       logging.warning('CPU stats cannot be retrieved on non-rooted device.')
98       return {}
99     timer_list = self._adb.GetProtectedFileContents('/proc/timer_list',
100                                                     log_result=False)
101     return proc_util.GetTimestampJiffies(timer_list)
102
103   def GetMemoryStats(self, pid):
104     memory_usage = self._adb.GetMemoryUsageForPid(pid)[0]
105     return {'ProportionalSetSize': memory_usage['Pss'] * 1024,
106             'SharedDirty': memory_usage['Shared_Dirty'] * 1024,
107             'PrivateDirty': memory_usage['Private_Dirty'] * 1024}
108
109   def GetIOStats(self, pid):
110     return {}
111
112   def GetChildPids(self, pid):
113     child_pids = []
114     ps = self._adb.RunShellCommand('ps', log_result=False)[1:]
115     for line in ps:
116       data = line.split()
117       curr_pid = data[1]
118       curr_name = data[-1]
119       if int(curr_pid) == pid:
120         name = curr_name
121         for line in ps:
122           data = line.split()
123           curr_pid = data[1]
124           curr_name = data[-1]
125           if curr_name.startswith(name) and curr_name != name:
126             child_pids.append(int(curr_pid))
127         break
128     return child_pids
129
130   def GetCommandLine(self, pid):
131     ps = self._adb.RunShellCommand('ps', log_result=False)[1:]
132     for line in ps:
133       data = line.split()
134       curr_pid = data[1]
135       curr_name = data[-1]
136       if int(curr_pid) == pid:
137         return curr_name
138     raise Exception("Could not get command line for %d" % pid)
139
140   def GetOSName(self):
141     return 'android'
142
143   def CanFlushIndividualFilesFromSystemCache(self):
144     return False
145
146   def FlushEntireSystemCache(self):
147     cache = cache_control.CacheControl(self._adb)
148     cache.DropRamCaches()
149
150   def FlushSystemCacheForDirectory(self, directory, ignoring=None):
151     raise NotImplementedError()
152
153   def LaunchApplication(self, application, parameters=None):
154     if not parameters:
155       parameters = ''
156     self._adb.RunShellCommand('am start ' + parameters + ' ' + application)
157
158   def IsApplicationRunning(self, application):
159     return len(self._adb.ExtractPid(application)) > 0
160
161   def CanLaunchApplication(self, application):
162     return True