_pslinux.py 84 KB

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