-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathsensors_python.py
More file actions
496 lines (416 loc) · 16 KB
/
sensors_python.py
File metadata and controls
496 lines (416 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# turing-smart-screen-python - a Python system monitor and library for USB-C displays like Turing Smart Screen or XuanFang
# https://github.com/mathoudebine/turing-smart-screen-python/
# Copyright (C) 2021-2023 Matthieu Houdebine (mathoudebine)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This file will use Python libraries (psutil, GPUtil, etc.) to get hardware sensors
# For all platforms (Linux, Windows, macOS) but not all HW is supported
import math
import platform
import sys
from collections import namedtuple
from enum import IntEnum, auto
from typing import Tuple
# Nvidia GPU
import GPUtil
# CPU & disk sensors
import psutil
import library.sensors.sensors as sensors
from library.log import logger
# AMD GPU on Linux
try:
import pyamdgpuinfo
except:
pyamdgpuinfo = None
# AMD GPU on Windows
try:
import pyadl
except:
pyadl = None
PNIC_BEFORE = {}
class GpuType(IntEnum):
UNSUPPORTED = auto()
AMD = auto()
NVIDIA = auto()
DETECTED_GPU = GpuType.UNSUPPORTED
# Function inspired of psutil/psutil/_pslinux.py:sensors_fans()
# Adapted to also get fan speed percentage instead of raw value
def sensors_fans():
"""Return hardware fans info (for CPU and other peripherals) as a
dict including hardware label and current speed.
Implementation notes:
- /sys/class/hwmon looks like the most recent interface to
retrieve this info, and this implementation relies on it
only (old distros will probably use something else)
- lm-sensors on Ubuntu 16.04 relies on /sys/class/hwmon
"""
from psutil._common import bcat, cat
import collections, glob, os
ret = collections.defaultdict(list)
basenames = glob.glob('/sys/class/hwmon/hwmon*/fan*_*')
if not basenames:
# CentOS has an intermediate /device directory:
# https://github.com/giampaolo/psutil/issues/971
basenames = glob.glob('/sys/class/hwmon/hwmon*/device/fan*_*')
basenames = sorted(set([x.split('_')[0] for x in basenames]))
for base in basenames:
try:
current_rpm = int(bcat(base + '_input'))
try:
max_rpm = int(bcat(base + '_max'))
except:
max_rpm = 1500 # Approximated: max fan speed is 1500 RPM
try:
min_rpm = int(bcat(base + '_min'))
except:
min_rpm = 0 # Approximated: min fan speed is 0 RPM
percent = int((current_rpm - min_rpm) / (max_rpm - min_rpm) * 100)
except (IOError, OSError) as err:
continue
unit_name = cat(os.path.join(os.path.dirname(base), 'name')).strip()
label = cat(base + '_label', fallback=os.path.basename(base)).strip()
custom_sfan = namedtuple('sfan', ['label', 'current', 'percent'])
ret[unit_name].append(custom_sfan(label, current_rpm, percent))
return dict(ret)
def is_cpu_fan(label: str) -> bool:
return ("cpu" in label.lower()) or ("proc" in label.lower())
class Cpu(sensors.Cpu):
@staticmethod
def percentage(interval: float) -> float:
try:
return psutil.cpu_percent(interval=interval)
except:
return math.nan
@staticmethod
def frequency() -> float:
try:
return psutil.cpu_freq().current
except:
return math.nan
@staticmethod
def load() -> Tuple[float, float, float]: # 1 / 5 / 15min avg (%):
try:
return psutil.getloadavg()
except:
return math.nan, math.nan, math.nan
@staticmethod
def temperature() -> float:
cpu_temp = math.nan
try:
sensors_temps = psutil.sensors_temperatures()
if 'coretemp' in sensors_temps:
# Intel CPU
cpu_temp = sensors_temps['coretemp'][0].current
elif 'k10temp' in sensors_temps:
# AMD CPU
cpu_temp = sensors_temps['k10temp'][0].current
elif 'cpu_thermal' in sensors_temps:
# ARM CPU
cpu_temp = sensors_temps['cpu_thermal'][0].current
elif 'zenpower' in sensors_temps:
# AMD CPU with zenpower (k10temp is in blacklist)
cpu_temp = sensors_temps['zenpower'][0].current
except:
# psutil.sensors_temperatures not available on Windows / MacOS
pass
return cpu_temp
@staticmethod
def fan_percent(fan_name: str = None) -> float:
try:
fans = sensors_fans()
if fans:
for name, entries in fans.items():
for entry in entries:
if fan_name is not None and fan_name == "%s/%s" % (name, entry.label):
# Manually selected fan
return entry.percent
elif is_cpu_fan(entry.label) or is_cpu_fan(name):
# Auto-detected fan
return entry.percent
except:
pass
return math.nan
class Gpu(sensors.Gpu):
@staticmethod
def stats() -> Tuple[
float, float, float, float, float]: # load (%) / used mem (%) / used mem (Mb) / total mem (Mb) / temp (°C)
if DETECTED_GPU == GpuType.AMD:
return GpuAmd.stats()
elif DETECTED_GPU == GpuType.NVIDIA:
return GpuNvidia.stats()
else:
return math.nan, math.nan, math.nan, math.nan, math.nan
@staticmethod
def fps() -> int:
if DETECTED_GPU == GpuType.AMD:
return GpuAmd.fps()
elif DETECTED_GPU == GpuType.NVIDIA:
return GpuNvidia.fps()
else:
return -1
@staticmethod
def fan_percent() -> float:
if DETECTED_GPU == GpuType.AMD:
return GpuAmd.fan_percent()
elif DETECTED_GPU == GpuType.NVIDIA:
return GpuNvidia.fan_percent()
else:
return math.nan
@staticmethod
def frequency() -> float:
if DETECTED_GPU == GpuType.AMD:
return GpuAmd.frequency()
elif DETECTED_GPU == GpuType.NVIDIA:
return GpuNvidia.frequency()
else:
return math.nan
@staticmethod
def is_available() -> bool:
global DETECTED_GPU
# Always use Nvidia GPU if available
if GpuNvidia.is_available():
logger.info("Detected Nvidia GPU(s)")
DETECTED_GPU = GpuType.NVIDIA
# Otherwise, use the AMD GPU / APU if available
elif GpuAmd.is_available():
logger.info("Detected AMD GPU(s)")
DETECTED_GPU = GpuType.AMD
else:
logger.warning("No supported GPU found")
DETECTED_GPU = GpuType.UNSUPPORTED
if sys.version_info >= (3, 11) and (platform.system() == "Linux" or platform.system() == "Darwin"):
logger.warning("If you have an AMD GPU, you may need to install some libraries manually: see "
"https://github.com/mathoudebine/turing-smart-screen-python/wiki/Troubleshooting#linux--macos-no-supported-gpu-found-with-an-amd-gpu-and-python-311")
return DETECTED_GPU != GpuType.UNSUPPORTED
class GpuNvidia(sensors.Gpu):
@staticmethod
def stats() -> Tuple[
float, float, float, float, float]: # load (%) / used mem (%) / used mem (Mb) / total mem (Mb) / temp (°C)
# Unlike other sensors, Nvidia GPU with GPUtil pulls in all the stats at once
nvidia_gpus = GPUtil.getGPUs()
try:
memory_used_all = [item.memoryUsed for item in nvidia_gpus]
memory_used_mb = sum(memory_used_all) / len(memory_used_all)
except:
memory_used_mb = math.nan
try:
memory_total_all = [item.memoryTotal for item in nvidia_gpus]
memory_total_mb = sum(memory_total_all) / len(memory_total_all)
except:
memory_total_mb = math.nan
try:
memory_percentage = (memory_used_mb / memory_total_mb) * 100
except:
memory_percentage = math.nan
try:
load_all = [item.load for item in nvidia_gpus]
load = (sum(load_all) / len(load_all)) * 100
except:
load = math.nan
try:
temperature_all = [item.temperature for item in nvidia_gpus]
temperature = sum(temperature_all) / len(temperature_all)
except:
temperature = math.nan
return load, memory_percentage, memory_used_mb, memory_total_mb, temperature
@staticmethod
def fps() -> int:
# Not supported by Python libraries
return -1
@staticmethod
def fan_percent() -> float:
try:
fans = sensors_fans()
if fans:
for name, entries in fans.items():
for entry in entries:
if "gpu" in (entry.label.lower() or name.lower()):
return entry.percent
except:
pass
return math.nan
@staticmethod
def frequency() -> float:
# Not supported by Python libraries
return math.nan
@staticmethod
def is_available() -> bool:
try:
return len(GPUtil.getGPUs()) > 0
except:
return False
class GpuAmd(sensors.Gpu):
@staticmethod
def stats() -> Tuple[
float, float, float, float, float]: # load (%) / used mem (%) / used mem (Mb) / total mem (Mb) / temp (°C)
if pyamdgpuinfo:
# Unlike other sensors, AMD GPU with pyamdgpuinfo pulls in all the stats at once
pyamdgpuinfo.detect_gpus()
amd_gpu = pyamdgpuinfo.get_gpu(0)
try:
memory_used_bytes = amd_gpu.query_vram_usage()
memory_used = memory_used_bytes / 1024 / 1024
except:
memory_used_bytes = math.nan
memory_used = math.nan
try:
memory_total_bytes = amd_gpu.memory_info["vram_size"]
memory_total = memory_total_bytes / 1024 / 1024
except:
memory_total_bytes = math.nan
memory_total = math.nan
try:
memory_percentage = (memory_used_bytes / memory_total_bytes) * 100
except:
memory_percentage = math.nan
try:
load = amd_gpu.query_load() * 100
except:
load = math.nan
try:
temperature = amd_gpu.query_temperature()
except:
temperature = math.nan
return load, memory_percentage, memory_used, memory_total, temperature
elif pyadl:
amd_gpu = pyadl.ADLManager.getInstance().getDevices()[0]
try:
load = amd_gpu.getCurrentUsage()
except:
load = math.nan
try:
temperature = amd_gpu.getCurrentTemperature()
except:
temperature = math.nan
# GPU memory data not supported by pyadl
return load, math.nan, math.nan, math.nan, temperature
@staticmethod
def fps() -> int:
# Not supported by Python libraries
return -1
@staticmethod
def fan_percent() -> float:
try:
# Try with psutil fans
fans = sensors_fans()
if fans:
for name, entries in fans.items():
for entry in entries:
if "gpu" in (entry.label.lower() or name.lower()):
return entry.percent
# Try with pyadl if psutil did not find GPU fan
if pyadl:
return pyadl.ADLManager.getInstance().getDevices()[0].getCurrentFanSpeed(
pyadl.ADL_DEVICE_FAN_SPEED_TYPE_PERCENTAGE)
except:
pass
return math.nan
@staticmethod
def frequency() -> float:
try:
if pyamdgpuinfo:
pyamdgpuinfo.detect_gpus()
return pyamdgpuinfo.get_gpu(0).query_sclk() / 1000000
elif pyadl:
return pyadl.ADLManager.getInstance().getDevices()[0].getCurrentEngineClock()
else:
return math.nan
except:
return math.nan
@staticmethod
def is_available() -> bool:
try:
if pyamdgpuinfo and pyamdgpuinfo.detect_gpus() > 0:
return True
elif pyadl and len(pyadl.ADLManager.getInstance().getDevices()) > 0:
return True
else:
return False
except:
return False
class Memory(sensors.Memory):
@staticmethod
def swap_percent() -> float:
try:
return psutil.swap_memory().percent
except:
return math.nan
@staticmethod
def virtual_percent() -> float:
try:
return psutil.virtual_memory().percent
except:
return math.nan
@staticmethod
def virtual_used() -> int: # In bytes
try:
# Do not use psutil.virtual_memory().used: from https://psutil.readthedocs.io/en/latest/#memory
# "It is calculated differently depending on the platform and designed for informational purposes only"
return psutil.virtual_memory().total - psutil.virtual_memory().available
except:
return -1
@staticmethod
def virtual_free() -> int: # In bytes
try:
# Do not use psutil.virtual_memory().free: from https://psutil.readthedocs.io/en/latest/#memory
# "note that this doesn’t reflect the actual memory available (use available instead)."
return psutil.virtual_memory().available
except:
return -1
class Disk(sensors.Disk):
@staticmethod
def disk_usage_percent() -> float:
try:
return psutil.disk_usage("/").percent
except:
return math.nan
@staticmethod
def disk_used() -> int: # In bytes
try:
return psutil.disk_usage("/").used
except:
return -1
@staticmethod
def disk_free() -> int: # In bytes
try:
return psutil.disk_usage("/").free
except:
return -1
class Net(sensors.Net):
@staticmethod
def stats(if_name, interval) -> Tuple[
int, int, int, int]: # up rate (B/s), uploaded (B), dl rate (B/s), downloaded (B)
try:
# Get current counters
pnic_after = psutil.net_io_counters(pernic=True)
upload_rate = 0
uploaded = 0
download_rate = 0
downloaded = 0
if if_name != "":
if if_name in pnic_after:
try:
upload_rate = (pnic_after[if_name].bytes_sent - PNIC_BEFORE[if_name].bytes_sent) / interval
uploaded = pnic_after[if_name].bytes_sent
download_rate = (pnic_after[if_name].bytes_recv - PNIC_BEFORE[if_name].bytes_recv) / interval
downloaded = pnic_after[if_name].bytes_recv
except:
# Interface might not be in PNIC_BEFORE for now
pass
PNIC_BEFORE.update({if_name: pnic_after[if_name]})
else:
logger.warning("Network interface '%s' not found. Check names in config.yaml." % if_name)
return upload_rate, uploaded, download_rate, downloaded
except:
return -1, -1, -1, -1