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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
GPU Reader for AMD Radeon Pro VII
|
| 4 |
+
Reads GPU sensor data using rocm-smi and sysfs fallback
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import subprocess
|
| 8 |
+
import os
|
| 9 |
+
import glob
|
| 10 |
+
import time
|
| 11 |
+
from typing import Dict, Optional, Tuple
|
| 12 |
+
|
| 13 |
+
class GPUReader:
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.base_path = "/sys/class/drm/card1/device"
|
| 16 |
+
self._find_hwmon_path()
|
| 17 |
+
|
| 18 |
+
def _find_hwmon_path(self):
|
| 19 |
+
"""Find the hwmon path for AMD GPU"""
|
| 20 |
+
self.hwmon_path = None
|
| 21 |
+
hwmon_base = os.path.join(self.base_path, "hwmon")
|
| 22 |
+
if os.path.exists(hwmon_base):
|
| 23 |
+
hwmons = os.listdir(hwmon_base)
|
| 24 |
+
if hwmons:
|
| 25 |
+
self.hwmon_path = os.path.join(hwmon_base, hwmons[0])
|
| 26 |
+
|
| 27 |
+
def read_file(self, path: str) -> Optional[str]:
|
| 28 |
+
"""Safely read a file and return its content"""
|
| 29 |
+
if not path or not os.path.exists(path):
|
| 30 |
+
return None
|
| 31 |
+
try:
|
| 32 |
+
with open(path, 'r') as f:
|
| 33 |
+
return f.read().strip()
|
| 34 |
+
except Exception:
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
def get_gpu_usage(self) -> Optional[float]:
|
| 38 |
+
"""Get GPU usage percentage using rocm-smi"""
|
| 39 |
+
try:
|
| 40 |
+
result = subprocess.run(['rocm-smi', '--showuse'],
|
| 41 |
+
capture_output=True, text=True, timeout=5)
|
| 42 |
+
if result.returncode == 0:
|
| 43 |
+
lines = result.stdout.split('\n')
|
| 44 |
+
for line in lines:
|
| 45 |
+
if 'GPU use' in line:
|
| 46 |
+
usage = line.split(':')[1].strip().replace('%', '')
|
| 47 |
+
return float(usage)
|
| 48 |
+
except:
|
| 49 |
+
# Fallback to sysfs
|
| 50 |
+
usage = self.read_file(os.path.join(self.base_path, "gpu_busy_percent"))
|
| 51 |
+
if usage:
|
| 52 |
+
return float(usage)
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
def get_vram_usage(self) -> Tuple[Optional[int], Optional[int]]:
|
| 56 |
+
"""Get VRAM usage (used, total) in MB"""
|
| 57 |
+
try:
|
| 58 |
+
# Try rocm-smi first
|
| 59 |
+
result = subprocess.run(['rocm-smi', '--showmeminfo', 'vram'],
|
| 60 |
+
capture_output=True, text=True, timeout=5)
|
| 61 |
+
if result.returncode == 0:
|
| 62 |
+
lines = result.stdout.split('\n')
|
| 63 |
+
used_mb, total_mb = None, None
|
| 64 |
+
for line in lines:
|
| 65 |
+
if 'VRAM Total' in line:
|
| 66 |
+
total_mb = int(line.split(':')[1].strip().replace('MB', ''))
|
| 67 |
+
elif 'VRAM Used' in line:
|
| 68 |
+
used_mb = int(line.split(':')[1].strip().replace('MB', ''))
|
| 69 |
+
if used_mb is not None and total_mb is not None:
|
| 70 |
+
return used_mb, total_mb
|
| 71 |
+
except:
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
+
# Fallback to sysfs
|
| 75 |
+
vram_used = self.read_file(os.path.join(self.base_path, "mem_info_vram_used"))
|
| 76 |
+
vram_total = self.read_file(os.path.join(self.base_path, "mem_info_vram_total"))
|
| 77 |
+
if vram_used and vram_total:
|
| 78 |
+
used_mb = int(vram_used) // (1024*1024)
|
| 79 |
+
total_mb = int(vram_total) // (1024*1024)
|
| 80 |
+
return used_mb, total_mb
|
| 81 |
+
return None, None
|
| 82 |
+
|
| 83 |
+
def get_temperature(self) -> Optional[float]:
|
| 84 |
+
"""Get GPU temperature in Celsius"""
|
| 85 |
+
if self.hwmon_path:
|
| 86 |
+
temp_raw = self.read_file(os.path.join(self.hwmon_path, "temp1_input"))
|
| 87 |
+
if temp_raw:
|
| 88 |
+
return int(temp_raw) // 1000
|
| 89 |
+
|
| 90 |
+
# Fallback to rocm-smi
|
| 91 |
+
try:
|
| 92 |
+
result = subprocess.run(['rocm-smi', '--showtemp'],
|
| 93 |
+
capture_output=True, text=True, timeout=5)
|
| 94 |
+
if result.returncode == 0:
|
| 95 |
+
lines = result.stdout.split('\n')
|
| 96 |
+
for line in lines:
|
| 97 |
+
if 'Temperature' in line and 'GPU' in line:
|
| 98 |
+
temp_str = line.split(':')[1].strip().replace('c', '').replace('C', '')
|
| 99 |
+
return float(temp_str)
|
| 100 |
+
except:
|
| 101 |
+
pass
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
def get_power_draw(self) -> Optional[float]:
|
| 105 |
+
"""Get GPU power draw in Watts"""
|
| 106 |
+
if self.hwmon_path:
|
| 107 |
+
power_raw = self.read_file(os.path.join(self.hwmon_path, "power1_input"))
|
| 108 |
+
if power_raw:
|
| 109 |
+
return int(power_raw) // 1000000
|
| 110 |
+
|
| 111 |
+
# Fallback to rocm-smi
|
| 112 |
+
try:
|
| 113 |
+
result = subprocess.run(['rocm-smi', '--showpower'],
|
| 114 |
+
capture_output=True, text=True, timeout=5)
|
| 115 |
+
if result.returncode == 0:
|
| 116 |
+
lines = result.stdout.split('\n')
|
| 117 |
+
for line in lines:
|
| 118 |
+
if 'Average Graphics Package Power' in line:
|
| 119 |
+
power_str = line.split(':')[1].strip().replace('W', '')
|
| 120 |
+
return float(power_str)
|
| 121 |
+
except:
|
| 122 |
+
pass
|
| 123 |
+
return None
|
| 124 |
+
|
| 125 |
+
def get_fan_speed(self) -> Tuple[Optional[int], Optional[int]]:
|
| 126 |
+
"""Get fan speed (RPM, PWM percentage)"""
|
| 127 |
+
rpm, pwm_pct = None, None
|
| 128 |
+
|
| 129 |
+
if self.hwmon_path:
|
| 130 |
+
# Get PWM value
|
| 131 |
+
fan_pwm = self.read_file(os.path.join(self.hwmon_path, "pwm1"))
|
| 132 |
+
if fan_pwm:
|
| 133 |
+
pwm_pct = (int(fan_pwm) * 100) // 255
|
| 134 |
+
|
| 135 |
+
# Get RPM
|
| 136 |
+
fan_rpm = self.read_file(os.path.join(self.hwmon_path, "fan1_input"))
|
| 137 |
+
if fan_rpm:
|
| 138 |
+
rpm = int(fan_rpm)
|
| 139 |
+
|
| 140 |
+
# Fallback to rocm-smi if needed
|
| 141 |
+
if rpm is None or pwm_pct is None:
|
| 142 |
+
try:
|
| 143 |
+
result = subprocess.run(['rocm-smi', '--showfan'],
|
| 144 |
+
capture_output=True, text=True, timeout=5)
|
| 145 |
+
if result.returncode == 0:
|
| 146 |
+
lines = result.stdout.split('\n')
|
| 147 |
+
for line in lines:
|
| 148 |
+
if 'Fan Speed' in line:
|
| 149 |
+
# Parse fan info
|
| 150 |
+
pass # Implementation would depend on exact output format
|
| 151 |
+
except:
|
| 152 |
+
pass
|
| 153 |
+
|
| 154 |
+
return rpm, pwm_pct
|
| 155 |
+
|
| 156 |
+
def get_clocks(self) -> Tuple[Optional[int], Optional[int]]:
|
| 157 |
+
"""Get GPU clocks (core, memory) in MHz"""
|
| 158 |
+
core_clock, mem_clock = None, None
|
| 159 |
+
|
| 160 |
+
# Try parsing sysfs
|
| 161 |
+
sclk_raw = self.read_file(os.path.join(self.base_path, "pp_dpm_sclk"))
|
| 162 |
+
mclk_raw = self.read_file(os.path.join(self.base_path, "pp_dpm_mclk"))
|
| 163 |
+
|
| 164 |
+
if sclk_raw:
|
| 165 |
+
for line in sclk_raw.split('\n'):
|
| 166 |
+
if '*' in line:
|
| 167 |
+
core_clock = int(line.split(':')[1].strip().split(' ')[0].replace('Mhz', ''))
|
| 168 |
+
break
|
| 169 |
+
|
| 170 |
+
if mclk_raw:
|
| 171 |
+
for line in mclk_raw.split('\n'):
|
| 172 |
+
if '*' in line:
|
| 173 |
+
mem_clock = int(line.split(':')[1].strip().split(' ')[0].replace('Mhz', ''))
|
| 174 |
+
break
|
| 175 |
+
|
| 176 |
+
# Fallback to rocm-smi
|
| 177 |
+
if core_clock is None or mem_clock is None:
|
| 178 |
+
try:
|
| 179 |
+
result = subprocess.run(['rocm-smi', '--showclocks'],
|
| 180 |
+
capture_output=True, text=True, timeout=5)
|
| 181 |
+
if result.returncode == 0:
|
| 182 |
+
lines = result.stdout.split('\n')
|
| 183 |
+
for line in lines:
|
| 184 |
+
if 'GPU clock' in line and core_clock is None:
|
| 185 |
+
core_str = line.split(':')[1].strip().replace('Mhz', '').replace('MHz', '')
|
| 186 |
+
core_clock = int(float(core_str))
|
| 187 |
+
elif 'Memory clock' in line and mem_clock is None:
|
| 188 |
+
mem_str = line.split(':')[1].strip().replace('Mhz', '').replace('MHz', '')
|
| 189 |
+
mem_clock = int(float(mem_str))
|
| 190 |
+
except:
|
| 191 |
+
pass
|
| 192 |
+
|
| 193 |
+
return core_clock, mem_clock
|
| 194 |
+
|
| 195 |
+
def get_all_data(self) -> Dict[str, any]:
|
| 196 |
+
"""Get all GPU data in a single call"""
|
| 197 |
+
data = {
|
| 198 |
+
'gpu_usage': self.get_gpu_usage(),
|
| 199 |
+
'vram_used': None,
|
| 200 |
+
'vram_total': None,
|
| 201 |
+
'temperature': self.get_temperature(),
|
| 202 |
+
'power_draw': self.get_power_draw(),
|
| 203 |
+
'fan_rpm': None,
|
| 204 |
+
'fan_pwm': None,
|
| 205 |
+
'core_clock': None,
|
| 206 |
+
'mem_clock': None
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
# Get VRAM usage
|
| 210 |
+
vram_used, vram_total = self.get_vram_usage()
|
| 211 |
+
data['vram_used'] = vram_used
|
| 212 |
+
data['vram_total'] = vram_total
|
| 213 |
+
|
| 214 |
+
# Get fan speed
|
| 215 |
+
fan_rpm, fan_pwm = self.get_fan_speed()
|
| 216 |
+
data['fan_rpm'] = fan_rpm
|
| 217 |
+
data['fan_pwm'] = fan_pwm
|
| 218 |
+
|
| 219 |
+
# Get clocks
|
| 220 |
+
core_clock, mem_clock = self.get_clocks()
|
| 221 |
+
data['core_clock'] = core_clock
|
| 222 |
+
data['mem_clock'] = mem_clock
|
| 223 |
+
|
| 224 |
+
return data
|
| 225 |
+
|
| 226 |
+
if __name__ == "__main__":
|
| 227 |
+
# Test the GPU reader
|
| 228 |
+
reader = GPUReader()
|
| 229 |
+
data = reader.get_all_data()
|
| 230 |
+
print("GPU Data:")
|
| 231 |
+
for key, value in data.items():
|
| 232 |
+
print(f" {key}: {value}")
|