_pslinux.py 84 KB

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