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
5from __future__ import annotations
6
7import warnings
8from collections import namedtuple
9from typing import TYPE_CHECKING
10from typing import NamedTuple
11
12if TYPE_CHECKING:
13 import socket
14
15 from ._enums import BatteryTime
16 from ._enums import ConnectionStatus
17 from ._enums import NicDuplex
18 from ._enums import ProcessIOPriority
19
20from ._common import AIX
21from ._common import BSD
22from ._common import FREEBSD
23from ._common import LINUX
24from ._common import MACOS
25from ._common import NETBSD
26from ._common import OPENBSD
27from ._common import SUNOS
28from ._common import WINDOWS
29
30# ===================================================================
31# --- system functions
32# ===================================================================
33
34
35# psutil.memory_swap()
36class sswap(NamedTuple):
37 total: int
38 used: int
39 free: int
40 percent: float
41 sin: int
42 sout: int
43
44
45# psutil.disk_usage()
46class sdiskusage(NamedTuple):
47 total: int
48 used: int
49 free: int
50 percent: float
51
52
53# psutil.disk_io_counters()
54class sdiskio(NamedTuple):
55 read_count: int
56 write_count: int
57 read_bytes: int
58 write_bytes: int
59 if not (NETBSD or OPENBSD):
60 read_time: int
61 write_time: int
62 if LINUX:
63 read_merged_count: int
64 write_merged_count: int
65 busy_time: int
66 if FREEBSD:
67 busy_time: int
68
69
70# psutil.disk_partitions()
71class sdiskpart(NamedTuple):
72 device: str
73 mountpoint: str
74 fstype: str
75 opts: str
76
77
78# psutil.net_io_counters()
79class snetio(NamedTuple):
80 bytes_sent: int
81 bytes_recv: int
82 packets_sent: int
83 packets_recv: int
84 errin: int
85 errout: int
86 dropin: int
87 dropout: int
88
89
90# psutil.users()
91class suser(NamedTuple):
92 name: str
93 terminal: str | None
94 host: str | None
95 started: float
96 pid: int | None
97
98
99# psutil.net_connections() and psutil.Process.net_connections()
100class addr(NamedTuple):
101 ip: str
102 port: int
103
104
105# psutil.net_connections()
106class sconn(NamedTuple):
107 fd: int
108 family: socket.AddressFamily
109 type: socket.SocketKind
110 laddr: addr | tuple | str
111 raddr: addr | tuple | str
112 status: ConnectionStatus
113 pid: int | None
114
115
116# psutil.net_if_addrs()
117class snicaddr(NamedTuple):
118 family: socket.AddressFamily
119 address: str | None
120 netmask: str | None
121 broadcast: str | None
122 ptp: str | None
123
124
125# psutil.net_if_stats()
126class snicstats(NamedTuple):
127 isup: bool
128 duplex: NicDuplex
129 speed: int
130 mtu: int
131 flags: str
132
133
134# psutil.cpu_times()
135class scputimes(NamedTuple):
136 user: float
137 system: float
138 idle: float
139 if LINUX or MACOS or BSD:
140 nice: float
141 if LINUX:
142 iowait: float
143 irq: float
144 softirq: float
145 steal: float
146 guest: float
147 guest_nice: float
148 if BSD:
149 irq: float
150 if SUNOS or AIX:
151 iowait: float
152 if WINDOWS:
153 irq: float
154 dpc: float
155
156 # DEPRECATED
157 @property
158 def interrupt(self):
159 msg = "'interrupt' field is deprecated, use 'irq' instead"
160 warnings.warn(msg, DeprecationWarning, stacklevel=2)
161 return self.irq
162
163
164# psutil.cpu_stats()
165class scpustats(NamedTuple):
166 ctx_switches: int
167 interrupts: int
168 soft_interrupts: int
169 syscalls: int
170
171
172# psutil.cpu_freq()
173class scpufreq(NamedTuple):
174 current: float
175 min: float
176 max: float
177
178
179# psutil.sensors_temperatures()
180class shwtemp(NamedTuple):
181 label: str
182 current: float
183 high: float | None
184 critical: float | None
185
186
187# psutil.sensors_battery()
188class sbattery(NamedTuple):
189 percent: float
190 secsleft: int | BatteryTime
191 power_plugged: bool | None
192
193
194# psutil.sensors_fans()
195class sfan(NamedTuple):
196 label: str
197 current: int
198
199
200if LINUX or WINDOWS or MACOS or BSD:
201
202 # psutil.heap_info()
203 class pheap(NamedTuple):
204 heap_used: int
205 mmap_used: int
206 if WINDOWS:
207 heap_count: int
208
209
210# psutil.memory_physical()
211class smem(NamedTuple):
212 total: int
213 available: int
214 percent: float
215 used: int
216 free: int
217 if LINUX:
218 active: int
219 inactive: int
220 buffers: int
221 cached: int
222 shared: int
223 slab: int
224 elif BSD:
225 active: int
226 inactive: int
227 buffers: int
228 cached: int
229 shared: int
230 wired: int
231 elif WINDOWS:
232 cached: int
233 wired: int
234 elif MACOS:
235 active: int
236 inactive: int
237 wired: int
238
239
240# ===================================================================
241# --- Process class
242# ===================================================================
243
244
245# psutil.Process.cpu_times()
246class pcputimes(NamedTuple):
247 user: float
248 system: float
249 children_user: float
250 children_system: float
251 if LINUX:
252 iowait: float
253
254
255# psutil.Process.open_files()
256class popenfile(NamedTuple):
257 path: str
258 fd: int
259 if LINUX:
260 position: int
261 mode: str
262 flags: int
263
264
265# psutil.Process.threads()
266class pthread(NamedTuple):
267 id: int
268 user_time: float
269 system_time: float
270
271
272# psutil.Process.uids()
273class puids(NamedTuple):
274 real: int
275 effective: int
276 saved: int
277
278
279# psutil.Process.gids()
280class pgids(NamedTuple):
281 real: int
282 effective: int
283 saved: int
284
285
286# psutil.Process.io_counters()
287class pio(NamedTuple):
288 read_count: int
289 write_count: int
290 read_bytes: int
291 write_bytes: int
292 if LINUX:
293 read_chars: int
294 write_chars: int
295 elif WINDOWS:
296 other_count: int
297 other_bytes: int
298
299
300# psutil.Process.ionice()
301class pionice(NamedTuple):
302 ioclass: ProcessIOPriority
303 value: int
304
305
306# psutil.Process.ctx_switches()
307class pctxsw(NamedTuple):
308 voluntary: int
309 involuntary: int
310
311
312# psutil.Process.page_faults()
313class ppagefaults(NamedTuple):
314 minor: int
315 major: int
316
317
318# psutil.Process().memory_footprint()
319if LINUX or MACOS or WINDOWS:
320
321 class pfootprint(NamedTuple):
322 uss: int
323 shared: int
324 if LINUX:
325 pss: int
326 swap: int
327
328
329# psutil.Process.net_connections()
330class pconn(NamedTuple):
331 fd: int
332 family: socket.AddressFamily
333 type: socket.SocketKind
334 laddr: addr | tuple | str
335 raddr: addr | tuple | str
336 status: ConnectionStatus
337
338
339# psutil.Process.memory_maps(grouped=True)
340class pmmap_grouped(NamedTuple):
341 path: str
342 rss: int
343 if LINUX:
344 size: int
345 pss: int
346 shared_clean: int
347 shared_dirty: int
348 private_clean: int
349 private_dirty: int
350 referenced: int
351 anonymous: int
352 swap: int
353 elif BSD:
354 private: int
355 ref_count: int
356 shadow_count: int
357 elif SUNOS:
358 anonymous: int
359 locked: int
360
361
362# psutil.Process.memory_maps(grouped=False)
363class pmmap_ext(NamedTuple):
364 addr: str
365 perms: str
366 path: str
367 rss: int
368 if LINUX:
369 size: int
370 pss: int
371 shared_clean: int
372 shared_dirty: int
373 private_clean: int
374 private_dirty: int
375 referenced: int
376 anonymous: int
377 swap: int
378 elif BSD:
379 private: int
380 ref_count: int
381 shadow_count: int
382 elif SUNOS:
383 anonymous: int
384 locked: int
385
386
387# ===================================================================
388# --- Process memory_info() / memory_extras() / memory_full_info()
389# ===================================================================
390
391if LINUX:
392
393 # psutil.Process().memory_info()
394 class pmem(NamedTuple):
395 rss: int
396 vms: int
397 shared: int
398 text: int
399 data: int
400
401 # DEPRECATED
402 @property
403 def lib(self):
404 # It has always been 0 since Linux 2.6.
405 msg = "'lib' field is deprecated and will be removed"
406 warnings.warn(msg, DeprecationWarning, stacklevel=2)
407 return 0
408
409 # DEPRECATED
410 @property
411 def dirty(self):
412 # It has always been 0 since Linux 2.6.
413 msg = "'dirty' field is deprecated and will be removed"
414 warnings.warn(msg, DeprecationWarning, stacklevel=2)
415 return 0
416
417 # psutil.Process().memory_extras()
418 class pmem_extras(NamedTuple):
419 peak_rss: int
420 peak_vms: int
421 rss_anon: int
422 rss_file: int
423 rss_shmem: int
424 swap_anon: int
425 locked: int
426
427 # psutil.Process().memory_full_info() (DEPRECATED)
428 class pfullmem(
429 namedtuple("pfullmem", pmem._fields + ("uss", "pss", "swap"))
430 ):
431 __slots__ = ()
432 lib = pmem.lib
433 dirty = pmem.dirty
434
435elif WINDOWS:
436
437 # psutil.Process.memory_info()
438 class pmem( # noqa: SLOT002
439 namedtuple("pmem", ("rss", "vms", "peak_rss", "peak_vms"))
440 ):
441 def __new__(cls, rss, vms, peak_rss, peak_vms, _deprecated=None):
442 inst = super().__new__(cls, rss, vms, peak_rss, peak_vms)
443 inst.__dict__["_deprecated"] = _deprecated or {}
444 return inst
445
446 def __getattr__(self, name):
447 depr = self.__dict__.get("_deprecated", {})
448 # DEPRECATED
449 if name in depr:
450 msg = f"pmem.{name} is deprecated"
451 if name in {
452 "paged_pool",
453 "nonpaged_pool",
454 "peak_paged_pool",
455 "peak_nonpaged_pool",
456 }:
457 msg += "; use memory_extras() instead"
458 elif name == "num_page_faults":
459 msg += "; use page_faults() instead"
460 warnings.warn(msg, DeprecationWarning, stacklevel=2)
461 return depr[name]
462
463 msg = f"{self.__class__.__name__} object has no attribute {name!r}"
464 raise AttributeError(msg)
465
466 # psutil.Process.memory_extras()
467 class pmem_extras(NamedTuple):
468 virtual: int
469 peak_virtual: int
470 paged_pool: int
471 nonpaged_pool: int
472 peak_paged_pool: int
473 peak_nonpaged_pool: int
474
475 # psutil.Process.memory_full_info() (DEPRECATED)
476 class pfullmem( # noqa: SLOT002
477 namedtuple("pfullmem", pmem._fields + ("uss",))
478 ):
479 __getattr__ = pmem.__getattr__
480
481elif MACOS:
482
483 # psutil.Process.memory_info()
484 class pmem(NamedTuple):
485 rss: int
486 vms: int
487
488 # psutil.Process.memory_extras()
489 class pmem_extras(NamedTuple):
490 phys_footprint: int
491 peak_phys_footprint: int
492
493 # psutil.Process.memory_full_info() (DEPRECATED)
494 pfullmem = namedtuple("pfullmem", pmem._fields + ("uss",))
495
496elif BSD:
497
498 # psutil.Process.memory_info()
499 class pmem(NamedTuple):
500 rss: int
501 vms: int
502 text: int
503 data: int
504 stack: int
505 peak_rss: int
506
507 # psutil.Process.memory_full_info() (DEPRECATED)
508 pfullmem = pmem
509
510elif SUNOS or AIX:
511
512 # psutil.Process.memory_info()
513 class pmem(NamedTuple):
514 rss: int
515 vms: int
516
517 # psutil.Process.memory_full_info() (DEPRECATED)
518 pfullmem = pmem