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# ===================================================================
88# --- Process class
89# ===================================================================
90
91# psutil.Process.cpu_times()
92pcputimes = nt(
93 "pcputimes", ("user", "system", "children_user", "children_system")
94)
95
96# psutil.Process.open_files()
97popenfile = nt("popenfile", ("path", "fd"))
98
99# psutil.Process.threads()
100pthread = nt("pthread", ("id", "user_time", "system_time"))
101
102# psutil.Process.uids()
103puids = nt("puids", ("real", "effective", "saved"))
104
105# psutil.Process.gids()
106pgids = nt("pgids", ("real", "effective", "saved"))
107
108# psutil.Process.io_counters()
109pio = nt("pio", ("read_count", "write_count", "read_bytes", "write_bytes"))
110
111# psutil.Process.ionice()
112pionice = nt("pionice", ("ioclass", "value"))
113
114# psutil.Process.ctx_switches()
115pctxsw = nt("pctxsw", ("voluntary", "involuntary"))
116
117# psutil.Process.net_connections()
118pconn = nt("pconn", ("fd", "family", "type", "laddr", "raddr", "status"))
119
120# psutil.net_connections() and psutil.Process.net_connections()
121addr = nt("addr", ("ip", "port"))
122
123# ===================================================================
124# --- Linux
125# ===================================================================
126
127if LINUX:
128
129 # This gets set from _pslinux.py
130 scputimes = None
131
132 # psutil.virtual_memory()
133 svmem = nt(
134 "svmem",
135 (
136 "total",
137 "available",
138 "percent",
139 "used",
140 "free",
141 "active",
142 "inactive",
143 "buffers",
144 "cached",
145 "shared",
146 "slab",
147 ),
148 )
149
150 # psutil.disk_io_counters()
151 sdiskio = nt(
152 "sdiskio",
153 (
154 "read_count",
155 "write_count",
156 "read_bytes",
157 "write_bytes",
158 "read_time",
159 "write_time",
160 "read_merged_count",
161 "write_merged_count",
162 "busy_time",
163 ),
164 )
165
166 # psutil.Process().open_files()
167 popenfile = nt("popenfile", ("path", "fd", "position", "mode", "flags"))
168
169 # psutil.Process().memory_info()
170 pmem = nt("pmem", ("rss", "vms", "shared", "text", "lib", "data", "dirty"))
171
172 # psutil.Process().memory_full_info()
173 pfullmem = nt("pfullmem", pmem._fields + ("uss", "pss", "swap"))
174
175 # psutil.Process().memory_maps(grouped=True)
176 pmmap_grouped = nt(
177 "pmmap_grouped",
178 (
179 "path",
180 "rss",
181 "size",
182 "pss",
183 "shared_clean",
184 "shared_dirty",
185 "private_clean",
186 "private_dirty",
187 "referenced",
188 "anonymous",
189 "swap",
190 ),
191 )
192
193 # psutil.Process().memory_maps(grouped=False)
194 pmmap_ext = nt(
195 "pmmap_ext", "addr perms " + " ".join(pmmap_grouped._fields)
196 )
197
198 # psutil.Process.io_counters()
199 pio = nt(
200 "pio",
201 (
202 "read_count",
203 "write_count",
204 "read_bytes",
205 "write_bytes",
206 "read_chars",
207 "write_chars",
208 ),
209 )
210
211 # psutil.Process.cpu_times()
212 pcputimes = nt(
213 "pcputimes",
214 ("user", "system", "children_user", "children_system", "iowait"),
215 )
216
217# ===================================================================
218# --- Windows
219# ===================================================================
220
221elif WINDOWS:
222
223 # psutil.cpu_times()
224 scputimes = nt("scputimes", ("user", "system", "idle", "interrupt", "dpc"))
225
226 # psutil.virtual_memory()
227 svmem = nt("svmem", ("total", "available", "percent", "used", "free"))
228
229 # psutil.Process.memory_info()
230 pmem = nt(
231 "pmem",
232 (
233 "rss",
234 "vms",
235 "num_page_faults",
236 "peak_wset",
237 "wset",
238 "peak_paged_pool",
239 "paged_pool",
240 "peak_nonpaged_pool",
241 "nonpaged_pool",
242 "pagefile",
243 "peak_pagefile",
244 "private",
245 ),
246 )
247
248 # psutil.Process.memory_full_info()
249 pfullmem = nt("pfullmem", pmem._fields + ("uss",))
250
251 # psutil.Process.memory_maps(grouped=True)
252 pmmap_grouped = nt("pmmap_grouped", ("path", "rss"))
253
254 # psutil.Process.memory_maps(grouped=False)
255 pmmap_ext = nt(
256 "pmmap_ext", "addr perms " + " ".join(pmmap_grouped._fields)
257 )
258
259 # psutil.Process.io_counters()
260 pio = nt(
261 "pio",
262 (
263 "read_count",
264 "write_count",
265 "read_bytes",
266 "write_bytes",
267 "other_count",
268 "other_bytes",
269 ),
270 )
271
272# ===================================================================
273# --- macOS
274# ===================================================================
275
276elif MACOS:
277
278 # psutil.cpu_times()
279 scputimes = nt("scputimes", ("user", "nice", "system", "idle"))
280
281 # psutil.virtual_memory()
282 svmem = nt(
283 "svmem",
284 (
285 "total",
286 "available",
287 "percent",
288 "used",
289 "free",
290 "active",
291 "inactive",
292 "wired",
293 ),
294 )
295
296 # psutil.Process.memory_info()
297 pmem = nt("pmem", ("rss", "vms", "pfaults", "pageins"))
298
299 # psutil.Process.memory_full_info()
300 pfullmem = nt("pfullmem", pmem._fields + ("uss",))
301
302# ===================================================================
303# --- BSD
304# ===================================================================
305
306elif BSD:
307
308 # psutil.virtual_memory()
309 svmem = nt(
310 "svmem",
311 (
312 "total",
313 "available",
314 "percent",
315 "used",
316 "free",
317 "active",
318 "inactive",
319 "buffers",
320 "cached",
321 "shared",
322 "wired",
323 ),
324 )
325
326 # psutil.cpu_times()
327 scputimes = nt("scputimes", ("user", "nice", "system", "idle", "irq"))
328
329 # psutil.Process.memory_info()
330 pmem = nt("pmem", ("rss", "vms", "text", "data", "stack"))
331
332 # psutil.Process.memory_full_info()
333 pfullmem = pmem
334
335 # psutil.Process.cpu_times()
336 pcputimes = nt(
337 "pcputimes", ("user", "system", "children_user", "children_system")
338 )
339
340 # psutil.Process.memory_maps(grouped=True)
341 pmmap_grouped = nt(
342 "pmmap_grouped", "path rss, private, ref_count, shadow_count"
343 )
344
345 # psutil.Process.memory_maps(grouped=False)
346 pmmap_ext = nt(
347 "pmmap_ext", "addr, perms path rss, private, ref_count, shadow_count"
348 )
349
350 # psutil.disk_io_counters()
351 if FREEBSD:
352 sdiskio = nt(
353 "sdiskio",
354 (
355 "read_count",
356 "write_count",
357 "read_bytes",
358 "write_bytes",
359 "read_time",
360 "write_time",
361 "busy_time",
362 ),
363 )
364 else:
365 sdiskio = nt(
366 "sdiskio",
367 ("read_count", "write_count", "read_bytes", "write_bytes"),
368 )
369
370# ===================================================================
371# --- SunOS
372# ===================================================================
373
374elif SUNOS:
375
376 # psutil.cpu_times()
377 scputimes = nt("scputimes", ("user", "system", "idle", "iowait"))
378
379 # psutil.cpu_times(percpu=True)
380 pcputimes = nt(
381 "pcputimes", ("user", "system", "children_user", "children_system")
382 )
383
384 # psutil.virtual_memory()
385 svmem = nt("svmem", ("total", "available", "percent", "used", "free"))
386
387 # psutil.Process.memory_info()
388 pmem = nt("pmem", ("rss", "vms"))
389
390 # psutil.Process.memory_full_info()
391 pfullmem = pmem
392
393 # psutil.Process.memory_maps(grouped=True)
394 pmmap_grouped = nt("pmmap_grouped", ("path", "rss", "anonymous", "locked"))
395
396 # psutil.Process.memory_maps(grouped=False)
397 pmmap_ext = nt(
398 "pmmap_ext", "addr perms " + " ".join(pmmap_grouped._fields)
399 )
400
401# ===================================================================
402# --- AIX
403# ===================================================================
404
405elif AIX:
406
407 # psutil.Process.memory_info()
408 pmem = nt("pmem", ("rss", "vms"))
409
410 # psutil.Process.memory_full_info()
411 pfullmem = pmem
412
413 # psutil.Process.cpu_times()
414 scputimes = nt("scputimes", ("user", "system", "idle", "iowait"))
415
416 # psutil.virtual_memory()
417 svmem = nt("svmem", ("total", "available", "percent", "used", "free"))