__init__.py 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  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. """psutil is a cross-platform library for retrieving information on
  5. running processes and system utilization (CPU, memory, disks, network,
  6. sensors) in Python. Supported platforms:
  7. - Linux
  8. - Windows
  9. - macOS
  10. - FreeBSD
  11. - OpenBSD
  12. - NetBSD
  13. - Sun Solaris
  14. - AIX
  15. Supported Python versions are cPython 3.6+ and PyPy.
  16. """
  17. import collections
  18. import contextlib
  19. import datetime
  20. import functools
  21. import os
  22. import signal
  23. import socket
  24. import subprocess
  25. import sys
  26. import threading
  27. import time
  28. try:
  29. import pwd
  30. except ImportError:
  31. pwd = None
  32. from . import _common
  33. from . import _ntuples as _ntp
  34. from ._common import AIX
  35. from ._common import BSD
  36. from ._common import CONN_CLOSE
  37. from ._common import CONN_CLOSE_WAIT
  38. from ._common import CONN_CLOSING
  39. from ._common import CONN_ESTABLISHED
  40. from ._common import CONN_FIN_WAIT1
  41. from ._common import CONN_FIN_WAIT2
  42. from ._common import CONN_LAST_ACK
  43. from ._common import CONN_LISTEN
  44. from ._common import CONN_NONE
  45. from ._common import CONN_SYN_RECV
  46. from ._common import CONN_SYN_SENT
  47. from ._common import CONN_TIME_WAIT
  48. from ._common import FREEBSD
  49. from ._common import LINUX
  50. from ._common import MACOS
  51. from ._common import NETBSD
  52. from ._common import NIC_DUPLEX_FULL
  53. from ._common import NIC_DUPLEX_HALF
  54. from ._common import NIC_DUPLEX_UNKNOWN
  55. from ._common import OPENBSD
  56. from ._common import OSX # deprecated alias
  57. from ._common import POSIX
  58. from ._common import POWER_TIME_UNKNOWN
  59. from ._common import POWER_TIME_UNLIMITED
  60. from ._common import STATUS_DEAD
  61. from ._common import STATUS_DISK_SLEEP
  62. from ._common import STATUS_IDLE
  63. from ._common import STATUS_LOCKED
  64. from ._common import STATUS_PARKED
  65. from ._common import STATUS_RUNNING
  66. from ._common import STATUS_SLEEPING
  67. from ._common import STATUS_STOPPED
  68. from ._common import STATUS_TRACING_STOP
  69. from ._common import STATUS_WAITING
  70. from ._common import STATUS_WAKING
  71. from ._common import STATUS_ZOMBIE
  72. from ._common import SUNOS
  73. from ._common import WINDOWS
  74. from ._common import AccessDenied
  75. from ._common import Error
  76. from ._common import NoSuchProcess
  77. from ._common import TimeoutExpired
  78. from ._common import ZombieProcess
  79. from ._common import debug
  80. from ._common import memoize_when_activated
  81. from ._common import wrap_numbers as _wrap_numbers
  82. if LINUX:
  83. # This is public API and it will be retrieved from _pslinux.py
  84. # via sys.modules.
  85. PROCFS_PATH = "/proc"
  86. from . import _pslinux as _psplatform
  87. from ._pslinux import IOPRIO_CLASS_BE # noqa: F401
  88. from ._pslinux import IOPRIO_CLASS_IDLE # noqa: F401
  89. from ._pslinux import IOPRIO_CLASS_NONE # noqa: F401
  90. from ._pslinux import IOPRIO_CLASS_RT # noqa: F401
  91. elif WINDOWS:
  92. from . import _pswindows as _psplatform
  93. from ._psutil_windows import ABOVE_NORMAL_PRIORITY_CLASS # noqa: F401
  94. from ._psutil_windows import BELOW_NORMAL_PRIORITY_CLASS # noqa: F401
  95. from ._psutil_windows import HIGH_PRIORITY_CLASS # noqa: F401
  96. from ._psutil_windows import IDLE_PRIORITY_CLASS # noqa: F401
  97. from ._psutil_windows import NORMAL_PRIORITY_CLASS # noqa: F401
  98. from ._psutil_windows import REALTIME_PRIORITY_CLASS # noqa: F401
  99. from ._pswindows import CONN_DELETE_TCB # noqa: F401
  100. from ._pswindows import IOPRIO_HIGH # noqa: F401
  101. from ._pswindows import IOPRIO_LOW # noqa: F401
  102. from ._pswindows import IOPRIO_NORMAL # noqa: F401
  103. from ._pswindows import IOPRIO_VERYLOW # noqa: F401
  104. elif MACOS:
  105. from . import _psosx as _psplatform
  106. elif BSD:
  107. from . import _psbsd as _psplatform
  108. elif SUNOS:
  109. from . import _pssunos as _psplatform
  110. from ._pssunos import CONN_BOUND # noqa: F401
  111. from ._pssunos import CONN_IDLE # noqa: F401
  112. # This is public writable API which is read from _pslinux.py and
  113. # _pssunos.py via sys.modules.
  114. PROCFS_PATH = "/proc"
  115. elif AIX:
  116. from . import _psaix as _psplatform
  117. # This is public API and it will be retrieved from _pslinux.py
  118. # via sys.modules.
  119. PROCFS_PATH = "/proc"
  120. else: # pragma: no cover
  121. msg = f"platform {sys.platform} is not supported"
  122. raise NotImplementedError(msg)
  123. # fmt: off
  124. __all__ = [
  125. # exceptions
  126. "Error", "NoSuchProcess", "ZombieProcess", "AccessDenied",
  127. "TimeoutExpired",
  128. # constants
  129. "version_info", "__version__",
  130. "STATUS_RUNNING", "STATUS_IDLE", "STATUS_SLEEPING", "STATUS_DISK_SLEEP",
  131. "STATUS_STOPPED", "STATUS_TRACING_STOP", "STATUS_ZOMBIE", "STATUS_DEAD",
  132. "STATUS_WAKING", "STATUS_LOCKED", "STATUS_WAITING", "STATUS_LOCKED",
  133. "STATUS_PARKED",
  134. "CONN_ESTABLISHED", "CONN_SYN_SENT", "CONN_SYN_RECV", "CONN_FIN_WAIT1",
  135. "CONN_FIN_WAIT2", "CONN_TIME_WAIT", "CONN_CLOSE", "CONN_CLOSE_WAIT",
  136. "CONN_LAST_ACK", "CONN_LISTEN", "CONN_CLOSING", "CONN_NONE",
  137. # "CONN_IDLE", "CONN_BOUND",
  138. "AF_LINK",
  139. "NIC_DUPLEX_FULL", "NIC_DUPLEX_HALF", "NIC_DUPLEX_UNKNOWN",
  140. "POWER_TIME_UNKNOWN", "POWER_TIME_UNLIMITED",
  141. "BSD", "FREEBSD", "LINUX", "NETBSD", "OPENBSD", "MACOS", "OSX", "POSIX",
  142. "SUNOS", "WINDOWS", "AIX",
  143. # "RLIM_INFINITY", "RLIMIT_AS", "RLIMIT_CORE", "RLIMIT_CPU", "RLIMIT_DATA",
  144. # "RLIMIT_FSIZE", "RLIMIT_LOCKS", "RLIMIT_MEMLOCK", "RLIMIT_NOFILE",
  145. # "RLIMIT_NPROC", "RLIMIT_RSS", "RLIMIT_STACK", "RLIMIT_MSGQUEUE",
  146. # "RLIMIT_NICE", "RLIMIT_RTPRIO", "RLIMIT_RTTIME", "RLIMIT_SIGPENDING",
  147. # classes
  148. "Process", "Popen",
  149. # functions
  150. "pid_exists", "pids", "process_iter", "wait_procs", # proc
  151. "virtual_memory", "swap_memory", # memory
  152. "cpu_times", "cpu_percent", "cpu_times_percent", "cpu_count", # cpu
  153. "cpu_stats", # "cpu_freq", "getloadavg"
  154. "net_io_counters", "net_connections", "net_if_addrs", # network
  155. "net_if_stats",
  156. "disk_io_counters", "disk_partitions", "disk_usage", # disk
  157. # "sensors_temperatures", "sensors_battery", "sensors_fans" # sensors
  158. "users", "boot_time", # others
  159. ]
  160. # fmt: on
  161. __all__.extend(_psplatform.__extra__all__)
  162. # Linux, FreeBSD
  163. if hasattr(_psplatform.Process, "rlimit"):
  164. # Populate global namespace with RLIM* constants.
  165. _globals = globals()
  166. _name = None
  167. for _name in dir(_psplatform.cext):
  168. if _name.startswith('RLIM') and _name.isupper():
  169. _globals[_name] = getattr(_psplatform.cext, _name)
  170. __all__.append(_name)
  171. del _globals, _name
  172. AF_LINK = _psplatform.AF_LINK
  173. __author__ = "Giampaolo Rodola'"
  174. __version__ = "7.2.2"
  175. version_info = tuple(int(num) for num in __version__.split('.'))
  176. _timer = getattr(time, 'monotonic', time.time)
  177. _TOTAL_PHYMEM = None
  178. _LOWEST_PID = None
  179. _SENTINEL = object()
  180. # Sanity check in case the user messed up with psutil installation
  181. # or did something weird with sys.path. In this case we might end
  182. # up importing a python module using a C extension module which
  183. # was compiled for a different version of psutil.
  184. # We want to prevent that by failing sooner rather than later.
  185. # See: https://github.com/giampaolo/psutil/issues/564
  186. if int(__version__.replace('.', '')) != getattr(
  187. _psplatform.cext, 'version', None
  188. ):
  189. msg = f"version conflict: {_psplatform.cext.__file__!r} C extension "
  190. msg += "module was built for another version of psutil"
  191. if hasattr(_psplatform.cext, 'version'):
  192. v = ".".join(list(str(_psplatform.cext.version)))
  193. msg += f" ({v} instead of {__version__})"
  194. else:
  195. msg += f" (different than {__version__})"
  196. what = getattr(
  197. _psplatform.cext,
  198. "__file__",
  199. "the existing psutil install directory",
  200. )
  201. msg += f"; you may try to 'pip uninstall psutil', manually remove {what}"
  202. msg += " or clean the virtual env somehow, then reinstall"
  203. raise ImportError(msg)
  204. # =====================================================================
  205. # --- Utils
  206. # =====================================================================
  207. if hasattr(_psplatform, 'ppid_map'):
  208. # Faster version (Windows and Linux).
  209. _ppid_map = _psplatform.ppid_map
  210. else: # pragma: no cover
  211. def _ppid_map():
  212. """Return a {pid: ppid, ...} dict for all running processes in
  213. one shot. Used to speed up Process.children().
  214. """
  215. ret = {}
  216. for pid in pids():
  217. try:
  218. ret[pid] = _psplatform.Process(pid).ppid()
  219. except (NoSuchProcess, ZombieProcess):
  220. pass
  221. return ret
  222. def _pprint_secs(secs):
  223. """Format seconds in a human readable form."""
  224. now = time.time()
  225. secs_ago = int(now - secs)
  226. fmt = "%H:%M:%S" if secs_ago < 60 * 60 * 24 else "%Y-%m-%d %H:%M:%S"
  227. return datetime.datetime.fromtimestamp(secs).strftime(fmt)
  228. def _check_conn_kind(kind):
  229. """Check net_connections()'s `kind` parameter."""
  230. kinds = tuple(_common.conn_tmap)
  231. if kind not in kinds:
  232. msg = f"invalid kind argument {kind!r}; valid ones are: {kinds}"
  233. raise ValueError(msg)
  234. # =====================================================================
  235. # --- Process class
  236. # =====================================================================
  237. class Process:
  238. """Represents an OS process with the given PID.
  239. If PID is omitted current process PID (os.getpid()) is used.
  240. Raise NoSuchProcess if PID does not exist.
  241. Note that most of the methods of this class do not make sure that
  242. the PID of the process being queried has been reused. That means
  243. that you may end up retrieving information for another process.
  244. The only exceptions for which process identity is pre-emptively
  245. checked and guaranteed are:
  246. - parent()
  247. - children()
  248. - nice() (set)
  249. - ionice() (set)
  250. - rlimit() (set)
  251. - cpu_affinity (set)
  252. - suspend()
  253. - resume()
  254. - send_signal()
  255. - terminate()
  256. - kill()
  257. To prevent this problem for all other methods you can use
  258. is_running() before querying the process.
  259. """
  260. def __init__(self, pid=None):
  261. self._init(pid)
  262. def _init(self, pid, _ignore_nsp=False):
  263. if pid is None:
  264. pid = os.getpid()
  265. else:
  266. if pid < 0:
  267. msg = f"pid must be a positive integer (got {pid})"
  268. raise ValueError(msg)
  269. try:
  270. _psplatform.cext.check_pid_range(pid)
  271. except OverflowError as err:
  272. msg = "process PID out of range"
  273. raise NoSuchProcess(pid, msg=msg) from err
  274. self._pid = pid
  275. self._name = None
  276. self._exe = None
  277. self._create_time = None
  278. self._gone = False
  279. self._pid_reused = False
  280. self._hash = None
  281. self._lock = threading.RLock()
  282. # used for caching on Windows only (on POSIX ppid may change)
  283. self._ppid = None
  284. # platform-specific modules define an _psplatform.Process
  285. # implementation class
  286. self._proc = _psplatform.Process(pid)
  287. self._last_sys_cpu_times = None
  288. self._last_proc_cpu_times = None
  289. self._exitcode = _SENTINEL
  290. self._ident = (self.pid, None)
  291. try:
  292. self._ident = self._get_ident()
  293. except AccessDenied:
  294. # This should happen on Windows only, since we use the fast
  295. # create time method. AFAIK, on all other platforms we are
  296. # able to get create time for all PIDs.
  297. pass
  298. except ZombieProcess:
  299. # Zombies can still be queried by this class (although
  300. # not always) and pids() return them so just go on.
  301. pass
  302. except NoSuchProcess:
  303. if not _ignore_nsp:
  304. msg = "process PID not found"
  305. raise NoSuchProcess(pid, msg=msg) from None
  306. self._gone = True
  307. def _get_ident(self):
  308. """Return a (pid, uid) tuple which is supposed to identify a
  309. Process instance univocally over time. The PID alone is not
  310. enough, as it can be assigned to a new process after this one
  311. terminates, so we add process creation time to the mix. We need
  312. this in order to prevent killing the wrong process later on.
  313. This is also known as PID reuse or PID recycling problem.
  314. The reliability of this strategy mostly depends on
  315. create_time() precision, which is 0.01 secs on Linux. The
  316. assumption is that, after a process terminates, the kernel
  317. won't reuse the same PID after such a short period of time
  318. (0.01 secs). Technically this is inherently racy, but
  319. practically it should be good enough.
  320. NOTE: unreliable on FreeBSD and OpenBSD as ctime is subject to
  321. system clock updates.
  322. """
  323. if WINDOWS:
  324. # Use create_time() fast method in order to speedup
  325. # `process_iter()`. This means we'll get AccessDenied for
  326. # most ADMIN processes, but that's fine since it means
  327. # we'll also get AccessDenied on kill().
  328. # https://github.com/giampaolo/psutil/issues/2366#issuecomment-2381646555
  329. self._create_time = self._proc.create_time(fast_only=True)
  330. return (self.pid, self._create_time)
  331. elif LINUX or NETBSD or OSX:
  332. # Use 'monotonic' process starttime since boot to form unique
  333. # process identity, since it is stable over changes to system
  334. # time.
  335. return (self.pid, self._proc.create_time(monotonic=True))
  336. else:
  337. return (self.pid, self.create_time())
  338. def __str__(self):
  339. info = collections.OrderedDict()
  340. info["pid"] = self.pid
  341. if self._name:
  342. info['name'] = self._name
  343. with self.oneshot():
  344. if self._pid_reused:
  345. info["status"] = "terminated + PID reused"
  346. else:
  347. try:
  348. info["name"] = self.name()
  349. info["status"] = self.status()
  350. except ZombieProcess:
  351. info["status"] = "zombie"
  352. except NoSuchProcess:
  353. info["status"] = "terminated"
  354. except AccessDenied:
  355. pass
  356. if self._exitcode not in {_SENTINEL, None}:
  357. info["exitcode"] = self._exitcode
  358. if self._create_time is not None:
  359. info['started'] = _pprint_secs(self._create_time)
  360. return "{}.{}({})".format(
  361. self.__class__.__module__,
  362. self.__class__.__name__,
  363. ", ".join([f"{k}={v!r}" for k, v in info.items()]),
  364. )
  365. __repr__ = __str__
  366. def __eq__(self, other):
  367. # Test for equality with another Process object based
  368. # on PID and creation time.
  369. if not isinstance(other, Process):
  370. return NotImplemented
  371. if OPENBSD or NETBSD or SUNOS: # pragma: no cover
  372. # Zombie processes on Open/NetBSD/illumos/Solaris have a
  373. # creation time of 0.0. This covers the case when a process
  374. # started normally (so it has a ctime), then it turned into a
  375. # zombie. It's important to do this because is_running()
  376. # depends on __eq__.
  377. pid1, ident1 = self._ident
  378. pid2, ident2 = other._ident
  379. if pid1 == pid2:
  380. if ident1 and not ident2:
  381. try:
  382. return self.status() == STATUS_ZOMBIE
  383. except Error:
  384. pass
  385. return self._ident == other._ident
  386. def __ne__(self, other):
  387. return not self == other
  388. def __hash__(self):
  389. if self._hash is None:
  390. self._hash = hash(self._ident)
  391. return self._hash
  392. def _raise_if_pid_reused(self):
  393. """Raises NoSuchProcess in case process PID has been reused."""
  394. if self._pid_reused or (not self.is_running() and self._pid_reused):
  395. # We may directly raise NSP in here already if PID is just
  396. # not running, but I prefer NSP to be raised naturally by
  397. # the actual Process API call. This way unit tests will tell
  398. # us if the API is broken (aka don't raise NSP when it
  399. # should). We also remain consistent with all other "get"
  400. # APIs which don't use _raise_if_pid_reused().
  401. msg = "process no longer exists and its PID has been reused"
  402. raise NoSuchProcess(self.pid, self._name, msg=msg)
  403. @property
  404. def pid(self):
  405. """The process PID."""
  406. return self._pid
  407. # --- utility methods
  408. @contextlib.contextmanager
  409. def oneshot(self):
  410. """Utility context manager which considerably speeds up the
  411. retrieval of multiple process information at the same time.
  412. Internally different process info (e.g. name, ppid, uids,
  413. gids, ...) may be fetched by using the same routine, but
  414. only one information is returned and the others are discarded.
  415. When using this context manager the internal routine is
  416. executed once (in the example below on name()) and the
  417. other info are cached.
  418. The cache is cleared when exiting the context manager block.
  419. The advice is to use this every time you retrieve more than
  420. one information about the process. If you're lucky, you'll
  421. get a hell of a speedup.
  422. >>> import psutil
  423. >>> p = psutil.Process()
  424. >>> with p.oneshot():
  425. ... p.name() # collect multiple info
  426. ... p.cpu_times() # return cached value
  427. ... p.cpu_percent() # return cached value
  428. ... p.create_time() # return cached value
  429. ...
  430. >>>
  431. """
  432. with self._lock:
  433. if hasattr(self, "_cache"):
  434. # NOOP: this covers the use case where the user enters the
  435. # context twice:
  436. #
  437. # >>> with p.oneshot():
  438. # ... with p.oneshot():
  439. # ...
  440. #
  441. # Also, since as_dict() internally uses oneshot()
  442. # I expect that the code below will be a pretty common
  443. # "mistake" that the user will make, so let's guard
  444. # against that:
  445. #
  446. # >>> with p.oneshot():
  447. # ... p.as_dict()
  448. # ...
  449. yield
  450. else:
  451. try:
  452. # cached in case cpu_percent() is used
  453. self.cpu_times.cache_activate(self)
  454. # cached in case memory_percent() is used
  455. self.memory_info.cache_activate(self)
  456. # cached in case parent() is used
  457. self.ppid.cache_activate(self)
  458. # cached in case username() is used
  459. if POSIX:
  460. self.uids.cache_activate(self)
  461. # specific implementation cache
  462. self._proc.oneshot_enter()
  463. yield
  464. finally:
  465. self.cpu_times.cache_deactivate(self)
  466. self.memory_info.cache_deactivate(self)
  467. self.ppid.cache_deactivate(self)
  468. if POSIX:
  469. self.uids.cache_deactivate(self)
  470. self._proc.oneshot_exit()
  471. def as_dict(self, attrs=None, ad_value=None):
  472. """Utility method returning process information as a
  473. hashable dictionary.
  474. If *attrs* is specified it must be a list of strings
  475. reflecting available Process class' attribute names
  476. (e.g. ['cpu_times', 'name']) else all public (read
  477. only) attributes are assumed.
  478. *ad_value* is the value which gets assigned in case
  479. AccessDenied or ZombieProcess exception is raised when
  480. retrieving that particular process information.
  481. """
  482. valid_names = _as_dict_attrnames
  483. if attrs is not None:
  484. if not isinstance(attrs, (list, tuple, set, frozenset)):
  485. msg = f"invalid attrs type {type(attrs)}"
  486. raise TypeError(msg)
  487. attrs = set(attrs)
  488. invalid_names = attrs - valid_names
  489. if invalid_names:
  490. msg = "invalid attr name{} {}".format(
  491. "s" if len(invalid_names) > 1 else "",
  492. ", ".join(map(repr, invalid_names)),
  493. )
  494. raise ValueError(msg)
  495. retdict = {}
  496. ls = attrs or valid_names
  497. with self.oneshot():
  498. for name in ls:
  499. try:
  500. if name == 'pid':
  501. ret = self.pid
  502. else:
  503. meth = getattr(self, name)
  504. ret = meth()
  505. except (AccessDenied, ZombieProcess):
  506. ret = ad_value
  507. except NotImplementedError:
  508. # in case of not implemented functionality (may happen
  509. # on old or exotic systems) we want to crash only if
  510. # the user explicitly asked for that particular attr
  511. if attrs:
  512. raise
  513. continue
  514. retdict[name] = ret
  515. return retdict
  516. def parent(self):
  517. """Return the parent process as a Process object pre-emptively
  518. checking whether PID has been reused.
  519. If no parent is known return None.
  520. """
  521. lowest_pid = _LOWEST_PID if _LOWEST_PID is not None else pids()[0]
  522. if self.pid == lowest_pid:
  523. return None
  524. ppid = self.ppid()
  525. if ppid is not None:
  526. # Get a fresh (non-cached) ctime in case the system clock
  527. # was updated. TODO: use a monotonic ctime on platforms
  528. # where it's supported.
  529. proc_ctime = Process(self.pid).create_time()
  530. try:
  531. parent = Process(ppid)
  532. if parent.create_time() <= proc_ctime:
  533. return parent
  534. # ...else ppid has been reused by another process
  535. except NoSuchProcess:
  536. pass
  537. def parents(self):
  538. """Return the parents of this process as a list of Process
  539. instances. If no parents are known return an empty list.
  540. """
  541. parents = []
  542. proc = self.parent()
  543. while proc is not None:
  544. parents.append(proc)
  545. proc = proc.parent()
  546. return parents
  547. def is_running(self):
  548. """Return whether this process is running.
  549. It also checks if PID has been reused by another process, in
  550. which case it will remove the process from `process_iter()`
  551. internal cache and return False.
  552. """
  553. if self._gone or self._pid_reused:
  554. return False
  555. try:
  556. # Checking if PID is alive is not enough as the PID might
  557. # have been reused by another process. Process identity /
  558. # uniqueness over time is guaranteed by (PID + creation
  559. # time) and that is verified in __eq__.
  560. self._pid_reused = self != Process(self.pid)
  561. if self._pid_reused:
  562. _pids_reused.add(self.pid)
  563. raise NoSuchProcess(self.pid)
  564. return True
  565. except ZombieProcess:
  566. # We should never get here as it's already handled in
  567. # Process.__init__; here just for extra safety.
  568. return True
  569. except NoSuchProcess:
  570. self._gone = True
  571. return False
  572. # --- actual API
  573. @memoize_when_activated
  574. def ppid(self):
  575. """The process parent PID.
  576. On Windows the return value is cached after first call.
  577. """
  578. # On POSIX we don't want to cache the ppid as it may unexpectedly
  579. # change to 1 (init) in case this process turns into a zombie:
  580. # https://github.com/giampaolo/psutil/issues/321
  581. # http://stackoverflow.com/questions/356722/
  582. # XXX should we check creation time here rather than in
  583. # Process.parent()?
  584. self._raise_if_pid_reused()
  585. if POSIX:
  586. return self._proc.ppid()
  587. else: # pragma: no cover
  588. self._ppid = self._ppid or self._proc.ppid()
  589. return self._ppid
  590. def name(self):
  591. """The process name. The return value is cached after first call."""
  592. # Process name is only cached on Windows as on POSIX it may
  593. # change, see:
  594. # https://github.com/giampaolo/psutil/issues/692
  595. if WINDOWS and self._name is not None:
  596. return self._name
  597. name = self._proc.name()
  598. if POSIX and len(name) >= 15:
  599. # On UNIX the name gets truncated to the first 15 characters.
  600. # If it matches the first part of the cmdline we return that
  601. # one instead because it's usually more explicative.
  602. # Examples are "gnome-keyring-d" vs. "gnome-keyring-daemon".
  603. try:
  604. cmdline = self.cmdline()
  605. except (AccessDenied, ZombieProcess):
  606. # Just pass and return the truncated name: it's better
  607. # than nothing. Note: there are actual cases where a
  608. # zombie process can return a name() but not a
  609. # cmdline(), see:
  610. # https://github.com/giampaolo/psutil/issues/2239
  611. pass
  612. else:
  613. if cmdline:
  614. extended_name = os.path.basename(cmdline[0])
  615. if extended_name.startswith(name):
  616. name = extended_name
  617. self._name = name
  618. self._proc._name = name
  619. return name
  620. def exe(self):
  621. """The process executable as an absolute path.
  622. May also be an empty string.
  623. The return value is cached after first call.
  624. """
  625. def guess_it(fallback):
  626. # try to guess exe from cmdline[0] in absence of a native
  627. # exe representation
  628. cmdline = self.cmdline()
  629. if cmdline and hasattr(os, 'access') and hasattr(os, 'X_OK'):
  630. exe = cmdline[0] # the possible exe
  631. # Attempt to guess only in case of an absolute path.
  632. # It is not safe otherwise as the process might have
  633. # changed cwd.
  634. if (
  635. os.path.isabs(exe)
  636. and os.path.isfile(exe)
  637. and os.access(exe, os.X_OK)
  638. ):
  639. return exe
  640. if isinstance(fallback, AccessDenied):
  641. raise fallback
  642. return fallback
  643. if self._exe is None:
  644. try:
  645. exe = self._proc.exe()
  646. except AccessDenied as err:
  647. return guess_it(fallback=err)
  648. else:
  649. if not exe:
  650. # underlying implementation can legitimately return an
  651. # empty string; if that's the case we don't want to
  652. # raise AD while guessing from the cmdline
  653. try:
  654. exe = guess_it(fallback=exe)
  655. except AccessDenied:
  656. pass
  657. self._exe = exe
  658. return self._exe
  659. def cmdline(self):
  660. """The command line this process has been called with."""
  661. return self._proc.cmdline()
  662. def status(self):
  663. """The process current status as a STATUS_* constant."""
  664. try:
  665. return self._proc.status()
  666. except ZombieProcess:
  667. return STATUS_ZOMBIE
  668. def username(self):
  669. """The name of the user that owns the process.
  670. On UNIX this is calculated by using *real* process uid.
  671. """
  672. if POSIX:
  673. if pwd is None:
  674. # might happen if python was installed from sources
  675. msg = "requires pwd module shipped with standard python"
  676. raise ImportError(msg)
  677. real_uid = self.uids().real
  678. try:
  679. return pwd.getpwuid(real_uid).pw_name
  680. except KeyError:
  681. # the uid can't be resolved by the system
  682. return str(real_uid)
  683. else:
  684. return self._proc.username()
  685. def create_time(self):
  686. """The process creation time as a floating point number
  687. expressed in seconds since the epoch (seconds since January 1,
  688. 1970, at midnight UTC). The return value, which is cached after
  689. first call, is based on the system clock, which means it may be
  690. affected by changes such as manual adjustments or time
  691. synchronization (e.g. NTP).
  692. """
  693. if self._create_time is None:
  694. self._create_time = self._proc.create_time()
  695. return self._create_time
  696. def cwd(self):
  697. """Process current working directory as an absolute path."""
  698. return self._proc.cwd()
  699. def nice(self, value=None):
  700. """Get or set process niceness (priority)."""
  701. if value is None:
  702. return self._proc.nice_get()
  703. else:
  704. self._raise_if_pid_reused()
  705. self._proc.nice_set(value)
  706. if POSIX:
  707. @memoize_when_activated
  708. def uids(self):
  709. """Return process UIDs as a (real, effective, saved)
  710. namedtuple.
  711. """
  712. return self._proc.uids()
  713. def gids(self):
  714. """Return process GIDs as a (real, effective, saved)
  715. namedtuple.
  716. """
  717. return self._proc.gids()
  718. def terminal(self):
  719. """The terminal associated with this process, if any,
  720. else None.
  721. """
  722. return self._proc.terminal()
  723. def num_fds(self):
  724. """Return the number of file descriptors opened by this
  725. process (POSIX only).
  726. """
  727. return self._proc.num_fds()
  728. # Linux, BSD, AIX and Windows only
  729. if hasattr(_psplatform.Process, "io_counters"):
  730. def io_counters(self):
  731. """Return process I/O statistics as a
  732. (read_count, write_count, read_bytes, write_bytes)
  733. namedtuple.
  734. Those are the number of read/write calls performed and the
  735. amount of bytes read and written by the process.
  736. """
  737. return self._proc.io_counters()
  738. # Linux and Windows
  739. if hasattr(_psplatform.Process, "ionice_get"):
  740. def ionice(self, ioclass=None, value=None):
  741. """Get or set process I/O niceness (priority).
  742. On Linux *ioclass* is one of the IOPRIO_CLASS_* constants.
  743. *value* is a number which goes from 0 to 7. The higher the
  744. value, the lower the I/O priority of the process.
  745. On Windows only *ioclass* is used and it can be set to 2
  746. (normal), 1 (low) or 0 (very low).
  747. Available on Linux and Windows > Vista only.
  748. """
  749. if ioclass is None:
  750. if value is not None:
  751. msg = "'ioclass' argument must be specified"
  752. raise ValueError(msg)
  753. return self._proc.ionice_get()
  754. else:
  755. self._raise_if_pid_reused()
  756. return self._proc.ionice_set(ioclass, value)
  757. # Linux / FreeBSD only
  758. if hasattr(_psplatform.Process, "rlimit"):
  759. def rlimit(self, resource, limits=None):
  760. """Get or set process resource limits as a (soft, hard)
  761. tuple.
  762. *resource* is one of the RLIMIT_* constants.
  763. *limits* is supposed to be a (soft, hard) tuple.
  764. See "man prlimit" for further info.
  765. Available on Linux and FreeBSD only.
  766. """
  767. if limits is not None:
  768. self._raise_if_pid_reused()
  769. return self._proc.rlimit(resource, limits)
  770. # Windows, Linux and FreeBSD only
  771. if hasattr(_psplatform.Process, "cpu_affinity_get"):
  772. def cpu_affinity(self, cpus=None):
  773. """Get or set process CPU affinity.
  774. If specified, *cpus* must be a list of CPUs for which you
  775. want to set the affinity (e.g. [0, 1]).
  776. If an empty list is passed, all egible CPUs are assumed
  777. (and set).
  778. (Windows, Linux and BSD only).
  779. """
  780. if cpus is None:
  781. return sorted(set(self._proc.cpu_affinity_get()))
  782. else:
  783. self._raise_if_pid_reused()
  784. if not cpus:
  785. if hasattr(self._proc, "_get_eligible_cpus"):
  786. cpus = self._proc._get_eligible_cpus()
  787. else:
  788. cpus = tuple(range(len(cpu_times(percpu=True))))
  789. self._proc.cpu_affinity_set(list(set(cpus)))
  790. # Linux, FreeBSD, SunOS
  791. if hasattr(_psplatform.Process, "cpu_num"):
  792. def cpu_num(self):
  793. """Return what CPU this process is currently running on.
  794. The returned number should be <= psutil.cpu_count()
  795. and <= len(psutil.cpu_percent(percpu=True)).
  796. It may be used in conjunction with
  797. psutil.cpu_percent(percpu=True) to observe the system
  798. workload distributed across CPUs.
  799. """
  800. return self._proc.cpu_num()
  801. # All platforms has it, but maybe not in the future.
  802. if hasattr(_psplatform.Process, "environ"):
  803. def environ(self):
  804. """The environment variables of the process as a dict. Note: this
  805. might not reflect changes made after the process started.
  806. """
  807. return self._proc.environ()
  808. if WINDOWS:
  809. def num_handles(self):
  810. """Return the number of handles opened by this process
  811. (Windows only).
  812. """
  813. return self._proc.num_handles()
  814. def num_ctx_switches(self):
  815. """Return the number of voluntary and involuntary context
  816. switches performed by this process.
  817. """
  818. return self._proc.num_ctx_switches()
  819. def num_threads(self):
  820. """Return the number of threads used by this process."""
  821. return self._proc.num_threads()
  822. if hasattr(_psplatform.Process, "threads"):
  823. def threads(self):
  824. """Return threads opened by process as a list of
  825. (id, user_time, system_time) namedtuples representing
  826. thread id and thread CPU times (user/system).
  827. On OpenBSD this method requires root access.
  828. """
  829. return self._proc.threads()
  830. def children(self, recursive=False):
  831. """Return the children of this process as a list of Process
  832. instances, pre-emptively checking whether PID has been reused.
  833. If *recursive* is True return all the parent descendants.
  834. Example (A == this process):
  835. A ─┐
  836. ├─ B (child) ─┐
  837. │ └─ X (grandchild) ─┐
  838. │ └─ Y (great grandchild)
  839. ├─ C (child)
  840. └─ D (child)
  841. >>> import psutil
  842. >>> p = psutil.Process()
  843. >>> p.children()
  844. B, C, D
  845. >>> p.children(recursive=True)
  846. B, X, Y, C, D
  847. Note that in the example above if process X disappears
  848. process Y won't be listed as the reference to process A
  849. is lost.
  850. """
  851. self._raise_if_pid_reused()
  852. ppid_map = _ppid_map()
  853. # Get a fresh (non-cached) ctime in case the system clock was
  854. # updated. TODO: use a monotonic ctime on platforms where it's
  855. # supported.
  856. proc_ctime = Process(self.pid).create_time()
  857. ret = []
  858. if not recursive:
  859. for pid, ppid in ppid_map.items():
  860. if ppid == self.pid:
  861. try:
  862. child = Process(pid)
  863. # if child happens to be older than its parent
  864. # (self) it means child's PID has been reused
  865. if proc_ctime <= child.create_time():
  866. ret.append(child)
  867. except (NoSuchProcess, ZombieProcess):
  868. pass
  869. else:
  870. # Construct a {pid: [child pids]} dict
  871. reverse_ppid_map = collections.defaultdict(list)
  872. for pid, ppid in ppid_map.items():
  873. reverse_ppid_map[ppid].append(pid)
  874. # Recursively traverse that dict, starting from self.pid,
  875. # such that we only call Process() on actual children
  876. seen = set()
  877. stack = [self.pid]
  878. while stack:
  879. pid = stack.pop()
  880. if pid in seen:
  881. # Since pids can be reused while the ppid_map is
  882. # constructed, there may be rare instances where
  883. # there's a cycle in the recorded process "tree".
  884. continue
  885. seen.add(pid)
  886. for child_pid in reverse_ppid_map[pid]:
  887. try:
  888. child = Process(child_pid)
  889. # if child happens to be older than its parent
  890. # (self) it means child's PID has been reused
  891. intime = proc_ctime <= child.create_time()
  892. if intime:
  893. ret.append(child)
  894. stack.append(child_pid)
  895. except (NoSuchProcess, ZombieProcess):
  896. pass
  897. return ret
  898. def cpu_percent(self, interval=None):
  899. """Return a float representing the current process CPU
  900. utilization as a percentage.
  901. When *interval* is 0.0 or None (default) compares process times
  902. to system CPU times elapsed since last call, returning
  903. immediately (non-blocking). That means that the first time
  904. this is called it will return a meaningful 0.0 value.
  905. When *interval* is > 0.0 compares process times to system CPU
  906. times elapsed before and after the interval (blocking).
  907. In this case is recommended for accuracy that this function
  908. be called with at least 0.1 seconds between calls.
  909. A value > 100.0 can be returned in case of processes running
  910. multiple threads on different CPU cores.
  911. The returned value is explicitly NOT split evenly between
  912. all available logical CPUs. This means that a busy loop process
  913. running on a system with 2 logical CPUs will be reported as
  914. having 100% CPU utilization instead of 50%.
  915. Examples:
  916. >>> import psutil
  917. >>> p = psutil.Process(os.getpid())
  918. >>> # blocking
  919. >>> p.cpu_percent(interval=1)
  920. 2.0
  921. >>> # non-blocking (percentage since last call)
  922. >>> p.cpu_percent(interval=None)
  923. 2.9
  924. >>>
  925. """
  926. blocking = interval is not None and interval > 0.0
  927. if interval is not None and interval < 0:
  928. msg = f"interval is not positive (got {interval!r})"
  929. raise ValueError(msg)
  930. num_cpus = cpu_count() or 1
  931. def timer():
  932. return _timer() * num_cpus
  933. if blocking:
  934. st1 = timer()
  935. pt1 = self._proc.cpu_times()
  936. time.sleep(interval)
  937. st2 = timer()
  938. pt2 = self._proc.cpu_times()
  939. else:
  940. st1 = self._last_sys_cpu_times
  941. pt1 = self._last_proc_cpu_times
  942. st2 = timer()
  943. pt2 = self._proc.cpu_times()
  944. if st1 is None or pt1 is None:
  945. self._last_sys_cpu_times = st2
  946. self._last_proc_cpu_times = pt2
  947. return 0.0
  948. delta_proc = (pt2.user - pt1.user) + (pt2.system - pt1.system)
  949. delta_time = st2 - st1
  950. # reset values for next call in case of interval == None
  951. self._last_sys_cpu_times = st2
  952. self._last_proc_cpu_times = pt2
  953. try:
  954. # This is the utilization split evenly between all CPUs.
  955. # E.g. a busy loop process on a 2-CPU-cores system at this
  956. # point is reported as 50% instead of 100%.
  957. overall_cpus_percent = (delta_proc / delta_time) * 100
  958. except ZeroDivisionError:
  959. # interval was too low
  960. return 0.0
  961. else:
  962. # Note 1:
  963. # in order to emulate "top" we multiply the value for the num
  964. # of CPU cores. This way the busy process will be reported as
  965. # having 100% (or more) usage.
  966. #
  967. # Note 2:
  968. # taskmgr.exe on Windows differs in that it will show 50%
  969. # instead.
  970. #
  971. # Note 3:
  972. # a percentage > 100 is legitimate as it can result from a
  973. # process with multiple threads running on different CPU
  974. # cores (top does the same), see:
  975. # http://stackoverflow.com/questions/1032357
  976. # https://github.com/giampaolo/psutil/issues/474
  977. single_cpu_percent = overall_cpus_percent * num_cpus
  978. return round(single_cpu_percent, 1)
  979. @memoize_when_activated
  980. def cpu_times(self):
  981. """Return a (user, system, children_user, children_system)
  982. namedtuple representing the accumulated process time, in
  983. seconds.
  984. This is similar to os.times() but per-process.
  985. On macOS and Windows children_user and children_system are
  986. always set to 0.
  987. """
  988. return self._proc.cpu_times()
  989. @memoize_when_activated
  990. def memory_info(self):
  991. """Return a namedtuple with variable fields depending on the
  992. platform, representing memory information about the process.
  993. The "portable" fields available on all platforms are `rss` and `vms`.
  994. All numbers are expressed in bytes.
  995. """
  996. return self._proc.memory_info()
  997. def memory_full_info(self):
  998. """This method returns the same information as memory_info(),
  999. plus, on some platform (Linux, macOS, Windows), also provides
  1000. additional metrics (USS, PSS and swap).
  1001. The additional metrics provide a better representation of actual
  1002. process memory usage.
  1003. Namely USS is the memory which is unique to a process and which
  1004. would be freed if the process was terminated right now.
  1005. It does so by passing through the whole process address.
  1006. As such it usually requires higher user privileges than
  1007. memory_info() and is considerably slower.
  1008. """
  1009. return self._proc.memory_full_info()
  1010. def memory_percent(self, memtype="rss"):
  1011. """Compare process memory to total physical system memory and
  1012. calculate process memory utilization as a percentage.
  1013. *memtype* argument is a string that dictates what type of
  1014. process memory you want to compare against (defaults to "rss").
  1015. The list of available strings can be obtained like this:
  1016. >>> psutil.Process().memory_info()._fields
  1017. ('rss', 'vms', 'shared', 'text', 'lib', 'data', 'dirty', 'uss', 'pss')
  1018. """
  1019. valid_types = list(_ntp.pfullmem._fields)
  1020. if memtype not in valid_types:
  1021. msg = (
  1022. f"invalid memtype {memtype!r}; valid types are"
  1023. f" {tuple(valid_types)!r}"
  1024. )
  1025. raise ValueError(msg)
  1026. fun = (
  1027. self.memory_info
  1028. if memtype in _ntp.pmem._fields
  1029. else self.memory_full_info
  1030. )
  1031. metrics = fun()
  1032. value = getattr(metrics, memtype)
  1033. # use cached value if available
  1034. total_phymem = _TOTAL_PHYMEM or virtual_memory().total
  1035. if not total_phymem > 0:
  1036. # we should never get here
  1037. msg = (
  1038. "can't calculate process memory percent because total physical"
  1039. f" system memory is not positive ({total_phymem!r})"
  1040. )
  1041. raise ValueError(msg)
  1042. return (value / float(total_phymem)) * 100
  1043. if hasattr(_psplatform.Process, "memory_maps"):
  1044. def memory_maps(self, grouped=True):
  1045. """Return process' mapped memory regions as a list of namedtuples
  1046. whose fields are variable depending on the platform.
  1047. If *grouped* is True the mapped regions with the same 'path'
  1048. are grouped together and the different memory fields are summed.
  1049. If *grouped* is False every mapped region is shown as a single
  1050. entity and the namedtuple will also include the mapped region's
  1051. address space ('addr') and permission set ('perms').
  1052. """
  1053. it = self._proc.memory_maps()
  1054. if grouped:
  1055. d = {}
  1056. for tupl in it:
  1057. path = tupl[2]
  1058. nums = tupl[3:]
  1059. try:
  1060. d[path] = list(map(lambda x, y: x + y, d[path], nums))
  1061. except KeyError:
  1062. d[path] = nums
  1063. return [_ntp.pmmap_grouped(path, *d[path]) for path in d]
  1064. else:
  1065. return [_ntp.pmmap_ext(*x) for x in it]
  1066. def open_files(self):
  1067. """Return files opened by process as a list of
  1068. (path, fd) namedtuples including the absolute file name
  1069. and file descriptor number.
  1070. """
  1071. return self._proc.open_files()
  1072. def net_connections(self, kind='inet'):
  1073. """Return socket connections opened by process as a list of
  1074. (fd, family, type, laddr, raddr, status) namedtuples.
  1075. The *kind* parameter filters for connections that match the
  1076. following criteria:
  1077. +------------+----------------------------------------------------+
  1078. | Kind Value | Connections using |
  1079. +------------+----------------------------------------------------+
  1080. | inet | IPv4 and IPv6 |
  1081. | inet4 | IPv4 |
  1082. | inet6 | IPv6 |
  1083. | tcp | TCP |
  1084. | tcp4 | TCP over IPv4 |
  1085. | tcp6 | TCP over IPv6 |
  1086. | udp | UDP |
  1087. | udp4 | UDP over IPv4 |
  1088. | udp6 | UDP over IPv6 |
  1089. | unix | UNIX socket (both UDP and TCP protocols) |
  1090. | all | the sum of all the possible families and protocols |
  1091. +------------+----------------------------------------------------+
  1092. """
  1093. _check_conn_kind(kind)
  1094. return self._proc.net_connections(kind)
  1095. @_common.deprecated_method(replacement="net_connections")
  1096. def connections(self, kind="inet"):
  1097. return self.net_connections(kind=kind)
  1098. # --- signals
  1099. if POSIX:
  1100. def _send_signal(self, sig):
  1101. assert not self.pid < 0, self.pid
  1102. self._raise_if_pid_reused()
  1103. pid, ppid, name = self.pid, self._ppid, self._name
  1104. if pid == 0:
  1105. # see "man 2 kill"
  1106. msg = (
  1107. "preventing sending signal to process with PID 0 as it "
  1108. "would affect every process in the process group of the "
  1109. "calling process (os.getpid()) instead of PID 0"
  1110. )
  1111. raise ValueError(msg)
  1112. try:
  1113. os.kill(pid, sig)
  1114. except ProcessLookupError as err:
  1115. if OPENBSD and pid_exists(pid):
  1116. # We do this because os.kill() lies in case of
  1117. # zombie processes.
  1118. raise ZombieProcess(pid, name, ppid) from err
  1119. self._gone = True
  1120. raise NoSuchProcess(pid, name) from err
  1121. except PermissionError as err:
  1122. raise AccessDenied(pid, name) from err
  1123. def send_signal(self, sig):
  1124. """Send a signal *sig* to process pre-emptively checking
  1125. whether PID has been reused (see signal module constants) .
  1126. On Windows only SIGTERM is valid and is treated as an alias
  1127. for kill().
  1128. """
  1129. if POSIX:
  1130. self._send_signal(sig)
  1131. else: # pragma: no cover
  1132. self._raise_if_pid_reused()
  1133. if sig != signal.SIGTERM and not self.is_running():
  1134. msg = "process no longer exists"
  1135. raise NoSuchProcess(self.pid, self._name, msg=msg)
  1136. self._proc.send_signal(sig)
  1137. def suspend(self):
  1138. """Suspend process execution with SIGSTOP pre-emptively checking
  1139. whether PID has been reused.
  1140. On Windows this has the effect of suspending all process threads.
  1141. """
  1142. if POSIX:
  1143. self._send_signal(signal.SIGSTOP)
  1144. else: # pragma: no cover
  1145. self._raise_if_pid_reused()
  1146. self._proc.suspend()
  1147. def resume(self):
  1148. """Resume process execution with SIGCONT pre-emptively checking
  1149. whether PID has been reused.
  1150. On Windows this has the effect of resuming all process threads.
  1151. """
  1152. if POSIX:
  1153. self._send_signal(signal.SIGCONT)
  1154. else: # pragma: no cover
  1155. self._raise_if_pid_reused()
  1156. self._proc.resume()
  1157. def terminate(self):
  1158. """Terminate the process with SIGTERM pre-emptively checking
  1159. whether PID has been reused.
  1160. On Windows this is an alias for kill().
  1161. """
  1162. if POSIX:
  1163. self._send_signal(signal.SIGTERM)
  1164. else: # pragma: no cover
  1165. self._raise_if_pid_reused()
  1166. self._proc.kill()
  1167. def kill(self):
  1168. """Kill the current process with SIGKILL pre-emptively checking
  1169. whether PID has been reused.
  1170. """
  1171. if POSIX:
  1172. self._send_signal(signal.SIGKILL)
  1173. else: # pragma: no cover
  1174. self._raise_if_pid_reused()
  1175. self._proc.kill()
  1176. def wait(self, timeout=None):
  1177. """Wait for process to terminate, and if process is a children
  1178. of os.getpid(), also return its exit code, else None.
  1179. On Windows there's no such limitation (exit code is always
  1180. returned).
  1181. If the process is already terminated, immediately return None
  1182. instead of raising NoSuchProcess.
  1183. If *timeout* (in seconds) is specified and process is still
  1184. alive, raise TimeoutExpired.
  1185. If *timeout=0* either return immediately or raise
  1186. TimeoutExpired (non-blocking).
  1187. To wait for multiple Process objects use psutil.wait_procs().
  1188. """
  1189. if self.pid == 0:
  1190. msg = "can't wait for PID 0"
  1191. raise ValueError(msg)
  1192. if timeout is not None:
  1193. if not isinstance(timeout, (int, float)):
  1194. msg = f"timeout must be an int or float (got {type(timeout)})"
  1195. raise TypeError(msg)
  1196. if timeout < 0:
  1197. msg = f"timeout must be positive or zero (got {timeout})"
  1198. raise ValueError(msg)
  1199. if self._exitcode is not _SENTINEL:
  1200. return self._exitcode
  1201. try:
  1202. self._exitcode = self._proc.wait(timeout)
  1203. except TimeoutExpired as err:
  1204. exc = TimeoutExpired(timeout, pid=self.pid, name=self._name)
  1205. raise exc from err
  1206. return self._exitcode
  1207. # The valid attr names which can be processed by Process.as_dict().
  1208. # fmt: off
  1209. _as_dict_attrnames = {
  1210. x for x in dir(Process) if not x.startswith("_") and x not in
  1211. {'send_signal', 'suspend', 'resume', 'terminate', 'kill', 'wait',
  1212. 'is_running', 'as_dict', 'parent', 'parents', 'children', 'rlimit',
  1213. 'connections', 'oneshot'}
  1214. }
  1215. # fmt: on
  1216. # =====================================================================
  1217. # --- Popen class
  1218. # =====================================================================
  1219. class Popen(Process):
  1220. """Same as subprocess.Popen, but in addition it provides all
  1221. psutil.Process methods in a single class.
  1222. For the following methods which are common to both classes, psutil
  1223. implementation takes precedence:
  1224. * send_signal()
  1225. * terminate()
  1226. * kill()
  1227. This is done in order to avoid killing another process in case its
  1228. PID has been reused, fixing BPO-6973.
  1229. >>> import psutil
  1230. >>> from subprocess import PIPE
  1231. >>> p = psutil.Popen(["python", "-c", "print 'hi'"], stdout=PIPE)
  1232. >>> p.name()
  1233. 'python'
  1234. >>> p.uids()
  1235. user(real=1000, effective=1000, saved=1000)
  1236. >>> p.username()
  1237. 'giampaolo'
  1238. >>> p.communicate()
  1239. ('hi', None)
  1240. >>> p.terminate()
  1241. >>> p.wait(timeout=2)
  1242. 0
  1243. >>>
  1244. """
  1245. def __init__(self, *args, **kwargs):
  1246. # Explicitly avoid to raise NoSuchProcess in case the process
  1247. # spawned by subprocess.Popen terminates too quickly, see:
  1248. # https://github.com/giampaolo/psutil/issues/193
  1249. self.__subproc = subprocess.Popen(*args, **kwargs)
  1250. self._init(self.__subproc.pid, _ignore_nsp=True)
  1251. def __dir__(self):
  1252. return sorted(set(dir(Popen) + dir(subprocess.Popen)))
  1253. def __enter__(self):
  1254. if hasattr(self.__subproc, '__enter__'):
  1255. self.__subproc.__enter__()
  1256. return self
  1257. def __exit__(self, *args, **kwargs):
  1258. if hasattr(self.__subproc, '__exit__'):
  1259. return self.__subproc.__exit__(*args, **kwargs)
  1260. else:
  1261. if self.stdout:
  1262. self.stdout.close()
  1263. if self.stderr:
  1264. self.stderr.close()
  1265. try:
  1266. # Flushing a BufferedWriter may raise an error.
  1267. if self.stdin:
  1268. self.stdin.close()
  1269. finally:
  1270. # Wait for the process to terminate, to avoid zombies.
  1271. self.wait()
  1272. def __getattribute__(self, name):
  1273. try:
  1274. return object.__getattribute__(self, name)
  1275. except AttributeError:
  1276. try:
  1277. return object.__getattribute__(self.__subproc, name)
  1278. except AttributeError:
  1279. msg = f"{self.__class__!r} has no attribute {name!r}"
  1280. raise AttributeError(msg) from None
  1281. def wait(self, timeout=None):
  1282. if self.__subproc.returncode is not None:
  1283. return self.__subproc.returncode
  1284. ret = super().wait(timeout)
  1285. self.__subproc.returncode = ret
  1286. return ret
  1287. # =====================================================================
  1288. # --- system processes related functions
  1289. # =====================================================================
  1290. def pids():
  1291. """Return a list of current running PIDs."""
  1292. global _LOWEST_PID
  1293. ret = sorted(_psplatform.pids())
  1294. _LOWEST_PID = ret[0]
  1295. return ret
  1296. def pid_exists(pid):
  1297. """Return True if given PID exists in the current process list.
  1298. This is faster than doing "pid in psutil.pids()" and
  1299. should be preferred.
  1300. """
  1301. if pid < 0:
  1302. return False
  1303. elif pid == 0 and POSIX:
  1304. # On POSIX we use os.kill() to determine PID existence.
  1305. # According to "man 2 kill" PID 0 has a special meaning
  1306. # though: it refers to <<every process in the process
  1307. # group of the calling process>> and that is not we want
  1308. # to do here.
  1309. return pid in pids()
  1310. else:
  1311. return _psplatform.pid_exists(pid)
  1312. _pmap = {}
  1313. _pids_reused = set()
  1314. def process_iter(attrs=None, ad_value=None):
  1315. """Return a generator yielding a Process instance for all
  1316. running processes.
  1317. Every new Process instance is only created once and then cached
  1318. into an internal table which is updated every time this is used.
  1319. Cache can optionally be cleared via `process_iter.cache_clear()`.
  1320. The sorting order in which processes are yielded is based on
  1321. their PIDs.
  1322. *attrs* and *ad_value* have the same meaning as in
  1323. Process.as_dict(). If *attrs* is specified as_dict() is called
  1324. and the resulting dict is stored as a 'info' attribute attached
  1325. to returned Process instance.
  1326. If *attrs* is an empty list it will retrieve all process info
  1327. (slow).
  1328. """
  1329. global _pmap
  1330. def add(pid):
  1331. proc = Process(pid)
  1332. pmap[proc.pid] = proc
  1333. return proc
  1334. def remove(pid):
  1335. pmap.pop(pid, None)
  1336. pmap = _pmap.copy()
  1337. a = set(pids())
  1338. b = set(pmap.keys())
  1339. new_pids = a - b
  1340. gone_pids = b - a
  1341. for pid in gone_pids:
  1342. remove(pid)
  1343. while _pids_reused:
  1344. pid = _pids_reused.pop()
  1345. debug(f"refreshing Process instance for reused PID {pid}")
  1346. remove(pid)
  1347. try:
  1348. ls = sorted(list(pmap.items()) + list(dict.fromkeys(new_pids).items()))
  1349. for pid, proc in ls:
  1350. try:
  1351. if proc is None: # new process
  1352. proc = add(pid)
  1353. if attrs is not None:
  1354. proc.info = proc.as_dict(attrs=attrs, ad_value=ad_value)
  1355. yield proc
  1356. except NoSuchProcess:
  1357. remove(pid)
  1358. finally:
  1359. _pmap = pmap
  1360. process_iter.cache_clear = lambda: _pmap.clear() # noqa: PLW0108
  1361. process_iter.cache_clear.__doc__ = "Clear process_iter() internal cache."
  1362. def wait_procs(procs, timeout=None, callback=None):
  1363. """Convenience function which waits for a list of processes to
  1364. terminate.
  1365. Return a (gone, alive) tuple indicating which processes
  1366. are gone and which ones are still alive.
  1367. The gone ones will have a new *returncode* attribute indicating
  1368. process exit status (may be None).
  1369. *callback* is a function which gets called every time a process
  1370. terminates (a Process instance is passed as callback argument).
  1371. Function will return as soon as all processes terminate or when
  1372. *timeout* occurs.
  1373. Differently from Process.wait() it will not raise TimeoutExpired if
  1374. *timeout* occurs.
  1375. Typical use case is:
  1376. - send SIGTERM to a list of processes
  1377. - give them some time to terminate
  1378. - send SIGKILL to those ones which are still alive
  1379. Example:
  1380. >>> def on_terminate(proc):
  1381. ... print("process {} terminated".format(proc))
  1382. ...
  1383. >>> for p in procs:
  1384. ... p.terminate()
  1385. ...
  1386. >>> gone, alive = wait_procs(procs, timeout=3, callback=on_terminate)
  1387. >>> for p in alive:
  1388. ... p.kill()
  1389. """
  1390. def check_gone(proc, timeout):
  1391. try:
  1392. returncode = proc.wait(timeout=timeout)
  1393. except (TimeoutExpired, subprocess.TimeoutExpired):
  1394. pass
  1395. else:
  1396. if returncode is not None or not proc.is_running():
  1397. # Set new Process instance attribute.
  1398. proc.returncode = returncode
  1399. gone.add(proc)
  1400. if callback is not None:
  1401. callback(proc)
  1402. if timeout is not None and not timeout >= 0:
  1403. msg = f"timeout must be a positive integer, got {timeout}"
  1404. raise ValueError(msg)
  1405. if callback is not None and not callable(callback):
  1406. msg = f"callback {callback!r} is not a callable"
  1407. raise TypeError(msg)
  1408. gone = set()
  1409. alive = set(procs)
  1410. if timeout is not None:
  1411. deadline = _timer() + timeout
  1412. while alive:
  1413. if timeout is not None and timeout <= 0:
  1414. break
  1415. for proc in alive:
  1416. # Make sure that every complete iteration (all processes)
  1417. # will last max 1 sec.
  1418. # We do this because we don't want to wait too long on a
  1419. # single process: in case it terminates too late other
  1420. # processes may disappear in the meantime and their PID
  1421. # reused.
  1422. max_timeout = 1.0 / len(alive)
  1423. if timeout is not None:
  1424. timeout = min((deadline - _timer()), max_timeout)
  1425. if timeout <= 0:
  1426. break
  1427. check_gone(proc, timeout)
  1428. else:
  1429. check_gone(proc, max_timeout)
  1430. alive = alive - gone # noqa: PLR6104
  1431. if alive:
  1432. # Last attempt over processes survived so far.
  1433. # timeout == 0 won't make this function wait any further.
  1434. for proc in alive:
  1435. check_gone(proc, 0)
  1436. alive = alive - gone # noqa: PLR6104
  1437. return (list(gone), list(alive))
  1438. # =====================================================================
  1439. # --- CPU related functions
  1440. # =====================================================================
  1441. def cpu_count(logical=True):
  1442. """Return the number of logical CPUs in the system (same as
  1443. os.cpu_count()).
  1444. If *logical* is False return the number of physical cores only
  1445. (e.g. hyper thread CPUs are excluded).
  1446. Return None if undetermined.
  1447. The return value is cached after first call.
  1448. If desired cache can be cleared like this:
  1449. >>> psutil.cpu_count.cache_clear()
  1450. """
  1451. if logical:
  1452. ret = _psplatform.cpu_count_logical()
  1453. else:
  1454. ret = _psplatform.cpu_count_cores()
  1455. if ret is not None and ret < 1:
  1456. ret = None
  1457. return ret
  1458. def cpu_times(percpu=False):
  1459. """Return system-wide CPU times as a namedtuple.
  1460. Every CPU time represents the seconds the CPU has spent in the
  1461. given mode. The namedtuple's fields availability varies depending on the
  1462. platform:
  1463. - user
  1464. - system
  1465. - idle
  1466. - nice (UNIX)
  1467. - iowait (Linux)
  1468. - irq (Linux, FreeBSD)
  1469. - softirq (Linux)
  1470. - steal (Linux >= 2.6.11)
  1471. - guest (Linux >= 2.6.24)
  1472. - guest_nice (Linux >= 3.2.0)
  1473. When *percpu* is True return a list of namedtuples for each CPU.
  1474. First element of the list refers to first CPU, second element
  1475. to second CPU and so on.
  1476. The order of the list is consistent across calls.
  1477. """
  1478. if not percpu:
  1479. return _psplatform.cpu_times()
  1480. else:
  1481. return _psplatform.per_cpu_times()
  1482. try:
  1483. _last_cpu_times = {threading.current_thread().ident: cpu_times()}
  1484. except Exception: # noqa: BLE001
  1485. # Don't want to crash at import time.
  1486. _last_cpu_times = {}
  1487. try:
  1488. _last_per_cpu_times = {
  1489. threading.current_thread().ident: cpu_times(percpu=True)
  1490. }
  1491. except Exception: # noqa: BLE001
  1492. # Don't want to crash at import time.
  1493. _last_per_cpu_times = {}
  1494. def _cpu_tot_time(times):
  1495. """Given a cpu_time() ntuple calculates the total CPU time
  1496. (including idle time).
  1497. """
  1498. tot = sum(times)
  1499. if LINUX:
  1500. # On Linux guest times are already accounted in "user" or
  1501. # "nice" times, so we subtract them from total.
  1502. # Htop does the same. References:
  1503. # https://github.com/giampaolo/psutil/pull/940
  1504. # http://unix.stackexchange.com/questions/178045
  1505. # https://github.com/torvalds/linux/blob/
  1506. # 447976ef4fd09b1be88b316d1a81553f1aa7cd07/kernel/sched/
  1507. # cputime.c#L158
  1508. tot -= getattr(times, "guest", 0) # Linux 2.6.24+
  1509. tot -= getattr(times, "guest_nice", 0) # Linux 3.2.0+
  1510. return tot
  1511. def _cpu_busy_time(times):
  1512. """Given a cpu_time() ntuple calculates the busy CPU time.
  1513. We do so by subtracting all idle CPU times.
  1514. """
  1515. busy = _cpu_tot_time(times)
  1516. busy -= times.idle
  1517. # Linux: "iowait" is time during which the CPU does not do anything
  1518. # (waits for IO to complete). On Linux IO wait is *not* accounted
  1519. # in "idle" time so we subtract it. Htop does the same.
  1520. # References:
  1521. # https://github.com/torvalds/linux/blob/
  1522. # 447976ef4fd09b1be88b316d1a81553f1aa7cd07/kernel/sched/cputime.c#L244
  1523. busy -= getattr(times, "iowait", 0)
  1524. return busy
  1525. def _cpu_times_deltas(t1, t2):
  1526. assert t1._fields == t2._fields, (t1, t2)
  1527. field_deltas = []
  1528. for field in _ntp.scputimes._fields:
  1529. field_delta = getattr(t2, field) - getattr(t1, field)
  1530. # CPU times are always supposed to increase over time
  1531. # or at least remain the same and that's because time
  1532. # cannot go backwards.
  1533. # Surprisingly sometimes this might not be the case (at
  1534. # least on Windows and Linux), see:
  1535. # https://github.com/giampaolo/psutil/issues/392
  1536. # https://github.com/giampaolo/psutil/issues/645
  1537. # https://github.com/giampaolo/psutil/issues/1210
  1538. # Trim negative deltas to zero to ignore decreasing fields.
  1539. # top does the same. Reference:
  1540. # https://gitlab.com/procps-ng/procps/blob/v3.3.12/top/top.c#L5063
  1541. field_delta = max(0, field_delta)
  1542. field_deltas.append(field_delta)
  1543. return _ntp.scputimes(*field_deltas)
  1544. def cpu_percent(interval=None, percpu=False):
  1545. """Return a float representing the current system-wide CPU
  1546. utilization as a percentage.
  1547. When *interval* is > 0.0 compares system CPU times elapsed before
  1548. and after the interval (blocking).
  1549. When *interval* is 0.0 or None compares system CPU times elapsed
  1550. since last call or module import, returning immediately (non
  1551. blocking). That means the first time this is called it will
  1552. return a meaningless 0.0 value which you should ignore.
  1553. In this case is recommended for accuracy that this function be
  1554. called with at least 0.1 seconds between calls.
  1555. When *percpu* is True returns a list of floats representing the
  1556. utilization as a percentage for each CPU.
  1557. First element of the list refers to first CPU, second element
  1558. to second CPU and so on.
  1559. The order of the list is consistent across calls.
  1560. Examples:
  1561. >>> # blocking, system-wide
  1562. >>> psutil.cpu_percent(interval=1)
  1563. 2.0
  1564. >>>
  1565. >>> # blocking, per-cpu
  1566. >>> psutil.cpu_percent(interval=1, percpu=True)
  1567. [2.0, 1.0]
  1568. >>>
  1569. >>> # non-blocking (percentage since last call)
  1570. >>> psutil.cpu_percent(interval=None)
  1571. 2.9
  1572. >>>
  1573. """
  1574. tid = threading.current_thread().ident
  1575. blocking = interval is not None and interval > 0.0
  1576. if interval is not None and interval < 0:
  1577. msg = f"interval is not positive (got {interval})"
  1578. raise ValueError(msg)
  1579. def calculate(t1, t2):
  1580. times_delta = _cpu_times_deltas(t1, t2)
  1581. all_delta = _cpu_tot_time(times_delta)
  1582. busy_delta = _cpu_busy_time(times_delta)
  1583. try:
  1584. busy_perc = (busy_delta / all_delta) * 100
  1585. except ZeroDivisionError:
  1586. return 0.0
  1587. else:
  1588. return round(busy_perc, 1)
  1589. # system-wide usage
  1590. if not percpu:
  1591. if blocking:
  1592. t1 = cpu_times()
  1593. time.sleep(interval)
  1594. else:
  1595. t1 = _last_cpu_times.get(tid) or cpu_times()
  1596. _last_cpu_times[tid] = cpu_times()
  1597. return calculate(t1, _last_cpu_times[tid])
  1598. # per-cpu usage
  1599. else:
  1600. ret = []
  1601. if blocking:
  1602. tot1 = cpu_times(percpu=True)
  1603. time.sleep(interval)
  1604. else:
  1605. tot1 = _last_per_cpu_times.get(tid) or cpu_times(percpu=True)
  1606. _last_per_cpu_times[tid] = cpu_times(percpu=True)
  1607. for t1, t2 in zip(tot1, _last_per_cpu_times[tid]):
  1608. ret.append(calculate(t1, t2))
  1609. return ret
  1610. # Use a separate dict for cpu_times_percent(), so it's independent from
  1611. # cpu_percent() and they can both be used within the same program.
  1612. _last_cpu_times_2 = _last_cpu_times.copy()
  1613. _last_per_cpu_times_2 = _last_per_cpu_times.copy()
  1614. def cpu_times_percent(interval=None, percpu=False):
  1615. """Same as cpu_percent() but provides utilization percentages
  1616. for each specific CPU time as is returned by cpu_times().
  1617. For instance, on Linux we'll get:
  1618. >>> cpu_times_percent()
  1619. cpupercent(user=4.8, nice=0.0, system=4.8, idle=90.5, iowait=0.0,
  1620. irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
  1621. >>>
  1622. *interval* and *percpu* arguments have the same meaning as in
  1623. cpu_percent().
  1624. """
  1625. tid = threading.current_thread().ident
  1626. blocking = interval is not None and interval > 0.0
  1627. if interval is not None and interval < 0:
  1628. msg = f"interval is not positive (got {interval!r})"
  1629. raise ValueError(msg)
  1630. def calculate(t1, t2):
  1631. nums = []
  1632. times_delta = _cpu_times_deltas(t1, t2)
  1633. all_delta = _cpu_tot_time(times_delta)
  1634. # "scale" is the value to multiply each delta with to get percentages.
  1635. # We use "max" to avoid division by zero (if all_delta is 0, then all
  1636. # fields are 0 so percentages will be 0 too. all_delta cannot be a
  1637. # fraction because cpu times are integers)
  1638. scale = 100.0 / max(1, all_delta)
  1639. for field_delta in times_delta:
  1640. field_perc = field_delta * scale
  1641. field_perc = round(field_perc, 1)
  1642. # make sure we don't return negative values or values over 100%
  1643. field_perc = min(max(0.0, field_perc), 100.0)
  1644. nums.append(field_perc)
  1645. return _ntp.scputimes(*nums)
  1646. # system-wide usage
  1647. if not percpu:
  1648. if blocking:
  1649. t1 = cpu_times()
  1650. time.sleep(interval)
  1651. else:
  1652. t1 = _last_cpu_times_2.get(tid) or cpu_times()
  1653. _last_cpu_times_2[tid] = cpu_times()
  1654. return calculate(t1, _last_cpu_times_2[tid])
  1655. # per-cpu usage
  1656. else:
  1657. ret = []
  1658. if blocking:
  1659. tot1 = cpu_times(percpu=True)
  1660. time.sleep(interval)
  1661. else:
  1662. tot1 = _last_per_cpu_times_2.get(tid) or cpu_times(percpu=True)
  1663. _last_per_cpu_times_2[tid] = cpu_times(percpu=True)
  1664. for t1, t2 in zip(tot1, _last_per_cpu_times_2[tid]):
  1665. ret.append(calculate(t1, t2))
  1666. return ret
  1667. def cpu_stats():
  1668. """Return CPU statistics."""
  1669. return _psplatform.cpu_stats()
  1670. if hasattr(_psplatform, "cpu_freq"):
  1671. def cpu_freq(percpu=False):
  1672. """Return CPU frequency as a namedtuple including current,
  1673. min and max frequency expressed in Mhz.
  1674. If *percpu* is True and the system supports per-cpu frequency
  1675. retrieval (Linux only) a list of frequencies is returned for
  1676. each CPU. If not a list with one element is returned.
  1677. """
  1678. ret = _psplatform.cpu_freq()
  1679. if percpu:
  1680. return ret
  1681. else:
  1682. num_cpus = float(len(ret))
  1683. if num_cpus == 0:
  1684. return None
  1685. elif num_cpus == 1:
  1686. return ret[0]
  1687. else:
  1688. currs, mins, maxs = 0.0, 0.0, 0.0
  1689. set_none = False
  1690. for cpu in ret:
  1691. currs += cpu.current
  1692. # On Linux if /proc/cpuinfo is used min/max are set
  1693. # to None.
  1694. if LINUX and cpu.min is None:
  1695. set_none = True
  1696. continue
  1697. mins += cpu.min
  1698. maxs += cpu.max
  1699. current = currs / num_cpus
  1700. if set_none:
  1701. min_ = max_ = None
  1702. else:
  1703. min_ = mins / num_cpus
  1704. max_ = maxs / num_cpus
  1705. return _ntp.scpufreq(current, min_, max_)
  1706. __all__.append("cpu_freq")
  1707. if hasattr(os, "getloadavg") or hasattr(_psplatform, "getloadavg"):
  1708. # Perform this hasattr check once on import time to either use the
  1709. # platform based code or proxy straight from the os module.
  1710. if hasattr(os, "getloadavg"):
  1711. getloadavg = os.getloadavg
  1712. else:
  1713. getloadavg = _psplatform.getloadavg
  1714. __all__.append("getloadavg")
  1715. # =====================================================================
  1716. # --- system memory related functions
  1717. # =====================================================================
  1718. def virtual_memory():
  1719. """Return statistics about system memory usage as a namedtuple
  1720. including the following fields, expressed in bytes:
  1721. - total:
  1722. total physical memory available.
  1723. - available:
  1724. the memory that can be given instantly to processes without the
  1725. system going into swap.
  1726. This is calculated by summing different memory values depending
  1727. on the platform and it is supposed to be used to monitor actual
  1728. memory usage in a cross platform fashion.
  1729. - percent:
  1730. the percentage usage calculated as (total - available) / total * 100
  1731. - used:
  1732. memory used, calculated differently depending on the platform and
  1733. designed for informational purposes only:
  1734. macOS: active + wired
  1735. BSD: active + wired + cached
  1736. Linux: total - free
  1737. - free:
  1738. memory not being used at all (zeroed) that is readily available;
  1739. note that this doesn't reflect the actual memory available
  1740. (use 'available' instead)
  1741. Platform-specific fields:
  1742. - active (UNIX):
  1743. memory currently in use or very recently used, and so it is in RAM.
  1744. - inactive (UNIX):
  1745. memory that is marked as not used.
  1746. - buffers (BSD, Linux):
  1747. cache for things like file system metadata.
  1748. - cached (BSD, macOS):
  1749. cache for various things.
  1750. - wired (macOS, BSD):
  1751. memory that is marked to always stay in RAM. It is never moved to disk.
  1752. - shared (BSD):
  1753. memory that may be simultaneously accessed by multiple processes.
  1754. The sum of 'used' and 'available' does not necessarily equal total.
  1755. On Windows 'available' and 'free' are the same.
  1756. """
  1757. global _TOTAL_PHYMEM
  1758. ret = _psplatform.virtual_memory()
  1759. # cached for later use in Process.memory_percent()
  1760. _TOTAL_PHYMEM = ret.total
  1761. return ret
  1762. def swap_memory():
  1763. """Return system swap memory statistics as a namedtuple including
  1764. the following fields:
  1765. - total: total swap memory in bytes
  1766. - used: used swap memory in bytes
  1767. - free: free swap memory in bytes
  1768. - percent: the percentage usage
  1769. - sin: no. of bytes the system has swapped in from disk (cumulative)
  1770. - sout: no. of bytes the system has swapped out from disk (cumulative)
  1771. 'sin' and 'sout' on Windows are meaningless and always set to 0.
  1772. """
  1773. return _psplatform.swap_memory()
  1774. # =====================================================================
  1775. # --- disks/partitions related functions
  1776. # =====================================================================
  1777. def disk_usage(path):
  1778. """Return disk usage statistics about the given *path* as a
  1779. namedtuple including total, used and free space expressed in bytes
  1780. plus the percentage usage.
  1781. """
  1782. return _psplatform.disk_usage(path)
  1783. def disk_partitions(all=False):
  1784. """Return mounted partitions as a list of
  1785. (device, mountpoint, fstype, opts) namedtuple.
  1786. 'opts' field is a raw string separated by commas indicating mount
  1787. options which may vary depending on the platform.
  1788. If *all* parameter is False return physical devices only and ignore
  1789. all others.
  1790. """
  1791. return _psplatform.disk_partitions(all)
  1792. def disk_io_counters(perdisk=False, nowrap=True):
  1793. """Return system disk I/O statistics as a namedtuple including
  1794. the following fields:
  1795. - read_count: number of reads
  1796. - write_count: number of writes
  1797. - read_bytes: number of bytes read
  1798. - write_bytes: number of bytes written
  1799. - read_time: time spent reading from disk (in ms)
  1800. - write_time: time spent writing to disk (in ms)
  1801. Platform specific:
  1802. - busy_time: (Linux, FreeBSD) time spent doing actual I/Os (in ms)
  1803. - read_merged_count (Linux): number of merged reads
  1804. - write_merged_count (Linux): number of merged writes
  1805. If *perdisk* is True return the same information for every
  1806. physical disk installed on the system as a dictionary
  1807. with partition names as the keys and the namedtuple
  1808. described above as the values.
  1809. If *nowrap* is True it detects and adjust the numbers which overflow
  1810. and wrap (restart from 0) and add "old value" to "new value" so that
  1811. the returned numbers will always be increasing or remain the same,
  1812. but never decrease.
  1813. "disk_io_counters.cache_clear()" can be used to invalidate the
  1814. cache.
  1815. On recent Windows versions 'diskperf -y' command may need to be
  1816. executed first otherwise this function won't find any disk.
  1817. """
  1818. kwargs = dict(perdisk=perdisk) if LINUX else {}
  1819. rawdict = _psplatform.disk_io_counters(**kwargs)
  1820. if not rawdict:
  1821. return {} if perdisk else None
  1822. if nowrap:
  1823. rawdict = _wrap_numbers(rawdict, 'psutil.disk_io_counters')
  1824. if perdisk:
  1825. for disk, fields in rawdict.items():
  1826. rawdict[disk] = _ntp.sdiskio(*fields)
  1827. return rawdict
  1828. else:
  1829. return _ntp.sdiskio(*(sum(x) for x in zip(*rawdict.values())))
  1830. disk_io_counters.cache_clear = functools.partial(
  1831. _wrap_numbers.cache_clear, 'psutil.disk_io_counters'
  1832. )
  1833. disk_io_counters.cache_clear.__doc__ = "Clears nowrap argument cache"
  1834. # =====================================================================
  1835. # --- network related functions
  1836. # =====================================================================
  1837. def net_io_counters(pernic=False, nowrap=True):
  1838. """Return network I/O statistics as a namedtuple including
  1839. the following fields:
  1840. - bytes_sent: number of bytes sent
  1841. - bytes_recv: number of bytes received
  1842. - packets_sent: number of packets sent
  1843. - packets_recv: number of packets received
  1844. - errin: total number of errors while receiving
  1845. - errout: total number of errors while sending
  1846. - dropin: total number of incoming packets which were dropped
  1847. - dropout: total number of outgoing packets which were dropped
  1848. (always 0 on macOS and BSD)
  1849. If *pernic* is True return the same information for every
  1850. network interface installed on the system as a dictionary
  1851. with network interface names as the keys and the namedtuple
  1852. described above as the values.
  1853. If *nowrap* is True it detects and adjust the numbers which overflow
  1854. and wrap (restart from 0) and add "old value" to "new value" so that
  1855. the returned numbers will always be increasing or remain the same,
  1856. but never decrease.
  1857. "net_io_counters.cache_clear()" can be used to invalidate the
  1858. cache.
  1859. """
  1860. rawdict = _psplatform.net_io_counters()
  1861. if not rawdict:
  1862. return {} if pernic else None
  1863. if nowrap:
  1864. rawdict = _wrap_numbers(rawdict, 'psutil.net_io_counters')
  1865. if pernic:
  1866. for nic, fields in rawdict.items():
  1867. rawdict[nic] = _ntp.snetio(*fields)
  1868. return rawdict
  1869. else:
  1870. return _ntp.snetio(*[sum(x) for x in zip(*rawdict.values())])
  1871. net_io_counters.cache_clear = functools.partial(
  1872. _wrap_numbers.cache_clear, 'psutil.net_io_counters'
  1873. )
  1874. net_io_counters.cache_clear.__doc__ = "Clears nowrap argument cache"
  1875. def net_connections(kind='inet'):
  1876. """Return system-wide socket connections as a list of
  1877. (fd, family, type, laddr, raddr, status, pid) namedtuples.
  1878. In case of limited privileges 'fd' and 'pid' may be set to -1
  1879. and None respectively.
  1880. The *kind* parameter filters for connections that fit the
  1881. following criteria:
  1882. +------------+----------------------------------------------------+
  1883. | Kind Value | Connections using |
  1884. +------------+----------------------------------------------------+
  1885. | inet | IPv4 and IPv6 |
  1886. | inet4 | IPv4 |
  1887. | inet6 | IPv6 |
  1888. | tcp | TCP |
  1889. | tcp4 | TCP over IPv4 |
  1890. | tcp6 | TCP over IPv6 |
  1891. | udp | UDP |
  1892. | udp4 | UDP over IPv4 |
  1893. | udp6 | UDP over IPv6 |
  1894. | unix | UNIX socket (both UDP and TCP protocols) |
  1895. | all | the sum of all the possible families and protocols |
  1896. +------------+----------------------------------------------------+
  1897. On macOS this function requires root privileges.
  1898. """
  1899. _check_conn_kind(kind)
  1900. return _psplatform.net_connections(kind)
  1901. def net_if_addrs():
  1902. """Return the addresses associated to each NIC (network interface
  1903. card) installed on the system as a dictionary whose keys are the
  1904. NIC names and value is a list of namedtuples for each address
  1905. assigned to the NIC. Each namedtuple includes 5 fields:
  1906. - family: can be either socket.AF_INET, socket.AF_INET6 or
  1907. psutil.AF_LINK, which refers to a MAC address.
  1908. - address: is the primary address and it is always set.
  1909. - netmask: and 'broadcast' and 'ptp' may be None.
  1910. - ptp: stands for "point to point" and references the
  1911. destination address on a point to point interface
  1912. (typically a VPN).
  1913. - broadcast: and *ptp* are mutually exclusive.
  1914. Note: you can have more than one address of the same family
  1915. associated with each interface.
  1916. """
  1917. rawlist = _psplatform.net_if_addrs()
  1918. rawlist.sort(key=lambda x: x[1]) # sort by family
  1919. ret = collections.defaultdict(list)
  1920. for name, fam, addr, mask, broadcast, ptp in rawlist:
  1921. try:
  1922. fam = socket.AddressFamily(fam)
  1923. except ValueError:
  1924. if WINDOWS and fam == -1:
  1925. fam = _psplatform.AF_LINK
  1926. elif (
  1927. hasattr(_psplatform, "AF_LINK") and fam == _psplatform.AF_LINK
  1928. ):
  1929. # Linux defines AF_LINK as an alias for AF_PACKET.
  1930. # We re-set the family here so that repr(family)
  1931. # will show AF_LINK rather than AF_PACKET
  1932. fam = _psplatform.AF_LINK
  1933. if fam == _psplatform.AF_LINK:
  1934. # The underlying C function may return an incomplete MAC
  1935. # address in which case we fill it with null bytes, see:
  1936. # https://github.com/giampaolo/psutil/issues/786
  1937. separator = ":" if POSIX else "-"
  1938. while addr.count(separator) < 5:
  1939. addr += f"{separator}00"
  1940. nt = _ntp.snicaddr(fam, addr, mask, broadcast, ptp)
  1941. # On Windows broadcast is None, so we determine it via
  1942. # ipaddress module.
  1943. if WINDOWS and fam in {socket.AF_INET, socket.AF_INET6}:
  1944. try:
  1945. broadcast = _common.broadcast_addr(nt)
  1946. except Exception as err: # noqa: BLE001
  1947. debug(err)
  1948. else:
  1949. if broadcast is not None:
  1950. nt._replace(broadcast=broadcast)
  1951. ret[name].append(nt)
  1952. return dict(ret)
  1953. def net_if_stats():
  1954. """Return information about each NIC (network interface card)
  1955. installed on the system as a dictionary whose keys are the
  1956. NIC names and value is a namedtuple with the following fields:
  1957. - isup: whether the interface is up (bool)
  1958. - duplex: can be either NIC_DUPLEX_FULL, NIC_DUPLEX_HALF or
  1959. NIC_DUPLEX_UNKNOWN
  1960. - speed: the NIC speed expressed in mega bits (MB); if it can't
  1961. be determined (e.g. 'localhost') it will be set to 0.
  1962. - mtu: the maximum transmission unit expressed in bytes.
  1963. """
  1964. return _psplatform.net_if_stats()
  1965. # =====================================================================
  1966. # --- sensors
  1967. # =====================================================================
  1968. # Linux, macOS
  1969. if hasattr(_psplatform, "sensors_temperatures"):
  1970. def sensors_temperatures(fahrenheit=False):
  1971. """Return hardware temperatures. Each entry is a namedtuple
  1972. representing a certain hardware sensor (it may be a CPU, an
  1973. hard disk or something else, depending on the OS and its
  1974. configuration).
  1975. All temperatures are expressed in celsius unless *fahrenheit*
  1976. is set to True.
  1977. """
  1978. def convert(n):
  1979. if n is not None:
  1980. return (float(n) * 9 / 5) + 32 if fahrenheit else n
  1981. ret = collections.defaultdict(list)
  1982. rawdict = _psplatform.sensors_temperatures()
  1983. for name, values in rawdict.items():
  1984. while values:
  1985. label, current, high, critical = values.pop(0)
  1986. current = convert(current)
  1987. high = convert(high)
  1988. critical = convert(critical)
  1989. if high and not critical:
  1990. critical = high
  1991. elif critical and not high:
  1992. high = critical
  1993. ret[name].append(_ntp.shwtemp(label, current, high, critical))
  1994. return dict(ret)
  1995. __all__.append("sensors_temperatures")
  1996. # Linux
  1997. if hasattr(_psplatform, "sensors_fans"):
  1998. def sensors_fans():
  1999. """Return fans speed. Each entry is a namedtuple
  2000. representing a certain hardware sensor.
  2001. All speed are expressed in RPM (rounds per minute).
  2002. """
  2003. return _psplatform.sensors_fans()
  2004. __all__.append("sensors_fans")
  2005. # Linux, Windows, FreeBSD, macOS
  2006. if hasattr(_psplatform, "sensors_battery"):
  2007. def sensors_battery():
  2008. """Return battery information. If no battery is installed
  2009. returns None.
  2010. - percent: battery power left as a percentage.
  2011. - secsleft: a rough approximation of how many seconds are left
  2012. before the battery runs out of power. May be
  2013. POWER_TIME_UNLIMITED or POWER_TIME_UNLIMITED.
  2014. - power_plugged: True if the AC power cable is connected.
  2015. """
  2016. return _psplatform.sensors_battery()
  2017. __all__.append("sensors_battery")
  2018. # =====================================================================
  2019. # --- other system related functions
  2020. # =====================================================================
  2021. def boot_time():
  2022. """Return the system boot time expressed in seconds since the epoch
  2023. (seconds since January 1, 1970, at midnight UTC). The returned
  2024. value is based on the system clock, which means it may be affected
  2025. by changes such as manual adjustments or time synchronization (e.g.
  2026. NTP).
  2027. """
  2028. return _psplatform.boot_time()
  2029. def users():
  2030. """Return users currently connected on the system as a list of
  2031. namedtuples including the following fields.
  2032. - user: the name of the user
  2033. - terminal: the tty or pseudo-tty associated with the user, if any.
  2034. - host: the host name associated with the entry, if any.
  2035. - started: the creation time as a floating point number expressed in
  2036. seconds since the epoch.
  2037. """
  2038. return _psplatform.users()
  2039. # =====================================================================
  2040. # --- Windows services
  2041. # =====================================================================
  2042. if WINDOWS:
  2043. def win_service_iter():
  2044. """Return a generator yielding a WindowsService instance for all
  2045. Windows services installed.
  2046. """
  2047. return _psplatform.win_service_iter()
  2048. def win_service_get(name):
  2049. """Get a Windows service by *name*.
  2050. Raise NoSuchProcess if no service with such name exists.
  2051. """
  2052. return _psplatform.win_service_get(name)
  2053. # =====================================================================
  2054. # --- malloc / heap
  2055. # =====================================================================
  2056. # Linux + glibc, Windows, macOS, FreeBSD, NetBSD
  2057. if hasattr(_psplatform, "heap_info"):
  2058. def heap_info():
  2059. """Return low-level heap statistics from the C heap allocator
  2060. (glibc).
  2061. - `heap_used`: the total number of bytes allocated via
  2062. malloc/free. These are typically allocations smaller than
  2063. MMAP_THRESHOLD.
  2064. - `mmap_used`: the total number of bytes allocated via `mmap()`
  2065. or via large ``malloc()`` allocations.
  2066. - `heap_count` (Windows only): number of private heaps created
  2067. via `HeapCreate()`.
  2068. """
  2069. return _ntp.pheap(*_psplatform.heap_info())
  2070. def heap_trim():
  2071. """Request that the underlying allocator free any unused memory
  2072. it's holding in the heap (typically small `malloc()`
  2073. allocations).
  2074. In practice, modern allocators rarely comply, so this is not a
  2075. general-purpose memory-reduction tool and won't meaningfully
  2076. shrink RSS in real programs. Its primary value is in **leak
  2077. detection tools**.
  2078. Calling `heap_trim()` before taking measurements helps reduce
  2079. allocator noise, giving you a cleaner baseline so that changes
  2080. in `heap_used` come from the code you're testing, not from
  2081. internal allocator caching or fragmentation. Its effectiveness
  2082. depends on allocator behavior and fragmentation patterns.
  2083. """
  2084. _psplatform.heap_trim()
  2085. __all__.append("heap_info")
  2086. __all__.append("heap_trim")
  2087. # =====================================================================
  2088. def _set_debug(value):
  2089. """Enable or disable PSUTIL_DEBUG option, which prints debugging
  2090. messages to stderr.
  2091. """
  2092. import psutil._common
  2093. psutil._common.PSUTIL_DEBUG = bool(value)
  2094. _psplatform.cext.set_debug(bool(value))
  2095. del memoize_when_activated