_psosx.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 errno
  6. import functools
  7. import os
  8. from collections import namedtuple
  9. from . import _common
  10. from . import _psposix
  11. from . import _psutil_osx as cext
  12. from ._common import AccessDenied
  13. from ._common import NoSuchProcess
  14. from ._common import ZombieProcess
  15. from ._common import conn_tmap
  16. from ._common import conn_to_ntuple
  17. from ._common import debug
  18. from ._common import isfile_strict
  19. from ._common import memoize_when_activated
  20. from ._common import parse_environ_block
  21. from ._common import usage_percent
  22. __extra__all__ = []
  23. # =====================================================================
  24. # --- globals
  25. # =====================================================================
  26. PAGESIZE = cext.getpagesize()
  27. AF_LINK = cext.AF_LINK
  28. TCP_STATUSES = {
  29. cext.TCPS_ESTABLISHED: _common.CONN_ESTABLISHED,
  30. cext.TCPS_SYN_SENT: _common.CONN_SYN_SENT,
  31. cext.TCPS_SYN_RECEIVED: _common.CONN_SYN_RECV,
  32. cext.TCPS_FIN_WAIT_1: _common.CONN_FIN_WAIT1,
  33. cext.TCPS_FIN_WAIT_2: _common.CONN_FIN_WAIT2,
  34. cext.TCPS_TIME_WAIT: _common.CONN_TIME_WAIT,
  35. cext.TCPS_CLOSED: _common.CONN_CLOSE,
  36. cext.TCPS_CLOSE_WAIT: _common.CONN_CLOSE_WAIT,
  37. cext.TCPS_LAST_ACK: _common.CONN_LAST_ACK,
  38. cext.TCPS_LISTEN: _common.CONN_LISTEN,
  39. cext.TCPS_CLOSING: _common.CONN_CLOSING,
  40. cext.PSUTIL_CONN_NONE: _common.CONN_NONE,
  41. }
  42. PROC_STATUSES = {
  43. cext.SIDL: _common.STATUS_IDLE,
  44. cext.SRUN: _common.STATUS_RUNNING,
  45. cext.SSLEEP: _common.STATUS_SLEEPING,
  46. cext.SSTOP: _common.STATUS_STOPPED,
  47. cext.SZOMB: _common.STATUS_ZOMBIE,
  48. }
  49. kinfo_proc_map = dict(
  50. ppid=0,
  51. ruid=1,
  52. euid=2,
  53. suid=3,
  54. rgid=4,
  55. egid=5,
  56. sgid=6,
  57. ttynr=7,
  58. ctime=8,
  59. status=9,
  60. name=10,
  61. )
  62. pidtaskinfo_map = dict(
  63. cpuutime=0,
  64. cpustime=1,
  65. rss=2,
  66. vms=3,
  67. pfaults=4,
  68. pageins=5,
  69. numthreads=6,
  70. volctxsw=7,
  71. )
  72. # =====================================================================
  73. # --- named tuples
  74. # =====================================================================
  75. # fmt: off
  76. # psutil.cpu_times()
  77. scputimes = namedtuple('scputimes', ['user', 'nice', 'system', 'idle'])
  78. # psutil.virtual_memory()
  79. svmem = namedtuple(
  80. 'svmem', ['total', 'available', 'percent', 'used', 'free',
  81. 'active', 'inactive', 'wired'])
  82. # psutil.Process.memory_info()
  83. pmem = namedtuple('pmem', ['rss', 'vms', 'pfaults', 'pageins'])
  84. # psutil.Process.memory_full_info()
  85. pfullmem = namedtuple('pfullmem', pmem._fields + ('uss', ))
  86. # fmt: on
  87. # =====================================================================
  88. # --- memory
  89. # =====================================================================
  90. def virtual_memory():
  91. """System virtual memory as a namedtuple."""
  92. total, active, inactive, wired, free, speculative = cext.virtual_mem()
  93. # This is how Zabbix calculate avail and used mem:
  94. # https://github.com/zabbix/zabbix/blob/master/src/libs/zbxsysinfo/osx/memory.c
  95. # Also see: https://github.com/giampaolo/psutil/issues/1277
  96. avail = inactive + free
  97. used = active + wired
  98. # This is NOT how Zabbix calculates free mem but it matches "free"
  99. # cmdline utility.
  100. free -= speculative
  101. percent = usage_percent((total - avail), total, round_=1)
  102. return svmem(total, avail, percent, used, free, active, inactive, wired)
  103. def swap_memory():
  104. """Swap system memory as a (total, used, free, sin, sout) tuple."""
  105. total, used, free, sin, sout = cext.swap_mem()
  106. percent = usage_percent(used, total, round_=1)
  107. return _common.sswap(total, used, free, percent, sin, sout)
  108. # =====================================================================
  109. # --- CPU
  110. # =====================================================================
  111. def cpu_times():
  112. """Return system CPU times as a namedtuple."""
  113. user, nice, system, idle = cext.cpu_times()
  114. return scputimes(user, nice, system, idle)
  115. def per_cpu_times():
  116. """Return system CPU times as a named tuple."""
  117. ret = []
  118. for cpu_t in cext.per_cpu_times():
  119. user, nice, system, idle = cpu_t
  120. item = scputimes(user, nice, system, idle)
  121. ret.append(item)
  122. return ret
  123. def cpu_count_logical():
  124. """Return the number of logical CPUs in the system."""
  125. return cext.cpu_count_logical()
  126. def cpu_count_cores():
  127. """Return the number of CPU cores in the system."""
  128. return cext.cpu_count_cores()
  129. def cpu_stats():
  130. ctx_switches, interrupts, soft_interrupts, syscalls, _traps = (
  131. cext.cpu_stats()
  132. )
  133. return _common.scpustats(
  134. ctx_switches, interrupts, soft_interrupts, syscalls
  135. )
  136. if cext.has_cpu_freq(): # not always available on ARM64
  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.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).net_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.net_if_mtu(name)
  210. flags = cext.net_if_flags(name)
  211. duplex, speed = cext.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. output_flags = ','.join(flags)
  220. isup = 'running' in flags
  221. ret[name] = _common.snicstats(
  222. isup, duplex, speed, mtu, output_flags
  223. )
  224. return ret
  225. # =====================================================================
  226. # --- other system functions
  227. # =====================================================================
  228. def boot_time():
  229. """The system boot time expressed in seconds since the epoch."""
  230. return cext.boot_time()
  231. try:
  232. INIT_BOOT_TIME = boot_time()
  233. except Exception as err: # noqa: BLE001
  234. # Don't want to crash at import time.
  235. debug(f"ignoring exception on import: {err!r}")
  236. INIT_BOOT_TIME = 0
  237. def adjust_proc_create_time(ctime):
  238. """Account for system clock updates."""
  239. if INIT_BOOT_TIME == 0:
  240. return ctime
  241. diff = INIT_BOOT_TIME - boot_time()
  242. if diff == 0 or abs(diff) < 1:
  243. return ctime
  244. debug("system clock was updated; adjusting process create_time()")
  245. if diff < 0:
  246. return ctime - diff
  247. return ctime + diff
  248. def users():
  249. """Return currently connected users as a list of namedtuples."""
  250. retlist = []
  251. rawlist = cext.users()
  252. for item in rawlist:
  253. user, tty, hostname, tstamp, pid = item
  254. if tty == '~':
  255. continue # reboot or shutdown
  256. if not tstamp:
  257. continue
  258. nt = _common.suser(user, tty or None, hostname or None, tstamp, pid)
  259. retlist.append(nt)
  260. return retlist
  261. # =====================================================================
  262. # --- processes
  263. # =====================================================================
  264. def pids():
  265. ls = cext.pids()
  266. if 0 not in ls:
  267. # On certain macOS versions pids() C doesn't return PID 0 but
  268. # "ps" does and the process is querable via sysctl():
  269. # https://travis-ci.org/giampaolo/psutil/jobs/309619941
  270. try:
  271. Process(0).create_time()
  272. ls.insert(0, 0)
  273. except NoSuchProcess:
  274. pass
  275. except AccessDenied:
  276. ls.insert(0, 0)
  277. return ls
  278. pid_exists = _psposix.pid_exists
  279. def wrap_exceptions(fun):
  280. """Decorator which translates bare OSError exceptions into
  281. NoSuchProcess and AccessDenied.
  282. """
  283. @functools.wraps(fun)
  284. def wrapper(self, *args, **kwargs):
  285. pid, ppid, name = self.pid, self._ppid, self._name
  286. try:
  287. return fun(self, *args, **kwargs)
  288. except ProcessLookupError as err:
  289. if cext.proc_is_zombie(pid):
  290. raise ZombieProcess(pid, name, ppid) from err
  291. raise NoSuchProcess(pid, name) from err
  292. except PermissionError as err:
  293. raise AccessDenied(pid, name) from err
  294. except cext.ZombieProcessError as err:
  295. raise ZombieProcess(pid, name, ppid) from err
  296. return wrapper
  297. class Process:
  298. """Wrapper class around underlying C implementation."""
  299. __slots__ = ["_cache", "_name", "_ppid", "pid"]
  300. def __init__(self, pid):
  301. self.pid = pid
  302. self._name = None
  303. self._ppid = None
  304. @wrap_exceptions
  305. @memoize_when_activated
  306. def _get_kinfo_proc(self):
  307. # Note: should work with all PIDs without permission issues.
  308. ret = cext.proc_kinfo_oneshot(self.pid)
  309. assert len(ret) == len(kinfo_proc_map)
  310. return ret
  311. @wrap_exceptions
  312. @memoize_when_activated
  313. def _get_pidtaskinfo(self):
  314. # Note: should work for PIDs owned by user only.
  315. ret = cext.proc_pidtaskinfo_oneshot(self.pid)
  316. assert len(ret) == len(pidtaskinfo_map)
  317. return ret
  318. def oneshot_enter(self):
  319. self._get_kinfo_proc.cache_activate(self)
  320. self._get_pidtaskinfo.cache_activate(self)
  321. def oneshot_exit(self):
  322. self._get_kinfo_proc.cache_deactivate(self)
  323. self._get_pidtaskinfo.cache_deactivate(self)
  324. @wrap_exceptions
  325. def name(self):
  326. name = self._get_kinfo_proc()[kinfo_proc_map['name']]
  327. return name if name is not None else cext.proc_name(self.pid)
  328. @wrap_exceptions
  329. def exe(self):
  330. return cext.proc_exe(self.pid)
  331. @wrap_exceptions
  332. def cmdline(self):
  333. return cext.proc_cmdline(self.pid)
  334. @wrap_exceptions
  335. def environ(self):
  336. return parse_environ_block(cext.proc_environ(self.pid))
  337. @wrap_exceptions
  338. def ppid(self):
  339. self._ppid = self._get_kinfo_proc()[kinfo_proc_map['ppid']]
  340. return self._ppid
  341. @wrap_exceptions
  342. def cwd(self):
  343. return cext.proc_cwd(self.pid)
  344. @wrap_exceptions
  345. def uids(self):
  346. rawtuple = self._get_kinfo_proc()
  347. return _common.puids(
  348. rawtuple[kinfo_proc_map['ruid']],
  349. rawtuple[kinfo_proc_map['euid']],
  350. rawtuple[kinfo_proc_map['suid']],
  351. )
  352. @wrap_exceptions
  353. def gids(self):
  354. rawtuple = self._get_kinfo_proc()
  355. return _common.puids(
  356. rawtuple[kinfo_proc_map['rgid']],
  357. rawtuple[kinfo_proc_map['egid']],
  358. rawtuple[kinfo_proc_map['sgid']],
  359. )
  360. @wrap_exceptions
  361. def terminal(self):
  362. tty_nr = self._get_kinfo_proc()[kinfo_proc_map['ttynr']]
  363. tmap = _psposix.get_terminal_map()
  364. try:
  365. return tmap[tty_nr]
  366. except KeyError:
  367. return None
  368. @wrap_exceptions
  369. def memory_info(self):
  370. rawtuple = self._get_pidtaskinfo()
  371. return pmem(
  372. rawtuple[pidtaskinfo_map['rss']],
  373. rawtuple[pidtaskinfo_map['vms']],
  374. rawtuple[pidtaskinfo_map['pfaults']],
  375. rawtuple[pidtaskinfo_map['pageins']],
  376. )
  377. @wrap_exceptions
  378. def memory_full_info(self):
  379. basic_mem = self.memory_info()
  380. uss = cext.proc_memory_uss(self.pid)
  381. return pfullmem(*basic_mem + (uss,))
  382. @wrap_exceptions
  383. def cpu_times(self):
  384. rawtuple = self._get_pidtaskinfo()
  385. return _common.pcputimes(
  386. rawtuple[pidtaskinfo_map['cpuutime']],
  387. rawtuple[pidtaskinfo_map['cpustime']],
  388. # children user / system times are not retrievable (set to 0)
  389. 0.0,
  390. 0.0,
  391. )
  392. @wrap_exceptions
  393. def create_time(self, monotonic=False):
  394. ctime = self._get_kinfo_proc()[kinfo_proc_map['ctime']]
  395. if not monotonic:
  396. ctime = adjust_proc_create_time(ctime)
  397. return ctime
  398. @wrap_exceptions
  399. def num_ctx_switches(self):
  400. # Unvoluntary value seems not to be available;
  401. # getrusage() numbers seems to confirm this theory.
  402. # We set it to 0.
  403. vol = self._get_pidtaskinfo()[pidtaskinfo_map['volctxsw']]
  404. return _common.pctxsw(vol, 0)
  405. @wrap_exceptions
  406. def num_threads(self):
  407. return self._get_pidtaskinfo()[pidtaskinfo_map['numthreads']]
  408. @wrap_exceptions
  409. def open_files(self):
  410. if self.pid == 0:
  411. return []
  412. files = []
  413. rawlist = cext.proc_open_files(self.pid)
  414. for path, fd in rawlist:
  415. if isfile_strict(path):
  416. ntuple = _common.popenfile(path, fd)
  417. files.append(ntuple)
  418. return files
  419. @wrap_exceptions
  420. def net_connections(self, kind='inet'):
  421. families, types = conn_tmap[kind]
  422. rawlist = cext.proc_net_connections(self.pid, families, types)
  423. ret = []
  424. for item in rawlist:
  425. fd, fam, type, laddr, raddr, status = item
  426. nt = conn_to_ntuple(
  427. fd, fam, type, laddr, raddr, status, TCP_STATUSES
  428. )
  429. ret.append(nt)
  430. return ret
  431. @wrap_exceptions
  432. def num_fds(self):
  433. if self.pid == 0:
  434. return 0
  435. return cext.proc_num_fds(self.pid)
  436. @wrap_exceptions
  437. def wait(self, timeout=None):
  438. return _psposix.wait_pid(self.pid, timeout, self._name)
  439. @wrap_exceptions
  440. def nice_get(self):
  441. return cext.proc_priority_get(self.pid)
  442. @wrap_exceptions
  443. def nice_set(self, value):
  444. return cext.proc_priority_set(self.pid, value)
  445. @wrap_exceptions
  446. def status(self):
  447. code = self._get_kinfo_proc()[kinfo_proc_map['status']]
  448. # XXX is '?' legit? (we're not supposed to return it anyway)
  449. return PROC_STATUSES.get(code, '?')
  450. @wrap_exceptions
  451. def threads(self):
  452. rawlist = cext.proc_threads(self.pid)
  453. retlist = []
  454. for thread_id, utime, stime in rawlist:
  455. ntuple = _common.pthread(thread_id, utime, stime)
  456. retlist.append(ntuple)
  457. return retlist