Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / platform_backend.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 weakref
6
7 from telemetry.core.platform import network_controller_backend
8 from telemetry.core.platform import profiling_controller_backend
9 from telemetry.core.platform import tracing_controller_backend
10
11
12 # pylint: disable=W0613
13
14 # pylint: disable=W0212
15 class OSVersion(str):
16   def __new__(cls, friendly_name, sortable_name, *args, **kwargs):
17     version = str.__new__(cls, friendly_name)
18     version._sortable_name = sortable_name
19     return version
20
21   def __lt__(self, other):
22     return self._sortable_name < other._sortable_name
23
24   def __gt__(self, other):
25     return self._sortable_name > other._sortable_name
26
27   def __le__(self, other):
28     return self._sortable_name <= other._sortable_name
29
30   def __ge__(self, other):
31     return self._sortable_name >= other._sortable_name
32
33
34 XP =           OSVersion('xp',            5.1)
35 VISTA =        OSVersion('vista',         6.0)
36 WIN7 =         OSVersion('win7',          6.1)
37 WIN8 =         OSVersion('win8',          6.2)
38
39 LEOPARD =      OSVersion('leopard',      105)
40 SNOWLEOPARD =  OSVersion('snowleopard',  106)
41 LION =         OSVersion('lion',         107)
42 MOUNTAINLION = OSVersion('mountainlion', 108)
43 MAVERICKS =    OSVersion('mavericks',    109)
44 YOSEMITE =     OSVersion('yosemite',     1010)
45
46
47 class PlatformBackend(object):
48
49   def __init__(self, device=None):
50     """ Initalize an instance of PlatformBackend from a device optionally.
51       Call sites need to use SupportsDevice before intialization to check
52       whether this platform backend supports the device.
53       If device is None, this constructor returns the host platform backend
54       which telemetry is running on.
55
56       Args:
57         device: an instance of telemetry.core.platform.device.Device.
58     """
59     if device and not self.SupportsDevice(device):
60       raise ValueError('Unsupported device: %s' % device.name)
61     self._platform = None
62     self._running_browser_backends = weakref.WeakSet()
63     self._network_controller_backend = (
64         network_controller_backend.NetworkControllerBackend(self))
65     self._tracing_controller_backend = (
66         tracing_controller_backend.TracingControllerBackend(self))
67     self._profiling_controller_backend = (
68         profiling_controller_backend.ProfilingControllerBackend(self))
69
70   @classmethod
71   def SupportsDevice(cls, device):
72     """ Returns whether this platform backend supports intialization from the
73     device. """
74     return False
75
76   def SetPlatform(self, platform):
77     assert self._platform == None
78     self._platform = platform
79
80   @property
81   def platform(self):
82     return self._platform
83
84   @property
85   def running_browser_backends(self):
86     return list(self._running_browser_backends)
87
88   @property
89   def network_controller_backend(self):
90     return self._network_controller_backend
91
92   @property
93   def tracing_controller_backend(self):
94     return self._tracing_controller_backend
95
96   @property
97   def profiling_controller_backend(self):
98     return self._profiling_controller_backend
99
100   def DidCreateBrowser(self, browser, browser_backend):
101     self.SetFullPerformanceModeEnabled(True)
102
103     # TODO(slamm): Remove this call when replay browser_backend dependencies
104     # get moved to platform. https://crbug.com/423962
105     self._network_controller_backend.UpdateReplay(browser_backend)
106
107   def DidStartBrowser(self, browser, browser_backend):
108     assert browser not in self._running_browser_backends
109     self._running_browser_backends.add(browser_backend)
110     self._tracing_controller_backend.DidStartBrowser(
111         browser, browser_backend)
112
113   def WillCloseBrowser(self, browser, browser_backend):
114     self._tracing_controller_backend.WillCloseBrowser(
115         browser, browser_backend)
116     self._profiling_controller_backend.WillCloseBrowser(
117         browser_backend)
118     # TODO(slamm): Move this call when replay's life cycle is no longer
119     # tied to the browser. https://crbug.com/424777
120     self._network_controller_backend.StopReplay()
121
122     is_last_browser = len(self._running_browser_backends) == 1
123     if is_last_browser:
124       self.SetFullPerformanceModeEnabled(False)
125
126     self._running_browser_backends.remove(browser_backend)
127
128   def GetBackendForBrowser(self, browser):
129     matches = [x for x in self._running_browser_backends
130                if x.browser == browser]
131     if len(matches) == 0:
132       raise Exception('No browser found')
133     assert len(matches) == 1
134     return matches[0]
135
136   def IsRawDisplayFrameRateSupported(self):
137     return False
138
139   def StartRawDisplayFrameRateMeasurement(self):
140     raise NotImplementedError()
141
142   def StopRawDisplayFrameRateMeasurement(self):
143     raise NotImplementedError()
144
145   def GetRawDisplayFrameRateMeasurements(self):
146     raise NotImplementedError()
147
148   def SetFullPerformanceModeEnabled(self, enabled):
149     pass
150
151   def CanMonitorThermalThrottling(self):
152     return False
153
154   def IsThermallyThrottled(self):
155     raise NotImplementedError()
156
157   def HasBeenThermallyThrottled(self):
158     raise NotImplementedError()
159
160   def GetSystemCommitCharge(self):
161     raise NotImplementedError()
162
163   def GetSystemTotalPhysicalMemory(self):
164     raise NotImplementedError()
165
166   def GetCpuStats(self, pid):
167     return {}
168
169   def GetCpuTimestamp(self):
170     return {}
171
172   def PurgeUnpinnedMemory(self):
173     pass
174
175   def GetMemoryStats(self, pid):
176     return {}
177
178   def GetIOStats(self, pid):
179     return {}
180
181   def GetChildPids(self, pid):
182     raise NotImplementedError()
183
184   def GetCommandLine(self, pid):
185     raise NotImplementedError()
186
187   def GetArchName(self):
188     raise NotImplementedError()
189
190   def GetOSName(self):
191     raise NotImplementedError()
192
193   def GetOSVersionName(self):
194     raise NotImplementedError()
195
196   def CanFlushIndividualFilesFromSystemCache(self):
197     raise NotImplementedError()
198
199   def FlushEntireSystemCache(self):
200     raise NotImplementedError()
201
202   def FlushSystemCacheForDirectory(self, directory, ignoring=None):
203     raise NotImplementedError()
204
205   def FlushDnsCache(self):
206     pass
207
208   def LaunchApplication(
209       self, application, parameters=None, elevate_privilege=False):
210     raise NotImplementedError()
211
212   def IsApplicationRunning(self, application):
213     raise NotImplementedError()
214
215   def CanLaunchApplication(self, application):
216     return False
217
218   def InstallApplication(self, application):
219     raise NotImplementedError()
220
221   def CanCaptureVideo(self):
222     return False
223
224   def StartVideoCapture(self, min_bitrate_mbps):
225     raise NotImplementedError()
226
227   @property
228   def is_video_capture_running(self):
229     return False
230
231   def StopVideoCapture(self):
232     raise NotImplementedError()
233
234   def CanMonitorPower(self):
235     return False
236
237   def CanMeasurePerApplicationPower(self):
238     return False
239
240   def StartMonitoringPower(self, browser):
241     raise NotImplementedError()
242
243   def StopMonitoringPower(self):
244     raise NotImplementedError()
245
246   def ReadMsr(self, msr_number, start=0, length=64):
247     """Read a CPU model-specific register (MSR).
248
249     Which MSRs are available depends on the CPU model.
250     On systems with multiple CPUs, this function may run on any CPU.
251
252     Args:
253       msr_number: The number of the register to read.
254       start: The least significant bit to read, zero-indexed.
255           (Said another way, the number of bits to right-shift the MSR value.)
256       length: The number of bits to read. MSRs are 64 bits, even on 32-bit CPUs.
257     """
258     raise NotImplementedError()