-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpumon.py
282 lines (262 loc) · 10.7 KB
/
gpumon.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
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
import os
import platform
from cffi import FFI
ffi = FFI()
ffi.cdef('''
typedef void wrap_nvml_handle;
wrap_nvml_handle* wrap_nvml_create();
int wrap_nvml_destroy(wrap_nvml_handle* nvmlh);
int wrap_nvml_get_gpucount(wrap_nvml_handle* nvmlh, int* gpucount);
int wrap_nvml_get_gpu_name(wrap_nvml_handle* nvmlh, int gpuindex, char* namebuf, int bufsize);
int wrap_nvml_get_tempC(wrap_nvml_handle* nvmlh, int gpuindex, unsigned int* tempC);
int wrap_nvml_get_fanpcnt(wrap_nvml_handle* nvmlh, int gpuindex, unsigned int* fanpcnt);
int wrap_nvml_get_power_usage(wrap_nvml_handle* nvmlh, int gpuindex, unsigned int* milliwatts);
typedef void wrap_adl_handle;
wrap_adl_handle* wrap_adl_create();
int wrap_adl_destroy(wrap_adl_handle* adlh);
int wrap_adl_get_gpucount(wrap_adl_handle* adlh, int* gpucount);
int wrap_adl_get_gpu_name(wrap_adl_handle* adlh, int gpuindex, char* namebuf, int bufsize);
int wrap_adl_get_gpu_pci_id(wrap_adl_handle* adlh, int gpuindex, char* idbuf, int bufsize);
int wrap_adl_get_tempC(wrap_adl_handle* adlh, int gpuindex, unsigned int* tempC);
int wrap_adl_get_fanpcnt(wrap_adl_handle* adlh, int gpuindex, unsigned int* fanpcnt);
int wrap_adl_get_power_usage(wrap_adl_handle* adlh, int gpuindex, unsigned int* milliwatts);
int wrap_nvml_get_current_clock(wrap_nvml_handle* nvmlh, int gpuindex, unsigned int *CoreClock, unsigned int *MemoryClock);
int wrap_nvml_get_base_clock(wrap_nvml_handle* nvmlh, int gpuindex, unsigned int *CoreClock, unsigned int *MemoryClock);
typedef void wrap_amdsysfs_handle;
wrap_amdsysfs_handle* wrap_amdsysfs_create();
int wrap_amdsysfs_destroy(wrap_amdsysfs_handle* sysfsh);
int wrap_amdsysfs_get_gpucount(wrap_amdsysfs_handle* sysfsh, int* gpucount);
int wrap_amdsysfs_get_tempC(wrap_amdsysfs_handle* sysfsh, int index, unsigned int* tempC);
int wrap_amdsysfs_get_fanpcnt(wrap_amdsysfs_handle* sysfsh, int index, unsigned int* fanpcnt);
int wrap_amdsysfs_get_power_usage(wrap_amdsysfs_handle* sysfsh, int index, unsigned int* milliwatts);
int wrap_amdsysfs_get_gpu_pci(wrap_amdsysfs_handle* sysfsh, int index, char* pcibuf, int bufsize);
int wrap_amdsysfs_get_vid_pid_subsysid(wrap_amdsysfs_handle* sysfsh, int index, char* buf, int bufsize);
int wrap_amdsysfs_get_clock(wrap_amdsysfs_handle* sysfsh, int index, unsigned int *baseCoreClock, unsigned int *baseMemoryClock, unsigned int *coreClock, unsigned int *memoryClock);
''')
if platform.system() == 'Linux':
lib = ffi.dlopen('./gpumon/libgpumon.so')
nvHandle = lib.wrap_nvml_create()
amdHandle = lib.wrap_adl_create()
fsHandle = lib.wrap_amdsysfs_create()
else:
nvHandle = None
amdHandle = None
fsHandle = None
def nvmlGetGpuCount():
gpuCount = 0
if nvHandle:
count = ffi.new("int*", 0)
lib.wrap_nvml_get_gpucount(nvHandle, count)
gpuCount = count[0]
ffi.release(count)
return gpuCount
def nvmlGetGpuName():
gpuName = None
if nvHandle:
count = ffi.new("int*", 0)
lib.wrap_nvml_get_gpucount(nvHandle, count)
name = ffi.new("char[128]")
if count[0]:
lib.wrap_nvml_get_gpu_name(nvHandle, 0, name, 128)
gpuName = ffi.string(name).decode()
ffi.release(count)
ffi.release(name)
return gpuName
def nvmlGetGpuInfo():
info = []
if nvHandle:
count = ffi.new("int*", 0)
lib.wrap_nvml_get_gpucount(nvHandle, count)
name = ffi.new("char[128]")
tempC = ffi.new("unsigned int*", 0)
fanpcnt = ffi.new("unsigned int*", 0)
power_usage = ffi.new("unsigned int*", 0)
for i in range(count[0]):
deviceinfo = {}
lib.wrap_nvml_get_gpu_name(nvHandle, i, name, 128)
deviceinfo['name'] = ffi.string(name).decode()
lib.wrap_nvml_get_tempC(nvHandle, i, tempC)
deviceinfo['tempC'] = tempC[0]
lib.wrap_nvml_get_fanpcnt(nvHandle, i, fanpcnt)
deviceinfo['fanpcnt'] = fanpcnt[0]
lib.wrap_nvml_get_power_usage(nvHandle, i, power_usage)
deviceinfo['power_usage'] = power_usage[0]
info.append(deviceinfo)
ffi.release(count)
ffi.release(name)
ffi.release(tempC)
ffi.release(fanpcnt)
ffi.release(power_usage)
return info
def nvmlGetGpuClock():
info = []
if nvHandle:
count = ffi.new("int*", 0)
lib.wrap_nvml_get_gpucount(nvHandle, count)
baseCoreClock = ffi.new("unsigned int*", 0)
baseMemoryClock = ffi.new("unsigned int*", 0)
currentCoreClock = ffi.new("unsigned int*", 0)
currentMemoryClock = ffi.new("unsigned int*", 0)
for i in range(count[0]):
deviceinfo = {}
lib.wrap_nvml_get_base_clock(nvHandle, i, baseCoreClock, baseMemoryClock)
deviceinfo['baseCoreClock'] = baseCoreClock[0]
deviceinfo['baseMemoryClock'] = baseMemoryClock[0]
lib.wrap_nvml_get_current_clock(nvHandle, i, currentCoreClock, currentMemoryClock)
deviceinfo['currentCoreClock'] = currentCoreClock[0]
deviceinfo['currentMemoryClock'] = currentMemoryClock[0]
info.append(deviceinfo)
ffi.release(count)
ffi.release(baseCoreClock)
ffi.release(baseMemoryClock)
ffi.release(currentCoreClock)
ffi.release(currentMemoryClock)
return info
def amdGetGpuCount():
gpuCount = 0
if amdHandle:
count = ffi.new("int*", 0)
lib.wrap_adl_get_gpucount(amdHandle, count)
gpuCount = count[0]
ffi.release(count)
return gpuCount
def amdGetGpuName():
gpuName = None
if amdHandle:
count = ffi.new("int*", 0)
lib.wrap_adl_get_gpucount(amdHandle, count)
name = ffi.new("char[128]")
if count[0]:
lib.wrap_adl_get_gpu_name(amdHandle, 0, name, 128)
gpuName = ffi.string(name).decode()
ffi.release(count)
ffi.release(name)
return gpuName
def amdGetGpuInfo():
info = []
if amdHandle:
count = ffi.new("int*", 0)
lib.wrap_adl_get_gpucount(amdHandle, count)
name = ffi.new("char[128]")
tempC = ffi.new("unsigned int*", 0)
fanpcnt = ffi.new("unsigned int*", 0)
power_usage = ffi.new("unsigned int*", 0)
for i in range(count[0]):
deviceinfo = {}
lib.wrap_adl_get_gpu_name(amdHandle, i, name, 128)
deviceinfo['name'] = ffi.string(name).decode()
lib.wrap_adl_get_tempC(amdHandle, i, tempC)
deviceinfo['tempC'] = tempC[0]
lib.wrap_adl_get_fanpcnt(amdHandle, i, fanpcnt)
deviceinfo['fanpcnt'] = fanpcnt[0]
lib.wrap_adl_get_power_usage(amdHandle, i, power_usage)
deviceinfo['power_usage'] = power_usage[0]
info.append(deviceinfo)
ffi.release(count)
ffi.release(name)
ffi.release(tempC)
ffi.release(fanpcnt)
ffi.release(power_usage)
return info
def fsGetGpuCount():
gpuCount = 0
if fsHandle:
count = ffi.new("int*", 0)
lib.wrap_amdsysfs_get_gpucount(fsHandle, count)
gpuCount = count[0]
ffi.release(count)
return gpuCount
def fsGetGpuNameByPci(pcinum):
with os.popen('lspci | grep VGA') as p:
lines = p.read().splitlines(False)
for l in lines:
pci = l.split('VGA compatible controller:')[0].strip()
name = l.split('VGA compatible controller:')[1].strip()
if pci == pcinum:
return name
return ''
def fsGetGpuName():
gpuName = ''
if fsHandle:
count = ffi.new("int*", 0)
pci = ffi.new("char[128]")
lib.wrap_amdsysfs_get_gpucount(fsHandle, count)
if count[0]:
lib.wrap_amdsysfs_get_gpu_pci(fsHandle, 0, pci, 128)
pcinum = ffi.string(pci).decode().strip()
gpuName = fsGetGpuNameByPci(pcinum)
ffi.release(count)
ffi.release(pci)
return gpuName
def getBoardName():
boardname = []
with os.popen('/opt/amdgpu-pro/bin/clinfo') as p:
pci = p.read().splitlines(False)
for l in pci:
if 'Board name:' in l:
name = l.split(':')[1].strip()
print(name)
boardname.append(name)
return boardname
def fsGetGpuInfo():
info = []
if fsHandle:
count = ffi.new("int*", 0)
lib.wrap_amdsysfs_get_gpucount(fsHandle, count)
print(count[0])
name = ffi.new("char[128]")
tempC = ffi.new("unsigned int*", 0)
fanpcnt = ffi.new("unsigned int*", 0)
power_usage = ffi.new("unsigned int*", 0)
bname = getBoardName()
for i in range(count[0]):
deviceinfo = {}
#if bname:
deviceinfo['name'] = bname[i] + '#'
#else:
lib.wrap_amdsysfs_get_vid_pid_subsysid(fsHandle, i, name, 128)
deviceinfo['name'] += ffi.string(name).decode().strip()
lib.wrap_amdsysfs_get_tempC(fsHandle, i, tempC)
deviceinfo['tempC'] = tempC[0]
lib.wrap_amdsysfs_get_fanpcnt(fsHandle, i, fanpcnt)
deviceinfo['fanpcnt'] = fanpcnt[0]
lib.wrap_amdsysfs_get_power_usage(fsHandle, i, power_usage)
deviceinfo['power_usage'] = power_usage[0]
info.append(deviceinfo)
ffi.release(count)
ffi.release(name)
ffi.release(tempC)
ffi.release(fanpcnt)
ffi.release(power_usage)
return info
def fsGetGpuClock():
info = []
if nvHandle:
count = ffi.new("int*", 0)
lib.wrap_amdsysfs_get_gpucount(fsHandle, count)
print(count[0])
baseCoreClock = ffi.new("unsigned int*", 0)
baseMemoryClock = ffi.new("unsigned int*", 0)
currentCoreClock = ffi.new("unsigned int*", 0)
currentMemoryClock = ffi.new("unsigned int*", 0)
for i in range(count[0]):
deviceinfo = {}
lib.wrap_amdsysfs_get_clock(fsHandle, i, baseCoreClock, baseMemoryClock, currentCoreClock, currentMemoryClock)
deviceinfo['baseCoreClock'] = baseCoreClock[0]
deviceinfo['baseMemoryClock'] = baseMemoryClock[0]
deviceinfo['currentCoreClock'] = currentCoreClock[0]
deviceinfo['currentMemoryClock'] = currentMemoryClock[0]
info.append(deviceinfo)
ffi.release(count)
ffi.release(baseCoreClock)
ffi.release(baseMemoryClock)
ffi.release(currentCoreClock)
ffi.release(currentMemoryClock)
return info
if __name__ == '__main__':
print(amdGetGpuInfo())
#print(nvmlGetGpuInfo())
print(fsGetGpuInfo())
#print(nvmlGetGpuClock())
print(fsGetGpuClock())
pass