Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/psutil/_psaix.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 # Copyright (c) 2009, Giampaolo Rodola' | |
2 # Copyright (c) 2017, Arnon Yaari | |
3 # All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """AIX platform implementation.""" | |
8 | |
9 import functools | |
10 import glob | |
11 import os | |
12 import re | |
13 import subprocess | |
14 import sys | |
15 from collections import namedtuple | |
16 | |
17 from . import _common | |
18 from . import _psposix | |
19 from . import _psutil_aix as cext | |
20 from . import _psutil_posix as cext_posix | |
21 from ._common import AccessDenied | |
22 from ._common import conn_to_ntuple | |
23 from ._common import get_procfs_path | |
24 from ._common import memoize_when_activated | |
25 from ._common import NIC_DUPLEX_FULL | |
26 from ._common import NIC_DUPLEX_HALF | |
27 from ._common import NIC_DUPLEX_UNKNOWN | |
28 from ._common import NoSuchProcess | |
29 from ._common import usage_percent | |
30 from ._common import ZombieProcess | |
31 from ._compat import FileNotFoundError | |
32 from ._compat import PermissionError | |
33 from ._compat import ProcessLookupError | |
34 from ._compat import PY3 | |
35 | |
36 | |
37 __extra__all__ = ["PROCFS_PATH"] | |
38 | |
39 | |
40 # ===================================================================== | |
41 # --- globals | |
42 # ===================================================================== | |
43 | |
44 | |
45 HAS_THREADS = hasattr(cext, "proc_threads") | |
46 HAS_NET_IO_COUNTERS = hasattr(cext, "net_io_counters") | |
47 HAS_PROC_IO_COUNTERS = hasattr(cext, "proc_io_counters") | |
48 | |
49 PAGE_SIZE = cext_posix.getpagesize() | |
50 AF_LINK = cext_posix.AF_LINK | |
51 | |
52 PROC_STATUSES = { | |
53 cext.SIDL: _common.STATUS_IDLE, | |
54 cext.SZOMB: _common.STATUS_ZOMBIE, | |
55 cext.SACTIVE: _common.STATUS_RUNNING, | |
56 cext.SSWAP: _common.STATUS_RUNNING, # TODO what status is this? | |
57 cext.SSTOP: _common.STATUS_STOPPED, | |
58 } | |
59 | |
60 TCP_STATUSES = { | |
61 cext.TCPS_ESTABLISHED: _common.CONN_ESTABLISHED, | |
62 cext.TCPS_SYN_SENT: _common.CONN_SYN_SENT, | |
63 cext.TCPS_SYN_RCVD: _common.CONN_SYN_RECV, | |
64 cext.TCPS_FIN_WAIT_1: _common.CONN_FIN_WAIT1, | |
65 cext.TCPS_FIN_WAIT_2: _common.CONN_FIN_WAIT2, | |
66 cext.TCPS_TIME_WAIT: _common.CONN_TIME_WAIT, | |
67 cext.TCPS_CLOSED: _common.CONN_CLOSE, | |
68 cext.TCPS_CLOSE_WAIT: _common.CONN_CLOSE_WAIT, | |
69 cext.TCPS_LAST_ACK: _common.CONN_LAST_ACK, | |
70 cext.TCPS_LISTEN: _common.CONN_LISTEN, | |
71 cext.TCPS_CLOSING: _common.CONN_CLOSING, | |
72 cext.PSUTIL_CONN_NONE: _common.CONN_NONE, | |
73 } | |
74 | |
75 proc_info_map = dict( | |
76 ppid=0, | |
77 rss=1, | |
78 vms=2, | |
79 create_time=3, | |
80 nice=4, | |
81 num_threads=5, | |
82 status=6, | |
83 ttynr=7) | |
84 | |
85 | |
86 # ===================================================================== | |
87 # --- named tuples | |
88 # ===================================================================== | |
89 | |
90 | |
91 # psutil.Process.memory_info() | |
92 pmem = namedtuple('pmem', ['rss', 'vms']) | |
93 # psutil.Process.memory_full_info() | |
94 pfullmem = pmem | |
95 # psutil.Process.cpu_times() | |
96 scputimes = namedtuple('scputimes', ['user', 'system', 'idle', 'iowait']) | |
97 # psutil.virtual_memory() | |
98 svmem = namedtuple('svmem', ['total', 'available', 'percent', 'used', 'free']) | |
99 | |
100 | |
101 # ===================================================================== | |
102 # --- memory | |
103 # ===================================================================== | |
104 | |
105 | |
106 def virtual_memory(): | |
107 total, avail, free, pinned, inuse = cext.virtual_mem() | |
108 percent = usage_percent((total - avail), total, round_=1) | |
109 return svmem(total, avail, percent, inuse, free) | |
110 | |
111 | |
112 def swap_memory(): | |
113 """Swap system memory as a (total, used, free, sin, sout) tuple.""" | |
114 total, free, sin, sout = cext.swap_mem() | |
115 used = total - free | |
116 percent = usage_percent(used, total, round_=1) | |
117 return _common.sswap(total, used, free, percent, sin, sout) | |
118 | |
119 | |
120 # ===================================================================== | |
121 # --- CPU | |
122 # ===================================================================== | |
123 | |
124 | |
125 def cpu_times(): | |
126 """Return system-wide CPU times as a named tuple""" | |
127 ret = cext.per_cpu_times() | |
128 return scputimes(*[sum(x) for x in zip(*ret)]) | |
129 | |
130 | |
131 def per_cpu_times(): | |
132 """Return system per-CPU times as a list of named tuples""" | |
133 ret = cext.per_cpu_times() | |
134 return [scputimes(*x) for x in ret] | |
135 | |
136 | |
137 def cpu_count_logical(): | |
138 """Return the number of logical CPUs in the system.""" | |
139 try: | |
140 return os.sysconf("SC_NPROCESSORS_ONLN") | |
141 except ValueError: | |
142 # mimic os.cpu_count() behavior | |
143 return None | |
144 | |
145 | |
146 def cpu_count_physical(): | |
147 cmd = "lsdev -Cc processor" | |
148 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, | |
149 stderr=subprocess.PIPE) | |
150 stdout, stderr = p.communicate() | |
151 if PY3: | |
152 stdout, stderr = [x.decode(sys.stdout.encoding) | |
153 for x in (stdout, stderr)] | |
154 if p.returncode != 0: | |
155 raise RuntimeError("%r command error\n%s" % (cmd, stderr)) | |
156 processors = stdout.strip().splitlines() | |
157 return len(processors) or None | |
158 | |
159 | |
160 def cpu_stats(): | |
161 """Return various CPU stats as a named tuple.""" | |
162 ctx_switches, interrupts, soft_interrupts, syscalls = cext.cpu_stats() | |
163 return _common.scpustats( | |
164 ctx_switches, interrupts, soft_interrupts, syscalls) | |
165 | |
166 | |
167 # ===================================================================== | |
168 # --- disks | |
169 # ===================================================================== | |
170 | |
171 | |
172 disk_io_counters = cext.disk_io_counters | |
173 disk_usage = _psposix.disk_usage | |
174 | |
175 | |
176 def disk_partitions(all=False): | |
177 """Return system disk partitions.""" | |
178 # TODO - the filtering logic should be better checked so that | |
179 # it tries to reflect 'df' as much as possible | |
180 retlist = [] | |
181 partitions = cext.disk_partitions() | |
182 for partition in partitions: | |
183 device, mountpoint, fstype, opts = partition | |
184 if device == 'none': | |
185 device = '' | |
186 if not all: | |
187 # Differently from, say, Linux, we don't have a list of | |
188 # common fs types so the best we can do, AFAIK, is to | |
189 # filter by filesystem having a total size > 0. | |
190 if not disk_usage(mountpoint).total: | |
191 continue | |
192 maxfile = maxpath = None # set later | |
193 ntuple = _common.sdiskpart(device, mountpoint, fstype, opts, | |
194 maxfile, maxpath) | |
195 retlist.append(ntuple) | |
196 return retlist | |
197 | |
198 | |
199 # ===================================================================== | |
200 # --- network | |
201 # ===================================================================== | |
202 | |
203 | |
204 net_if_addrs = cext_posix.net_if_addrs | |
205 | |
206 if HAS_NET_IO_COUNTERS: | |
207 net_io_counters = cext.net_io_counters | |
208 | |
209 | |
210 def net_connections(kind, _pid=-1): | |
211 """Return socket connections. If pid == -1 return system-wide | |
212 connections (as opposed to connections opened by one process only). | |
213 """ | |
214 cmap = _common.conn_tmap | |
215 if kind not in cmap: | |
216 raise ValueError("invalid %r kind argument; choose between %s" | |
217 % (kind, ', '.join([repr(x) for x in cmap]))) | |
218 families, types = _common.conn_tmap[kind] | |
219 rawlist = cext.net_connections(_pid) | |
220 ret = [] | |
221 for item in rawlist: | |
222 fd, fam, type_, laddr, raddr, status, pid = item | |
223 if fam not in families: | |
224 continue | |
225 if type_ not in types: | |
226 continue | |
227 nt = conn_to_ntuple(fd, fam, type_, laddr, raddr, status, | |
228 TCP_STATUSES, pid=pid if _pid == -1 else None) | |
229 ret.append(nt) | |
230 return ret | |
231 | |
232 | |
233 def net_if_stats(): | |
234 """Get NIC stats (isup, duplex, speed, mtu).""" | |
235 duplex_map = {"Full": NIC_DUPLEX_FULL, | |
236 "Half": NIC_DUPLEX_HALF} | |
237 names = set([x[0] for x in net_if_addrs()]) | |
238 ret = {} | |
239 for name in names: | |
240 isup, mtu = cext.net_if_stats(name) | |
241 | |
242 # try to get speed and duplex | |
243 # TODO: rewrite this in C (entstat forks, so use truss -f to follow. | |
244 # looks like it is using an undocumented ioctl?) | |
245 duplex = "" | |
246 speed = 0 | |
247 p = subprocess.Popen(["/usr/bin/entstat", "-d", name], | |
248 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
249 stdout, stderr = p.communicate() | |
250 if PY3: | |
251 stdout, stderr = [x.decode(sys.stdout.encoding) | |
252 for x in (stdout, stderr)] | |
253 if p.returncode == 0: | |
254 re_result = re.search( | |
255 r"Running: (\d+) Mbps.*?(\w+) Duplex", stdout) | |
256 if re_result is not None: | |
257 speed = int(re_result.group(1)) | |
258 duplex = re_result.group(2) | |
259 | |
260 duplex = duplex_map.get(duplex, NIC_DUPLEX_UNKNOWN) | |
261 ret[name] = _common.snicstats(isup, duplex, speed, mtu) | |
262 return ret | |
263 | |
264 | |
265 # ===================================================================== | |
266 # --- other system functions | |
267 # ===================================================================== | |
268 | |
269 | |
270 def boot_time(): | |
271 """The system boot time expressed in seconds since the epoch.""" | |
272 return cext.boot_time() | |
273 | |
274 | |
275 def users(): | |
276 """Return currently connected users as a list of namedtuples.""" | |
277 retlist = [] | |
278 rawlist = cext.users() | |
279 localhost = (':0.0', ':0') | |
280 for item in rawlist: | |
281 user, tty, hostname, tstamp, user_process, pid = item | |
282 # note: the underlying C function includes entries about | |
283 # system boot, run level and others. We might want | |
284 # to use them in the future. | |
285 if not user_process: | |
286 continue | |
287 if hostname in localhost: | |
288 hostname = 'localhost' | |
289 nt = _common.suser(user, tty, hostname, tstamp, pid) | |
290 retlist.append(nt) | |
291 return retlist | |
292 | |
293 | |
294 # ===================================================================== | |
295 # --- processes | |
296 # ===================================================================== | |
297 | |
298 | |
299 def pids(): | |
300 """Returns a list of PIDs currently running on the system.""" | |
301 return [int(x) for x in os.listdir(get_procfs_path()) if x.isdigit()] | |
302 | |
303 | |
304 def pid_exists(pid): | |
305 """Check for the existence of a unix pid.""" | |
306 return os.path.exists(os.path.join(get_procfs_path(), str(pid), "psinfo")) | |
307 | |
308 | |
309 def wrap_exceptions(fun): | |
310 """Call callable into a try/except clause and translate ENOENT, | |
311 EACCES and EPERM in NoSuchProcess or AccessDenied exceptions. | |
312 """ | |
313 @functools.wraps(fun) | |
314 def wrapper(self, *args, **kwargs): | |
315 try: | |
316 return fun(self, *args, **kwargs) | |
317 except (FileNotFoundError, ProcessLookupError): | |
318 # ENOENT (no such file or directory) gets raised on open(). | |
319 # ESRCH (no such process) can get raised on read() if | |
320 # process is gone in meantime. | |
321 if not pid_exists(self.pid): | |
322 raise NoSuchProcess(self.pid, self._name) | |
323 else: | |
324 raise ZombieProcess(self.pid, self._name, self._ppid) | |
325 except PermissionError: | |
326 raise AccessDenied(self.pid, self._name) | |
327 return wrapper | |
328 | |
329 | |
330 class Process(object): | |
331 """Wrapper class around underlying C implementation.""" | |
332 | |
333 __slots__ = ["pid", "_name", "_ppid", "_procfs_path", "_cache"] | |
334 | |
335 def __init__(self, pid): | |
336 self.pid = pid | |
337 self._name = None | |
338 self._ppid = None | |
339 self._procfs_path = get_procfs_path() | |
340 | |
341 def oneshot_enter(self): | |
342 self._proc_basic_info.cache_activate(self) | |
343 self._proc_cred.cache_activate(self) | |
344 | |
345 def oneshot_exit(self): | |
346 self._proc_basic_info.cache_deactivate(self) | |
347 self._proc_cred.cache_deactivate(self) | |
348 | |
349 @wrap_exceptions | |
350 @memoize_when_activated | |
351 def _proc_basic_info(self): | |
352 return cext.proc_basic_info(self.pid, self._procfs_path) | |
353 | |
354 @wrap_exceptions | |
355 @memoize_when_activated | |
356 def _proc_cred(self): | |
357 return cext.proc_cred(self.pid, self._procfs_path) | |
358 | |
359 @wrap_exceptions | |
360 def name(self): | |
361 if self.pid == 0: | |
362 return "swapper" | |
363 # note: max 16 characters | |
364 return cext.proc_name(self.pid, self._procfs_path).rstrip("\x00") | |
365 | |
366 @wrap_exceptions | |
367 def exe(self): | |
368 # there is no way to get executable path in AIX other than to guess, | |
369 # and guessing is more complex than what's in the wrapping class | |
370 cmdline = self.cmdline() | |
371 if not cmdline: | |
372 return '' | |
373 exe = cmdline[0] | |
374 if os.path.sep in exe: | |
375 # relative or absolute path | |
376 if not os.path.isabs(exe): | |
377 # if cwd has changed, we're out of luck - this may be wrong! | |
378 exe = os.path.abspath(os.path.join(self.cwd(), exe)) | |
379 if (os.path.isabs(exe) and | |
380 os.path.isfile(exe) and | |
381 os.access(exe, os.X_OK)): | |
382 return exe | |
383 # not found, move to search in PATH using basename only | |
384 exe = os.path.basename(exe) | |
385 # search for exe name PATH | |
386 for path in os.environ["PATH"].split(":"): | |
387 possible_exe = os.path.abspath(os.path.join(path, exe)) | |
388 if (os.path.isfile(possible_exe) and | |
389 os.access(possible_exe, os.X_OK)): | |
390 return possible_exe | |
391 return '' | |
392 | |
393 @wrap_exceptions | |
394 def cmdline(self): | |
395 return cext.proc_args(self.pid) | |
396 | |
397 @wrap_exceptions | |
398 def environ(self): | |
399 return cext.proc_environ(self.pid) | |
400 | |
401 @wrap_exceptions | |
402 def create_time(self): | |
403 return self._proc_basic_info()[proc_info_map['create_time']] | |
404 | |
405 @wrap_exceptions | |
406 def num_threads(self): | |
407 return self._proc_basic_info()[proc_info_map['num_threads']] | |
408 | |
409 if HAS_THREADS: | |
410 @wrap_exceptions | |
411 def threads(self): | |
412 rawlist = cext.proc_threads(self.pid) | |
413 retlist = [] | |
414 for thread_id, utime, stime in rawlist: | |
415 ntuple = _common.pthread(thread_id, utime, stime) | |
416 retlist.append(ntuple) | |
417 # The underlying C implementation retrieves all OS threads | |
418 # and filters them by PID. At this point we can't tell whether | |
419 # an empty list means there were no connections for process or | |
420 # process is no longer active so we force NSP in case the PID | |
421 # is no longer there. | |
422 if not retlist: | |
423 # will raise NSP if process is gone | |
424 os.stat('%s/%s' % (self._procfs_path, self.pid)) | |
425 return retlist | |
426 | |
427 @wrap_exceptions | |
428 def connections(self, kind='inet'): | |
429 ret = net_connections(kind, _pid=self.pid) | |
430 # The underlying C implementation retrieves all OS connections | |
431 # and filters them by PID. At this point we can't tell whether | |
432 # an empty list means there were no connections for process or | |
433 # process is no longer active so we force NSP in case the PID | |
434 # is no longer there. | |
435 if not ret: | |
436 # will raise NSP if process is gone | |
437 os.stat('%s/%s' % (self._procfs_path, self.pid)) | |
438 return ret | |
439 | |
440 @wrap_exceptions | |
441 def nice_get(self): | |
442 return cext_posix.getpriority(self.pid) | |
443 | |
444 @wrap_exceptions | |
445 def nice_set(self, value): | |
446 return cext_posix.setpriority(self.pid, value) | |
447 | |
448 @wrap_exceptions | |
449 def ppid(self): | |
450 self._ppid = self._proc_basic_info()[proc_info_map['ppid']] | |
451 return self._ppid | |
452 | |
453 @wrap_exceptions | |
454 def uids(self): | |
455 real, effective, saved, _, _, _ = self._proc_cred() | |
456 return _common.puids(real, effective, saved) | |
457 | |
458 @wrap_exceptions | |
459 def gids(self): | |
460 _, _, _, real, effective, saved = self._proc_cred() | |
461 return _common.puids(real, effective, saved) | |
462 | |
463 @wrap_exceptions | |
464 def cpu_times(self): | |
465 cpu_times = cext.proc_cpu_times(self.pid, self._procfs_path) | |
466 return _common.pcputimes(*cpu_times) | |
467 | |
468 @wrap_exceptions | |
469 def terminal(self): | |
470 ttydev = self._proc_basic_info()[proc_info_map['ttynr']] | |
471 # convert from 64-bit dev_t to 32-bit dev_t and then map the device | |
472 ttydev = (((ttydev & 0x0000FFFF00000000) >> 16) | (ttydev & 0xFFFF)) | |
473 # try to match rdev of /dev/pts/* files ttydev | |
474 for dev in glob.glob("/dev/**/*"): | |
475 if os.stat(dev).st_rdev == ttydev: | |
476 return dev | |
477 return None | |
478 | |
479 @wrap_exceptions | |
480 def cwd(self): | |
481 procfs_path = self._procfs_path | |
482 try: | |
483 result = os.readlink("%s/%s/cwd" % (procfs_path, self.pid)) | |
484 return result.rstrip('/') | |
485 except FileNotFoundError: | |
486 os.stat("%s/%s" % (procfs_path, self.pid)) # raise NSP or AD | |
487 return None | |
488 | |
489 @wrap_exceptions | |
490 def memory_info(self): | |
491 ret = self._proc_basic_info() | |
492 rss = ret[proc_info_map['rss']] * 1024 | |
493 vms = ret[proc_info_map['vms']] * 1024 | |
494 return pmem(rss, vms) | |
495 | |
496 memory_full_info = memory_info | |
497 | |
498 @wrap_exceptions | |
499 def status(self): | |
500 code = self._proc_basic_info()[proc_info_map['status']] | |
501 # XXX is '?' legit? (we're not supposed to return it anyway) | |
502 return PROC_STATUSES.get(code, '?') | |
503 | |
504 def open_files(self): | |
505 # TODO rewrite without using procfiles (stat /proc/pid/fd/* and then | |
506 # find matching name of the inode) | |
507 p = subprocess.Popen(["/usr/bin/procfiles", "-n", str(self.pid)], | |
508 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
509 stdout, stderr = p.communicate() | |
510 if PY3: | |
511 stdout, stderr = [x.decode(sys.stdout.encoding) | |
512 for x in (stdout, stderr)] | |
513 if "no such process" in stderr.lower(): | |
514 raise NoSuchProcess(self.pid, self._name) | |
515 procfiles = re.findall(r"(\d+): S_IFREG.*\s*.*name:(.*)\n", stdout) | |
516 retlist = [] | |
517 for fd, path in procfiles: | |
518 path = path.strip() | |
519 if path.startswith("//"): | |
520 path = path[1:] | |
521 if path.lower() == "cannot be retrieved": | |
522 continue | |
523 retlist.append(_common.popenfile(path, int(fd))) | |
524 return retlist | |
525 | |
526 @wrap_exceptions | |
527 def num_fds(self): | |
528 if self.pid == 0: # no /proc/0/fd | |
529 return 0 | |
530 return len(os.listdir("%s/%s/fd" % (self._procfs_path, self.pid))) | |
531 | |
532 @wrap_exceptions | |
533 def num_ctx_switches(self): | |
534 return _common.pctxsw( | |
535 *cext.proc_num_ctx_switches(self.pid)) | |
536 | |
537 @wrap_exceptions | |
538 def wait(self, timeout=None): | |
539 return _psposix.wait_pid(self.pid, timeout, self._name) | |
540 | |
541 if HAS_PROC_IO_COUNTERS: | |
542 @wrap_exceptions | |
543 def io_counters(self): | |
544 try: | |
545 rc, wc, rb, wb = cext.proc_io_counters(self.pid) | |
546 except OSError: | |
547 # if process is terminated, proc_io_counters returns OSError | |
548 # instead of NSP | |
549 if not pid_exists(self.pid): | |
550 raise NoSuchProcess(self.pid, self._name) | |
551 raise | |
552 return _common.pio(rc, wc, rb, wb) |