39
43
"""Return the amount of used memory currently in use on the system, in bytes."""
40
44
return _psutil_bsd.get_total_virtmem() - _psutil_bsd.get_avail_virtmem()
46
_cputimes_ntuple = namedtuple('cputimes', 'user nice system idle irq')
42
47
def get_system_cpu_times():
43
"""Return a dict representing the following CPU times:
44
user, nice, system, idle, interrupt."""
45
values = _psutil_bsd.get_system_cpu_times()
46
return dict(user=values[0], nice=values[1], system=values[2],
47
idle=values[3], irq=values[4])
50
def wrap_privileges(callable):
51
"""Call callable into a try/except clause so that if an
52
OSError EPERM exception is raised we translate it into
55
def wrapper(*args, **kwargs):
57
return callable(*args, **kwargs)
59
if err.errno == errno.EPERM:
64
def prevent_zombie(method):
48
"""Return system CPU times as a named tuple"""
49
user, nice, system, idle, irq = _psutil_bsd.get_system_cpu_times()
50
return _cputimes_ntuple(user, nice, system, idle, irq)
53
"""Returns a list of PIDs currently running on the system."""
54
return _psutil_bsd.get_pid_list()
57
"""Check For the existence of a unix pid."""
58
return _psposix.pid_exists(pid)
61
def wrap_exceptions(method):
65
62
"""Call method(self, pid) into a try/except clause so that if an
66
63
OSError "No such process" exception is raised we assume the process
67
64
has died and raise psutil.NoSuchProcess instead.
69
def wrapper(self, pid, *args, **kwargs):
66
def wrapper(self, *args, **kwargs):
71
return method(self, pid, *args, **kwargs)
68
return method(self, *args, **kwargs)
72
69
except OSError, err:
73
70
if err.errno == errno.ESRCH:
74
raise NoSuchProcess(pid)
71
raise NoSuchProcess(self.pid, self._process_name)
72
if err.errno in (errno.EPERM, errno.EACCES):
73
raise AccessDenied(self.pid, self._process_name)
82
def get_process_info(self, pid):
83
"""Returns a tuple that can be passed to the psutil.ProcessInfo class
86
infoTuple = _psutil_bsd.get_process_info(pid)
90
def kill_process(self, pid, sig=signal.SIGKILL):
91
"""Terminates the process with the given PID."""
97
if err.errno == errno.ESRCH:
98
raise NoSuchProcess(pid)
102
def get_cpu_times(self, pid):
78
_psutil_bsd.SSTOP : STATUS_STOPPED,
79
_psutil_bsd.SSLEEP : STATUS_SLEEPING,
80
_psutil_bsd.SRUN : STATUS_RUNNING,
81
_psutil_bsd.SIDL : STATUS_IDLE,
82
_psutil_bsd.SWAIT : STATUS_WAITING,
83
_psutil_bsd.SLOCK : STATUS_LOCKED,
84
_psutil_bsd.SZOMB : STATUS_ZOMBIE,
88
class BSDProcess(object):
89
"""Wrapper class around underlying C implementation."""
91
__slots__ = ["pid", "_process_name"]
93
def __init__(self, pid):
95
self._process_name = None
98
def get_process_name(self):
99
"""Return process name as a string of limited len (15)."""
100
return _psutil_bsd.get_process_name(self.pid)
103
def get_process_exe(self):
104
"""Return process executable pathname."""
105
return _psutil_bsd.get_process_exe(self.pid)
108
def get_process_cmdline(self):
109
"""Return process cmdline as a list of arguments."""
110
return _psutil_bsd.get_process_cmdline(self.pid)
113
def get_process_ppid(self):
114
"""Return process parent pid."""
115
return _psutil_bsd.get_process_ppid(self.pid)
118
def get_process_uids(self):
119
"""Return real, effective and saved user ids."""
120
real, effective, saved = _psutil_bsd.get_process_uids(self.pid)
121
return ntuple_uids(real, effective, saved)
124
def get_process_gids(self):
125
"""Return real, effective and saved group ids."""
126
real, effective, saved = _psutil_bsd.get_process_gids(self.pid)
127
return ntuple_gids(real, effective, saved)
130
def get_cpu_times(self):
103
131
"""return a tuple containing process user/kernel time."""
104
return _psutil_bsd.get_cpu_times(pid)
132
user, system = _psutil_bsd.get_cpu_times(self.pid)
133
return ntuple_cputimes(user, system)
107
def get_memory_info(self, pid):
136
def get_memory_info(self):
108
137
"""Return a tuple with the process' RSS and VMS size."""
109
return _psutil_bsd.get_memory_info(pid)
112
def get_process_create_time(self, pid):
113
return _psutil_bsd.get_process_create_time(pid)
115
def get_pid_list(self):
116
"""Returns a list of PIDs currently running on the system."""
117
return _psutil_bsd.get_pid_list()
119
def pid_exists(self, pid):
120
"""Check For the existence of a unix pid."""
138
rss, vms = _psutil_bsd.get_memory_info(self.pid)
139
return ntuple_meminfo(rss, vms)
142
def get_process_create_time(self):
143
"""Return the start time of the process as a number of seconds since
145
return _psutil_bsd.get_process_create_time(self.pid)
148
def get_process_num_threads(self):
149
"""Return the number of threads belonging to the process."""
150
return _psutil_bsd.get_process_num_threads(self.pid)
153
def get_process_threads(self):
154
"""Return the number of threads belonging to the process."""
155
rawlist = _psutil_bsd.get_process_threads(self.pid)
157
for thread_id, utime, stime in rawlist:
158
ntuple = ntuple_thread(thread_id, utime, stime)
159
retlist.append(ntuple)
163
def get_open_files(self):
164
"""Return files opened by process by parsing lsof output."""
165
lsof = _psposix.LsofParser(self.pid, self._process_name)
166
return lsof.get_process_open_files()
168
def get_connections(self):
169
"""Return network connections opened by a process as a list of
170
namedtuples by parsing lsof output.
172
lsof = _psposix.LsofParser(self.pid, self._process_name)
173
return lsof.get_process_connections()
176
def process_wait(self, timeout=None):
127
return e.errno == errno.EPERM
178
return _psposix.wait_pid(self.pid, timeout)
179
except TimeoutExpired:
180
raise TimeoutExpired(self.pid, self._process_name)
183
def get_process_nice(self):
184
return _psutil_posix.getpriority(self.pid)
187
def set_process_nice(self, value):
188
return _psutil_posix.setpriority(self.pid, value)
191
def get_process_status(self):
192
code = _psutil_bsd.get_process_status(self.pid)
193
if code in _status_map:
194
return _status_map[code]
195
return constant(-1, "?")
198
def get_process_io_counters(self):
199
rc, wc, rb, wb = _psutil_bsd.get_process_io_counters(self.pid)
200
return ntuple_io(rc, wc, rb, wb)
203
PlatformProcess = BSDProcess