-
Notifications
You must be signed in to change notification settings - Fork 46
/
bindings.go
524 lines (468 loc) · 18.6 KB
/
bindings.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gonvml
// #cgo LDFLAGS: -ldl
/*
#include <stddef.h>
#include <dlfcn.h>
#include <stdlib.h>
#include "nvml.h"
// nvmlHandle is the handle for dynamically loaded libnvidia-ml.so
void *nvmlHandle;
nvmlReturn_t (*nvmlInitFunc)(void);
nvmlReturn_t (*nvmlShutdownFunc)(void);
const char* (*nvmlErrorStringFunc)(nvmlReturn_t result);
const char* nvmlErrorString(nvmlReturn_t result) {
if (nvmlErrorStringFunc == NULL) {
return "nvmlErrorString Function Not Found";
}
return nvmlErrorStringFunc(result);
}
nvmlReturn_t (*nvmlSystemGetDriverVersionFunc)(char *version, unsigned int length);
nvmlReturn_t nvmlSystemGetDriverVersion(char *version, unsigned int length) {
if (nvmlSystemGetDriverVersionFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlSystemGetDriverVersionFunc(version, length);
}
nvmlReturn_t (*nvmlDeviceGetCountFunc)(unsigned int *deviceCount);
nvmlReturn_t nvmlDeviceGetCount(unsigned int *deviceCount) {
if (nvmlDeviceGetCountFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetCountFunc(deviceCount);
}
nvmlReturn_t (*nvmlDeviceGetHandleByIndexFunc)(unsigned int index, nvmlDevice_t *device);
nvmlReturn_t nvmlDeviceGetHandleByIndex(unsigned int index, nvmlDevice_t *device) {
if (nvmlDeviceGetHandleByIndexFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetHandleByIndexFunc(index, device);
}
nvmlReturn_t (*nvmlDeviceGetMinorNumberFunc)(nvmlDevice_t device, unsigned int *minorNumber);
nvmlReturn_t nvmlDeviceGetMinorNumber(nvmlDevice_t device, unsigned int *minorNumber) {
if (nvmlDeviceGetMinorNumberFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetMinorNumberFunc(device, minorNumber);
}
nvmlReturn_t (*nvmlDeviceGetUUIDFunc)(nvmlDevice_t device, char *uuid, unsigned int length);
nvmlReturn_t nvmlDeviceGetUUID(nvmlDevice_t device, char *uuid, unsigned int length) {
if (nvmlDeviceGetUUIDFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetUUIDFunc(device, uuid, length);
}
nvmlReturn_t (*nvmlDeviceGetNameFunc)(nvmlDevice_t device, char *name, unsigned int length);
nvmlReturn_t nvmlDeviceGetName(nvmlDevice_t device, char *name, unsigned int length) {
if (nvmlDeviceGetNameFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetNameFunc(device, name, length);
}
nvmlReturn_t (*nvmlDeviceGetMemoryInfoFunc)(nvmlDevice_t device, nvmlMemory_t *memory);
nvmlReturn_t nvmlDeviceGetMemoryInfo(nvmlDevice_t device, nvmlMemory_t *memory) {
if (nvmlDeviceGetMemoryInfoFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetMemoryInfoFunc(device, memory);
}
nvmlReturn_t (*nvmlDeviceGetUtilizationRatesFunc)(nvmlDevice_t device, nvmlUtilization_t *utilization);
nvmlReturn_t nvmlDeviceGetUtilizationRates(nvmlDevice_t device, nvmlUtilization_t *utilization) {
if (nvmlDeviceGetUtilizationRatesFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetUtilizationRatesFunc(device, utilization);
}
nvmlReturn_t (*nvmlDeviceGetPowerUsageFunc)(nvmlDevice_t device, unsigned int *power);
nvmlReturn_t nvmlDeviceGetPowerUsage(nvmlDevice_t device, unsigned int *power) {
if (nvmlDeviceGetPowerUsageFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetPowerUsageFunc(device, power);
}
nvmlReturn_t (*nvmlDeviceGetTemperatureFunc)(nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp);
nvmlReturn_t nvmlDeviceGetTemperature(nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp) {
if (nvmlDeviceGetTemperatureFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetTemperatureFunc(device, sensorType, temp);
}
nvmlReturn_t (*nvmlDeviceGetFanSpeedFunc)(nvmlDevice_t device, unsigned int *speed);
nvmlReturn_t nvmlDeviceGetFanSpeed(nvmlDevice_t device, unsigned int *speed) {
if (nvmlDeviceGetFanSpeedFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetFanSpeedFunc(device, speed);
}
nvmlReturn_t (*nvmlDeviceGetEncoderUtilizationFunc)(nvmlDevice_t device, unsigned int* utilization, unsigned int* samplingPeriodUs);
nvmlReturn_t nvmlDeviceGetEncoderUtilization(nvmlDevice_t device, unsigned int* utilization, unsigned int* samplingPeriodUs) {
if (nvmlDeviceGetEncoderUtilizationFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetEncoderUtilizationFunc(device, utilization, samplingPeriodUs);
}
nvmlReturn_t (*nvmlDeviceGetDecoderUtilizationFunc)(nvmlDevice_t device, unsigned int* utilization, unsigned int* samplingPeriodUs);
nvmlReturn_t nvmlDeviceGetDecoderUtilization(nvmlDevice_t device, unsigned int* utilization, unsigned int* samplingPeriodUs) {
if (nvmlDeviceGetDecoderUtilizationFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
return nvmlDeviceGetDecoderUtilizationFunc(device, utilization, samplingPeriodUs);
}
nvmlReturn_t (*nvmlDeviceGetSamplesFunc)(nvmlDevice_t device, nvmlSamplingType_t type, unsigned long long lastSeenTimeStamp, nvmlValueType_t *sampleValType, unsigned int *sampleCount, nvmlSample_t *samples);
// Loads the "libnvidia-ml.so.1" shared library.
// Loads all symbols needed and initializes NVML.
// Call this before calling any other methods.
nvmlReturn_t nvmlInit_dl(void) {
nvmlHandle = dlopen("libnvidia-ml.so.1", RTLD_LAZY);
if (nvmlHandle == NULL) {
return NVML_ERROR_LIBRARY_NOT_FOUND;
}
nvmlInitFunc = dlsym(nvmlHandle, "nvmlInit_v2");
if (nvmlInitFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlShutdownFunc = dlsym(nvmlHandle, "nvmlShutdown");
if (nvmlShutdownFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlErrorStringFunc = dlsym(nvmlHandle, "nvmlErrorString");
if (nvmlErrorStringFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlSystemGetDriverVersionFunc = dlsym(nvmlHandle, "nvmlSystemGetDriverVersion");
if (nvmlSystemGetDriverVersionFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetCountFunc = dlsym(nvmlHandle, "nvmlDeviceGetCount_v2");
if (nvmlDeviceGetCountFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetHandleByIndexFunc = dlsym(nvmlHandle, "nvmlDeviceGetHandleByIndex_v2");
if (nvmlDeviceGetHandleByIndexFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetMinorNumberFunc = dlsym(nvmlHandle, "nvmlDeviceGetMinorNumber");
if (nvmlDeviceGetMinorNumberFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetUUIDFunc = dlsym(nvmlHandle, "nvmlDeviceGetUUID");
if (nvmlDeviceGetUUIDFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetNameFunc = dlsym(nvmlHandle, "nvmlDeviceGetName");
if (nvmlDeviceGetNameFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetMemoryInfoFunc = dlsym(nvmlHandle, "nvmlDeviceGetMemoryInfo");
if (nvmlDeviceGetMemoryInfoFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetUtilizationRatesFunc = dlsym(nvmlHandle, "nvmlDeviceGetUtilizationRates");
if (nvmlDeviceGetUtilizationRatesFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetPowerUsageFunc = dlsym(nvmlHandle, "nvmlDeviceGetPowerUsage");
if (nvmlDeviceGetPowerUsageFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetTemperatureFunc = dlsym(nvmlHandle, "nvmlDeviceGetTemperature");
if (nvmlDeviceGetTemperatureFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetFanSpeedFunc = dlsym(nvmlHandle, "nvmlDeviceGetFanSpeed");
if (nvmlDeviceGetFanSpeedFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetSamplesFunc = dlsym(nvmlHandle, "nvmlDeviceGetSamples");
if (nvmlDeviceGetSamplesFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetEncoderUtilizationFunc = dlsym(nvmlHandle, "nvmlDeviceGetEncoderUtilization");
if (nvmlDeviceGetEncoderUtilizationFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlDeviceGetDecoderUtilizationFunc = dlsym(nvmlHandle, "nvmlDeviceGetDecoderUtilization");
if (nvmlDeviceGetDecoderUtilizationFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlReturn_t result = nvmlInitFunc();
if (result != NVML_SUCCESS) {
dlclose(nvmlHandle);
nvmlHandle = NULL;
return result;
}
return NVML_SUCCESS;
}
// Shuts down NVML and decrements the reference count on the dynamically loaded
// "libnvidia-ml.so.1" library.
// Call this once NVML is no longer being used.
nvmlReturn_t nvmlShutdown_dl(void) {
if (nvmlHandle == NULL) {
return NVML_SUCCESS;
}
if (nvmlShutdownFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
nvmlReturn_t r = nvmlShutdownFunc();
if (r != NVML_SUCCESS) {
return r;
}
return (dlclose(nvmlHandle) ? NVML_ERROR_UNKNOWN : NVML_SUCCESS);
}
// This function is here because the API provided by NVML is not very user
// friendly. This function can be used to get average utilization.gpu or
// power.draw.
//
// `device`: The identifier of the target device.
// `type`: Type of sampling event. Only NVML_TOTAL_POWER_SAMPLES and NVML_GPU_UTILIZATION_SAMPLES are supported.
// `lastSeenTimeStamp`: Return average using samples with timestamp greather than this timestamp. Unix epoch in micro seconds.
// `averageUsage`: Reference in which average is returned.
//
// In my experiments, I found that NVML_GPU_UTILIZATION_SAMPLES buffer stores
// 100 samples that are uniformly spread with ~6 samples per second. So the
// buffer stores last ~16s of data.
// NVML_TOTAL_POWER_SAMPLES buffer stores 120 samples, but in different runs I
// noticed them to be non-uniformly separated. Sometimes 120 samples only
// consisted of 10s of data and sometimes they were spread over 60s.
//
nvmlReturn_t nvmlDeviceGetAverageUsage(nvmlDevice_t device, nvmlSamplingType_t type, unsigned long long lastSeenTimeStamp, unsigned int* averageUsage) {
if (nvmlHandle == NULL) {
return NVML_ERROR_LIBRARY_NOT_FOUND;
}
if (nvmlDeviceGetSamplesFunc == NULL) {
return NVML_ERROR_FUNCTION_NOT_FOUND;
}
// We don't really use this because both the metrics we support
// averagePowerUsage and averageGPUUtilization are unsigned int.
nvmlValueType_t sampleValType;
// This will be set to the number of samples that can be queried. We would
// need to allocate an array of this size to store the samples.
unsigned int sampleCount;
// Invoking this method with `samples` set to NULL sets the sampleCount.
nvmlReturn_t r = nvmlDeviceGetSamplesFunc(device, type, lastSeenTimeStamp, &sampleValType, &sampleCount, NULL);
if (r != NVML_SUCCESS) {
return r;
}
// Allocate memory to store sampleCount samples.
// In my experiments, the sampleCount at this stage was always 120 for
// NVML_TOTAL_POWER_SAMPLES and 100 for NVML_GPU_UTILIZATION_SAMPLES
nvmlSample_t* samples = (nvmlSample_t*) malloc(sampleCount * sizeof(nvmlSample_t));
r = nvmlDeviceGetSamplesFunc(device, type, lastSeenTimeStamp, &sampleValType, &sampleCount, samples);
if (r != NVML_SUCCESS) {
free(samples);
return r;
}
int i = 0;
unsigned int sum = 0;
for (; i < sampleCount; i++) {
sum += samples[i].sampleValue.uiVal;
}
*averageUsage = sum/sampleCount;
free(samples);
return r;
}
*/
import "C"
import (
"errors"
"fmt"
"time"
)
const (
szDriver = C.NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE
szName = C.NVML_DEVICE_NAME_BUFFER_SIZE
szUUID = C.NVML_DEVICE_UUID_BUFFER_SIZE
)
var errLibraryNotLoaded = errors.New("could not load NVML library")
// Initialize initializes NVML.
// Call this before calling any other methods.
func Initialize() error {
return errorString(C.nvmlInit_dl())
}
// Shutdown shuts down NVML.
// Call this once NVML is no longer being used.
func Shutdown() error {
return errorString(C.nvmlShutdown_dl())
}
// errorString takes a nvmlReturn_t and converts it into a golang error.
// It uses a nvml method to convert to a user friendly error message.
func errorString(ret C.nvmlReturn_t) error {
if ret == C.NVML_SUCCESS {
return nil
}
// We need to special case this because if nvml library is not found
// nvmlErrorString() method will not work.
if ret == C.NVML_ERROR_LIBRARY_NOT_FOUND || C.nvmlHandle == nil {
return errLibraryNotLoaded
}
err := C.GoString(C.nvmlErrorString(ret))
return fmt.Errorf("nvml: %v", err)
}
// SystemDriverVersion returns the the driver version on the system.
func SystemDriverVersion() (string, error) {
if C.nvmlHandle == nil {
return "", errLibraryNotLoaded
}
var driver [szDriver]C.char
r := C.nvmlSystemGetDriverVersion(&driver[0], szDriver)
return C.GoString(&driver[0]), errorString(r)
}
// DeviceCount returns the number of nvidia devices on the system.
func DeviceCount() (uint, error) {
if C.nvmlHandle == nil {
return 0, errLibraryNotLoaded
}
var n C.uint
r := C.nvmlDeviceGetCount(&n)
return uint(n), errorString(r)
}
// Device is the handle for the device.
// This handle is obtained by calling DeviceHandleByIndex().
type Device struct {
dev C.nvmlDevice_t
}
// DeviceHandleByIndex returns the device handle for a particular index.
// The indices range from 0 to DeviceCount()-1. The order in which NVML
// enumerates devices has no guarantees of consistency between reboots.
func DeviceHandleByIndex(idx uint) (Device, error) {
if C.nvmlHandle == nil {
return Device{}, errLibraryNotLoaded
}
var dev C.nvmlDevice_t
r := C.nvmlDeviceGetHandleByIndex(C.uint(idx), &dev)
return Device{dev}, errorString(r)
}
// MinorNumber returns the minor number for the device.
// The minor number for the device is such that the Nvidia device node
// file for each GPU will have the form /dev/nvidia[minor number].
func (d Device) MinorNumber() (uint, error) {
if C.nvmlHandle == nil {
return 0, errLibraryNotLoaded
}
var n C.uint
r := C.nvmlDeviceGetMinorNumber(d.dev, &n)
return uint(n), errorString(r)
}
// UUID returns the globally unique immutable UUID associated with this device.
func (d Device) UUID() (string, error) {
if C.nvmlHandle == nil {
return "", errLibraryNotLoaded
}
var uuid [szUUID]C.char
r := C.nvmlDeviceGetUUID(d.dev, &uuid[0], szUUID)
return C.GoString(&uuid[0]), errorString(r)
}
// Name returns the product name of the device.
func (d Device) Name() (string, error) {
if C.nvmlHandle == nil {
return "", errLibraryNotLoaded
}
var name [szName]C.char
r := C.nvmlDeviceGetName(d.dev, &name[0], szName)
return C.GoString(&name[0]), errorString(r)
}
// MemoryInfo returns the total and used memory (in bytes) of the device.
func (d Device) MemoryInfo() (uint64, uint64, error) {
if C.nvmlHandle == nil {
return 0, 0, errLibraryNotLoaded
}
var memory C.nvmlMemory_t
r := C.nvmlDeviceGetMemoryInfo(d.dev, &memory)
return uint64(memory.total), uint64(memory.used), errorString(r)
}
// UtilizationRates returns the percent of time over the past sample period during which:
// utilization.gpu: one or more kernels were executing on the GPU.
// utilization.memory: global (device) memory was being read or written.
func (d Device) UtilizationRates() (uint, uint, error) {
if C.nvmlHandle == nil {
return 0, 0, errLibraryNotLoaded
}
var utilization C.nvmlUtilization_t
r := C.nvmlDeviceGetUtilizationRates(d.dev, &utilization)
return uint(utilization.gpu), uint(utilization.memory), errorString(r)
}
// PowerUsage returns the power usage for this GPU and its associated circuitry
// in milliwatts. The reading is accurate to within +/- 5% of current power draw.
func (d Device) PowerUsage() (uint, error) {
if C.nvmlHandle == nil {
return 0, errLibraryNotLoaded
}
var n C.uint
r := C.nvmlDeviceGetPowerUsage(d.dev, &n)
return uint(n), errorString(r)
}
// AveragePowerUsage returns the power usage for this GPU and its associated circuitry
// in milliwatts averaged over the samples collected in the last `since` duration.
func (d Device) AveragePowerUsage(since time.Duration) (uint, error) {
if C.nvmlHandle == nil {
return 0, errLibraryNotLoaded
}
lastTs := C.ulonglong(time.Now().Add(-1*since).UnixNano() / 1000)
var n C.uint
r := C.nvmlDeviceGetAverageUsage(d.dev, C.NVML_TOTAL_POWER_SAMPLES, lastTs, &n)
return uint(n), errorString(r)
}
// AverageGPUUtilization returns the utilization.gpu metric (percent of time
// one of more kernels were executing on the GPU) averaged over the samples
// collected in the last `since` duration.
func (d Device) AverageGPUUtilization(since time.Duration) (uint, error) {
if C.nvmlHandle == nil {
return 0, errLibraryNotLoaded
}
lastTs := C.ulonglong(time.Now().Add(-1*since).UnixNano() / 1000)
var n C.uint
r := C.nvmlDeviceGetAverageUsage(d.dev, C.NVML_GPU_UTILIZATION_SAMPLES, lastTs, &n)
return uint(n), errorString(r)
}
// Temperature returns the temperature for this GPU in Celsius.
func (d Device) Temperature() (uint, error) {
if C.nvmlHandle == nil {
return 0, errLibraryNotLoaded
}
var n C.uint
r := C.nvmlDeviceGetTemperature(d.dev, C.NVML_TEMPERATURE_GPU, &n)
return uint(n), errorString(r)
}
// FanSpeed returns the temperature for this GPU in the percentage of its full
// speed, with 100 being the maximum.
func (d Device) FanSpeed() (uint, error) {
if C.nvmlHandle == nil {
return 0, errLibraryNotLoaded
}
var n C.uint
r := C.nvmlDeviceGetFanSpeed(d.dev, &n)
return uint(n), errorString(r)
}
// EncoderUtilization returns the percent of time over the last sample period during which the GPU video encoder was being used.
// The sampling period is variable and is returned in the second return argument in microseconds.
func (d Device) EncoderUtilization() (uint, uint, error) {
if C.nvmlHandle == nil {
return 0, 0, errLibraryNotLoaded
}
var n C.uint
var sp C.uint
r := C.nvmlDeviceGetEncoderUtilization(d.dev, &n, &sp)
return uint(n), uint(sp), errorString(r)
}
// DecoderUtilization returns the percent of time over the last sample period during which the GPU video decoder was being used.
// The sampling period is variable and is returned in the second return argument in microseconds.
func (d Device) DecoderUtilization() (uint, uint, error) {
if C.nvmlHandle == nil {
return 0, 0, errLibraryNotLoaded
}
var n C.uint
var sp C.uint
r := C.nvmlDeviceGetDecoderUtilization(d.dev, &n, &sp)
return uint(n), uint(sp), errorString(r)
}