-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
273 lines (232 loc) · 6.59 KB
/
device.go
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
package nvml
//#cgo CFLAGS: -I /usr/src/gdk/nvml/include -I /usr/include/nvidia/gdk
//#cgo LDFLAGS: -lnvidia-ml -L /usr/src/gdk/nvml/lib
//#include <nvml.h>
import "C"
import (
"fmt"
"io/ioutil"
"strings"
)
type Device struct {
device C.nvmlDevice_t
}
type PciInfo struct {
BusId string
Domain uint
Bus uint
Device uint
PciDeviceId uint
PciSubSystemId uint
reserved0 uint
reserved1 uint
reserved2 uint
reserved3 uint
}
type TemperatureThreshold struct {
Slowdown uint
Shutdown uint
}
type UtilizationRate struct {
GPU uint
Memory uint
}
type Memory struct {
Total uint64
Free uint64
Used uint64
}
type Clock struct {
Graphics uint
GraphicsMax uint
SM uint
SMMax uint
Mem uint
MemMax uint
}
type Process struct {
PID uint
Name string
MemoryUsed uint64
}
func (d *Device) Name() (string, error) {
var name [C.NVML_DEVICE_NAME_BUFFER_SIZE]C.char
result := C.nvmlDeviceGetName(d.device, &name[0], C.NVML_DEVICE_NAME_BUFFER_SIZE)
return C.GoString(&name[0]), err(result)
}
func (d *Device) MinorName() (uint, error) {
var name C.uint
result := C.nvmlDeviceGetMinorNumber(d.device, &name)
return uint(name), err(result)
}
func (d *Device) UUID() (string, error) {
var uuid [C.NVML_DEVICE_UUID_BUFFER_SIZE]C.char
result := C.nvmlDeviceGetUUID(d.device, &uuid[0], C.NVML_DEVICE_UUID_BUFFER_SIZE)
return C.GoString(&uuid[0]), err(result)
}
func (d *Device) PciInfo() (PciInfo, error) {
var info C.nvmlPciInfo_t
result := C.nvmlDeviceGetPciInfo_v2(d.device, &info)
return PciInfo{
BusId: C.GoString(&info.busId[0]),
Domain: uint(info.domain),
Bus: uint(info.bus),
Device: uint(info.device),
PciDeviceId: uint(info.pciDeviceId),
PciSubSystemId: uint(info.pciSubSystemId),
reserved0: uint(info.reserved0),
reserved1: uint(info.reserved1),
reserved2: uint(info.reserved2),
reserved3: uint(info.reserved3),
}, err(result)
}
func (d *Device) TemperatureThreshold() (TemperatureThreshold, error) {
var slowdown C.uint
var shutdown C.uint
result := C.nvmlDeviceGetTemperatureThreshold(d.device, C.NVML_TEMPERATURE_THRESHOLD_SLOWDOWN, &slowdown)
if e := err(result); e != nil {
return TemperatureThreshold{}, e
}
result = C.nvmlDeviceGetTemperatureThreshold(d.device, C.NVML_TEMPERATURE_THRESHOLD_SHUTDOWN, &shutdown)
return TemperatureThreshold{
Slowdown: uint(slowdown),
Shutdown: uint(shutdown),
}, err(result)
}
func (d *Device) Temperature() (uint, error) {
var temperature C.uint
result := C.nvmlDeviceGetTemperature(d.device, C.NVML_TEMPERATURE_GPU, &temperature)
return uint(temperature), err(result)
}
func (d *Device) UtilizationRates() (UtilizationRate, error) {
var utilization C.nvmlUtilization_t
result := C.nvmlDeviceGetUtilizationRates(d.device, &utilization)
return UtilizationRate{
GPU: uint(utilization.gpu),
Memory: uint(utilization.memory),
}, err(result)
}
func (d *Device) FanSpeed() (uint, error) {
var speed C.uint
result := C.nvmlDeviceGetFanSpeed(d.device, &speed)
return uint(speed), err(result)
}
func (d *Device) Memory() (Memory, error) {
var memory C.nvmlMemory_t
result := C.nvmlDeviceGetMemoryInfo(d.device, &memory)
return Memory{
Total: uint64(memory.total),
Free: uint64(memory.free),
Used: uint64(memory.used),
}, err(result)
}
func (d *Device) Bar1Memory() (Memory, error) {
var memory C.nvmlBAR1Memory_t
result := C.nvmlDeviceGetBAR1MemoryInfo(d.device, &memory)
return Memory{
Total: uint64(memory.bar1Total),
Free: uint64(memory.bar1Free),
Used: uint64(memory.bar1Used),
}, err(result)
}
func (d *Device) Clock() (Clock, error) {
var clock, maxClock C.uint
var res Clock
result := C.nvmlDeviceGetMaxClockInfo(d.device, C.NVML_CLOCK_GRAPHICS, &maxClock)
if e := err(result); e != nil {
return res, e
}
res.GraphicsMax = uint(maxClock)
result = C.nvmlDeviceGetClockInfo(d.device, C.NVML_CLOCK_GRAPHICS, &clock)
if e := err(result); e != nil {
return res, e
}
res.Graphics = uint(clock)
result = C.nvmlDeviceGetMaxClockInfo(d.device, C.NVML_CLOCK_SM, &maxClock)
if e := err(result); e != nil {
return res, e
}
res.SMMax = uint(maxClock)
result = C.nvmlDeviceGetClockInfo(d.device, C.NVML_CLOCK_SM, &clock)
if e := err(result); e != nil {
return res, e
}
res.SM = uint(clock)
result = C.nvmlDeviceGetMaxClockInfo(d.device, C.NVML_CLOCK_MEM, &maxClock)
if e := err(result); e != nil {
return res, e
}
res.MemMax = uint(maxClock)
result = C.nvmlDeviceGetClockInfo(d.device, C.NVML_CLOCK_MEM, &clock)
if e := err(result); e != nil {
return res, e
}
res.Mem = uint(clock)
return res, nil
}
func (d *Device) RunningProcess() ([]Process, error) {
var processCount C.uint
var processes = make([]C.nvmlProcessInfo_t, 1)
for {
result := C.nvmlDeviceGetComputeRunningProcesses(d.device, &processCount, &processes[0])
if result == C.NVML_ERROR_INSUFFICIENT_SIZE {
processes = make([]C.nvmlProcessInfo_t, uint(processCount))
} else if e := err(result); e != nil {
return nil, e
} else {
break
}
}
var res = make([]Process, uint(processCount))
var i uint = 0
for ; i < uint(processCount); i++ {
b, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/comm", uint(processes[i].pid)))
if err != nil {
return nil, err
}
res[i].PID = uint(processes[i].pid)
res[i].MemoryUsed = uint64(processes[i].usedGpuMemory)
res[i].Name = strings.Trim(string(b), "\n")
}
return res, nil
}
func (d *Device) PowerUsage() (uint, error) {
var usage C.uint
result := C.nvmlDeviceGetPowerUsage(d.device, &usage)
return uint(usage), err(result)
}
func (d *Device) ComputeMode() (string, error) {
var mode C.nvmlComputeMode_t
result := C.nvmlDeviceGetComputeMode(d.device, &mode)
return computeModeString(mode), err(result)
}
func (d *Device) PerformanceState() (string, error) {
var state C.nvmlPstates_t
result := C.nvmlDeviceGetPerformanceState(d.device, &state)
return stateString(state), err(result)
}
func (d *Device) PowerState() (string, error) {
var state C.nvmlPstates_t
result := C.nvmlDeviceGetPowerState(d.device, &state)
return stateString(state), err(result)
}
func computeModeString(mode C.nvmlComputeMode_t) string {
switch mode {
case C.NVML_COMPUTEMODE_DEFAULT:
return "Default"
case C.NVML_COMPUTEMODE_EXCLUSIVE_THREAD:
return "ExclusiveThread"
case C.NVML_COMPUTEMODE_PROHIBITED:
return "Prohibited"
case C.NVML_COMPUTEMODE_EXCLUSIVE_PROCESS:
return "ExclusiveProcess"
default:
return "Unknown"
}
}
func stateString(state C.nvmlPstates_t) string {
if state == C.NVML_PSTATE_UNKNOWN {
return "UNKNOWN"
}
return fmt.Sprintf("P%v", state)
}