_ntuples.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. from collections import namedtuple as nt
  5. from ._common import AIX
  6. from ._common import BSD
  7. from ._common import FREEBSD
  8. from ._common import LINUX
  9. from ._common import MACOS
  10. from ._common import SUNOS
  11. from ._common import WINDOWS
  12. # ===================================================================
  13. # --- system functions
  14. # ===================================================================
  15. # psutil.swap_memory()
  16. sswap = nt("sswap", ("total", "used", "free", "percent", "sin", "sout"))
  17. # psutil.disk_usage()
  18. sdiskusage = nt("sdiskusage", ("total", "used", "free", "percent"))
  19. # psutil.disk_io_counters()
  20. sdiskio = nt(
  21. "sdiskio",
  22. (
  23. "read_count",
  24. "write_count",
  25. "read_bytes",
  26. "write_bytes",
  27. "read_time",
  28. "write_time",
  29. ),
  30. )
  31. # psutil.disk_partitions()
  32. sdiskpart = nt("sdiskpart", ("device", "mountpoint", "fstype", "opts"))
  33. # psutil.net_io_counters()
  34. snetio = nt(
  35. "snetio",
  36. (
  37. "bytes_sent",
  38. "bytes_recv",
  39. "packets_sent",
  40. "packets_recv",
  41. "errin",
  42. "errout",
  43. "dropin",
  44. "dropout",
  45. ),
  46. )
  47. # psutil.users()
  48. suser = nt("suser", ("name", "terminal", "host", "started", "pid"))
  49. # psutil.net_connections()
  50. sconn = nt(
  51. "sconn", ("fd", "family", "type", "laddr", "raddr", "status", "pid")
  52. )
  53. # psutil.net_if_addrs()
  54. snicaddr = nt("snicaddr", ("family", "address", "netmask", "broadcast", "ptp"))
  55. # psutil.net_if_stats()
  56. snicstats = nt("snicstats", ("isup", "duplex", "speed", "mtu", "flags"))
  57. # psutil.cpu_stats()
  58. scpustats = nt(
  59. "scpustats", ("ctx_switches", "interrupts", "soft_interrupts", "syscalls")
  60. )
  61. # psutil.cpu_freq()
  62. scpufreq = nt("scpufreq", ("current", "min", "max"))
  63. # psutil.sensors_temperatures()
  64. shwtemp = nt("shwtemp", ("label", "current", "high", "critical"))
  65. # psutil.sensors_battery()
  66. sbattery = nt("sbattery", ("percent", "secsleft", "power_plugged"))
  67. # psutil.sensors_fans()
  68. sfan = nt("sfan", ("label", "current"))
  69. # psutil.heap_info() (mallinfo2 Linux struct)
  70. if LINUX or WINDOWS or MACOS or BSD:
  71. pheap = nt(
  72. "pheap",
  73. [
  74. "heap_used", # uordblks, memory allocated via malloc()
  75. "mmap_used", # hblkhd, memory allocated via mmap() (large blocks)
  76. ],
  77. )
  78. if WINDOWS:
  79. pheap = nt("pheap", pheap._fields + ("heap_count",))
  80. # ===================================================================
  81. # --- Process class
  82. # ===================================================================
  83. # psutil.Process.cpu_times()
  84. pcputimes = nt(
  85. "pcputimes", ("user", "system", "children_user", "children_system")
  86. )
  87. # psutil.Process.open_files()
  88. popenfile = nt("popenfile", ("path", "fd"))
  89. # psutil.Process.threads()
  90. pthread = nt("pthread", ("id", "user_time", "system_time"))
  91. # psutil.Process.uids()
  92. puids = nt("puids", ("real", "effective", "saved"))
  93. # psutil.Process.gids()
  94. pgids = nt("pgids", ("real", "effective", "saved"))
  95. # psutil.Process.io_counters()
  96. pio = nt("pio", ("read_count", "write_count", "read_bytes", "write_bytes"))
  97. # psutil.Process.ionice()
  98. pionice = nt("pionice", ("ioclass", "value"))
  99. # psutil.Process.ctx_switches()
  100. pctxsw = nt("pctxsw", ("voluntary", "involuntary"))
  101. # psutil.Process.net_connections()
  102. pconn = nt("pconn", ("fd", "family", "type", "laddr", "raddr", "status"))
  103. # psutil.net_connections() and psutil.Process.net_connections()
  104. addr = nt("addr", ("ip", "port"))
  105. # ===================================================================
  106. # --- Linux
  107. # ===================================================================
  108. if LINUX:
  109. # This gets set from _pslinux.py
  110. scputimes = None
  111. # psutil.virtual_memory()
  112. svmem = nt(
  113. "svmem",
  114. (
  115. "total",
  116. "available",
  117. "percent",
  118. "used",
  119. "free",
  120. "active",
  121. "inactive",
  122. "buffers",
  123. "cached",
  124. "shared",
  125. "slab",
  126. ),
  127. )
  128. # psutil.disk_io_counters()
  129. sdiskio = nt(
  130. "sdiskio",
  131. (
  132. "read_count",
  133. "write_count",
  134. "read_bytes",
  135. "write_bytes",
  136. "read_time",
  137. "write_time",
  138. "read_merged_count",
  139. "write_merged_count",
  140. "busy_time",
  141. ),
  142. )
  143. # psutil.Process().open_files()
  144. popenfile = nt("popenfile", ("path", "fd", "position", "mode", "flags"))
  145. # psutil.Process().memory_info()
  146. pmem = nt("pmem", ("rss", "vms", "shared", "text", "lib", "data", "dirty"))
  147. # psutil.Process().memory_full_info()
  148. pfullmem = nt("pfullmem", pmem._fields + ("uss", "pss", "swap"))
  149. # psutil.Process().memory_maps(grouped=True)
  150. pmmap_grouped = nt(
  151. "pmmap_grouped",
  152. (
  153. "path",
  154. "rss",
  155. "size",
  156. "pss",
  157. "shared_clean",
  158. "shared_dirty",
  159. "private_clean",
  160. "private_dirty",
  161. "referenced",
  162. "anonymous",
  163. "swap",
  164. ),
  165. )
  166. # psutil.Process().memory_maps(grouped=False)
  167. pmmap_ext = nt(
  168. "pmmap_ext", "addr perms " + " ".join(pmmap_grouped._fields)
  169. )
  170. # psutil.Process.io_counters()
  171. pio = nt(
  172. "pio",
  173. (
  174. "read_count",
  175. "write_count",
  176. "read_bytes",
  177. "write_bytes",
  178. "read_chars",
  179. "write_chars",
  180. ),
  181. )
  182. # psutil.Process.cpu_times()
  183. pcputimes = nt(
  184. "pcputimes",
  185. ("user", "system", "children_user", "children_system", "iowait"),
  186. )
  187. # ===================================================================
  188. # --- Windows
  189. # ===================================================================
  190. elif WINDOWS:
  191. # psutil.cpu_times()
  192. scputimes = nt("scputimes", ("user", "system", "idle", "interrupt", "dpc"))
  193. # psutil.virtual_memory()
  194. svmem = nt("svmem", ("total", "available", "percent", "used", "free"))
  195. # psutil.Process.memory_info()
  196. pmem = nt(
  197. "pmem",
  198. (
  199. "rss",
  200. "vms",
  201. "num_page_faults",
  202. "peak_wset",
  203. "wset",
  204. "peak_paged_pool",
  205. "paged_pool",
  206. "peak_nonpaged_pool",
  207. "nonpaged_pool",
  208. "pagefile",
  209. "peak_pagefile",
  210. "private",
  211. ),
  212. )
  213. # psutil.Process.memory_full_info()
  214. pfullmem = nt("pfullmem", pmem._fields + ("uss",))
  215. # psutil.Process.memory_maps(grouped=True)
  216. pmmap_grouped = nt("pmmap_grouped", ("path", "rss"))
  217. # psutil.Process.memory_maps(grouped=False)
  218. pmmap_ext = nt(
  219. "pmmap_ext", "addr perms " + " ".join(pmmap_grouped._fields)
  220. )
  221. # psutil.Process.io_counters()
  222. pio = nt(
  223. "pio",
  224. (
  225. "read_count",
  226. "write_count",
  227. "read_bytes",
  228. "write_bytes",
  229. "other_count",
  230. "other_bytes",
  231. ),
  232. )
  233. # ===================================================================
  234. # --- macOS
  235. # ===================================================================
  236. elif MACOS:
  237. # psutil.cpu_times()
  238. scputimes = nt("scputimes", ("user", "nice", "system", "idle"))
  239. # psutil.virtual_memory()
  240. svmem = nt(
  241. "svmem",
  242. (
  243. "total",
  244. "available",
  245. "percent",
  246. "used",
  247. "free",
  248. "active",
  249. "inactive",
  250. "wired",
  251. ),
  252. )
  253. # psutil.Process.memory_info()
  254. pmem = nt("pmem", ("rss", "vms", "pfaults", "pageins"))
  255. # psutil.Process.memory_full_info()
  256. pfullmem = nt("pfullmem", pmem._fields + ("uss",))
  257. # ===================================================================
  258. # --- BSD
  259. # ===================================================================
  260. elif BSD:
  261. # psutil.virtual_memory()
  262. svmem = nt(
  263. "svmem",
  264. (
  265. "total",
  266. "available",
  267. "percent",
  268. "used",
  269. "free",
  270. "active",
  271. "inactive",
  272. "buffers",
  273. "cached",
  274. "shared",
  275. "wired",
  276. ),
  277. )
  278. # psutil.cpu_times()
  279. scputimes = nt("scputimes", ("user", "nice", "system", "idle", "irq"))
  280. # psutil.Process.memory_info()
  281. pmem = nt("pmem", ("rss", "vms", "text", "data", "stack"))
  282. # psutil.Process.memory_full_info()
  283. pfullmem = pmem
  284. # psutil.Process.cpu_times()
  285. pcputimes = nt(
  286. "pcputimes", ("user", "system", "children_user", "children_system")
  287. )
  288. # psutil.Process.memory_maps(grouped=True)
  289. pmmap_grouped = nt(
  290. "pmmap_grouped", "path rss, private, ref_count, shadow_count"
  291. )
  292. # psutil.Process.memory_maps(grouped=False)
  293. pmmap_ext = nt(
  294. "pmmap_ext", "addr, perms path rss, private, ref_count, shadow_count"
  295. )
  296. # psutil.disk_io_counters()
  297. if FREEBSD:
  298. sdiskio = nt(
  299. "sdiskio",
  300. (
  301. "read_count",
  302. "write_count",
  303. "read_bytes",
  304. "write_bytes",
  305. "read_time",
  306. "write_time",
  307. "busy_time",
  308. ),
  309. )
  310. else:
  311. sdiskio = nt(
  312. "sdiskio",
  313. ("read_count", "write_count", "read_bytes", "write_bytes"),
  314. )
  315. # ===================================================================
  316. # --- SunOS
  317. # ===================================================================
  318. elif SUNOS:
  319. # psutil.cpu_times()
  320. scputimes = nt("scputimes", ("user", "system", "idle", "iowait"))
  321. # psutil.cpu_times(percpu=True)
  322. pcputimes = nt(
  323. "pcputimes", ("user", "system", "children_user", "children_system")
  324. )
  325. # psutil.virtual_memory()
  326. svmem = nt("svmem", ("total", "available", "percent", "used", "free"))
  327. # psutil.Process.memory_info()
  328. pmem = nt("pmem", ("rss", "vms"))
  329. # psutil.Process.memory_full_info()
  330. pfullmem = pmem
  331. # psutil.Process.memory_maps(grouped=True)
  332. pmmap_grouped = nt("pmmap_grouped", ("path", "rss", "anonymous", "locked"))
  333. # psutil.Process.memory_maps(grouped=False)
  334. pmmap_ext = nt(
  335. "pmmap_ext", "addr perms " + " ".join(pmmap_grouped._fields)
  336. )
  337. # ===================================================================
  338. # --- AIX
  339. # ===================================================================
  340. elif AIX:
  341. # psutil.Process.memory_info()
  342. pmem = nt("pmem", ("rss", "vms"))
  343. # psutil.Process.memory_full_info()
  344. pfullmem = pmem
  345. # psutil.Process.cpu_times()
  346. scputimes = nt("scputimes", ("user", "system", "idle", "iowait"))
  347. # psutil.virtual_memory()
  348. svmem = nt("svmem", ("total", "available", "percent", "used", "free"))