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.
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
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
19 from pylib.perf import surface_stats_collector # pylint: disable=F0401
21 surface_stats_collector = None
24 class AndroidPlatformBackend(platform_backend.PlatformBackend):
25 def __init__(self, adb, no_performance_mode):
26 super(AndroidPlatformBackend, self).__init__()
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!')
36 def IsRawDisplayFrameRateSupported(self):
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()
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))
54 self._surface_stats_collector = None
56 def GetRawDisplayFrameRateMeasurements(self):
57 ret = self._raw_display_frame_rate_measurements
58 self._raw_display_frame_rate_measurements = []
61 def SetFullPerformanceModeEnabled(self, enabled):
62 if self._no_performance_mode:
65 self._perf_tests_setup.SetHighPerfMode()
67 self._perf_tests_setup.SetDefaultPerfMode()
69 def CanMonitorThermalThrottling(self):
72 def IsThermallyThrottled(self):
73 return self._thermal_throttle.IsThrottled()
75 def HasBeenThermallyThrottled(self):
76 return self._thermal_throttle.HasBeenThrottled()
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
84 def GetCpuStats(self, pid):
85 if not self._adb.CanAccessProtectedFileContents():
86 logging.warning('CPU stats cannot be retrieved on non-rooted device.')
88 stats = self._adb.GetProtectedFileContents('/proc/%s/stat' % pid,
91 logging.warning('Unable to get /proc/%s/stat, process gone?', pid)
93 return proc_util.GetCpuStats(stats[0].split())
95 def GetCpuTimestamp(self):
96 if not self._adb.CanAccessProtectedFileContents():
97 logging.warning('CPU stats cannot be retrieved on non-rooted device.')
99 timer_list = self._adb.GetProtectedFileContents('/proc/timer_list',
101 return proc_util.GetTimestampJiffies(timer_list)
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}
109 def GetIOStats(self, pid):
112 def GetChildPids(self, pid):
114 ps = self._adb.RunShellCommand('ps', log_result=False)[1:]
119 if int(curr_pid) == pid:
125 if curr_name.startswith(name) and curr_name != name:
126 child_pids.append(int(curr_pid))
130 def GetCommandLine(self, pid):
131 ps = self._adb.RunShellCommand('ps', log_result=False)[1:]
136 if int(curr_pid) == pid:
138 raise Exception("Could not get command line for %d" % pid)
143 def CanFlushIndividualFilesFromSystemCache(self):
146 def FlushEntireSystemCache(self):
147 cache = cache_control.CacheControl(self._adb)
148 cache.DropRamCaches()
150 def FlushSystemCacheForDirectory(self, directory, ignoring=None):
151 raise NotImplementedError()
153 def LaunchApplication(self, application, parameters=None):
156 self._adb.RunShellCommand('am start ' + parameters + ' ' + application)
158 def IsApplicationRunning(self, application):
159 return len(self._adb.ExtractPid(application)) > 0
161 def CanLaunchApplication(self, application):