OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # $Id: _psosx.py 794 2010-11-12 13:29:52Z g.rodola $ | 3 # $Id: _psosx.py 1142 2011-10-05 18:45:49Z g.rodola $ |
4 # | 4 # |
| 5 # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 6 # Use of this source code is governed by a BSD-style license that can be |
| 7 # found in the LICENSE file. |
| 8 |
| 9 """OSX platform implementation.""" |
5 | 10 |
6 import errno | 11 import errno |
7 import os | 12 import os |
8 | 13 |
9 try: | 14 import _psutil_osx |
10 from collections import namedtuple | 15 import _psutil_posix |
11 except ImportError: | 16 import _psposix |
12 from psutil.compat import namedtuple # python < 2.6 | 17 from psutil.error import AccessDenied, NoSuchProcess, TimeoutExpired |
| 18 from psutil._compat import namedtuple |
| 19 from psutil._common import * |
13 | 20 |
14 import _psutil_osx | 21 __extra__all__ = [] |
15 import _psposix | |
16 from psutil.error import AccessDenied, NoSuchProcess | |
17 | 22 |
18 # --- constants | 23 # --- constants |
19 | 24 |
20 NUM_CPUS = _psutil_osx.get_num_cpus() | 25 NUM_CPUS = _psutil_osx.get_num_cpus() |
21 TOTAL_PHYMEM = _psutil_osx.get_total_phymem() | 26 BOOT_TIME = _psutil_osx.get_system_boot_time() |
| 27 _TERMINAL_MAP = _psposix._get_terminal_map() |
| 28 _cputimes_ntuple = namedtuple('cputimes', 'user nice system idle') |
22 | 29 |
23 # --- functions | 30 # --- functions |
24 | 31 |
25 def avail_phymem(): | 32 def phymem_usage(): |
26 "Return the amount of physical memory available on the system, in bytes." | 33 """Physical system memory as a (total, used, free) tuple.""" |
27 return _psutil_osx.get_avail_phymem() | 34 total = _psutil_osx.get_total_phymem() |
| 35 free = _psutil_osx.get_avail_phymem() |
| 36 used = total - free |
| 37 percent = usage_percent(used, total, _round=1) |
| 38 return ntuple_sysmeminfo(total, used, free, percent) |
28 | 39 |
29 def used_phymem(): | 40 def virtmem_usage(): |
30 "Return the amount of physical memory currently in use on the system, in byt
es." | 41 """Virtual system memory as a (total, used, free) tuple.""" |
31 return TOTAL_PHYMEM - _psutil_osx.get_avail_phymem() | 42 total = _psutil_osx.get_total_virtmem() |
32 | 43 free = _psutil_osx.get_avail_virtmem() |
33 def total_virtmem(): | 44 used = total - free |
34 "Return the amount of total virtual memory available on the system, in bytes
." | 45 percent = usage_percent(used, total, _round=1) |
35 return _psutil_osx.get_total_virtmem() | 46 return ntuple_sysmeminfo(total, used, free, percent) |
36 | |
37 def avail_virtmem(): | |
38 "Return the amount of virtual memory currently in use on the system, in byte
s." | |
39 return _psutil_osx.get_avail_virtmem() | |
40 | |
41 def used_virtmem(): | |
42 """Return the amount of used memory currently in use on the system, in bytes
.""" | |
43 return _psutil_osx.get_total_virtmem() - _psutil_osx.get_avail_virtmem() | |
44 | 47 |
45 def get_system_cpu_times(): | 48 def get_system_cpu_times(): |
46 """Return a dict representing the following CPU times: | 49 """Return system CPU times as a namedtuple.""" |
47 user, nice, system, idle.""" | 50 user, nice, system, idle = _psutil_osx.get_system_cpu_times() |
48 values = _psutil_osx.get_system_cpu_times() | 51 return _cputimes_ntuple(user, nice, system, idle) |
49 return dict(user=values[0], nice=values[1], system=values[2], idle=values[3]
) | |
50 | 52 |
51 def get_pid_list(): | 53 def get_system_per_cpu_times(): |
52 """Returns a list of PIDs currently running on the system.""" | 54 """Return system CPU times as a named tuple""" |
53 return _psutil_osx.get_pid_list() | 55 ret = [] |
| 56 for cpu_t in _psutil_osx.get_system_per_cpu_times(): |
| 57 user, nice, system, idle = cpu_t |
| 58 item = _cputimes_ntuple(user, nice, system, idle) |
| 59 ret.append(item) |
| 60 return ret |
54 | 61 |
55 def pid_exists(pid): | 62 def disk_partitions(all=False): |
56 """Check For the existence of a unix pid.""" | 63 retlist = [] |
57 return _psposix.pid_exists(pid) | 64 partitions = _psutil_osx.get_disk_partitions() |
| 65 for partition in partitions: |
| 66 device, mountpoint, fstype = partition |
| 67 if device == 'none': |
| 68 device = '' |
| 69 if not all: |
| 70 if not os.path.isabs(device) \ |
| 71 or not os.path.exists(device): |
| 72 continue |
| 73 ntuple = ntuple_partition(device, mountpoint, fstype) |
| 74 retlist.append(ntuple) |
| 75 return retlist |
| 76 |
| 77 get_pid_list = _psutil_osx.get_pid_list |
| 78 pid_exists = _psposix.pid_exists |
| 79 get_disk_usage = _psposix.get_disk_usage |
| 80 network_io_counters = _psutil_osx.get_network_io_counters |
| 81 disk_io_counters = _psutil_osx.get_disk_io_counters |
58 | 82 |
59 # --- decorator | 83 # --- decorator |
60 | 84 |
61 def wrap_exceptions(callable): | 85 def wrap_exceptions(callable): |
62 """Call callable into a try/except clause so that if an | 86 """Call callable into a try/except clause so that if an |
63 OSError EPERM exception is raised we translate it into | 87 OSError EPERM exception is raised we translate it into |
64 psutil.AccessDenied. | 88 psutil.AccessDenied. |
65 """ | 89 """ |
66 def wrapper(self, *args, **kwargs): | 90 def wrapper(self, *args, **kwargs): |
67 try: | 91 try: |
68 return callable(self, *args, **kwargs) | 92 return callable(self, *args, **kwargs) |
69 except OSError, err: | 93 except OSError, err: |
70 if err.errno == errno.ESRCH: | 94 if err.errno == errno.ESRCH: |
71 raise NoSuchProcess(self.pid, self._process_name) | 95 raise NoSuchProcess(self.pid, self._process_name) |
72 if err.errno in (errno.EPERM, errno.EACCES): | 96 if err.errno in (errno.EPERM, errno.EACCES): |
73 raise AccessDenied(self.pid, self._process_name) | 97 raise AccessDenied(self.pid, self._process_name) |
74 raise | 98 raise |
75 return wrapper | 99 return wrapper |
76 | 100 |
77 | 101 |
78 class OSXProcess(object): | 102 _status_map = { |
| 103 _psutil_osx.SIDL : STATUS_IDLE, |
| 104 _psutil_osx.SRUN : STATUS_RUNNING, |
| 105 _psutil_osx.SSLEEP : STATUS_SLEEPING, |
| 106 _psutil_osx.SSTOP : STATUS_STOPPED, |
| 107 _psutil_osx.SZOMB : STATUS_ZOMBIE, |
| 108 } |
| 109 |
| 110 class Process(object): |
79 """Wrapper class around underlying C implementation.""" | 111 """Wrapper class around underlying C implementation.""" |
80 | 112 |
81 _meminfo_ntuple = namedtuple('meminfo', 'rss vms') | |
82 _cputimes_ntuple = namedtuple('cputimes', 'user system') | |
83 __slots__ = ["pid", "_process_name"] | 113 __slots__ = ["pid", "_process_name"] |
84 | 114 |
85 def __init__(self, pid): | 115 def __init__(self, pid): |
86 self.pid = pid | 116 self.pid = pid |
87 self._process_name = None | 117 self._process_name = None |
88 | 118 |
89 @wrap_exceptions | 119 @wrap_exceptions |
90 def get_process_name(self): | 120 def get_process_name(self): |
91 """Return process name as a string of limited len (15).""" | 121 """Return process name as a string of limited len (15).""" |
92 return _psutil_osx.get_process_name(self.pid) | 122 return _psutil_osx.get_process_name(self.pid) |
(...skipping 11 matching lines...) Expand all Loading... |
104 if not pid_exists(self.pid): | 134 if not pid_exists(self.pid): |
105 raise NoSuchProcess(self.pid, self._process_name) | 135 raise NoSuchProcess(self.pid, self._process_name) |
106 return _psutil_osx.get_process_cmdline(self.pid) | 136 return _psutil_osx.get_process_cmdline(self.pid) |
107 | 137 |
108 @wrap_exceptions | 138 @wrap_exceptions |
109 def get_process_ppid(self): | 139 def get_process_ppid(self): |
110 """Return process parent pid.""" | 140 """Return process parent pid.""" |
111 return _psutil_osx.get_process_ppid(self.pid) | 141 return _psutil_osx.get_process_ppid(self.pid) |
112 | 142 |
113 @wrap_exceptions | 143 @wrap_exceptions |
114 def get_process_uid(self): | 144 def get_process_uids(self): |
115 """Return process real user id.""" | 145 real, effective, saved = _psutil_osx.get_process_uids(self.pid) |
116 return _psutil_osx.get_process_uid(self.pid) | 146 return ntuple_uids(real, effective, saved) |
117 | 147 |
118 @wrap_exceptions | 148 @wrap_exceptions |
119 def get_process_gid(self): | 149 def get_process_gids(self): |
120 """Return process real group id.""" | 150 real, effective, saved = _psutil_osx.get_process_gids(self.pid) |
121 return _psutil_osx.get_process_gid(self.pid) | 151 return ntuple_gids(real, effective, saved) |
| 152 |
| 153 @wrap_exceptions |
| 154 def get_process_terminal(self): |
| 155 tty_nr = _psutil_osx.get_process_tty_nr(self.pid) |
| 156 try: |
| 157 return _TERMINAL_MAP[tty_nr] |
| 158 except KeyError: |
| 159 return None |
122 | 160 |
123 @wrap_exceptions | 161 @wrap_exceptions |
124 def get_memory_info(self): | 162 def get_memory_info(self): |
125 """Return a tuple with the process' RSS and VMS size.""" | 163 """Return a tuple with the process' RSS and VMS size.""" |
126 rss, vms = _psutil_osx.get_memory_info(self.pid) | 164 rss, vms = _psutil_osx.get_memory_info(self.pid) |
127 return self._meminfo_ntuple(rss, vms) | 165 return ntuple_meminfo(rss, vms) |
128 | 166 |
129 @wrap_exceptions | 167 @wrap_exceptions |
130 def get_cpu_times(self): | 168 def get_cpu_times(self): |
131 user, system = _psutil_osx.get_cpu_times(self.pid) | 169 user, system = _psutil_osx.get_cpu_times(self.pid) |
132 return self._cputimes_ntuple(user, system) | 170 return ntuple_cputimes(user, system) |
133 | 171 |
134 @wrap_exceptions | 172 @wrap_exceptions |
135 def get_process_create_time(self): | 173 def get_process_create_time(self): |
136 """Return the start time of the process as a number of seconds since | 174 """Return the start time of the process as a number of seconds since |
137 the epoch.""" | 175 the epoch.""" |
138 return _psutil_osx.get_process_create_time(self.pid) | 176 return _psutil_osx.get_process_create_time(self.pid) |
139 | 177 |
140 @wrap_exceptions | 178 @wrap_exceptions |
141 def get_process_num_threads(self): | 179 def get_process_num_threads(self): |
142 """Return the number of threads belonging to the process.""" | 180 """Return the number of threads belonging to the process.""" |
143 return _psutil_osx.get_process_num_threads(self.pid) | 181 return _psutil_osx.get_process_num_threads(self.pid) |
144 | 182 |
| 183 @wrap_exceptions |
145 def get_open_files(self): | 184 def get_open_files(self): |
146 """Return files opened by process by parsing lsof output.""" | 185 """Return files opened by process.""" |
147 lsof = _psposix.LsofParser(self.pid, self._process_name) | 186 if self.pid == 0: |
148 return lsof.get_process_open_files() | 187 raise AccessDenied(self.pid, self._process_name) |
| 188 files = [] |
| 189 rawlist = _psutil_osx.get_process_open_files(self.pid) |
| 190 for path, fd in rawlist: |
| 191 if os.path.isfile(path): |
| 192 ntuple = ntuple_openfile(path, fd) |
| 193 files.append(ntuple) |
| 194 return files |
149 | 195 |
| 196 @wrap_exceptions |
150 def get_connections(self): | 197 def get_connections(self): |
151 """Return etwork connections opened by a process as a list of | 198 """Return etwork connections opened by a process as a list of |
152 namedtuples.""" | 199 namedtuples.""" |
153 lsof = _psposix.LsofParser(self.pid, self._process_name) | 200 retlist = _psutil_osx.get_process_connections(self.pid) |
154 return lsof.get_process_connections() | 201 return [ntuple_connection(*conn) for conn in retlist] |
155 | 202 |
156 PlatformProcess = OSXProcess | 203 @wrap_exceptions |
| 204 def process_wait(self, timeout=None): |
| 205 try: |
| 206 return _psposix.wait_pid(self.pid, timeout) |
| 207 except TimeoutExpired: |
| 208 raise TimeoutExpired(self.pid, self._process_name) |
157 | 209 |
| 210 @wrap_exceptions |
| 211 def get_process_nice(self): |
| 212 return _psutil_posix.getpriority(self.pid) |
| 213 |
| 214 @wrap_exceptions |
| 215 def set_process_nice(self, value): |
| 216 return _psutil_posix.setpriority(self.pid, value) |
| 217 |
| 218 @wrap_exceptions |
| 219 def get_process_status(self): |
| 220 code = _psutil_osx.get_process_status(self.pid) |
| 221 if code in _status_map: |
| 222 return _status_map[code] |
| 223 return constant(-1, "?") |
| 224 |
| 225 @wrap_exceptions |
| 226 def get_process_threads(self): |
| 227 """Return the number of threads belonging to the process.""" |
| 228 rawlist = _psutil_osx.get_process_threads(self.pid) |
| 229 retlist = [] |
| 230 for thread_id, utime, stime in rawlist: |
| 231 ntuple = ntuple_thread(thread_id, utime, stime) |
| 232 retlist.append(ntuple) |
| 233 return retlist |
OLD | NEW |