comparison env/lib/python3.7/site-packages/psutil/_psaix.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
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 = os.sysconf('SC_PAGE_SIZE')
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 ntuple = _common.sdiskpart(device, mountpoint, fstype, opts)
193 retlist.append(ntuple)
194 return retlist
195
196
197 # =====================================================================
198 # --- network
199 # =====================================================================
200
201
202 net_if_addrs = cext_posix.net_if_addrs
203
204 if HAS_NET_IO_COUNTERS:
205 net_io_counters = cext.net_io_counters
206
207
208 def net_connections(kind, _pid=-1):
209 """Return socket connections. If pid == -1 return system-wide
210 connections (as opposed to connections opened by one process only).
211 """
212 cmap = _common.conn_tmap
213 if kind not in cmap:
214 raise ValueError("invalid %r kind argument; choose between %s"
215 % (kind, ', '.join([repr(x) for x in cmap])))
216 families, types = _common.conn_tmap[kind]
217 rawlist = cext.net_connections(_pid)
218 ret = []
219 for item in rawlist:
220 fd, fam, type_, laddr, raddr, status, pid = item
221 if fam not in families:
222 continue
223 if type_ not in types:
224 continue
225 nt = conn_to_ntuple(fd, fam, type_, laddr, raddr, status,
226 TCP_STATUSES, pid=pid if _pid == -1 else None)
227 ret.append(nt)
228 return ret
229
230
231 def net_if_stats():
232 """Get NIC stats (isup, duplex, speed, mtu)."""
233 duplex_map = {"Full": NIC_DUPLEX_FULL,
234 "Half": NIC_DUPLEX_HALF}
235 names = set([x[0] for x in net_if_addrs()])
236 ret = {}
237 for name in names:
238 isup, mtu = cext.net_if_stats(name)
239
240 # try to get speed and duplex
241 # TODO: rewrite this in C (entstat forks, so use truss -f to follow.
242 # looks like it is using an undocumented ioctl?)
243 duplex = ""
244 speed = 0
245 p = subprocess.Popen(["/usr/bin/entstat", "-d", name],
246 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
247 stdout, stderr = p.communicate()
248 if PY3:
249 stdout, stderr = [x.decode(sys.stdout.encoding)
250 for x in (stdout, stderr)]
251 if p.returncode == 0:
252 re_result = re.search(
253 r"Running: (\d+) Mbps.*?(\w+) Duplex", stdout)
254 if re_result is not None:
255 speed = int(re_result.group(1))
256 duplex = re_result.group(2)
257
258 duplex = duplex_map.get(duplex, NIC_DUPLEX_UNKNOWN)
259 ret[name] = _common.snicstats(isup, duplex, speed, mtu)
260 return ret
261
262
263 # =====================================================================
264 # --- other system functions
265 # =====================================================================
266
267
268 def boot_time():
269 """The system boot time expressed in seconds since the epoch."""
270 return cext.boot_time()
271
272
273 def users():
274 """Return currently connected users as a list of namedtuples."""
275 retlist = []
276 rawlist = cext.users()
277 localhost = (':0.0', ':0')
278 for item in rawlist:
279 user, tty, hostname, tstamp, user_process, pid = item
280 # note: the underlying C function includes entries about
281 # system boot, run level and others. We might want
282 # to use them in the future.
283 if not user_process:
284 continue
285 if hostname in localhost:
286 hostname = 'localhost'
287 nt = _common.suser(user, tty, hostname, tstamp, pid)
288 retlist.append(nt)
289 return retlist
290
291
292 # =====================================================================
293 # --- processes
294 # =====================================================================
295
296
297 def pids():
298 """Returns a list of PIDs currently running on the system."""
299 return [int(x) for x in os.listdir(get_procfs_path()) if x.isdigit()]
300
301
302 def pid_exists(pid):
303 """Check for the existence of a unix pid."""
304 return os.path.exists(os.path.join(get_procfs_path(), str(pid), "psinfo"))
305
306
307 def wrap_exceptions(fun):
308 """Call callable into a try/except clause and translate ENOENT,
309 EACCES and EPERM in NoSuchProcess or AccessDenied exceptions.
310 """
311 @functools.wraps(fun)
312 def wrapper(self, *args, **kwargs):
313 try:
314 return fun(self, *args, **kwargs)
315 except (FileNotFoundError, ProcessLookupError):
316 # ENOENT (no such file or directory) gets raised on open().
317 # ESRCH (no such process) can get raised on read() if
318 # process is gone in meantime.
319 if not pid_exists(self.pid):
320 raise NoSuchProcess(self.pid, self._name)
321 else:
322 raise ZombieProcess(self.pid, self._name, self._ppid)
323 except PermissionError:
324 raise AccessDenied(self.pid, self._name)
325 return wrapper
326
327
328 class Process(object):
329 """Wrapper class around underlying C implementation."""
330
331 __slots__ = ["pid", "_name", "_ppid", "_procfs_path", "_cache"]
332
333 def __init__(self, pid):
334 self.pid = pid
335 self._name = None
336 self._ppid = None
337 self._procfs_path = get_procfs_path()
338
339 def oneshot_enter(self):
340 self._proc_basic_info.cache_activate(self)
341 self._proc_cred.cache_activate(self)
342
343 def oneshot_exit(self):
344 self._proc_basic_info.cache_deactivate(self)
345 self._proc_cred.cache_deactivate(self)
346
347 @wrap_exceptions
348 @memoize_when_activated
349 def _proc_basic_info(self):
350 return cext.proc_basic_info(self.pid, self._procfs_path)
351
352 @wrap_exceptions
353 @memoize_when_activated
354 def _proc_cred(self):
355 return cext.proc_cred(self.pid, self._procfs_path)
356
357 @wrap_exceptions
358 def name(self):
359 if self.pid == 0:
360 return "swapper"
361 # note: max 16 characters
362 return cext.proc_name(self.pid, self._procfs_path).rstrip("\x00")
363
364 @wrap_exceptions
365 def exe(self):
366 # there is no way to get executable path in AIX other than to guess,
367 # and guessing is more complex than what's in the wrapping class
368 cmdline = self.cmdline()
369 if not cmdline:
370 return ''
371 exe = cmdline[0]
372 if os.path.sep in exe:
373 # relative or absolute path
374 if not os.path.isabs(exe):
375 # if cwd has changed, we're out of luck - this may be wrong!
376 exe = os.path.abspath(os.path.join(self.cwd(), exe))
377 if (os.path.isabs(exe) and
378 os.path.isfile(exe) and
379 os.access(exe, os.X_OK)):
380 return exe
381 # not found, move to search in PATH using basename only
382 exe = os.path.basename(exe)
383 # search for exe name PATH
384 for path in os.environ["PATH"].split(":"):
385 possible_exe = os.path.abspath(os.path.join(path, exe))
386 if (os.path.isfile(possible_exe) and
387 os.access(possible_exe, os.X_OK)):
388 return possible_exe
389 return ''
390
391 @wrap_exceptions
392 def cmdline(self):
393 return cext.proc_args(self.pid)
394
395 @wrap_exceptions
396 def environ(self):
397 return cext.proc_environ(self.pid)
398
399 @wrap_exceptions
400 def create_time(self):
401 return self._proc_basic_info()[proc_info_map['create_time']]
402
403 @wrap_exceptions
404 def num_threads(self):
405 return self._proc_basic_info()[proc_info_map['num_threads']]
406
407 if HAS_THREADS:
408 @wrap_exceptions
409 def threads(self):
410 rawlist = cext.proc_threads(self.pid)
411 retlist = []
412 for thread_id, utime, stime in rawlist:
413 ntuple = _common.pthread(thread_id, utime, stime)
414 retlist.append(ntuple)
415 # The underlying C implementation retrieves all OS threads
416 # and filters them by PID. At this point we can't tell whether
417 # an empty list means there were no connections for process or
418 # process is no longer active so we force NSP in case the PID
419 # is no longer there.
420 if not retlist:
421 # will raise NSP if process is gone
422 os.stat('%s/%s' % (self._procfs_path, self.pid))
423 return retlist
424
425 @wrap_exceptions
426 def connections(self, kind='inet'):
427 ret = net_connections(kind, _pid=self.pid)
428 # The underlying C implementation retrieves all OS connections
429 # and filters them by PID. At this point we can't tell whether
430 # an empty list means there were no connections for process or
431 # process is no longer active so we force NSP in case the PID
432 # is no longer there.
433 if not ret:
434 # will raise NSP if process is gone
435 os.stat('%s/%s' % (self._procfs_path, self.pid))
436 return ret
437
438 @wrap_exceptions
439 def nice_get(self):
440 return cext_posix.getpriority(self.pid)
441
442 @wrap_exceptions
443 def nice_set(self, value):
444 return cext_posix.setpriority(self.pid, value)
445
446 @wrap_exceptions
447 def ppid(self):
448 self._ppid = self._proc_basic_info()[proc_info_map['ppid']]
449 return self._ppid
450
451 @wrap_exceptions
452 def uids(self):
453 real, effective, saved, _, _, _ = self._proc_cred()
454 return _common.puids(real, effective, saved)
455
456 @wrap_exceptions
457 def gids(self):
458 _, _, _, real, effective, saved = self._proc_cred()
459 return _common.puids(real, effective, saved)
460
461 @wrap_exceptions
462 def cpu_times(self):
463 cpu_times = cext.proc_cpu_times(self.pid, self._procfs_path)
464 return _common.pcputimes(*cpu_times)
465
466 @wrap_exceptions
467 def terminal(self):
468 ttydev = self._proc_basic_info()[proc_info_map['ttynr']]
469 # convert from 64-bit dev_t to 32-bit dev_t and then map the device
470 ttydev = (((ttydev & 0x0000FFFF00000000) >> 16) | (ttydev & 0xFFFF))
471 # try to match rdev of /dev/pts/* files ttydev
472 for dev in glob.glob("/dev/**/*"):
473 if os.stat(dev).st_rdev == ttydev:
474 return dev
475 return None
476
477 @wrap_exceptions
478 def cwd(self):
479 procfs_path = self._procfs_path
480 try:
481 result = os.readlink("%s/%s/cwd" % (procfs_path, self.pid))
482 return result.rstrip('/')
483 except FileNotFoundError:
484 os.stat("%s/%s" % (procfs_path, self.pid)) # raise NSP or AD
485 return None
486
487 @wrap_exceptions
488 def memory_info(self):
489 ret = self._proc_basic_info()
490 rss = ret[proc_info_map['rss']] * 1024
491 vms = ret[proc_info_map['vms']] * 1024
492 return pmem(rss, vms)
493
494 memory_full_info = memory_info
495
496 @wrap_exceptions
497 def status(self):
498 code = self._proc_basic_info()[proc_info_map['status']]
499 # XXX is '?' legit? (we're not supposed to return it anyway)
500 return PROC_STATUSES.get(code, '?')
501
502 def open_files(self):
503 # TODO rewrite without using procfiles (stat /proc/pid/fd/* and then
504 # find matching name of the inode)
505 p = subprocess.Popen(["/usr/bin/procfiles", "-n", str(self.pid)],
506 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
507 stdout, stderr = p.communicate()
508 if PY3:
509 stdout, stderr = [x.decode(sys.stdout.encoding)
510 for x in (stdout, stderr)]
511 if "no such process" in stderr.lower():
512 raise NoSuchProcess(self.pid, self._name)
513 procfiles = re.findall(r"(\d+): S_IFREG.*\s*.*name:(.*)\n", stdout)
514 retlist = []
515 for fd, path in procfiles:
516 path = path.strip()
517 if path.startswith("//"):
518 path = path[1:]
519 if path.lower() == "cannot be retrieved":
520 continue
521 retlist.append(_common.popenfile(path, int(fd)))
522 return retlist
523
524 @wrap_exceptions
525 def num_fds(self):
526 if self.pid == 0: # no /proc/0/fd
527 return 0
528 return len(os.listdir("%s/%s/fd" % (self._procfs_path, self.pid)))
529
530 @wrap_exceptions
531 def num_ctx_switches(self):
532 return _common.pctxsw(
533 *cext.proc_num_ctx_switches(self.pid))
534
535 @wrap_exceptions
536 def wait(self, timeout=None):
537 return _psposix.wait_pid(self.pid, timeout, self._name)
538
539 if HAS_PROC_IO_COUNTERS:
540 @wrap_exceptions
541 def io_counters(self):
542 try:
543 rc, wc, rb, wb = cext.proc_io_counters(self.pid)
544 except OSError:
545 # if process is terminated, proc_io_counters returns OSError
546 # instead of NSP
547 if not pid_exists(self.pid):
548 raise NoSuchProcess(self.pid, self._name)
549 raise
550 return _common.pio(rc, wc, rb, wb)