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.
10 import resource # pylint: disable=F0401
12 resource = None # Not available on all platforms
14 from ctypes import util
15 from telemetry.core.platform import posix_platform_backend
17 class MacPlatformBackend(posix_platform_backend.PosixPlatformBackend):
19 super(MacPlatformBackend, self).__init__()
22 def StartRawDisplayFrameRateMeasurement(self):
23 raise NotImplementedError()
25 def StopRawDisplayFrameRateMeasurement(self):
26 raise NotImplementedError()
28 def GetRawDisplayFrameRateMeasurements(self):
29 raise NotImplementedError()
31 def IsThermallyThrottled(self):
32 raise NotImplementedError()
34 def HasBeenThermallyThrottled(self):
35 raise NotImplementedError()
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)]
61 self.size = ctypes.sizeof(self)
62 super(ProcTaskInfo, self).__init__()
64 proc_info = ProcTaskInfo()
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)
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}
75 def GetCpuTimestamp(self):
76 """Return current timestamp in seconds."""
77 return {'TotalTime': time.time()}
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
88 def GetMemoryStats(self, pid):
89 rss_vsz = self._GetPsOutput(['rss', 'vsz'], pid)
91 rss, vsz = rss_vsz[0].split()
92 return {'VM': 1024 * int(vsz),
93 'WorkingSetSize': 1024 * int(rss)}
99 def GetOSVersionName(self):
100 os_version = os.uname()[2]
102 if os_version.startswith('9.'):
104 if os_version.startswith('10.'):
106 if os_version.startswith('11.'):
108 if os_version.startswith('12.'):
109 return 'mountainlion'
110 #if os_version.startswith('13.'):
113 def CanFlushIndividualFilesFromSystemCache(self):
116 def FlushEntireSystemCache(self):
117 p = subprocess.Popen(['purge'])
119 assert p.returncode == 0, 'Failed to flush system cache'