Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: third_party/psutil/psutil/_psbsd.py

Issue 8919026: Remove psutil from tree, install via install-build-deps.sh (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 #
3 # $Id: _psbsd.py 1142 2011-10-05 18:45:49Z g.rodola $
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."""
10
11 import errno
12 import os
13
14 import _psutil_bsd
15 import _psutil_posix
16 import _psposix
17 from psutil.error import AccessDenied, NoSuchProcess, TimeoutExpired
18 from psutil._compat import namedtuple
19 from psutil._common import *
20
21 __extra__all__ = []
22
23 # --- constants
24
25 NUM_CPUS = _psutil_bsd.get_num_cpus()
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')
29
30 # --- public functions
31
32 def phymem_usage():
33 """Physical system memory as a (total, used, free) tuple."""
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)
40
41 def virtmem_usage():
42 """Virtual system memory as a (total, used, free) tuple."""
43 total = _psutil_bsd.get_total_virtmem()
44 free = _psutil_bsd.get_avail_virtmem()
45 used = total - free
46 percent = usage_percent(used, total, _round=1)
47 return ntuple_sysmeminfo(total, used, free, percent)
48
49 def get_system_cpu_times():
50 """Return system per-CPU times as a named tuple"""
51 user, nice, system, idle, irq = _psutil_bsd.get_system_cpu_times()
52 return _cputimes_ntuple(user, nice, system, idle, irq)
53
54 def get_system_per_cpu_times():
55 """Return system CPU times as a named tuple"""
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
62
63 def disk_partitions(all=False):
64 retlist = []
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
82
83
84 def wrap_exceptions(method):
85 """Call method(self, pid) into a try/except clause so that if an
86 OSError "No such process" exception is raised we assume the process
87 has died and raise psutil.NoSuchProcess instead.
88 """
89 def wrapper(self, *args, **kwargs):
90 try:
91 return method(self, *args, **kwargs)
92 except OSError, err:
93 if err.errno == errno.ESRCH:
94 raise NoSuchProcess(self.pid, self._process_name)
95 if err.errno in (errno.EPERM, errno.EACCES):
96 raise AccessDenied(self.pid, self._process_name)
97 raise
98 return wrapper
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 }
109
110
111 class Process(object):
112 """Wrapper class around underlying C implementation."""
113
114 __slots__ = ["pid", "_process_name"]
115
116 def __init__(self, pid):
117 self.pid = pid
118 self._process_name = None
119
120 @wrap_exceptions
121 def get_process_name(self):
122 """Return process name as a string of limited len (15)."""
123 return _psutil_bsd.get_process_name(self.pid)
124
125 @wrap_exceptions
126 def get_process_exe(self):
127 """Return process executable pathname."""
128 return _psutil_bsd.get_process_exe(self.pid)
129
130 @wrap_exceptions
131 def get_process_cmdline(self):
132 """Return process cmdline as a list of arguments."""
133 return _psutil_bsd.get_process_cmdline(self.pid)
134
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
144 def get_process_ppid(self):
145 """Return process parent pid."""
146 return _psutil_bsd.get_process_ppid(self.pid)
147
148 @wrap_exceptions
149 def get_process_uids(self):
150 """Return real, effective and saved user ids."""
151 real, effective, saved = _psutil_bsd.get_process_uids(self.pid)
152 return ntuple_uids(real, effective, saved)
153
154 @wrap_exceptions
155 def get_process_gids(self):
156 """Return real, effective and saved group ids."""
157 real, effective, saved = _psutil_bsd.get_process_gids(self.pid)
158 return ntuple_gids(real, effective, saved)
159
160 @wrap_exceptions
161 def get_cpu_times(self):
162 """return a tuple containing process user/kernel time."""
163 user, system = _psutil_bsd.get_cpu_times(self.pid)
164 return ntuple_cputimes(user, system)
165
166 @wrap_exceptions
167 def get_memory_info(self):
168 """Return a tuple with the process' RSS and VMS size."""
169 rss, vms = _psutil_bsd.get_memory_info(self.pid)
170 return ntuple_meminfo(rss, vms)
171
172 @wrap_exceptions
173 def get_process_create_time(self):
174 """Return the start time of the process as a number of seconds since
175 the epoch."""
176 return _psutil_bsd.get_process_create_time(self.pid)
177
178 @wrap_exceptions
179 def get_process_num_threads(self):
180 """Return the number of threads belonging to the process."""
181 return _psutil_bsd.get_process_num_threads(self.pid)
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
193 def get_open_files(self):
194 """Return files opened by process by parsing lsof output."""
195 lsof = _psposix.LsofParser(self.pid, self._process_name)
196 return lsof.get_process_open_files()
197
198 def get_connections(self):
199 """Return network connections opened by a process as a list of
200 namedtuples by parsing lsof output.
201 """
202 lsof = _psposix.LsofParser(self.pid, self._process_name)
203 return lsof.get_process_connections()
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)
211
212 @wrap_exceptions
213 def get_process_nice(self):
214 return _psutil_posix.getpriority(self.pid)
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)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698