_pslinux.py 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  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. """Linux platform implementation."""
  5. import base64
  6. import collections
  7. import enum
  8. import errno
  9. import functools
  10. import glob
  11. import os
  12. import re
  13. import resource
  14. import socket
  15. import struct
  16. import sys
  17. import warnings
  18. from collections import defaultdict
  19. from collections import namedtuple
  20. from . import _common
  21. from . import _ntuples as ntp
  22. from . import _psposix
  23. from . import _psutil_linux as cext
  24. from ._common import ENCODING
  25. from ._common import NIC_DUPLEX_FULL
  26. from ._common import NIC_DUPLEX_HALF
  27. from ._common import NIC_DUPLEX_UNKNOWN
  28. from ._common import AccessDenied
  29. from ._common import NoSuchProcess
  30. from ._common import ZombieProcess
  31. from ._common import bcat
  32. from ._common import cat
  33. from ._common import debug
  34. from ._common import decode
  35. from ._common import get_procfs_path
  36. from ._common import isfile_strict
  37. from ._common import memoize
  38. from ._common import memoize_when_activated
  39. from ._common import open_binary
  40. from ._common import open_text
  41. from ._common import parse_environ_block
  42. from ._common import path_exists_strict
  43. from ._common import supports_ipv6
  44. from ._common import usage_percent
  45. # fmt: off
  46. __extra__all__ = [
  47. 'PROCFS_PATH',
  48. # io prio constants
  49. "IOPRIO_CLASS_NONE", "IOPRIO_CLASS_RT", "IOPRIO_CLASS_BE",
  50. "IOPRIO_CLASS_IDLE",
  51. # connection status constants
  52. "CONN_ESTABLISHED", "CONN_SYN_SENT", "CONN_SYN_RECV", "CONN_FIN_WAIT1",
  53. "CONN_FIN_WAIT2", "CONN_TIME_WAIT", "CONN_CLOSE", "CONN_CLOSE_WAIT",
  54. "CONN_LAST_ACK", "CONN_LISTEN", "CONN_CLOSING",
  55. ]
  56. # fmt: on
  57. # =====================================================================
  58. # --- globals
  59. # =====================================================================
  60. POWER_SUPPLY_PATH = "/sys/class/power_supply"
  61. HAS_PROC_SMAPS = os.path.exists(f"/proc/{os.getpid()}/smaps")
  62. HAS_PROC_SMAPS_ROLLUP = os.path.exists(f"/proc/{os.getpid()}/smaps_rollup")
  63. HAS_PROC_IO_PRIORITY = hasattr(cext, "proc_ioprio_get")
  64. HAS_CPU_AFFINITY = hasattr(cext, "proc_cpu_affinity_get")
  65. # Number of clock ticks per second
  66. CLOCK_TICKS = os.sysconf("SC_CLK_TCK")
  67. PAGESIZE = cext.getpagesize()
  68. LITTLE_ENDIAN = sys.byteorder == 'little'
  69. UNSET = object()
  70. # "man iostat" states that sectors are equivalent with blocks and have
  71. # a size of 512 bytes. Despite this value can be queried at runtime
  72. # via /sys/block/{DISK}/queue/hw_sector_size and results may vary
  73. # between 1k, 2k, or 4k... 512 appears to be a magic constant used
  74. # throughout Linux source code:
  75. # * https://stackoverflow.com/a/38136179/376587
  76. # * https://lists.gt.net/linux/kernel/2241060
  77. # * https://github.com/giampaolo/psutil/issues/1305
  78. # * https://github.com/torvalds/linux/blob/
  79. # 4f671fe2f9523a1ea206f63fe60a7c7b3a56d5c7/include/linux/bio.h#L99
  80. # * https://lkml.org/lkml/2015/8/17/234
  81. DISK_SECTOR_SIZE = 512
  82. AddressFamily = enum.IntEnum(
  83. 'AddressFamily', {'AF_LINK': int(socket.AF_PACKET)}
  84. )
  85. AF_LINK = AddressFamily.AF_LINK
  86. # ioprio_* constants http://linux.die.net/man/2/ioprio_get
  87. class IOPriority(enum.IntEnum):
  88. IOPRIO_CLASS_NONE = 0
  89. IOPRIO_CLASS_RT = 1
  90. IOPRIO_CLASS_BE = 2
  91. IOPRIO_CLASS_IDLE = 3
  92. globals().update(IOPriority.__members__)
  93. # See:
  94. # https://github.com/torvalds/linux/blame/master/fs/proc/array.c
  95. # ...and (TASK_* constants):
  96. # https://github.com/torvalds/linux/blob/master/include/linux/sched.h
  97. PROC_STATUSES = {
  98. "R": _common.STATUS_RUNNING,
  99. "S": _common.STATUS_SLEEPING,
  100. "D": _common.STATUS_DISK_SLEEP,
  101. "T": _common.STATUS_STOPPED,
  102. "t": _common.STATUS_TRACING_STOP,
  103. "Z": _common.STATUS_ZOMBIE,
  104. "X": _common.STATUS_DEAD,
  105. "x": _common.STATUS_DEAD,
  106. "K": _common.STATUS_WAKE_KILL,
  107. "W": _common.STATUS_WAKING,
  108. "I": _common.STATUS_IDLE,
  109. "P": _common.STATUS_PARKED,
  110. }
  111. # https://github.com/torvalds/linux/blob/master/include/net/tcp_states.h
  112. TCP_STATUSES = {
  113. "01": _common.CONN_ESTABLISHED,
  114. "02": _common.CONN_SYN_SENT,
  115. "03": _common.CONN_SYN_RECV,
  116. "04": _common.CONN_FIN_WAIT1,
  117. "05": _common.CONN_FIN_WAIT2,
  118. "06": _common.CONN_TIME_WAIT,
  119. "07": _common.CONN_CLOSE,
  120. "08": _common.CONN_CLOSE_WAIT,
  121. "09": _common.CONN_LAST_ACK,
  122. "0A": _common.CONN_LISTEN,
  123. "0B": _common.CONN_CLOSING,
  124. }
  125. # =====================================================================
  126. # --- utils
  127. # =====================================================================
  128. def readlink(path):
  129. """Wrapper around os.readlink()."""
  130. assert isinstance(path, str), path
  131. path = os.readlink(path)
  132. # readlink() might return paths containing null bytes ('\x00')
  133. # resulting in "TypeError: must be encoded string without NULL
  134. # bytes, not str" errors when the string is passed to other
  135. # fs-related functions (os.*, open(), ...).
  136. # Apparently everything after '\x00' is garbage (we can have
  137. # ' (deleted)', 'new' and possibly others), see:
  138. # https://github.com/giampaolo/psutil/issues/717
  139. path = path.split('\x00')[0]
  140. # Certain paths have ' (deleted)' appended. Usually this is
  141. # bogus as the file actually exists. Even if it doesn't we
  142. # don't care.
  143. if path.endswith(' (deleted)') and not path_exists_strict(path):
  144. path = path[:-10]
  145. return path
  146. def file_flags_to_mode(flags):
  147. """Convert file's open() flags into a readable string.
  148. Used by Process.open_files().
  149. """
  150. modes_map = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'}
  151. mode = modes_map[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]
  152. if flags & os.O_APPEND:
  153. mode = mode.replace('w', 'a', 1)
  154. mode = mode.replace('w+', 'r+')
  155. # possible values: r, w, a, r+, a+
  156. return mode
  157. def is_storage_device(name):
  158. """Return True if the given name refers to a root device (e.g.
  159. "sda", "nvme0n1") as opposed to a logical partition (e.g. "sda1",
  160. "nvme0n1p1"). If name is a virtual device (e.g. "loop1", "ram")
  161. return True.
  162. """
  163. # Re-adapted from iostat source code, see:
  164. # https://github.com/sysstat/sysstat/blob/
  165. # 97912938cd476645b267280069e83b1c8dc0e1c7/common.c#L208
  166. # Some devices may have a slash in their name (e.g. cciss/c0d0...).
  167. name = name.replace('/', '!')
  168. including_virtual = True
  169. if including_virtual:
  170. path = f"/sys/block/{name}"
  171. else:
  172. path = f"/sys/block/{name}/device"
  173. return os.access(path, os.F_OK)
  174. @memoize
  175. def _scputimes_ntuple(procfs_path):
  176. """Return a namedtuple of variable fields depending on the CPU times
  177. available on this Linux kernel version which may be:
  178. (user, nice, system, idle, iowait, irq, softirq, [steal, [guest,
  179. [guest_nice]]])
  180. Used by cpu_times() function.
  181. """
  182. with open_binary(f"{procfs_path}/stat") as f:
  183. values = f.readline().split()[1:]
  184. fields = ['user', 'nice', 'system', 'idle', 'iowait', 'irq', 'softirq']
  185. vlen = len(values)
  186. if vlen >= 8:
  187. # Linux >= 2.6.11
  188. fields.append('steal')
  189. if vlen >= 9:
  190. # Linux >= 2.6.24
  191. fields.append('guest')
  192. if vlen >= 10:
  193. # Linux >= 3.2.0
  194. fields.append('guest_nice')
  195. return namedtuple('scputimes', fields)
  196. # Set it into _ntuples.py namespace.
  197. try:
  198. ntp.scputimes = _scputimes_ntuple("/proc")
  199. except Exception as err: # noqa: BLE001
  200. # Don't want to crash at import time.
  201. debug(f"ignoring exception on import: {err!r}")
  202. ntp.scputimes = namedtuple('scputimes', 'user system idle')(0.0, 0.0, 0.0)
  203. # XXX: must be available also at this module level in order to be
  204. # serialized (tests/test_misc.py::TestMisc::test_serialization).
  205. scputimes = ntp.scputimes
  206. # =====================================================================
  207. # --- system memory
  208. # =====================================================================
  209. def calculate_avail_vmem(mems):
  210. """Fallback for kernels < 3.14 where /proc/meminfo does not provide
  211. "MemAvailable", see:
  212. https://blog.famzah.net/2014/09/24/.
  213. This code reimplements the algorithm outlined here:
  214. https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
  215. commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
  216. We use this function also when "MemAvailable" returns 0 (possibly a
  217. kernel bug, see: https://github.com/giampaolo/psutil/issues/1915).
  218. In that case this routine matches "free" CLI tool result ("available"
  219. column).
  220. XXX: on recent kernels this calculation may differ by ~1.5% compared
  221. to "MemAvailable:", as it's calculated slightly differently.
  222. It is still way more realistic than doing (free + cached) though.
  223. See:
  224. * https://gitlab.com/procps-ng/procps/issues/42
  225. * https://github.com/famzah/linux-memavailable-procfs/issues/2
  226. """
  227. # Note about "fallback" value. According to:
  228. # https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
  229. # commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
  230. # ...long ago "available" memory was calculated as (free + cached),
  231. # We use fallback when one of these is missing from /proc/meminfo:
  232. # "Active(file)": introduced in 2.6.28 / Dec 2008
  233. # "Inactive(file)": introduced in 2.6.28 / Dec 2008
  234. # "SReclaimable": introduced in 2.6.19 / Nov 2006
  235. # /proc/zoneinfo: introduced in 2.6.13 / Aug 2005
  236. free = mems[b'MemFree:']
  237. fallback = free + mems.get(b"Cached:", 0)
  238. try:
  239. lru_active_file = mems[b'Active(file):']
  240. lru_inactive_file = mems[b'Inactive(file):']
  241. slab_reclaimable = mems[b'SReclaimable:']
  242. except KeyError as err:
  243. debug(
  244. f"{err.args[0]} is missing from /proc/meminfo; using an"
  245. " approximation for calculating available memory"
  246. )
  247. return fallback
  248. try:
  249. f = open_binary(f"{get_procfs_path()}/zoneinfo")
  250. except OSError:
  251. return fallback # kernel 2.6.13
  252. watermark_low = 0
  253. with f:
  254. for line in f:
  255. line = line.strip()
  256. if line.startswith(b'low'):
  257. watermark_low += int(line.split()[1])
  258. watermark_low *= PAGESIZE
  259. avail = free - watermark_low
  260. pagecache = lru_active_file + lru_inactive_file
  261. pagecache -= min(pagecache / 2, watermark_low)
  262. avail += pagecache
  263. avail += slab_reclaimable - min(slab_reclaimable / 2.0, watermark_low)
  264. return int(avail)
  265. def virtual_memory():
  266. """Report virtual memory stats.
  267. This implementation mimics procps-ng-3.3.12, aka "free" CLI tool:
  268. https://gitlab.com/procps-ng/procps/blob/
  269. 24fd2605c51fccc375ab0287cec33aa767f06718/proc/sysinfo.c#L778-791
  270. The returned values are supposed to match both "free" and "vmstat -s"
  271. CLI tools.
  272. """
  273. missing_fields = []
  274. mems = {}
  275. with open_binary(f"{get_procfs_path()}/meminfo") as f:
  276. for line in f:
  277. fields = line.split()
  278. mems[fields[0]] = int(fields[1]) * 1024
  279. # /proc doc states that the available fields in /proc/meminfo vary
  280. # by architecture and compile options, but these 3 values are also
  281. # returned by sysinfo(2); as such we assume they are always there.
  282. total = mems[b'MemTotal:']
  283. free = mems[b'MemFree:']
  284. try:
  285. buffers = mems[b'Buffers:']
  286. except KeyError:
  287. # https://github.com/giampaolo/psutil/issues/1010
  288. buffers = 0
  289. missing_fields.append('buffers')
  290. try:
  291. cached = mems[b"Cached:"]
  292. except KeyError:
  293. cached = 0
  294. missing_fields.append('cached')
  295. else:
  296. # "free" cmdline utility sums reclaimable to cached.
  297. # Older versions of procps used to add slab memory instead.
  298. # This got changed in:
  299. # https://gitlab.com/procps-ng/procps/commit/
  300. # 05d751c4f076a2f0118b914c5e51cfbb4762ad8e
  301. cached += mems.get(b"SReclaimable:", 0) # since kernel 2.6.19
  302. try:
  303. shared = mems[b'Shmem:'] # since kernel 2.6.32
  304. except KeyError:
  305. try:
  306. shared = mems[b'MemShared:'] # kernels 2.4
  307. except KeyError:
  308. shared = 0
  309. missing_fields.append('shared')
  310. try:
  311. active = mems[b"Active:"]
  312. except KeyError:
  313. active = 0
  314. missing_fields.append('active')
  315. try:
  316. inactive = mems[b"Inactive:"]
  317. except KeyError:
  318. try:
  319. inactive = (
  320. mems[b"Inact_dirty:"]
  321. + mems[b"Inact_clean:"]
  322. + mems[b"Inact_laundry:"]
  323. )
  324. except KeyError:
  325. inactive = 0
  326. missing_fields.append('inactive')
  327. try:
  328. slab = mems[b"Slab:"]
  329. except KeyError:
  330. slab = 0
  331. # - starting from 4.4.0 we match free's "available" column.
  332. # Before 4.4.0 we calculated it as (free + buffers + cached)
  333. # which matched htop.
  334. # - free and htop available memory differs as per:
  335. # http://askubuntu.com/a/369589
  336. # http://unix.stackexchange.com/a/65852/168884
  337. # - MemAvailable has been introduced in kernel 3.14
  338. try:
  339. avail = mems[b'MemAvailable:']
  340. except KeyError:
  341. avail = calculate_avail_vmem(mems)
  342. else:
  343. if avail == 0:
  344. # Yes, it can happen (probably a kernel bug):
  345. # https://github.com/giampaolo/psutil/issues/1915
  346. # In this case "free" CLI tool makes an estimate. We do the same,
  347. # and it matches "free" CLI tool.
  348. avail = calculate_avail_vmem(mems)
  349. if avail < 0:
  350. avail = 0
  351. missing_fields.append('available')
  352. elif avail > total:
  353. # If avail is greater than total or our calculation overflows,
  354. # that's symptomatic of running within a LCX container where such
  355. # values will be dramatically distorted over those of the host.
  356. # https://gitlab.com/procps-ng/procps/blob/
  357. # 24fd2605c51fccc375ab0287cec33aa767f06718/proc/sysinfo.c#L764
  358. avail = free
  359. used = total - avail
  360. percent = usage_percent((total - avail), total, round_=1)
  361. # Warn about missing metrics which are set to 0.
  362. if missing_fields:
  363. msg = "{} memory stats couldn't be determined and {} set to 0".format(
  364. ", ".join(missing_fields),
  365. "was" if len(missing_fields) == 1 else "were",
  366. )
  367. warnings.warn(msg, RuntimeWarning, stacklevel=2)
  368. return ntp.svmem(
  369. total,
  370. avail,
  371. percent,
  372. used,
  373. free,
  374. active,
  375. inactive,
  376. buffers,
  377. cached,
  378. shared,
  379. slab,
  380. )
  381. def swap_memory():
  382. """Return swap memory metrics."""
  383. mems = {}
  384. with open_binary(f"{get_procfs_path()}/meminfo") as f:
  385. for line in f:
  386. fields = line.split()
  387. mems[fields[0]] = int(fields[1]) * 1024
  388. # We prefer /proc/meminfo over sysinfo() syscall so that
  389. # psutil.PROCFS_PATH can be used in order to allow retrieval
  390. # for linux containers, see:
  391. # https://github.com/giampaolo/psutil/issues/1015
  392. try:
  393. total = mems[b'SwapTotal:']
  394. free = mems[b'SwapFree:']
  395. except KeyError:
  396. _, _, _, _, total, free, unit_multiplier = cext.linux_sysinfo()
  397. total *= unit_multiplier
  398. free *= unit_multiplier
  399. used = total - free
  400. percent = usage_percent(used, total, round_=1)
  401. # get pgin/pgouts
  402. try:
  403. f = open_binary(f"{get_procfs_path()}/vmstat")
  404. except OSError as err:
  405. # see https://github.com/giampaolo/psutil/issues/722
  406. msg = (
  407. "'sin' and 'sout' swap memory stats couldn't "
  408. f"be determined and were set to 0 ({err})"
  409. )
  410. warnings.warn(msg, RuntimeWarning, stacklevel=2)
  411. sin = sout = 0
  412. else:
  413. with f:
  414. sin = sout = None
  415. for line in f:
  416. # values are expressed in 4 kilo bytes, we want
  417. # bytes instead
  418. if line.startswith(b'pswpin'):
  419. sin = int(line.split(b' ')[1]) * 4 * 1024
  420. elif line.startswith(b'pswpout'):
  421. sout = int(line.split(b' ')[1]) * 4 * 1024
  422. if sin is not None and sout is not None:
  423. break
  424. else:
  425. # we might get here when dealing with exotic Linux
  426. # flavors, see:
  427. # https://github.com/giampaolo/psutil/issues/313
  428. msg = "'sin' and 'sout' swap memory stats couldn't "
  429. msg += "be determined and were set to 0"
  430. warnings.warn(msg, RuntimeWarning, stacklevel=2)
  431. sin = sout = 0
  432. return ntp.sswap(total, used, free, percent, sin, sout)
  433. # malloc / heap functions; require glibc
  434. if hasattr(cext, "heap_info"):
  435. heap_info = cext.heap_info
  436. heap_trim = cext.heap_trim
  437. # =====================================================================
  438. # --- CPU
  439. # =====================================================================
  440. def cpu_times():
  441. """Return a named tuple representing the following system-wide
  442. CPU times:
  443. (user, nice, system, idle, iowait, irq, softirq [steal, [guest,
  444. [guest_nice]]])
  445. Last 3 fields may not be available on all Linux kernel versions.
  446. """
  447. procfs_path = get_procfs_path()
  448. with open_binary(f"{procfs_path}/stat") as f:
  449. values = f.readline().split()
  450. fields = values[1 : len(ntp.scputimes._fields) + 1]
  451. fields = [float(x) / CLOCK_TICKS for x in fields]
  452. return ntp.scputimes(*fields)
  453. def per_cpu_times():
  454. """Return a list of namedtuple representing the CPU times
  455. for every CPU available on the system.
  456. """
  457. procfs_path = get_procfs_path()
  458. cpus = []
  459. with open_binary(f"{procfs_path}/stat") as f:
  460. # get rid of the first line which refers to system wide CPU stats
  461. f.readline()
  462. for line in f:
  463. if line.startswith(b'cpu'):
  464. values = line.split()
  465. fields = values[1 : len(ntp.scputimes._fields) + 1]
  466. fields = [float(x) / CLOCK_TICKS for x in fields]
  467. entry = ntp.scputimes(*fields)
  468. cpus.append(entry)
  469. return cpus
  470. def cpu_count_logical():
  471. """Return the number of logical CPUs in the system."""
  472. try:
  473. return os.sysconf("SC_NPROCESSORS_ONLN")
  474. except ValueError:
  475. # as a second fallback we try to parse /proc/cpuinfo
  476. num = 0
  477. with open_binary(f"{get_procfs_path()}/cpuinfo") as f:
  478. for line in f:
  479. if line.lower().startswith(b'processor'):
  480. num += 1
  481. # unknown format (e.g. amrel/sparc architectures), see:
  482. # https://github.com/giampaolo/psutil/issues/200
  483. # try to parse /proc/stat as a last resort
  484. if num == 0:
  485. search = re.compile(r'cpu\d')
  486. with open_text(f"{get_procfs_path()}/stat") as f:
  487. for line in f:
  488. line = line.split(' ')[0]
  489. if search.match(line):
  490. num += 1
  491. if num == 0:
  492. # mimic os.cpu_count()
  493. return None
  494. return num
  495. def cpu_count_cores():
  496. """Return the number of CPU cores in the system."""
  497. # Method #1
  498. ls = set()
  499. # These 2 files are the same but */core_cpus_list is newer while
  500. # */thread_siblings_list is deprecated and may disappear in the future.
  501. # https://www.kernel.org/doc/Documentation/admin-guide/cputopology.rst
  502. # https://github.com/giampaolo/psutil/pull/1727#issuecomment-707624964
  503. # https://lkml.org/lkml/2019/2/26/41
  504. p1 = "/sys/devices/system/cpu/cpu[0-9]*/topology/core_cpus_list"
  505. p2 = "/sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list"
  506. for path in glob.glob(p1) or glob.glob(p2):
  507. with open_binary(path) as f:
  508. ls.add(f.read().strip())
  509. result = len(ls)
  510. if result != 0:
  511. return result
  512. # Method #2
  513. mapping = {}
  514. current_info = {}
  515. with open_binary(f"{get_procfs_path()}/cpuinfo") as f:
  516. for line in f:
  517. line = line.strip().lower()
  518. if not line:
  519. # new section
  520. try:
  521. mapping[current_info[b'physical id']] = current_info[
  522. b'cpu cores'
  523. ]
  524. except KeyError:
  525. pass
  526. current_info = {}
  527. elif line.startswith((b'physical id', b'cpu cores')):
  528. # ongoing section
  529. key, value = line.split(b'\t:', 1)
  530. current_info[key] = int(value)
  531. result = sum(mapping.values())
  532. return result or None # mimic os.cpu_count()
  533. def cpu_stats():
  534. """Return various CPU stats as a named tuple."""
  535. with open_binary(f"{get_procfs_path()}/stat") as f:
  536. ctx_switches = None
  537. interrupts = None
  538. soft_interrupts = None
  539. for line in f:
  540. if line.startswith(b'ctxt'):
  541. ctx_switches = int(line.split()[1])
  542. elif line.startswith(b'intr'):
  543. interrupts = int(line.split()[1])
  544. elif line.startswith(b'softirq'):
  545. soft_interrupts = int(line.split()[1])
  546. if (
  547. ctx_switches is not None
  548. and soft_interrupts is not None
  549. and interrupts is not None
  550. ):
  551. break
  552. syscalls = 0
  553. return ntp.scpustats(ctx_switches, interrupts, soft_interrupts, syscalls)
  554. def _cpu_get_cpuinfo_freq():
  555. """Return current CPU frequency from cpuinfo if available."""
  556. with open_binary(f"{get_procfs_path()}/cpuinfo") as f:
  557. return [
  558. float(line.split(b':', 1)[1])
  559. for line in f
  560. if line.lower().startswith(b'cpu mhz')
  561. ]
  562. if os.path.exists("/sys/devices/system/cpu/cpufreq/policy0") or os.path.exists(
  563. "/sys/devices/system/cpu/cpu0/cpufreq"
  564. ):
  565. def cpu_freq():
  566. """Return frequency metrics for all CPUs.
  567. Contrarily to other OSes, Linux updates these values in
  568. real-time.
  569. """
  570. cpuinfo_freqs = _cpu_get_cpuinfo_freq()
  571. paths = glob.glob(
  572. "/sys/devices/system/cpu/cpufreq/policy[0-9]*"
  573. ) or glob.glob("/sys/devices/system/cpu/cpu[0-9]*/cpufreq")
  574. paths.sort(key=lambda x: int(re.search(r"[0-9]+", x).group()))
  575. ret = []
  576. pjoin = os.path.join
  577. for i, path in enumerate(paths):
  578. if len(paths) == len(cpuinfo_freqs):
  579. # take cached value from cpuinfo if available, see:
  580. # https://github.com/giampaolo/psutil/issues/1851
  581. curr = cpuinfo_freqs[i] * 1000
  582. else:
  583. curr = bcat(pjoin(path, "scaling_cur_freq"), fallback=None)
  584. if curr is None:
  585. # Likely an old RedHat, see:
  586. # https://github.com/giampaolo/psutil/issues/1071
  587. curr = bcat(pjoin(path, "cpuinfo_cur_freq"), fallback=None)
  588. if curr is None:
  589. online_path = f"/sys/devices/system/cpu/cpu{i}/online"
  590. # if cpu core is offline, set to all zeroes
  591. if cat(online_path, fallback=None) == "0\n":
  592. ret.append(ntp.scpufreq(0.0, 0.0, 0.0))
  593. continue
  594. msg = "can't find current frequency file"
  595. raise NotImplementedError(msg)
  596. curr = int(curr) / 1000
  597. max_ = int(bcat(pjoin(path, "scaling_max_freq"))) / 1000
  598. min_ = int(bcat(pjoin(path, "scaling_min_freq"))) / 1000
  599. ret.append(ntp.scpufreq(curr, min_, max_))
  600. return ret
  601. else:
  602. def cpu_freq():
  603. """Alternate implementation using /proc/cpuinfo.
  604. min and max frequencies are not available and are set to None.
  605. """
  606. return [ntp.scpufreq(x, 0.0, 0.0) for x in _cpu_get_cpuinfo_freq()]
  607. # =====================================================================
  608. # --- network
  609. # =====================================================================
  610. net_if_addrs = cext.net_if_addrs
  611. class _Ipv6UnsupportedError(Exception):
  612. pass
  613. class NetConnections:
  614. """A wrapper on top of /proc/net/* files, retrieving per-process
  615. and system-wide open connections (TCP, UDP, UNIX) similarly to
  616. "netstat -an".
  617. Note: in case of UNIX sockets we're only able to determine the
  618. local endpoint/path, not the one it's connected to.
  619. According to [1] it would be possible but not easily.
  620. [1] http://serverfault.com/a/417946
  621. """
  622. def __init__(self):
  623. # The string represents the basename of the corresponding
  624. # /proc/net/{proto_name} file.
  625. tcp4 = ("tcp", socket.AF_INET, socket.SOCK_STREAM)
  626. tcp6 = ("tcp6", socket.AF_INET6, socket.SOCK_STREAM)
  627. udp4 = ("udp", socket.AF_INET, socket.SOCK_DGRAM)
  628. udp6 = ("udp6", socket.AF_INET6, socket.SOCK_DGRAM)
  629. unix = ("unix", socket.AF_UNIX, None)
  630. self.tmap = {
  631. "all": (tcp4, tcp6, udp4, udp6, unix),
  632. "tcp": (tcp4, tcp6),
  633. "tcp4": (tcp4,),
  634. "tcp6": (tcp6,),
  635. "udp": (udp4, udp6),
  636. "udp4": (udp4,),
  637. "udp6": (udp6,),
  638. "unix": (unix,),
  639. "inet": (tcp4, tcp6, udp4, udp6),
  640. "inet4": (tcp4, udp4),
  641. "inet6": (tcp6, udp6),
  642. }
  643. self._procfs_path = None
  644. def get_proc_inodes(self, pid):
  645. inodes = defaultdict(list)
  646. for fd in os.listdir(f"{self._procfs_path}/{pid}/fd"):
  647. try:
  648. inode = readlink(f"{self._procfs_path}/{pid}/fd/{fd}")
  649. except (FileNotFoundError, ProcessLookupError):
  650. # ENOENT == file which is gone in the meantime;
  651. # os.stat(f"/proc/{self.pid}") will be done later
  652. # to force NSP (if it's the case)
  653. continue
  654. except OSError as err:
  655. if err.errno == errno.EINVAL:
  656. # not a link
  657. continue
  658. if err.errno == errno.ENAMETOOLONG:
  659. # file name too long
  660. debug(err)
  661. continue
  662. raise
  663. else:
  664. if inode.startswith('socket:['):
  665. # the process is using a socket
  666. inode = inode[8:][:-1]
  667. inodes[inode].append((pid, int(fd)))
  668. return inodes
  669. def get_all_inodes(self):
  670. inodes = {}
  671. for pid in pids():
  672. try:
  673. inodes.update(self.get_proc_inodes(pid))
  674. except (FileNotFoundError, ProcessLookupError, PermissionError):
  675. # os.listdir() is gonna raise a lot of access denied
  676. # exceptions in case of unprivileged user; that's fine
  677. # as we'll just end up returning a connection with PID
  678. # and fd set to None anyway.
  679. # Both netstat -an and lsof does the same so it's
  680. # unlikely we can do any better.
  681. # ENOENT just means a PID disappeared on us.
  682. continue
  683. return inodes
  684. @staticmethod
  685. def decode_address(addr, family):
  686. """Accept an "ip:port" address as displayed in /proc/net/*
  687. and convert it into a human readable form, like:
  688. "0500000A:0016" -> ("10.0.0.5", 22)
  689. "0000000000000000FFFF00000100007F:9E49" -> ("::ffff:127.0.0.1", 40521)
  690. The IP address portion is a little or big endian four-byte
  691. hexadecimal number; that is, the least significant byte is listed
  692. first, so we need to reverse the order of the bytes to convert it
  693. to an IP address.
  694. The port is represented as a two-byte hexadecimal number.
  695. Reference:
  696. http://linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html
  697. """
  698. ip, port = addr.split(':')
  699. port = int(port, 16)
  700. # this usually refers to a local socket in listen mode with
  701. # no end-points connected
  702. if not port:
  703. return ()
  704. ip = ip.encode('ascii')
  705. if family == socket.AF_INET:
  706. # see: https://github.com/giampaolo/psutil/issues/201
  707. if LITTLE_ENDIAN:
  708. ip = socket.inet_ntop(family, base64.b16decode(ip)[::-1])
  709. else:
  710. ip = socket.inet_ntop(family, base64.b16decode(ip))
  711. else: # IPv6
  712. ip = base64.b16decode(ip)
  713. try:
  714. # see: https://github.com/giampaolo/psutil/issues/201
  715. if LITTLE_ENDIAN:
  716. ip = socket.inet_ntop(
  717. socket.AF_INET6,
  718. struct.pack('>4I', *struct.unpack('<4I', ip)),
  719. )
  720. else:
  721. ip = socket.inet_ntop(
  722. socket.AF_INET6,
  723. struct.pack('<4I', *struct.unpack('<4I', ip)),
  724. )
  725. except ValueError:
  726. # see: https://github.com/giampaolo/psutil/issues/623
  727. if not supports_ipv6():
  728. raise _Ipv6UnsupportedError from None
  729. raise
  730. return ntp.addr(ip, port)
  731. @staticmethod
  732. def process_inet(file, family, type_, inodes, filter_pid=None):
  733. """Parse /proc/net/tcp* and /proc/net/udp* files."""
  734. if file.endswith('6') and not os.path.exists(file):
  735. # IPv6 not supported
  736. return
  737. with open_text(file) as f:
  738. f.readline() # skip the first line
  739. for lineno, line in enumerate(f, 1):
  740. try:
  741. _, laddr, raddr, status, _, _, _, _, _, inode = (
  742. line.split()[:10]
  743. )
  744. except ValueError:
  745. msg = (
  746. f"error while parsing {file}; malformed line"
  747. f" {lineno} {line!r}"
  748. )
  749. raise RuntimeError(msg) from None
  750. if inode in inodes:
  751. # # We assume inet sockets are unique, so we error
  752. # # out if there are multiple references to the
  753. # # same inode. We won't do this for UNIX sockets.
  754. # if len(inodes[inode]) > 1 and family != socket.AF_UNIX:
  755. # raise ValueError("ambiguous inode with multiple "
  756. # "PIDs references")
  757. pid, fd = inodes[inode][0]
  758. else:
  759. pid, fd = None, -1
  760. if filter_pid is not None and filter_pid != pid:
  761. continue
  762. else:
  763. if type_ == socket.SOCK_STREAM:
  764. status = TCP_STATUSES[status]
  765. else:
  766. status = _common.CONN_NONE
  767. try:
  768. laddr = NetConnections.decode_address(laddr, family)
  769. raddr = NetConnections.decode_address(raddr, family)
  770. except _Ipv6UnsupportedError:
  771. continue
  772. yield (fd, family, type_, laddr, raddr, status, pid)
  773. @staticmethod
  774. def process_unix(file, family, inodes, filter_pid=None):
  775. """Parse /proc/net/unix files."""
  776. with open_text(file) as f:
  777. f.readline() # skip the first line
  778. for line in f:
  779. tokens = line.split()
  780. try:
  781. _, _, _, _, type_, _, inode = tokens[0:7]
  782. except ValueError:
  783. if ' ' not in line:
  784. # see: https://github.com/giampaolo/psutil/issues/766
  785. continue
  786. msg = (
  787. f"error while parsing {file}; malformed line {line!r}"
  788. )
  789. raise RuntimeError(msg) # noqa: B904
  790. if inode in inodes: # noqa: SIM108
  791. # With UNIX sockets we can have a single inode
  792. # referencing many file descriptors.
  793. pairs = inodes[inode]
  794. else:
  795. pairs = [(None, -1)]
  796. for pid, fd in pairs:
  797. if filter_pid is not None and filter_pid != pid:
  798. continue
  799. else:
  800. path = tokens[-1] if len(tokens) == 8 else ''
  801. type_ = _common.socktype_to_enum(int(type_))
  802. # XXX: determining the remote endpoint of a
  803. # UNIX socket on Linux is not possible, see:
  804. # https://serverfault.com/questions/252723/
  805. raddr = ""
  806. status = _common.CONN_NONE
  807. yield (fd, family, type_, path, raddr, status, pid)
  808. def retrieve(self, kind, pid=None):
  809. self._procfs_path = get_procfs_path()
  810. if pid is not None:
  811. inodes = self.get_proc_inodes(pid)
  812. if not inodes:
  813. # no connections for this process
  814. return []
  815. else:
  816. inodes = self.get_all_inodes()
  817. ret = set()
  818. for proto_name, family, type_ in self.tmap[kind]:
  819. path = f"{self._procfs_path}/net/{proto_name}"
  820. if family in {socket.AF_INET, socket.AF_INET6}:
  821. ls = self.process_inet(
  822. path, family, type_, inodes, filter_pid=pid
  823. )
  824. else:
  825. ls = self.process_unix(path, family, inodes, filter_pid=pid)
  826. for fd, family, type_, laddr, raddr, status, bound_pid in ls:
  827. if pid:
  828. conn = ntp.pconn(fd, family, type_, laddr, raddr, status)
  829. else:
  830. conn = ntp.sconn(
  831. fd, family, type_, laddr, raddr, status, bound_pid
  832. )
  833. ret.add(conn)
  834. return list(ret)
  835. _net_connections = NetConnections()
  836. def net_connections(kind='inet'):
  837. """Return system-wide open connections."""
  838. return _net_connections.retrieve(kind)
  839. def net_io_counters():
  840. """Return network I/O statistics for every network interface
  841. installed on the system as a dict of raw tuples.
  842. """
  843. with open_text(f"{get_procfs_path()}/net/dev") as f:
  844. lines = f.readlines()
  845. retdict = {}
  846. for line in lines[2:]:
  847. colon = line.rfind(':')
  848. assert colon > 0, repr(line)
  849. name = line[:colon].strip()
  850. fields = line[colon + 1 :].strip().split()
  851. (
  852. # in
  853. bytes_recv,
  854. packets_recv,
  855. errin,
  856. dropin,
  857. _fifoin, # unused
  858. _framein, # unused
  859. _compressedin, # unused
  860. _multicastin, # unused
  861. # out
  862. bytes_sent,
  863. packets_sent,
  864. errout,
  865. dropout,
  866. _fifoout, # unused
  867. _collisionsout, # unused
  868. _carrierout, # unused
  869. _compressedout, # unused
  870. ) = map(int, fields)
  871. retdict[name] = (
  872. bytes_sent,
  873. bytes_recv,
  874. packets_sent,
  875. packets_recv,
  876. errin,
  877. errout,
  878. dropin,
  879. dropout,
  880. )
  881. return retdict
  882. def net_if_stats():
  883. """Get NIC stats (isup, duplex, speed, mtu)."""
  884. duplex_map = {
  885. cext.DUPLEX_FULL: NIC_DUPLEX_FULL,
  886. cext.DUPLEX_HALF: NIC_DUPLEX_HALF,
  887. cext.DUPLEX_UNKNOWN: NIC_DUPLEX_UNKNOWN,
  888. }
  889. names = net_io_counters().keys()
  890. ret = {}
  891. for name in names:
  892. try:
  893. mtu = cext.net_if_mtu(name)
  894. flags = cext.net_if_flags(name)
  895. duplex, speed = cext.net_if_duplex_speed(name)
  896. except OSError as err:
  897. # https://github.com/giampaolo/psutil/issues/1279
  898. if err.errno != errno.ENODEV:
  899. raise
  900. debug(err)
  901. else:
  902. output_flags = ','.join(flags)
  903. isup = 'running' in flags
  904. ret[name] = ntp.snicstats(
  905. isup, duplex_map[duplex], speed, mtu, output_flags
  906. )
  907. return ret
  908. # =====================================================================
  909. # --- disks
  910. # =====================================================================
  911. disk_usage = _psposix.disk_usage
  912. def disk_io_counters(perdisk=False):
  913. """Return disk I/O statistics for every disk installed on the
  914. system as a dict of raw tuples.
  915. """
  916. def read_procfs():
  917. # OK, this is a bit confusing. The format of /proc/diskstats can
  918. # have 3 variations.
  919. # On Linux 2.4 each line has always 15 fields, e.g.:
  920. # "3 0 8 hda 8 8 8 8 8 8 8 8 8 8 8"
  921. # On Linux 2.6+ each line *usually* has 14 fields, and the disk
  922. # name is in another position, like this:
  923. # "3 0 hda 8 8 8 8 8 8 8 8 8 8 8"
  924. # ...unless (Linux 2.6) the line refers to a partition instead
  925. # of a disk, in which case the line has less fields (7):
  926. # "3 1 hda1 8 8 8 8"
  927. # 4.18+ has 4 fields added:
  928. # "3 0 hda 8 8 8 8 8 8 8 8 8 8 8 0 0 0 0"
  929. # 5.5 has 2 more fields.
  930. # See:
  931. # https://www.kernel.org/doc/Documentation/iostats.txt
  932. # https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats
  933. with open_text(f"{get_procfs_path()}/diskstats") as f:
  934. lines = f.readlines()
  935. for line in lines:
  936. fields = line.split()
  937. flen = len(fields)
  938. # fmt: off
  939. if flen == 15:
  940. # Linux 2.4
  941. name = fields[3]
  942. reads = int(fields[2])
  943. (reads_merged, rbytes, rtime, writes, writes_merged,
  944. wbytes, wtime, _, busy_time, _) = map(int, fields[4:14])
  945. elif flen == 14 or flen >= 18:
  946. # Linux 2.6+, line referring to a disk
  947. name = fields[2]
  948. (reads, reads_merged, rbytes, rtime, writes, writes_merged,
  949. wbytes, wtime, _, busy_time, _) = map(int, fields[3:14])
  950. elif flen == 7:
  951. # Linux 2.6+, line referring to a partition
  952. name = fields[2]
  953. reads, rbytes, writes, wbytes = map(int, fields[3:])
  954. rtime = wtime = reads_merged = writes_merged = busy_time = 0
  955. else:
  956. msg = f"not sure how to interpret line {line!r}"
  957. raise ValueError(msg)
  958. yield (name, reads, writes, rbytes, wbytes, rtime, wtime,
  959. reads_merged, writes_merged, busy_time)
  960. # fmt: on
  961. def read_sysfs():
  962. for block in os.listdir('/sys/block'):
  963. for root, _, files in os.walk(os.path.join('/sys/block', block)):
  964. if 'stat' not in files:
  965. continue
  966. with open_text(os.path.join(root, 'stat')) as f:
  967. fields = f.read().strip().split()
  968. name = os.path.basename(root)
  969. # fmt: off
  970. (reads, reads_merged, rbytes, rtime, writes, writes_merged,
  971. wbytes, wtime, _, busy_time) = map(int, fields[:10])
  972. yield (name, reads, writes, rbytes, wbytes, rtime,
  973. wtime, reads_merged, writes_merged, busy_time)
  974. # fmt: on
  975. if os.path.exists(f"{get_procfs_path()}/diskstats"):
  976. gen = read_procfs()
  977. elif os.path.exists('/sys/block'):
  978. gen = read_sysfs()
  979. else:
  980. msg = (
  981. f"{get_procfs_path()}/diskstats nor /sys/block are available on"
  982. " this system"
  983. )
  984. raise NotImplementedError(msg)
  985. retdict = {}
  986. for entry in gen:
  987. # fmt: off
  988. (name, reads, writes, rbytes, wbytes, rtime, wtime, reads_merged,
  989. writes_merged, busy_time) = entry
  990. if not perdisk and not is_storage_device(name):
  991. # perdisk=False means we want to calculate totals so we skip
  992. # partitions (e.g. 'sda1', 'nvme0n1p1') and only include
  993. # base disk devices (e.g. 'sda', 'nvme0n1'). Base disks
  994. # include a total of all their partitions + some extra size
  995. # of their own:
  996. # $ cat /proc/diskstats
  997. # 259 0 sda 10485760 ...
  998. # 259 1 sda1 5186039 ...
  999. # 259 1 sda2 5082039 ...
  1000. # See:
  1001. # https://github.com/giampaolo/psutil/pull/1313
  1002. continue
  1003. rbytes *= DISK_SECTOR_SIZE
  1004. wbytes *= DISK_SECTOR_SIZE
  1005. retdict[name] = (reads, writes, rbytes, wbytes, rtime, wtime,
  1006. reads_merged, writes_merged, busy_time)
  1007. # fmt: on
  1008. return retdict
  1009. class RootFsDeviceFinder:
  1010. """disk_partitions() may return partitions with device == "/dev/root"
  1011. or "rootfs". This container class uses different strategies to try to
  1012. obtain the real device path. Resources:
  1013. https://bootlin.com/blog/find-root-device/
  1014. https://www.systutorials.com/how-to-find-the-disk-where-root-is-on-in-bash-on-linux/.
  1015. """
  1016. __slots__ = ['major', 'minor']
  1017. def __init__(self):
  1018. dev = os.stat("/").st_dev
  1019. self.major = os.major(dev)
  1020. self.minor = os.minor(dev)
  1021. def ask_proc_partitions(self):
  1022. with open_text(f"{get_procfs_path()}/partitions") as f:
  1023. for line in f.readlines()[2:]:
  1024. fields = line.split()
  1025. if len(fields) < 4: # just for extra safety
  1026. continue
  1027. major = int(fields[0]) if fields[0].isdigit() else None
  1028. minor = int(fields[1]) if fields[1].isdigit() else None
  1029. name = fields[3]
  1030. if major == self.major and minor == self.minor:
  1031. if name: # just for extra safety
  1032. return f"/dev/{name}"
  1033. def ask_sys_dev_block(self):
  1034. path = f"/sys/dev/block/{self.major}:{self.minor}/uevent"
  1035. with open_text(path) as f:
  1036. for line in f:
  1037. if line.startswith("DEVNAME="):
  1038. name = line.strip().rpartition("DEVNAME=")[2]
  1039. if name: # just for extra safety
  1040. return f"/dev/{name}"
  1041. def ask_sys_class_block(self):
  1042. needle = f"{self.major}:{self.minor}"
  1043. files = glob.iglob("/sys/class/block/*/dev")
  1044. for file in files:
  1045. try:
  1046. f = open_text(file)
  1047. except FileNotFoundError: # race condition
  1048. continue
  1049. else:
  1050. with f:
  1051. data = f.read().strip()
  1052. if data == needle:
  1053. name = os.path.basename(os.path.dirname(file))
  1054. return f"/dev/{name}"
  1055. def find(self):
  1056. path = None
  1057. if path is None:
  1058. try:
  1059. path = self.ask_proc_partitions()
  1060. except OSError as err:
  1061. debug(err)
  1062. if path is None:
  1063. try:
  1064. path = self.ask_sys_dev_block()
  1065. except OSError as err:
  1066. debug(err)
  1067. if path is None:
  1068. try:
  1069. path = self.ask_sys_class_block()
  1070. except OSError as err:
  1071. debug(err)
  1072. # We use exists() because the "/dev/*" part of the path is hard
  1073. # coded, so we want to be sure.
  1074. if path is not None and os.path.exists(path):
  1075. return path
  1076. def disk_partitions(all=False):
  1077. """Return mounted disk partitions as a list of namedtuples."""
  1078. fstypes = set()
  1079. procfs_path = get_procfs_path()
  1080. if not all:
  1081. with open_text(f"{procfs_path}/filesystems") as f:
  1082. for line in f:
  1083. line = line.strip()
  1084. if not line.startswith("nodev"):
  1085. fstypes.add(line.strip())
  1086. else:
  1087. # ignore all lines starting with "nodev" except "nodev zfs"
  1088. fstype = line.split("\t")[1]
  1089. if fstype == "zfs":
  1090. fstypes.add("zfs")
  1091. # See: https://github.com/giampaolo/psutil/issues/1307
  1092. if procfs_path == "/proc" and os.path.isfile('/etc/mtab'):
  1093. mounts_path = os.path.realpath("/etc/mtab")
  1094. else:
  1095. mounts_path = os.path.realpath(f"{procfs_path}/self/mounts")
  1096. retlist = []
  1097. partitions = cext.disk_partitions(mounts_path)
  1098. for partition in partitions:
  1099. device, mountpoint, fstype, opts = partition
  1100. if device == 'none':
  1101. device = ''
  1102. if device in {"/dev/root", "rootfs"}:
  1103. device = RootFsDeviceFinder().find() or device
  1104. if not all:
  1105. if not device or fstype not in fstypes:
  1106. continue
  1107. ntuple = ntp.sdiskpart(device, mountpoint, fstype, opts)
  1108. retlist.append(ntuple)
  1109. return retlist
  1110. # =====================================================================
  1111. # --- sensors
  1112. # =====================================================================
  1113. def sensors_temperatures():
  1114. """Return hardware (CPU and others) temperatures as a dict
  1115. including hardware name, label, current, max and critical
  1116. temperatures.
  1117. Implementation notes:
  1118. - /sys/class/hwmon looks like the most recent interface to
  1119. retrieve this info, and this implementation relies on it
  1120. only (old distros will probably use something else)
  1121. - lm-sensors on Ubuntu 16.04 relies on /sys/class/hwmon
  1122. - /sys/class/thermal/thermal_zone* is another one but it's more
  1123. difficult to parse
  1124. """
  1125. ret = collections.defaultdict(list)
  1126. basenames = glob.glob('/sys/class/hwmon/hwmon*/temp*_*')
  1127. # CentOS has an intermediate /device directory:
  1128. # https://github.com/giampaolo/psutil/issues/971
  1129. # https://github.com/nicolargo/glances/issues/1060
  1130. basenames.extend(glob.glob('/sys/class/hwmon/hwmon*/device/temp*_*'))
  1131. basenames = sorted({x.split('_')[0] for x in basenames})
  1132. # Only add the coretemp hwmon entries if they're not already in
  1133. # /sys/class/hwmon/
  1134. # https://github.com/giampaolo/psutil/issues/1708
  1135. # https://github.com/giampaolo/psutil/pull/1648
  1136. basenames2 = glob.glob(
  1137. '/sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_*'
  1138. )
  1139. repl = re.compile(r"/sys/devices/platform/coretemp.*/hwmon/")
  1140. for name in basenames2:
  1141. altname = repl.sub('/sys/class/hwmon/', name)
  1142. if altname not in basenames:
  1143. basenames.append(name)
  1144. for base in basenames:
  1145. try:
  1146. path = base + '_input'
  1147. current = float(bcat(path)) / 1000.0
  1148. path = os.path.join(os.path.dirname(base), 'name')
  1149. unit_name = cat(path).strip()
  1150. except (OSError, ValueError):
  1151. # A lot of things can go wrong here, so let's just skip the
  1152. # whole entry. Sure thing is Linux's /sys/class/hwmon really
  1153. # is a stinky broken mess.
  1154. # https://github.com/giampaolo/psutil/issues/1009
  1155. # https://github.com/giampaolo/psutil/issues/1101
  1156. # https://github.com/giampaolo/psutil/issues/1129
  1157. # https://github.com/giampaolo/psutil/issues/1245
  1158. # https://github.com/giampaolo/psutil/issues/1323
  1159. continue
  1160. high = bcat(base + '_max', fallback=None)
  1161. critical = bcat(base + '_crit', fallback=None)
  1162. label = cat(base + '_label', fallback='').strip()
  1163. if high is not None:
  1164. try:
  1165. high = float(high) / 1000.0
  1166. except ValueError:
  1167. high = None
  1168. if critical is not None:
  1169. try:
  1170. critical = float(critical) / 1000.0
  1171. except ValueError:
  1172. critical = None
  1173. ret[unit_name].append((label, current, high, critical))
  1174. # Indication that no sensors were detected in /sys/class/hwmon/
  1175. if not basenames:
  1176. basenames = glob.glob('/sys/class/thermal/thermal_zone*')
  1177. basenames = sorted(set(basenames))
  1178. for base in basenames:
  1179. try:
  1180. path = os.path.join(base, 'temp')
  1181. current = float(bcat(path)) / 1000.0
  1182. path = os.path.join(base, 'type')
  1183. unit_name = cat(path).strip()
  1184. except (OSError, ValueError) as err:
  1185. debug(err)
  1186. continue
  1187. trip_paths = glob.glob(base + '/trip_point*')
  1188. trip_points = {
  1189. '_'.join(os.path.basename(p).split('_')[0:3])
  1190. for p in trip_paths
  1191. }
  1192. critical = None
  1193. high = None
  1194. for trip_point in trip_points:
  1195. path = os.path.join(base, trip_point + "_type")
  1196. trip_type = cat(path, fallback='').strip()
  1197. if trip_type == 'critical':
  1198. critical = bcat(
  1199. os.path.join(base, trip_point + "_temp"), fallback=None
  1200. )
  1201. elif trip_type == 'high':
  1202. high = bcat(
  1203. os.path.join(base, trip_point + "_temp"), fallback=None
  1204. )
  1205. if high is not None:
  1206. try:
  1207. high = float(high) / 1000.0
  1208. except ValueError:
  1209. high = None
  1210. if critical is not None:
  1211. try:
  1212. critical = float(critical) / 1000.0
  1213. except ValueError:
  1214. critical = None
  1215. ret[unit_name].append(('', current, high, critical))
  1216. return dict(ret)
  1217. def sensors_fans():
  1218. """Return hardware fans info (for CPU and other peripherals) as a
  1219. dict including hardware label and current speed.
  1220. Implementation notes:
  1221. - /sys/class/hwmon looks like the most recent interface to
  1222. retrieve this info, and this implementation relies on it
  1223. only (old distros will probably use something else)
  1224. - lm-sensors on Ubuntu 16.04 relies on /sys/class/hwmon
  1225. """
  1226. ret = collections.defaultdict(list)
  1227. basenames = glob.glob('/sys/class/hwmon/hwmon*/fan*_*')
  1228. if not basenames:
  1229. # CentOS has an intermediate /device directory:
  1230. # https://github.com/giampaolo/psutil/issues/971
  1231. basenames = glob.glob('/sys/class/hwmon/hwmon*/device/fan*_*')
  1232. basenames = sorted({x.split("_")[0] for x in basenames})
  1233. for base in basenames:
  1234. try:
  1235. current = int(bcat(base + '_input'))
  1236. except OSError as err:
  1237. debug(err)
  1238. continue
  1239. unit_name = cat(os.path.join(os.path.dirname(base), 'name')).strip()
  1240. label = cat(base + '_label', fallback='').strip()
  1241. ret[unit_name].append(ntp.sfan(label, current))
  1242. return dict(ret)
  1243. def sensors_battery():
  1244. """Return battery information.
  1245. Implementation note: it appears /sys/class/power_supply/BAT0/
  1246. directory structure may vary and provide files with the same
  1247. meaning but under different names, see:
  1248. https://github.com/giampaolo/psutil/issues/966.
  1249. """
  1250. null = object()
  1251. def multi_bcat(*paths):
  1252. """Attempt to read the content of multiple files which may
  1253. not exist. If none of them exist return None.
  1254. """
  1255. for path in paths:
  1256. ret = bcat(path, fallback=null)
  1257. if ret != null:
  1258. try:
  1259. return int(ret)
  1260. except ValueError:
  1261. return ret.strip()
  1262. return None
  1263. bats = [
  1264. x
  1265. for x in os.listdir(POWER_SUPPLY_PATH)
  1266. if x.startswith('BAT') or 'battery' in x.lower()
  1267. ]
  1268. if not bats:
  1269. return None
  1270. # Get the first available battery. Usually this is "BAT0", except
  1271. # some rare exceptions:
  1272. # https://github.com/giampaolo/psutil/issues/1238
  1273. root = os.path.join(POWER_SUPPLY_PATH, min(bats))
  1274. # Base metrics.
  1275. energy_now = multi_bcat(root + "/energy_now", root + "/charge_now")
  1276. power_now = multi_bcat(root + "/power_now", root + "/current_now")
  1277. energy_full = multi_bcat(root + "/energy_full", root + "/charge_full")
  1278. time_to_empty = multi_bcat(root + "/time_to_empty_now")
  1279. # Percent. If we have energy_full the percentage will be more
  1280. # accurate compared to reading /capacity file (float vs. int).
  1281. if energy_full is not None and energy_now is not None:
  1282. try:
  1283. percent = 100.0 * energy_now / energy_full
  1284. except ZeroDivisionError:
  1285. percent = 0.0
  1286. else:
  1287. percent = int(cat(root + "/capacity", fallback=-1))
  1288. if percent == -1:
  1289. return None
  1290. # Is AC power cable plugged in?
  1291. # Note: AC0 is not always available and sometimes (e.g. CentOS7)
  1292. # it's called "AC".
  1293. power_plugged = None
  1294. online = multi_bcat(
  1295. os.path.join(POWER_SUPPLY_PATH, "AC0/online"),
  1296. os.path.join(POWER_SUPPLY_PATH, "AC/online"),
  1297. )
  1298. if online is not None:
  1299. power_plugged = online == 1
  1300. else:
  1301. status = cat(root + "/status", fallback="").strip().lower()
  1302. if status == "discharging":
  1303. power_plugged = False
  1304. elif status in {"charging", "full"}:
  1305. power_plugged = True
  1306. # Seconds left.
  1307. # Note to self: we may also calculate the charging ETA as per:
  1308. # https://github.com/thialfihar/dotfiles/blob/
  1309. # 013937745fd9050c30146290e8f963d65c0179e6/bin/battery.py#L55
  1310. if power_plugged:
  1311. secsleft = _common.POWER_TIME_UNLIMITED
  1312. elif energy_now is not None and power_now is not None:
  1313. try:
  1314. secsleft = int(energy_now / abs(power_now) * 3600)
  1315. except ZeroDivisionError:
  1316. secsleft = _common.POWER_TIME_UNKNOWN
  1317. elif time_to_empty is not None:
  1318. secsleft = int(time_to_empty * 60)
  1319. if secsleft < 0:
  1320. secsleft = _common.POWER_TIME_UNKNOWN
  1321. else:
  1322. secsleft = _common.POWER_TIME_UNKNOWN
  1323. return ntp.sbattery(percent, secsleft, power_plugged)
  1324. # =====================================================================
  1325. # --- other system functions
  1326. # =====================================================================
  1327. def users():
  1328. """Return currently connected users as a list of namedtuples."""
  1329. retlist = []
  1330. rawlist = cext.users()
  1331. for item in rawlist:
  1332. user, tty, hostname, tstamp, pid = item
  1333. nt = ntp.suser(user, tty or None, hostname, tstamp, pid)
  1334. retlist.append(nt)
  1335. return retlist
  1336. def boot_time():
  1337. """Return the system boot time expressed in seconds since the epoch."""
  1338. path = f"{get_procfs_path()}/stat"
  1339. with open_binary(path) as f:
  1340. for line in f:
  1341. if line.startswith(b'btime'):
  1342. return float(line.strip().split()[1])
  1343. msg = f"line 'btime' not found in {path}"
  1344. raise RuntimeError(msg)
  1345. # =====================================================================
  1346. # --- processes
  1347. # =====================================================================
  1348. def pids():
  1349. """Returns a list of PIDs currently running on the system."""
  1350. path = get_procfs_path().encode(ENCODING)
  1351. return [int(x) for x in os.listdir(path) if x.isdigit()]
  1352. def pid_exists(pid):
  1353. """Check for the existence of a unix PID. Linux TIDs are not
  1354. supported (always return False).
  1355. """
  1356. if not _psposix.pid_exists(pid):
  1357. return False
  1358. else:
  1359. # Linux's apparently does not distinguish between PIDs and TIDs
  1360. # (thread IDs).
  1361. # listdir("/proc") won't show any TID (only PIDs) but
  1362. # os.stat("/proc/{tid}") will succeed if {tid} exists.
  1363. # os.kill() can also be passed a TID. This is quite confusing.
  1364. # In here we want to enforce this distinction and support PIDs
  1365. # only, see:
  1366. # https://github.com/giampaolo/psutil/issues/687
  1367. try:
  1368. # Note: already checked that this is faster than using a
  1369. # regular expr. Also (a lot) faster than doing
  1370. # 'return pid in pids()'
  1371. path = f"{get_procfs_path()}/{pid}/status"
  1372. with open_binary(path) as f:
  1373. for line in f:
  1374. if line.startswith(b"Tgid:"):
  1375. tgid = int(line.split()[1])
  1376. # If tgid and pid are the same then we're
  1377. # dealing with a process PID.
  1378. return tgid == pid
  1379. msg = f"'Tgid' line not found in {path}"
  1380. raise ValueError(msg)
  1381. except (OSError, ValueError):
  1382. return pid in pids()
  1383. def ppid_map():
  1384. """Obtain a {pid: ppid, ...} dict for all running processes in
  1385. one shot. Used to speed up Process.children().
  1386. """
  1387. ret = {}
  1388. procfs_path = get_procfs_path()
  1389. for pid in pids():
  1390. try:
  1391. with open_binary(f"{procfs_path}/{pid}/stat") as f:
  1392. data = f.read()
  1393. except (FileNotFoundError, ProcessLookupError):
  1394. pass
  1395. except PermissionError as err:
  1396. raise AccessDenied(pid) from err
  1397. else:
  1398. rpar = data.rfind(b')')
  1399. dset = data[rpar + 2 :].split()
  1400. ppid = int(dset[1])
  1401. ret[pid] = ppid
  1402. return ret
  1403. def wrap_exceptions(fun):
  1404. """Decorator which translates bare OSError exceptions into
  1405. NoSuchProcess and AccessDenied.
  1406. """
  1407. @functools.wraps(fun)
  1408. def wrapper(self, *args, **kwargs):
  1409. pid, name = self.pid, self._name
  1410. try:
  1411. return fun(self, *args, **kwargs)
  1412. except PermissionError as err:
  1413. raise AccessDenied(pid, name) from err
  1414. except ProcessLookupError as err:
  1415. self._raise_if_zombie()
  1416. raise NoSuchProcess(pid, name) from err
  1417. except FileNotFoundError as err:
  1418. self._raise_if_zombie()
  1419. # /proc/PID directory may still exist, but the files within
  1420. # it may not, indicating the process is gone, see:
  1421. # https://github.com/giampaolo/psutil/issues/2418
  1422. if not os.path.exists(f"{self._procfs_path}/{pid}/stat"):
  1423. raise NoSuchProcess(pid, name) from err
  1424. raise
  1425. return wrapper
  1426. class Process:
  1427. """Linux process implementation."""
  1428. __slots__ = [
  1429. "_cache",
  1430. "_ctime",
  1431. "_name",
  1432. "_ppid",
  1433. "_procfs_path",
  1434. "pid",
  1435. ]
  1436. def __init__(self, pid):
  1437. self.pid = pid
  1438. self._name = None
  1439. self._ppid = None
  1440. self._ctime = None
  1441. self._procfs_path = get_procfs_path()
  1442. def _is_zombie(self):
  1443. # Note: most of the times Linux is able to return info about the
  1444. # process even if it's a zombie, and /proc/{pid} will exist.
  1445. # There are some exceptions though, like exe(), cmdline() and
  1446. # memory_maps(). In these cases /proc/{pid}/{file} exists but
  1447. # it's empty. Instead of returning a "null" value we'll raise an
  1448. # exception.
  1449. try:
  1450. data = bcat(f"{self._procfs_path}/{self.pid}/stat")
  1451. except OSError:
  1452. return False
  1453. else:
  1454. rpar = data.rfind(b')')
  1455. status = data[rpar + 2 : rpar + 3]
  1456. return status == b"Z"
  1457. def _raise_if_zombie(self):
  1458. if self._is_zombie():
  1459. raise ZombieProcess(self.pid, self._name, self._ppid)
  1460. def _raise_if_not_alive(self):
  1461. """Raise NSP if the process disappeared on us."""
  1462. # For those C function who do not raise NSP, possibly returning
  1463. # incorrect or incomplete result.
  1464. os.stat(f"{self._procfs_path}/{self.pid}")
  1465. def _readlink(self, path, fallback=UNSET):
  1466. # * https://github.com/giampaolo/psutil/issues/503
  1467. # os.readlink('/proc/pid/exe') may raise ESRCH (ProcessLookupError)
  1468. # instead of ENOENT (FileNotFoundError) when it races.
  1469. # * ENOENT may occur also if the path actually exists if PID is
  1470. # a low PID (~0-20 range).
  1471. # * https://github.com/giampaolo/psutil/issues/2514
  1472. try:
  1473. return readlink(path)
  1474. except (FileNotFoundError, ProcessLookupError):
  1475. if os.path.lexists(f"{self._procfs_path}/{self.pid}"):
  1476. self._raise_if_zombie()
  1477. if fallback is not UNSET:
  1478. return fallback
  1479. raise
  1480. @wrap_exceptions
  1481. @memoize_when_activated
  1482. def _parse_stat_file(self):
  1483. """Parse /proc/{pid}/stat file and return a dict with various
  1484. process info.
  1485. Using "man proc" as a reference: where "man proc" refers to
  1486. position N always subtract 3 (e.g ppid position 4 in
  1487. 'man proc' == position 1 in here).
  1488. The return value is cached in case oneshot() ctx manager is
  1489. in use.
  1490. """
  1491. data = bcat(f"{self._procfs_path}/{self.pid}/stat")
  1492. # Process name is between parentheses. It can contain spaces and
  1493. # other parentheses. This is taken into account by looking for
  1494. # the first occurrence of "(" and the last occurrence of ")".
  1495. rpar = data.rfind(b')')
  1496. name = data[data.find(b'(') + 1 : rpar]
  1497. fields = data[rpar + 2 :].split()
  1498. ret = {}
  1499. ret['name'] = name
  1500. ret['status'] = fields[0]
  1501. ret['ppid'] = fields[1]
  1502. ret['ttynr'] = fields[4]
  1503. ret['utime'] = fields[11]
  1504. ret['stime'] = fields[12]
  1505. ret['children_utime'] = fields[13]
  1506. ret['children_stime'] = fields[14]
  1507. ret['create_time'] = fields[19]
  1508. ret['cpu_num'] = fields[36]
  1509. try:
  1510. ret['blkio_ticks'] = fields[39] # aka 'delayacct_blkio_ticks'
  1511. except IndexError:
  1512. # https://github.com/giampaolo/psutil/issues/2455
  1513. debug("can't get blkio_ticks, set iowait to 0")
  1514. ret['blkio_ticks'] = 0
  1515. return ret
  1516. @wrap_exceptions
  1517. @memoize_when_activated
  1518. def _read_status_file(self):
  1519. """Read /proc/{pid}/stat file and return its content.
  1520. The return value is cached in case oneshot() ctx manager is
  1521. in use.
  1522. """
  1523. with open_binary(f"{self._procfs_path}/{self.pid}/status") as f:
  1524. return f.read()
  1525. @wrap_exceptions
  1526. @memoize_when_activated
  1527. def _read_smaps_file(self):
  1528. with open_binary(f"{self._procfs_path}/{self.pid}/smaps") as f:
  1529. return f.read().strip()
  1530. def oneshot_enter(self):
  1531. self._parse_stat_file.cache_activate(self)
  1532. self._read_status_file.cache_activate(self)
  1533. self._read_smaps_file.cache_activate(self)
  1534. def oneshot_exit(self):
  1535. self._parse_stat_file.cache_deactivate(self)
  1536. self._read_status_file.cache_deactivate(self)
  1537. self._read_smaps_file.cache_deactivate(self)
  1538. @wrap_exceptions
  1539. def name(self):
  1540. # XXX - gets changed later and probably needs refactoring
  1541. return decode(self._parse_stat_file()['name'])
  1542. @wrap_exceptions
  1543. def exe(self):
  1544. return self._readlink(
  1545. f"{self._procfs_path}/{self.pid}/exe", fallback=""
  1546. )
  1547. @wrap_exceptions
  1548. def cmdline(self):
  1549. with open_text(f"{self._procfs_path}/{self.pid}/cmdline") as f:
  1550. data = f.read()
  1551. if not data:
  1552. # may happen in case of zombie process
  1553. self._raise_if_zombie()
  1554. return []
  1555. # 'man proc' states that args are separated by null bytes '\0'
  1556. # and last char is supposed to be a null byte. Nevertheless
  1557. # some processes may change their cmdline after being started
  1558. # (via setproctitle() or similar), they are usually not
  1559. # compliant with this rule and use spaces instead. Google
  1560. # Chrome process is an example. See:
  1561. # https://github.com/giampaolo/psutil/issues/1179
  1562. sep = '\x00' if data.endswith('\x00') else ' '
  1563. if data.endswith(sep):
  1564. data = data[:-1]
  1565. cmdline = data.split(sep)
  1566. # Sometimes last char is a null byte '\0' but the args are
  1567. # separated by spaces, see: https://github.com/giampaolo/psutil/
  1568. # issues/1179#issuecomment-552984549
  1569. if sep == '\x00' and len(cmdline) == 1 and ' ' in data:
  1570. cmdline = data.split(' ')
  1571. return cmdline
  1572. @wrap_exceptions
  1573. def environ(self):
  1574. with open_text(f"{self._procfs_path}/{self.pid}/environ") as f:
  1575. data = f.read()
  1576. return parse_environ_block(data)
  1577. @wrap_exceptions
  1578. def terminal(self):
  1579. tty_nr = int(self._parse_stat_file()['ttynr'])
  1580. tmap = _psposix.get_terminal_map()
  1581. try:
  1582. return tmap[tty_nr]
  1583. except KeyError:
  1584. return None
  1585. # May not be available on old kernels.
  1586. if os.path.exists(f"/proc/{os.getpid()}/io"):
  1587. @wrap_exceptions
  1588. def io_counters(self):
  1589. fname = f"{self._procfs_path}/{self.pid}/io"
  1590. fields = {}
  1591. with open_binary(fname) as f:
  1592. for line in f:
  1593. # https://github.com/giampaolo/psutil/issues/1004
  1594. line = line.strip()
  1595. if line:
  1596. try:
  1597. name, value = line.split(b': ')
  1598. except ValueError:
  1599. # https://github.com/giampaolo/psutil/issues/1004
  1600. continue
  1601. else:
  1602. fields[name] = int(value)
  1603. if not fields:
  1604. msg = f"{fname} file was empty"
  1605. raise RuntimeError(msg)
  1606. try:
  1607. return ntp.pio(
  1608. fields[b'syscr'], # read syscalls
  1609. fields[b'syscw'], # write syscalls
  1610. fields[b'read_bytes'], # read bytes
  1611. fields[b'write_bytes'], # write bytes
  1612. fields[b'rchar'], # read chars
  1613. fields[b'wchar'], # write chars
  1614. )
  1615. except KeyError as err:
  1616. msg = (
  1617. f"{err.args[0]!r} field was not found in {fname}; found"
  1618. f" fields are {fields!r}"
  1619. )
  1620. raise ValueError(msg) from None
  1621. @wrap_exceptions
  1622. def cpu_times(self):
  1623. values = self._parse_stat_file()
  1624. utime = float(values['utime']) / CLOCK_TICKS
  1625. stime = float(values['stime']) / CLOCK_TICKS
  1626. children_utime = float(values['children_utime']) / CLOCK_TICKS
  1627. children_stime = float(values['children_stime']) / CLOCK_TICKS
  1628. iowait = float(values['blkio_ticks']) / CLOCK_TICKS
  1629. return ntp.pcputimes(
  1630. utime, stime, children_utime, children_stime, iowait
  1631. )
  1632. @wrap_exceptions
  1633. def cpu_num(self):
  1634. """What CPU the process is on."""
  1635. return int(self._parse_stat_file()['cpu_num'])
  1636. @wrap_exceptions
  1637. def wait(self, timeout=None):
  1638. return _psposix.wait_pid(self.pid, timeout)
  1639. @wrap_exceptions
  1640. def create_time(self, monotonic=False):
  1641. # The 'starttime' field in /proc/[pid]/stat is expressed in
  1642. # jiffies (clock ticks per second), a relative value which
  1643. # represents the number of clock ticks that have passed since
  1644. # the system booted until the process was created. It never
  1645. # changes and is unaffected by system clock updates.
  1646. if self._ctime is None:
  1647. self._ctime = (
  1648. float(self._parse_stat_file()['create_time']) / CLOCK_TICKS
  1649. )
  1650. if monotonic:
  1651. return self._ctime
  1652. # Add the boot time, returning time expressed in seconds since
  1653. # the epoch. This is subject to system clock updates.
  1654. return self._ctime + boot_time()
  1655. @wrap_exceptions
  1656. def memory_info(self):
  1657. # ============================================================
  1658. # | FIELD | DESCRIPTION | AKA | TOP |
  1659. # ============================================================
  1660. # | rss | resident set size | | RES |
  1661. # | vms | total program size | size | VIRT |
  1662. # | shared | shared pages (from shared mappings) | | SHR |
  1663. # | text | text ('code') | trs | CODE |
  1664. # | lib | library (unused in Linux 2.6) | lrs | |
  1665. # | data | data + stack | drs | DATA |
  1666. # | dirty | dirty pages (unused in Linux 2.6) | dt | |
  1667. # ============================================================
  1668. with open_binary(f"{self._procfs_path}/{self.pid}/statm") as f:
  1669. vms, rss, shared, text, lib, data, dirty = (
  1670. int(x) * PAGESIZE for x in f.readline().split()[:7]
  1671. )
  1672. return ntp.pmem(rss, vms, shared, text, lib, data, dirty)
  1673. if HAS_PROC_SMAPS_ROLLUP or HAS_PROC_SMAPS:
  1674. def _parse_smaps_rollup(self):
  1675. # /proc/pid/smaps_rollup was added to Linux in 2017. Faster
  1676. # than /proc/pid/smaps. It reports higher PSS than */smaps
  1677. # (from 1k up to 200k higher; tested against all processes).
  1678. # IMPORTANT: /proc/pid/smaps_rollup is weird, because it
  1679. # raises ESRCH / ENOENT for many PIDs, even if they're alive
  1680. # (also as root). In that case we'll use /proc/pid/smaps as
  1681. # fallback, which is slower but has a +50% success rate
  1682. # compared to /proc/pid/smaps_rollup.
  1683. uss = pss = swap = 0
  1684. with open_binary(
  1685. f"{self._procfs_path}/{self.pid}/smaps_rollup"
  1686. ) as f:
  1687. for line in f:
  1688. if line.startswith(b"Private_"):
  1689. # Private_Clean, Private_Dirty, Private_Hugetlb
  1690. uss += int(line.split()[1]) * 1024
  1691. elif line.startswith(b"Pss:"):
  1692. pss = int(line.split()[1]) * 1024
  1693. elif line.startswith(b"Swap:"):
  1694. swap = int(line.split()[1]) * 1024
  1695. return (uss, pss, swap)
  1696. @wrap_exceptions
  1697. def _parse_smaps(
  1698. self,
  1699. # Gets Private_Clean, Private_Dirty, Private_Hugetlb.
  1700. _private_re=re.compile(br"\nPrivate.*:\s+(\d+)"),
  1701. _pss_re=re.compile(br"\nPss\:\s+(\d+)"),
  1702. _swap_re=re.compile(br"\nSwap\:\s+(\d+)"),
  1703. ):
  1704. # /proc/pid/smaps does not exist on kernels < 2.6.14 or if
  1705. # CONFIG_MMU kernel configuration option is not enabled.
  1706. # Note: using 3 regexes is faster than reading the file
  1707. # line by line.
  1708. #
  1709. # You might be tempted to calculate USS by subtracting
  1710. # the "shared" value from the "resident" value in
  1711. # /proc/<pid>/statm. But at least on Linux, statm's "shared"
  1712. # value actually counts pages backed by files, which has
  1713. # little to do with whether the pages are actually shared.
  1714. # /proc/self/smaps on the other hand appears to give us the
  1715. # correct information.
  1716. smaps_data = self._read_smaps_file()
  1717. # Note: smaps file can be empty for certain processes.
  1718. # The code below will not crash though and will result to 0.
  1719. uss = sum(map(int, _private_re.findall(smaps_data))) * 1024
  1720. pss = sum(map(int, _pss_re.findall(smaps_data))) * 1024
  1721. swap = sum(map(int, _swap_re.findall(smaps_data))) * 1024
  1722. return (uss, pss, swap)
  1723. @wrap_exceptions
  1724. def memory_full_info(self):
  1725. if HAS_PROC_SMAPS_ROLLUP: # faster
  1726. try:
  1727. uss, pss, swap = self._parse_smaps_rollup()
  1728. except (ProcessLookupError, FileNotFoundError):
  1729. uss, pss, swap = self._parse_smaps()
  1730. else:
  1731. uss, pss, swap = self._parse_smaps()
  1732. basic_mem = self.memory_info()
  1733. return ntp.pfullmem(*basic_mem + (uss, pss, swap))
  1734. else:
  1735. memory_full_info = memory_info
  1736. if HAS_PROC_SMAPS:
  1737. @wrap_exceptions
  1738. def memory_maps(self):
  1739. """Return process's mapped memory regions as a list of named
  1740. tuples. Fields are explained in 'man proc'; here is an updated
  1741. (Apr 2012) version: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/proc.txt?id=b76437579d1344b612cf1851ae610c636cec7db0.
  1742. /proc/{PID}/smaps does not exist on kernels < 2.6.14 or if
  1743. CONFIG_MMU kernel configuration option is not enabled.
  1744. """
  1745. def get_blocks(lines, current_block):
  1746. data = {}
  1747. for line in lines:
  1748. fields = line.split(None, 5)
  1749. if not fields[0].endswith(b':'):
  1750. # new block section
  1751. yield (current_block.pop(), data)
  1752. current_block.append(line)
  1753. else:
  1754. try:
  1755. data[fields[0]] = int(fields[1]) * 1024
  1756. except (ValueError, IndexError):
  1757. if fields[0].startswith(b'VmFlags:'):
  1758. # see issue #369
  1759. continue
  1760. msg = f"don't know how to interpret line {line!r}"
  1761. raise ValueError(msg) from None
  1762. yield (current_block.pop(), data)
  1763. data = self._read_smaps_file()
  1764. # Note: smaps file can be empty for certain processes or for
  1765. # zombies.
  1766. if not data:
  1767. self._raise_if_zombie()
  1768. return []
  1769. lines = data.split(b'\n')
  1770. ls = []
  1771. first_line = lines.pop(0)
  1772. current_block = [first_line]
  1773. for header, data in get_blocks(lines, current_block):
  1774. hfields = header.split(None, 5)
  1775. try:
  1776. addr, perms, _offset, _dev, _inode, path = hfields
  1777. except ValueError:
  1778. addr, perms, _offset, _dev, _inode, path = hfields + ['']
  1779. if not path:
  1780. path = '[anon]'
  1781. else:
  1782. path = decode(path)
  1783. path = path.strip()
  1784. if path.endswith(' (deleted)') and not path_exists_strict(
  1785. path
  1786. ):
  1787. path = path[:-10]
  1788. item = (
  1789. decode(addr),
  1790. decode(perms),
  1791. path,
  1792. data.get(b'Rss:', 0),
  1793. data.get(b'Size:', 0),
  1794. data.get(b'Pss:', 0),
  1795. data.get(b'Shared_Clean:', 0),
  1796. data.get(b'Shared_Dirty:', 0),
  1797. data.get(b'Private_Clean:', 0),
  1798. data.get(b'Private_Dirty:', 0),
  1799. data.get(b'Referenced:', 0),
  1800. data.get(b'Anonymous:', 0),
  1801. data.get(b'Swap:', 0),
  1802. )
  1803. ls.append(item)
  1804. return ls
  1805. @wrap_exceptions
  1806. def cwd(self):
  1807. return self._readlink(
  1808. f"{self._procfs_path}/{self.pid}/cwd", fallback=""
  1809. )
  1810. @wrap_exceptions
  1811. def num_ctx_switches(
  1812. self, _ctxsw_re=re.compile(br'ctxt_switches:\t(\d+)')
  1813. ):
  1814. data = self._read_status_file()
  1815. ctxsw = _ctxsw_re.findall(data)
  1816. if not ctxsw:
  1817. msg = (
  1818. "'voluntary_ctxt_switches' and"
  1819. " 'nonvoluntary_ctxt_switches'lines were not found in"
  1820. f" {self._procfs_path}/{self.pid}/status; the kernel is"
  1821. " probably older than 2.6.23"
  1822. )
  1823. raise NotImplementedError(msg)
  1824. return ntp.pctxsw(int(ctxsw[0]), int(ctxsw[1]))
  1825. @wrap_exceptions
  1826. def num_threads(self, _num_threads_re=re.compile(br'Threads:\t(\d+)')):
  1827. # Using a re is faster than iterating over file line by line.
  1828. data = self._read_status_file()
  1829. return int(_num_threads_re.findall(data)[0])
  1830. @wrap_exceptions
  1831. def threads(self):
  1832. thread_ids = os.listdir(f"{self._procfs_path}/{self.pid}/task")
  1833. thread_ids.sort()
  1834. retlist = []
  1835. hit_enoent = False
  1836. for thread_id in thread_ids:
  1837. fname = f"{self._procfs_path}/{self.pid}/task/{thread_id}/stat"
  1838. try:
  1839. with open_binary(fname) as f:
  1840. st = f.read().strip()
  1841. except (FileNotFoundError, ProcessLookupError):
  1842. # no such file or directory or no such process;
  1843. # it means thread disappeared on us
  1844. hit_enoent = True
  1845. continue
  1846. # ignore the first two values ("pid (exe)")
  1847. st = st[st.find(b')') + 2 :]
  1848. values = st.split(b' ')
  1849. utime = float(values[11]) / CLOCK_TICKS
  1850. stime = float(values[12]) / CLOCK_TICKS
  1851. ntuple = ntp.pthread(int(thread_id), utime, stime)
  1852. retlist.append(ntuple)
  1853. if hit_enoent:
  1854. self._raise_if_not_alive()
  1855. return retlist
  1856. @wrap_exceptions
  1857. def nice_get(self):
  1858. # with open_text(f"{self._procfs_path}/{self.pid}/stat") as f:
  1859. # data = f.read()
  1860. # return int(data.split()[18])
  1861. # Use C implementation
  1862. return cext.proc_priority_get(self.pid)
  1863. @wrap_exceptions
  1864. def nice_set(self, value):
  1865. return cext.proc_priority_set(self.pid, value)
  1866. # starting from CentOS 6.
  1867. if HAS_CPU_AFFINITY:
  1868. @wrap_exceptions
  1869. def cpu_affinity_get(self):
  1870. return cext.proc_cpu_affinity_get(self.pid)
  1871. def _get_eligible_cpus(
  1872. self, _re=re.compile(br"Cpus_allowed_list:\t(\d+)-(\d+)")
  1873. ):
  1874. # See: https://github.com/giampaolo/psutil/issues/956
  1875. data = self._read_status_file()
  1876. match = _re.findall(data)
  1877. if match:
  1878. return list(range(int(match[0][0]), int(match[0][1]) + 1))
  1879. else:
  1880. return list(range(len(per_cpu_times())))
  1881. @wrap_exceptions
  1882. def cpu_affinity_set(self, cpus):
  1883. try:
  1884. cext.proc_cpu_affinity_set(self.pid, cpus)
  1885. except (OSError, ValueError) as err:
  1886. if isinstance(err, ValueError) or err.errno == errno.EINVAL:
  1887. eligible_cpus = self._get_eligible_cpus()
  1888. all_cpus = tuple(range(len(per_cpu_times())))
  1889. for cpu in cpus:
  1890. if cpu not in all_cpus:
  1891. msg = (
  1892. f"invalid CPU {cpu!r}; choose between"
  1893. f" {eligible_cpus!r}"
  1894. )
  1895. raise ValueError(msg) from None
  1896. if cpu not in eligible_cpus:
  1897. msg = (
  1898. f"CPU number {cpu} is not eligible; choose"
  1899. f" between {eligible_cpus}"
  1900. )
  1901. raise ValueError(msg) from err
  1902. raise
  1903. # only starting from kernel 2.6.13
  1904. if HAS_PROC_IO_PRIORITY:
  1905. @wrap_exceptions
  1906. def ionice_get(self):
  1907. ioclass, value = cext.proc_ioprio_get(self.pid)
  1908. ioclass = IOPriority(ioclass)
  1909. return ntp.pionice(ioclass, value)
  1910. @wrap_exceptions
  1911. def ionice_set(self, ioclass, value):
  1912. if value is None:
  1913. value = 0
  1914. if value and ioclass in {
  1915. IOPriority.IOPRIO_CLASS_IDLE,
  1916. IOPriority.IOPRIO_CLASS_NONE,
  1917. }:
  1918. msg = f"{ioclass!r} ioclass accepts no value"
  1919. raise ValueError(msg)
  1920. if value < 0 or value > 7:
  1921. msg = "value not in 0-7 range"
  1922. raise ValueError(msg)
  1923. return cext.proc_ioprio_set(self.pid, ioclass, value)
  1924. if hasattr(resource, "prlimit"):
  1925. @wrap_exceptions
  1926. def rlimit(self, resource_, limits=None):
  1927. # If pid is 0 prlimit() applies to the calling process and
  1928. # we don't want that. We should never get here though as
  1929. # PID 0 is not supported on Linux.
  1930. if self.pid == 0:
  1931. msg = "can't use prlimit() against PID 0 process"
  1932. raise ValueError(msg)
  1933. try:
  1934. if limits is None:
  1935. # get
  1936. return resource.prlimit(self.pid, resource_)
  1937. else:
  1938. # set
  1939. if len(limits) != 2:
  1940. msg = (
  1941. "second argument must be a (soft, hard) "
  1942. f"tuple, got {limits!r}"
  1943. )
  1944. raise ValueError(msg)
  1945. resource.prlimit(self.pid, resource_, limits)
  1946. except OSError as err:
  1947. if err.errno == errno.ENOSYS:
  1948. # I saw this happening on Travis:
  1949. # https://travis-ci.org/giampaolo/psutil/jobs/51368273
  1950. self._raise_if_zombie()
  1951. raise
  1952. @wrap_exceptions
  1953. def status(self):
  1954. letter = self._parse_stat_file()['status']
  1955. letter = letter.decode()
  1956. # XXX is '?' legit? (we're not supposed to return it anyway)
  1957. return PROC_STATUSES.get(letter, '?')
  1958. @wrap_exceptions
  1959. def open_files(self):
  1960. retlist = []
  1961. files = os.listdir(f"{self._procfs_path}/{self.pid}/fd")
  1962. hit_enoent = False
  1963. for fd in files:
  1964. file = f"{self._procfs_path}/{self.pid}/fd/{fd}"
  1965. try:
  1966. path = readlink(file)
  1967. except (FileNotFoundError, ProcessLookupError):
  1968. # ENOENT == file which is gone in the meantime
  1969. hit_enoent = True
  1970. continue
  1971. except OSError as err:
  1972. if err.errno == errno.EINVAL:
  1973. # not a link
  1974. continue
  1975. if err.errno == errno.ENAMETOOLONG:
  1976. # file name too long
  1977. debug(err)
  1978. continue
  1979. raise
  1980. else:
  1981. # If path is not an absolute there's no way to tell
  1982. # whether it's a regular file or not, so we skip it.
  1983. # A regular file is always supposed to be have an
  1984. # absolute path though.
  1985. if path.startswith('/') and isfile_strict(path):
  1986. # Get file position and flags.
  1987. file = f"{self._procfs_path}/{self.pid}/fdinfo/{fd}"
  1988. try:
  1989. with open_binary(file) as f:
  1990. pos = int(f.readline().split()[1])
  1991. flags = int(f.readline().split()[1], 8)
  1992. except (FileNotFoundError, ProcessLookupError):
  1993. # fd gone in the meantime; process may
  1994. # still be alive
  1995. hit_enoent = True
  1996. else:
  1997. mode = file_flags_to_mode(flags)
  1998. ntuple = ntp.popenfile(
  1999. path, int(fd), int(pos), mode, flags
  2000. )
  2001. retlist.append(ntuple)
  2002. if hit_enoent:
  2003. self._raise_if_not_alive()
  2004. return retlist
  2005. @wrap_exceptions
  2006. def net_connections(self, kind='inet'):
  2007. ret = _net_connections.retrieve(kind, self.pid)
  2008. self._raise_if_not_alive()
  2009. return ret
  2010. @wrap_exceptions
  2011. def num_fds(self):
  2012. return len(os.listdir(f"{self._procfs_path}/{self.pid}/fd"))
  2013. @wrap_exceptions
  2014. def ppid(self):
  2015. return int(self._parse_stat_file()['ppid'])
  2016. @wrap_exceptions
  2017. def uids(self, _uids_re=re.compile(br'Uid:\t(\d+)\t(\d+)\t(\d+)')):
  2018. data = self._read_status_file()
  2019. real, effective, saved = _uids_re.findall(data)[0]
  2020. return ntp.puids(int(real), int(effective), int(saved))
  2021. @wrap_exceptions
  2022. def gids(self, _gids_re=re.compile(br'Gid:\t(\d+)\t(\d+)\t(\d+)')):
  2023. data = self._read_status_file()
  2024. real, effective, saved = _gids_re.findall(data)[0]
  2025. return ntp.pgids(int(real), int(effective), int(saved))