_psosx.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """macOS platform implementation."""
  5. import contextlib
  6. import errno
  7. import functools
  8. import os
  9. from collections import namedtuple
  10. from . import _common
  11. from . import _psposix
  12. from . import _psutil_osx as cext
  13. from . import _psutil_posix as cext_posix
  14. from ._common import AccessDenied
  15. from ._common import conn_tmap
  16. from ._common import conn_to_ntuple
  17. from ._common import isfile_strict
  18. from ._common import memoize_when_activated
  19. from ._common import NoSuchProcess
  20. from ._common import parse_environ_block
  21. from ._common import usage_percent
  22. from ._common import ZombieProcess
  23. from ._compat import PermissionError
  24. from ._compat import ProcessLookupError
  25. __extra__all__ = []
  26. # =====================================================================
  27. # --- globals
  28. # =====================================================================
  29. PAGESIZE = os.sysconf("SC_PAGE_SIZE")
  30. AF_LINK = cext_posix.AF_LINK
  31. TCP_STATUSES = {
  32. cext.TCPS_ESTABLISHED: _common.CONN_ESTABLISHED,
  33. cext.TCPS_SYN_SENT: _common.CONN_SYN_SENT,
  34. cext.TCPS_SYN_RECEIVED: _common.CONN_SYN_RECV,
  35. cext.TCPS_FIN_WAIT_1: _common.CONN_FIN_WAIT1,
  36. cext.TCPS_FIN_WAIT_2: _common.CONN_FIN_WAIT2,
  37. cext.TCPS_TIME_WAIT: _common.CONN_TIME_WAIT,
  38. cext.TCPS_CLOSED: _common.CONN_CLOSE,
  39. cext.TCPS_CLOSE_WAIT: _common.CONN_CLOSE_WAIT,
  40. cext.TCPS_LAST_ACK: _common.CONN_LAST_ACK,
  41. cext.TCPS_LISTEN: _common.CONN_LISTEN,
  42. cext.TCPS_CLOSING: _common.CONN_CLOSING,
  43. cext.PSUTIL_CONN_NONE: _common.CONN_NONE,
  44. }
  45. PROC_STATUSES = {
  46. cext.SIDL: _common.STATUS_IDLE,
  47. cext.SRUN: _common.STATUS_RUNNING,
  48. cext.SSLEEP: _common.STATUS_SLEEPING,
  49. cext.SSTOP: _common.STATUS_STOPPED,
  50. cext.SZOMB: _common.STATUS_ZOMBIE,
  51. }
  52. kinfo_proc_map = dict(
  53. ppid=0,
  54. ruid=1,
  55. euid=2,
  56. suid=3,
  57. rgid=4,
  58. egid=5,
  59. sgid=6,
  60. ttynr=7,
  61. ctime=8,
  62. status=9,
  63. name=10,
  64. )
  65. pidtaskinfo_map = dict(
  66. cpuutime=0,
  67. cpustime=1,
  68. rss=2,
  69. vms=3,
  70. pfaults=4,
  71. pageins=5,
  72. numthreads=6,
  73. volctxsw=7,
  74. )
  75. # =====================================================================
  76. # --- named tuples
  77. # =====================================================================
  78. # psutil.cpu_times()
  79. scputimes = namedtuple('scputimes', ['user', 'nice', 'system', 'idle'])
  80. # psutil.virtual_memory()
  81. svmem = namedtuple(
  82. 'svmem', ['total', 'available', 'percent', 'used', 'free',
  83. 'active', 'inactive', 'wired'])
  84. # psutil.Process.memory_info()
  85. pmem = namedtuple('pmem', ['rss', 'vms', 'pfaults', 'pageins'])
  86. # psutil.Process.memory_full_info()
  87. pfullmem = namedtuple('pfullmem', pmem._fields + ('uss', ))
  88. # =====================================================================
  89. # --- memory
  90. # =====================================================================
  91. def virtual_memory():
  92. """System virtual memory as a namedtuple."""
  93. total, active, inactive, wired, free, speculative = cext.virtual_mem()
  94. # This is how Zabbix calculate avail and used mem:
  95. # https://github.com/zabbix/zabbix/blob/trunk/src/libs/zbxsysinfo/
  96. # osx/memory.c
  97. # Also see: https://github.com/giampaolo/psutil/issues/1277
  98. avail = inactive + free
  99. used = active + wired
  100. # This is NOT how Zabbix calculates free mem but it matches "free"
  101. # cmdline utility.
  102. free -= speculative
  103. percent = usage_percent((total - avail), total, round_=1)
  104. return svmem(total, avail, percent, used, free,
  105. active, inactive, wired)
  106. def swap_memory():
  107. """Swap system memory as a (total, used, free, sin, sout) tuple."""
  108. total, used, free, sin, sout = cext.swap_mem()
  109. percent = usage_percent(used, total, round_=1)
  110. return _common.sswap(total, used, free, percent, sin, sout)
  111. # =====================================================================
  112. # --- CPU
  113. # =====================================================================
  114. def cpu_times():
  115. """Return system CPU times as a namedtuple."""
  116. user, nice, system, idle = cext.cpu_times()
  117. return scputimes(user, nice, system, idle)
  118. def per_cpu_times():
  119. """Return system CPU times as a named tuple"""
  120. ret = []
  121. for cpu_t in cext.per_cpu_times():
  122. user, nice, system, idle = cpu_t
  123. item = scputimes(user, nice, system, idle)
  124. ret.append(item)
  125. return ret
  126. def cpu_count_logical():
  127. """Return the number of logical CPUs in the system."""
  128. return cext.cpu_count_logical()
  129. def cpu_count_physical():
  130. """Return the number of physical CPUs in the system."""
  131. return cext.cpu_count_phys()
  132. def cpu_stats():
  133. ctx_switches, interrupts, soft_interrupts, syscalls, traps = \
  134. cext.cpu_stats()
  135. return _common.scpustats(
  136. ctx_switches, interrupts, soft_interrupts, syscalls)
  137. def cpu_freq():
  138. """Return CPU frequency.
  139. On macOS per-cpu frequency is not supported.
  140. Also, the returned frequency never changes, see:
  141. https://arstechnica.com/civis/viewtopic.php?f=19&t=465002
  142. """
  143. curr, min_, max_ = cext.cpu_freq()
  144. return [_common.scpufreq(curr, min_, max_)]
  145. # =====================================================================
  146. # --- disks
  147. # =====================================================================
  148. disk_usage = _psposix.disk_usage
  149. disk_io_counters = cext.disk_io_counters
  150. def disk_partitions(all=False):
  151. """Return mounted disk partitions as a list of namedtuples."""
  152. retlist = []
  153. partitions = cext.disk_partitions()
  154. for partition in partitions:
  155. device, mountpoint, fstype, opts = partition
  156. if device == 'none':
  157. device = ''
  158. if not all:
  159. if not os.path.isabs(device) or not os.path.exists(device):
  160. continue
  161. ntuple = _common.sdiskpart(device, mountpoint, fstype, opts)
  162. retlist.append(ntuple)
  163. return retlist
  164. # =====================================================================
  165. # --- sensors
  166. # =====================================================================
  167. def sensors_battery():
  168. """Return battery information."""
  169. try:
  170. percent, minsleft, power_plugged = cext.sensors_battery()
  171. except NotImplementedError:
  172. # no power source - return None according to interface
  173. return None
  174. power_plugged = power_plugged == 1
  175. if power_plugged:
  176. secsleft = _common.POWER_TIME_UNLIMITED
  177. elif minsleft == -1:
  178. secsleft = _common.POWER_TIME_UNKNOWN
  179. else:
  180. secsleft = minsleft * 60
  181. return _common.sbattery(percent, secsleft, power_plugged)
  182. # =====================================================================
  183. # --- network
  184. # =====================================================================
  185. net_io_counters = cext.net_io_counters
  186. net_if_addrs = cext_posix.net_if_addrs
  187. def net_connections(kind='inet'):
  188. """System-wide network connections."""
  189. # Note: on macOS this will fail with AccessDenied unless
  190. # the process is owned by root.
  191. ret = []
  192. for pid in pids():
  193. try:
  194. cons = Process(pid).connections(kind)
  195. except NoSuchProcess:
  196. continue
  197. else:
  198. if cons:
  199. for c in cons:
  200. c = list(c) + [pid]
  201. ret.append(_common.sconn(*c))
  202. return ret
  203. def net_if_stats():
  204. """Get NIC stats (isup, duplex, speed, mtu)."""
  205. names = net_io_counters().keys()
  206. ret = {}
  207. for name in names:
  208. try:
  209. mtu = cext_posix.net_if_mtu(name)
  210. isup = cext_posix.net_if_is_running(name)
  211. duplex, speed = cext_posix.net_if_duplex_speed(name)
  212. except OSError as err:
  213. # https://github.com/giampaolo/psutil/issues/1279
  214. if err.errno != errno.ENODEV:
  215. raise
  216. else:
  217. if hasattr(_common, 'NicDuplex'):
  218. duplex = _common.NicDuplex(duplex)
  219. ret[name] = _common.snicstats(isup, duplex, speed, mtu)
  220. return ret
  221. # =====================================================================
  222. # --- other system functions
  223. # =====================================================================
  224. def boot_time():
  225. """The system boot time expressed in seconds since the epoch."""
  226. return cext.boot_time()
  227. def users():
  228. """Return currently connected users as a list of namedtuples."""
  229. retlist = []
  230. rawlist = cext.users()
  231. for item in rawlist:
  232. user, tty, hostname, tstamp, pid = item
  233. if tty == '~':
  234. continue # reboot or shutdown
  235. if not tstamp:
  236. continue
  237. nt = _common.suser(user, tty or None, hostname or None, tstamp, pid)
  238. retlist.append(nt)
  239. return retlist
  240. # =====================================================================
  241. # --- processes
  242. # =====================================================================
  243. def pids():
  244. ls = cext.pids()
  245. if 0 not in ls:
  246. # On certain macOS versions pids() C doesn't return PID 0 but
  247. # "ps" does and the process is querable via sysctl():
  248. # https://travis-ci.org/giampaolo/psutil/jobs/309619941
  249. try:
  250. Process(0).create_time()
  251. ls.insert(0, 0)
  252. except NoSuchProcess:
  253. pass
  254. except AccessDenied:
  255. ls.insert(0, 0)
  256. return ls
  257. pid_exists = _psposix.pid_exists
  258. def is_zombie(pid):
  259. try:
  260. st = cext.proc_kinfo_oneshot(pid)[kinfo_proc_map['status']]
  261. return st == cext.SZOMB
  262. except Exception:
  263. return False
  264. def wrap_exceptions(fun):
  265. """Decorator which translates bare OSError exceptions into
  266. NoSuchProcess and AccessDenied.
  267. """
  268. @functools.wraps(fun)
  269. def wrapper(self, *args, **kwargs):
  270. try:
  271. return fun(self, *args, **kwargs)
  272. except ProcessLookupError:
  273. if is_zombie(self.pid):
  274. raise ZombieProcess(self.pid, self._name, self._ppid)
  275. else:
  276. raise NoSuchProcess(self.pid, self._name)
  277. except PermissionError:
  278. raise AccessDenied(self.pid, self._name)
  279. except cext.ZombieProcessError:
  280. raise ZombieProcess(self.pid, self._name, self._ppid)
  281. return wrapper
  282. @contextlib.contextmanager
  283. def catch_zombie(proc):
  284. """There are some poor C APIs which incorrectly raise ESRCH when
  285. the process is still alive or it's a zombie, or even RuntimeError
  286. (those who don't set errno). This is here in order to solve:
  287. https://github.com/giampaolo/psutil/issues/1044
  288. """
  289. try:
  290. yield
  291. except (OSError, RuntimeError) as err:
  292. if isinstance(err, RuntimeError) or err.errno == errno.ESRCH:
  293. try:
  294. # status() is not supposed to lie and correctly detect
  295. # zombies so if it raises ESRCH it's true.
  296. status = proc.status()
  297. except NoSuchProcess:
  298. raise err
  299. else:
  300. if status == _common.STATUS_ZOMBIE:
  301. raise ZombieProcess(proc.pid, proc._name, proc._ppid)
  302. else:
  303. raise AccessDenied(proc.pid, proc._name)
  304. else:
  305. raise
  306. class Process(object):
  307. """Wrapper class around underlying C implementation."""
  308. __slots__ = ["pid", "_name", "_ppid", "_cache"]
  309. def __init__(self, pid):
  310. self.pid = pid
  311. self._name = None
  312. self._ppid = None
  313. @wrap_exceptions
  314. @memoize_when_activated
  315. def _get_kinfo_proc(self):
  316. # Note: should work with all PIDs without permission issues.
  317. ret = cext.proc_kinfo_oneshot(self.pid)
  318. assert len(ret) == len(kinfo_proc_map)
  319. return ret
  320. @wrap_exceptions
  321. @memoize_when_activated
  322. def _get_pidtaskinfo(self):
  323. # Note: should work for PIDs owned by user only.
  324. with catch_zombie(self):
  325. ret = cext.proc_pidtaskinfo_oneshot(self.pid)
  326. assert len(ret) == len(pidtaskinfo_map)
  327. return ret
  328. def oneshot_enter(self):
  329. self._get_kinfo_proc.cache_activate(self)
  330. self._get_pidtaskinfo.cache_activate(self)
  331. def oneshot_exit(self):
  332. self._get_kinfo_proc.cache_deactivate(self)
  333. self._get_pidtaskinfo.cache_deactivate(self)
  334. @wrap_exceptions
  335. def name(self):
  336. name = self._get_kinfo_proc()[kinfo_proc_map['name']]
  337. return name if name is not None else cext.proc_name(self.pid)
  338. @wrap_exceptions
  339. def exe(self):
  340. with catch_zombie(self):
  341. return cext.proc_exe(self.pid)
  342. @wrap_exceptions
  343. def cmdline(self):
  344. with catch_zombie(self):
  345. return cext.proc_cmdline(self.pid)
  346. @wrap_exceptions
  347. def environ(self):
  348. with catch_zombie(self):
  349. return parse_environ_block(cext.proc_environ(self.pid))
  350. @wrap_exceptions
  351. def ppid(self):
  352. self._ppid = self._get_kinfo_proc()[kinfo_proc_map['ppid']]
  353. return self._ppid
  354. @wrap_exceptions
  355. def cwd(self):
  356. with catch_zombie(self):
  357. return cext.proc_cwd(self.pid)
  358. @wrap_exceptions
  359. def uids(self):
  360. rawtuple = self._get_kinfo_proc()
  361. return _common.puids(
  362. rawtuple[kinfo_proc_map['ruid']],
  363. rawtuple[kinfo_proc_map['euid']],
  364. rawtuple[kinfo_proc_map['suid']])
  365. @wrap_exceptions
  366. def gids(self):
  367. rawtuple = self._get_kinfo_proc()
  368. return _common.puids(
  369. rawtuple[kinfo_proc_map['rgid']],
  370. rawtuple[kinfo_proc_map['egid']],
  371. rawtuple[kinfo_proc_map['sgid']])
  372. @wrap_exceptions
  373. def terminal(self):
  374. tty_nr = self._get_kinfo_proc()[kinfo_proc_map['ttynr']]
  375. tmap = _psposix.get_terminal_map()
  376. try:
  377. return tmap[tty_nr]
  378. except KeyError:
  379. return None
  380. @wrap_exceptions
  381. def memory_info(self):
  382. rawtuple = self._get_pidtaskinfo()
  383. return pmem(
  384. rawtuple[pidtaskinfo_map['rss']],
  385. rawtuple[pidtaskinfo_map['vms']],
  386. rawtuple[pidtaskinfo_map['pfaults']],
  387. rawtuple[pidtaskinfo_map['pageins']],
  388. )
  389. @wrap_exceptions
  390. def memory_full_info(self):
  391. basic_mem = self.memory_info()
  392. uss = cext.proc_memory_uss(self.pid)
  393. return pfullmem(*basic_mem + (uss, ))
  394. @wrap_exceptions
  395. def cpu_times(self):
  396. rawtuple = self._get_pidtaskinfo()
  397. return _common.pcputimes(
  398. rawtuple[pidtaskinfo_map['cpuutime']],
  399. rawtuple[pidtaskinfo_map['cpustime']],
  400. # children user / system times are not retrievable (set to 0)
  401. 0.0, 0.0)
  402. @wrap_exceptions
  403. def create_time(self):
  404. return self._get_kinfo_proc()[kinfo_proc_map['ctime']]
  405. @wrap_exceptions
  406. def num_ctx_switches(self):
  407. # Unvoluntary value seems not to be available;
  408. # getrusage() numbers seems to confirm this theory.
  409. # We set it to 0.
  410. vol = self._get_pidtaskinfo()[pidtaskinfo_map['volctxsw']]
  411. return _common.pctxsw(vol, 0)
  412. @wrap_exceptions
  413. def num_threads(self):
  414. return self._get_pidtaskinfo()[pidtaskinfo_map['numthreads']]
  415. @wrap_exceptions
  416. def open_files(self):
  417. if self.pid == 0:
  418. return []
  419. files = []
  420. with catch_zombie(self):
  421. rawlist = cext.proc_open_files(self.pid)
  422. for path, fd in rawlist:
  423. if isfile_strict(path):
  424. ntuple = _common.popenfile(path, fd)
  425. files.append(ntuple)
  426. return files
  427. @wrap_exceptions
  428. def connections(self, kind='inet'):
  429. if kind not in conn_tmap:
  430. raise ValueError("invalid %r kind argument; choose between %s"
  431. % (kind, ', '.join([repr(x) for x in conn_tmap])))
  432. families, types = conn_tmap[kind]
  433. with catch_zombie(self):
  434. rawlist = cext.proc_connections(self.pid, families, types)
  435. ret = []
  436. for item in rawlist:
  437. fd, fam, type, laddr, raddr, status = item
  438. nt = conn_to_ntuple(fd, fam, type, laddr, raddr, status,
  439. TCP_STATUSES)
  440. ret.append(nt)
  441. return ret
  442. @wrap_exceptions
  443. def num_fds(self):
  444. if self.pid == 0:
  445. return 0
  446. with catch_zombie(self):
  447. return cext.proc_num_fds(self.pid)
  448. @wrap_exceptions
  449. def wait(self, timeout=None):
  450. return _psposix.wait_pid(self.pid, timeout, self._name)
  451. @wrap_exceptions
  452. def nice_get(self):
  453. with catch_zombie(self):
  454. return cext_posix.getpriority(self.pid)
  455. @wrap_exceptions
  456. def nice_set(self, value):
  457. with catch_zombie(self):
  458. return cext_posix.setpriority(self.pid, value)
  459. @wrap_exceptions
  460. def status(self):
  461. code = self._get_kinfo_proc()[kinfo_proc_map['status']]
  462. # XXX is '?' legit? (we're not supposed to return it anyway)
  463. return PROC_STATUSES.get(code, '?')
  464. @wrap_exceptions
  465. def threads(self):
  466. rawlist = cext.proc_threads(self.pid)
  467. retlist = []
  468. for thread_id, utime, stime in rawlist:
  469. ntuple = _common.pthread(thread_id, utime, stime)
  470. retlist.append(ntuple)
  471. return retlist