_psosx.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 . import _common
  9. from . import _ntuples as ntp
  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. # --- memory
  74. # =====================================================================
  75. def virtual_memory():
  76. """System virtual memory as a namedtuple."""
  77. total, active, inactive, wired, free, speculative = cext.virtual_mem()
  78. # This is how Zabbix calculate avail and used mem:
  79. # https://github.com/zabbix/zabbix/blob/master/src/libs/zbxsysinfo/osx/memory.c
  80. # Also see: https://github.com/giampaolo/psutil/issues/1277
  81. avail = inactive + free
  82. used = active + wired
  83. # This is NOT how Zabbix calculates free mem but it matches "free"
  84. # cmdline utility.
  85. free -= speculative
  86. percent = usage_percent((total - avail), total, round_=1)
  87. return ntp.svmem(
  88. total, avail, percent, used, free, active, inactive, wired
  89. )
  90. def swap_memory():
  91. """Swap system memory as a (total, used, free, sin, sout) tuple."""
  92. total, used, free, sin, sout = cext.swap_mem()
  93. percent = usage_percent(used, total, round_=1)
  94. return ntp.sswap(total, used, free, percent, sin, sout)
  95. # malloc / heap functions
  96. heap_info = cext.heap_info
  97. heap_trim = cext.heap_trim
  98. # =====================================================================
  99. # --- CPU
  100. # =====================================================================
  101. def cpu_times():
  102. """Return system CPU times as a namedtuple."""
  103. user, nice, system, idle = cext.cpu_times()
  104. return ntp.scputimes(user, nice, system, idle)
  105. def per_cpu_times():
  106. """Return system CPU times as a named tuple."""
  107. ret = []
  108. for cpu_t in cext.per_cpu_times():
  109. user, nice, system, idle = cpu_t
  110. item = ntp.scputimes(user, nice, system, idle)
  111. ret.append(item)
  112. return ret
  113. def cpu_count_logical():
  114. """Return the number of logical CPUs in the system."""
  115. return cext.cpu_count_logical()
  116. def cpu_count_cores():
  117. """Return the number of CPU cores in the system."""
  118. return cext.cpu_count_cores()
  119. def cpu_stats():
  120. ctx_switches, interrupts, soft_interrupts, syscalls, _traps = (
  121. cext.cpu_stats()
  122. )
  123. return ntp.scpustats(ctx_switches, interrupts, soft_interrupts, syscalls)
  124. if cext.has_cpu_freq(): # not always available on ARM64
  125. def cpu_freq():
  126. """Return CPU frequency.
  127. On macOS per-cpu frequency is not supported.
  128. Also, the returned frequency never changes, see:
  129. https://arstechnica.com/civis/viewtopic.php?f=19&t=465002.
  130. """
  131. curr, min_, max_ = cext.cpu_freq()
  132. return [ntp.scpufreq(curr, min_, max_)]
  133. # =====================================================================
  134. # --- disks
  135. # =====================================================================
  136. disk_usage = _psposix.disk_usage
  137. disk_io_counters = cext.disk_io_counters
  138. def disk_partitions(all=False):
  139. """Return mounted disk partitions as a list of namedtuples."""
  140. retlist = []
  141. partitions = cext.disk_partitions()
  142. for partition in partitions:
  143. device, mountpoint, fstype, opts = partition
  144. if device == 'none':
  145. device = ''
  146. if not all:
  147. if not os.path.isabs(device) or not os.path.exists(device):
  148. continue
  149. ntuple = ntp.sdiskpart(device, mountpoint, fstype, opts)
  150. retlist.append(ntuple)
  151. return retlist
  152. # =====================================================================
  153. # --- sensors
  154. # =====================================================================
  155. def sensors_battery():
  156. """Return battery information."""
  157. try:
  158. percent, minsleft, power_plugged = cext.sensors_battery()
  159. except NotImplementedError:
  160. # no power source - return None according to interface
  161. return None
  162. power_plugged = power_plugged == 1
  163. if power_plugged:
  164. secsleft = _common.POWER_TIME_UNLIMITED
  165. elif minsleft == -1:
  166. secsleft = _common.POWER_TIME_UNKNOWN
  167. else:
  168. secsleft = minsleft * 60
  169. return ntp.sbattery(percent, secsleft, power_plugged)
  170. # =====================================================================
  171. # --- network
  172. # =====================================================================
  173. net_io_counters = cext.net_io_counters
  174. net_if_addrs = cext.net_if_addrs
  175. def net_connections(kind='inet'):
  176. """System-wide network connections."""
  177. # Note: on macOS this will fail with AccessDenied unless
  178. # the process is owned by root.
  179. ret = []
  180. for pid in pids():
  181. try:
  182. cons = Process(pid).net_connections(kind)
  183. except NoSuchProcess:
  184. continue
  185. else:
  186. if cons:
  187. for c in cons:
  188. c = list(c) + [pid]
  189. ret.append(ntp.sconn(*c))
  190. return ret
  191. def net_if_stats():
  192. """Get NIC stats (isup, duplex, speed, mtu)."""
  193. names = net_io_counters().keys()
  194. ret = {}
  195. for name in names:
  196. try:
  197. mtu = cext.net_if_mtu(name)
  198. flags = cext.net_if_flags(name)
  199. duplex, speed = cext.net_if_duplex_speed(name)
  200. except OSError as err:
  201. # https://github.com/giampaolo/psutil/issues/1279
  202. if err.errno != errno.ENODEV:
  203. raise
  204. else:
  205. if hasattr(_common, 'NicDuplex'):
  206. duplex = _common.NicDuplex(duplex)
  207. output_flags = ','.join(flags)
  208. isup = 'running' in flags
  209. ret[name] = ntp.snicstats(isup, duplex, speed, mtu, output_flags)
  210. return ret
  211. # =====================================================================
  212. # --- other system functions
  213. # =====================================================================
  214. def boot_time():
  215. """The system boot time expressed in seconds since the epoch."""
  216. return cext.boot_time()
  217. try:
  218. INIT_BOOT_TIME = boot_time()
  219. except Exception as err: # noqa: BLE001
  220. # Don't want to crash at import time.
  221. debug(f"ignoring exception on import: {err!r}")
  222. INIT_BOOT_TIME = 0
  223. def adjust_proc_create_time(ctime):
  224. """Account for system clock updates."""
  225. if INIT_BOOT_TIME == 0:
  226. return ctime
  227. diff = INIT_BOOT_TIME - boot_time()
  228. if diff == 0 or abs(diff) < 1:
  229. return ctime
  230. debug("system clock was updated; adjusting process create_time()")
  231. if diff < 0:
  232. return ctime - diff
  233. return ctime + diff
  234. def users():
  235. """Return currently connected users as a list of namedtuples."""
  236. retlist = []
  237. rawlist = cext.users()
  238. for item in rawlist:
  239. user, tty, hostname, tstamp, pid = item
  240. if tty == '~':
  241. continue # reboot or shutdown
  242. if not tstamp:
  243. continue
  244. nt = ntp.suser(user, tty or None, hostname or None, tstamp, pid)
  245. retlist.append(nt)
  246. return retlist
  247. # =====================================================================
  248. # --- processes
  249. # =====================================================================
  250. def pids():
  251. ls = cext.pids()
  252. if 0 not in ls:
  253. # On certain macOS versions pids() C doesn't return PID 0 but
  254. # "ps" does and the process is querable via sysctl():
  255. # https://travis-ci.org/giampaolo/psutil/jobs/309619941
  256. try:
  257. Process(0).create_time()
  258. ls.insert(0, 0)
  259. except NoSuchProcess:
  260. pass
  261. except AccessDenied:
  262. ls.insert(0, 0)
  263. return ls
  264. pid_exists = _psposix.pid_exists
  265. def wrap_exceptions(fun):
  266. """Decorator which translates bare OSError exceptions into
  267. NoSuchProcess and AccessDenied.
  268. """
  269. @functools.wraps(fun)
  270. def wrapper(self, *args, **kwargs):
  271. pid, ppid, name = self.pid, self._ppid, self._name
  272. try:
  273. return fun(self, *args, **kwargs)
  274. except ProcessLookupError as err:
  275. if cext.proc_is_zombie(pid):
  276. raise ZombieProcess(pid, name, ppid) from err
  277. raise NoSuchProcess(pid, name) from err
  278. except PermissionError as err:
  279. raise AccessDenied(pid, name) from err
  280. except cext.ZombieProcessError as err:
  281. raise ZombieProcess(pid, name, ppid) from err
  282. return wrapper
  283. class Process:
  284. """Wrapper class around underlying C implementation."""
  285. __slots__ = ["_cache", "_name", "_ppid", "pid"]
  286. def __init__(self, pid):
  287. self.pid = pid
  288. self._name = None
  289. self._ppid = None
  290. @wrap_exceptions
  291. @memoize_when_activated
  292. def _get_kinfo_proc(self):
  293. # Note: should work with all PIDs without permission issues.
  294. ret = cext.proc_kinfo_oneshot(self.pid)
  295. assert len(ret) == len(kinfo_proc_map)
  296. return ret
  297. @wrap_exceptions
  298. @memoize_when_activated
  299. def _get_pidtaskinfo(self):
  300. # Note: should work for PIDs owned by user only.
  301. ret = cext.proc_pidtaskinfo_oneshot(self.pid)
  302. assert len(ret) == len(pidtaskinfo_map)
  303. return ret
  304. def oneshot_enter(self):
  305. self._get_kinfo_proc.cache_activate(self)
  306. self._get_pidtaskinfo.cache_activate(self)
  307. def oneshot_exit(self):
  308. self._get_kinfo_proc.cache_deactivate(self)
  309. self._get_pidtaskinfo.cache_deactivate(self)
  310. @wrap_exceptions
  311. def name(self):
  312. name = self._get_kinfo_proc()[kinfo_proc_map['name']]
  313. return name if name is not None else cext.proc_name(self.pid)
  314. @wrap_exceptions
  315. def exe(self):
  316. return cext.proc_exe(self.pid)
  317. @wrap_exceptions
  318. def cmdline(self):
  319. return cext.proc_cmdline(self.pid)
  320. @wrap_exceptions
  321. def environ(self):
  322. return parse_environ_block(cext.proc_environ(self.pid))
  323. @wrap_exceptions
  324. def ppid(self):
  325. self._ppid = self._get_kinfo_proc()[kinfo_proc_map['ppid']]
  326. return self._ppid
  327. @wrap_exceptions
  328. def cwd(self):
  329. return cext.proc_cwd(self.pid)
  330. @wrap_exceptions
  331. def uids(self):
  332. rawtuple = self._get_kinfo_proc()
  333. return ntp.puids(
  334. rawtuple[kinfo_proc_map['ruid']],
  335. rawtuple[kinfo_proc_map['euid']],
  336. rawtuple[kinfo_proc_map['suid']],
  337. )
  338. @wrap_exceptions
  339. def gids(self):
  340. rawtuple = self._get_kinfo_proc()
  341. return ntp.puids(
  342. rawtuple[kinfo_proc_map['rgid']],
  343. rawtuple[kinfo_proc_map['egid']],
  344. rawtuple[kinfo_proc_map['sgid']],
  345. )
  346. @wrap_exceptions
  347. def terminal(self):
  348. tty_nr = self._get_kinfo_proc()[kinfo_proc_map['ttynr']]
  349. tmap = _psposix.get_terminal_map()
  350. try:
  351. return tmap[tty_nr]
  352. except KeyError:
  353. return None
  354. @wrap_exceptions
  355. def memory_info(self):
  356. rawtuple = self._get_pidtaskinfo()
  357. return ntp.pmem(
  358. rawtuple[pidtaskinfo_map['rss']],
  359. rawtuple[pidtaskinfo_map['vms']],
  360. rawtuple[pidtaskinfo_map['pfaults']],
  361. rawtuple[pidtaskinfo_map['pageins']],
  362. )
  363. @wrap_exceptions
  364. def memory_full_info(self):
  365. basic_mem = self.memory_info()
  366. uss = cext.proc_memory_uss(self.pid)
  367. return ntp.pfullmem(*basic_mem + (uss,))
  368. @wrap_exceptions
  369. def cpu_times(self):
  370. rawtuple = self._get_pidtaskinfo()
  371. return ntp.pcputimes(
  372. rawtuple[pidtaskinfo_map['cpuutime']],
  373. rawtuple[pidtaskinfo_map['cpustime']],
  374. # children user / system times are not retrievable (set to 0)
  375. 0.0,
  376. 0.0,
  377. )
  378. @wrap_exceptions
  379. def create_time(self, monotonic=False):
  380. ctime = self._get_kinfo_proc()[kinfo_proc_map['ctime']]
  381. if not monotonic:
  382. ctime = adjust_proc_create_time(ctime)
  383. return ctime
  384. @wrap_exceptions
  385. def num_ctx_switches(self):
  386. # Unvoluntary value seems not to be available;
  387. # getrusage() numbers seems to confirm this theory.
  388. # We set it to 0.
  389. vol = self._get_pidtaskinfo()[pidtaskinfo_map['volctxsw']]
  390. return ntp.pctxsw(vol, 0)
  391. @wrap_exceptions
  392. def num_threads(self):
  393. return self._get_pidtaskinfo()[pidtaskinfo_map['numthreads']]
  394. @wrap_exceptions
  395. def open_files(self):
  396. if self.pid == 0:
  397. return []
  398. files = []
  399. rawlist = cext.proc_open_files(self.pid)
  400. for path, fd in rawlist:
  401. if isfile_strict(path):
  402. ntuple = ntp.popenfile(path, fd)
  403. files.append(ntuple)
  404. return files
  405. @wrap_exceptions
  406. def net_connections(self, kind='inet'):
  407. families, types = conn_tmap[kind]
  408. rawlist = cext.proc_net_connections(self.pid, families, types)
  409. ret = []
  410. for item in rawlist:
  411. fd, fam, type, laddr, raddr, status = item
  412. nt = conn_to_ntuple(
  413. fd, fam, type, laddr, raddr, status, TCP_STATUSES
  414. )
  415. ret.append(nt)
  416. return ret
  417. @wrap_exceptions
  418. def num_fds(self):
  419. if self.pid == 0:
  420. return 0
  421. return cext.proc_num_fds(self.pid)
  422. @wrap_exceptions
  423. def wait(self, timeout=None):
  424. return _psposix.wait_pid(self.pid, timeout)
  425. @wrap_exceptions
  426. def nice_get(self):
  427. return cext.proc_priority_get(self.pid)
  428. @wrap_exceptions
  429. def nice_set(self, value):
  430. return cext.proc_priority_set(self.pid, value)
  431. @wrap_exceptions
  432. def status(self):
  433. code = self._get_kinfo_proc()[kinfo_proc_map['status']]
  434. # XXX is '?' legit? (we're not supposed to return it anyway)
  435. return PROC_STATUSES.get(code, '?')
  436. @wrap_exceptions
  437. def threads(self):
  438. rawlist = cext.proc_threads(self.pid)
  439. retlist = []
  440. for thread_id, utime, stime in rawlist:
  441. ntuple = ntp.pthread(thread_id, utime, stime)
  442. retlist.append(ntuple)
  443. return retlist