-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFanDriverHardwareLib.py
executable file
·99 lines (90 loc) · 3.42 KB
/
FanDriverHardwareLib.py
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
import psutil
import subprocess
import json
import platform
class HardwareLib:
def __init__(self):
if self.get_nvidia_gpu_temp():
self.gpuModel = "Nvidia"
elif self.get_amd_gpu_temp():
self.gpuModel = "AMD"
elif self.get_intel_gpu_temp():
self.gpuModel = "Intel"
else:
self.gpuModel = None
try:
temperatures = psutil.sensors_temperatures()
temp = temperatures['coretemp'][0][1]
self.cpuModel = "Intel"
except:
try:
temperatures = psutil.sensors_temperatures()
temp = temperatures['k10temp'][0][1]
self.cpuModel = "AMD"
except:
self.cpuModel = None
print(f"Platform established as {self.cpuModel} CPU with {self.gpuModel} GPU")
def get_nvidia_gpu_temp(self):
try:
result = subprocess.run(['/usr/bin/nvidia-smi', '--query-gpu=temperature.gpu', '--format=csv,noheader,nounits'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
temp = result.stdout.decode('utf-8').strip()
return int(temp)
except:
return None
def get_amd_gpu_temp(self):
try:
temperatures = psutil.sensors_temperatures()
return int(temperatures['amdgpu'][0][1])
except:
return get_amd_gpu_temp_backup()
def get_amd_gpu_temp_backup(self):
try:
result = subprocess.run(['/opt/rocm/bin/rocm-smi', '--showtemp'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
output = result.stdout.decode('utf-8')
# Parse the output to find the temperature
for line in output.splitlines():
if "Temperature" in line:
# Example output line: "Temperature: 45 C"
temp = int(line.split(":")[1].strip().split(" ")[0])
print(f"Using AMD backup method")
return temp
except:
return None
def get_intel_gpu_temp(self):
try:
result = subprocess.run(['intel_gpu_top', '-l', '1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
output = result.stdout.decode('utf-8')
# Parse the output for temperature line
for line in output.splitlines():
if "temperature" in line:
# Example: temperature = 45 C
temp = int(line.split(":")[1].strip().split(" ")[0])
return temp
except:
return None
def get_gpu_temp(self):
match self.gpuModel:
case "Nvidia":
return self.get_nvidia_gpu_temp()
case "AMD":
return self.get_amd_gpu_temp()
case "Intel":
return self.get_intel_gpu_temp()
case _:
return None
def get_cpu_usage(self):
try:
return int(psutil.cpu_percent(interval=1))
except:
return None
def get_cpu_temp(self):
try:
temperatures = psutil.sensors_temperatures()
if self.cpuModel == "Intel":
temp = temperatures['coretemp'][0][1]
elif self.cpuModel == "AMD":
temp = temperatures['k10temp'][0][1]
return int(temp)
except:
return None