Bump gonvml module and remove CGO dependency.

Signed-off-by: Carlos de Paula <me@carlosedp.com>
This commit is contained in:
Carlos de Paula
2019-09-04 15:27:57 -03:00
parent 84fe3db5cf
commit 8cd98fbd60
13 changed files with 238 additions and 10 deletions

View File

@@ -111,6 +111,38 @@ nvmlReturn_t nvmlDeviceGetPowerUsage(nvmlDevice_t device, unsigned int *power) {
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.
@@ -169,10 +201,26 @@ nvmlReturn_t nvmlInit_dl(void) {
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);
@@ -384,7 +432,7 @@ func (d Device) MemoryInfo() (uint64, uint64, error) {
// UtilizationRates returns the percent of time over the past sample period during which:
// utilization.gpu: one or more kernels were executing on the GPU.
// utilizatoin.memory: global (device) memory was being read or written.
// 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
@@ -429,3 +477,48 @@ func (d Device) AverageGPUUtilization(since time.Duration) (uint, error) {
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)
}