- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / mac_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 ctypes
6 import os
7 import subprocess
8 import time
9 try:
10   import resource  # pylint: disable=F0401
11 except ImportError:
12   resource = None  # Not available on all platforms
13
14 from ctypes import util
15 from telemetry.core.platform import posix_platform_backend
16
17 class MacPlatformBackend(posix_platform_backend.PosixPlatformBackend):
18   def __init__(self):
19     super(MacPlatformBackend, self).__init__()
20     self.libproc = None
21
22   def StartRawDisplayFrameRateMeasurement(self):
23     raise NotImplementedError()
24
25   def StopRawDisplayFrameRateMeasurement(self):
26     raise NotImplementedError()
27
28   def GetRawDisplayFrameRateMeasurements(self):
29     raise NotImplementedError()
30
31   def IsThermallyThrottled(self):
32     raise NotImplementedError()
33
34   def HasBeenThermallyThrottled(self):
35     raise NotImplementedError()
36
37   def GetCpuStats(self, pid):
38     """Return current cpu processing time of pid in seconds."""
39     class ProcTaskInfo(ctypes.Structure):
40       """Struct for proc_pidinfo() call."""
41       _fields_ = [("pti_virtual_size", ctypes.c_uint64),
42                   ("pti_resident_size", ctypes.c_uint64),
43                   ("pti_total_user", ctypes.c_uint64),
44                   ("pti_total_system", ctypes.c_uint64),
45                   ("pti_threads_user", ctypes.c_uint64),
46                   ("pti_threads_system", ctypes.c_uint64),
47                   ("pti_policy", ctypes.c_int32),
48                   ("pti_faults", ctypes.c_int32),
49                   ("pti_pageins", ctypes.c_int32),
50                   ("pti_cow_faults", ctypes.c_int32),
51                   ("pti_messages_sent", ctypes.c_int32),
52                   ("pti_messages_received", ctypes.c_int32),
53                   ("pti_syscalls_mach", ctypes.c_int32),
54                   ("pti_syscalls_unix", ctypes.c_int32),
55                   ("pti_csw", ctypes.c_int32),
56                   ("pti_threadnum", ctypes.c_int32),
57                   ("pti_numrunning", ctypes.c_int32),
58                   ("pti_priority", ctypes.c_int32)]
59       PROC_PIDTASKINFO = 4
60       def __init__(self):
61         self.size = ctypes.sizeof(self)
62         super(ProcTaskInfo, self).__init__()
63
64     proc_info = ProcTaskInfo()
65     if not self.libproc:
66       self.libproc = ctypes.CDLL(util.find_library('libproc'))
67     self.libproc.proc_pidinfo(pid, proc_info.PROC_PIDTASKINFO, 0,
68                               ctypes.byref(proc_info), proc_info.size)
69
70     # Convert nanoseconds to seconds
71     cpu_time = (proc_info.pti_total_user / 1000000000.0 +
72                 proc_info.pti_total_system / 1000000000.0)
73     return {'CpuProcessTime': cpu_time}
74
75   def GetCpuTimestamp(self):
76     """Return current timestamp in seconds."""
77     return {'TotalTime': time.time()}
78
79   def GetSystemCommitCharge(self):
80     vm_stat = self._RunCommand(['vm_stat'])
81     for stat in vm_stat.splitlines():
82       key, value = stat.split(':')
83       if key == 'Pages active':
84         pages_active = int(value.strip()[:-1])  # Strip trailing '.'
85         return pages_active * resource.getpagesize() / 1024
86     return 0
87
88   def GetMemoryStats(self, pid):
89     rss_vsz = self._GetPsOutput(['rss', 'vsz'], pid)
90     if rss_vsz:
91       rss, vsz = rss_vsz[0].split()
92       return {'VM': 1024 * int(vsz),
93               'WorkingSetSize': 1024 * int(rss)}
94     return {}
95
96   def GetOSName(self):
97     return 'mac'
98
99   def GetOSVersionName(self):
100     os_version = os.uname()[2]
101
102     if os_version.startswith('9.'):
103       return 'leopard'
104     if os_version.startswith('10.'):
105       return 'snowleopard'
106     if os_version.startswith('11.'):
107       return 'lion'
108     if os_version.startswith('12.'):
109       return 'mountainlion'
110     #if os_version.startswith('13.'):
111     #  return 'mavericks'
112
113   def CanFlushIndividualFilesFromSystemCache(self):
114     return False
115
116   def FlushEntireSystemCache(self):
117     p = subprocess.Popen(['purge'])
118     p.wait()
119     assert p.returncode == 0, 'Failed to flush system cache'