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