__init__.py 86 KB

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