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