From cff683eac14a3bbb7235db1a91f4f83f6e3ee7ba Mon Sep 17 00:00:00 2001 From: philsmd Date: Tue, 15 Dec 2015 20:34:07 +0100 Subject: [PATCH] change in library loading: libnvidia-ml now does load dynamically only on NVidia systems --- include/ext_nvml.h | 26 ++++++--- include/shared.h | 4 +- src/Makefile | 2 +- src/ext_nvml.c | 137 +++++++++++++++++++++++++++++++++++++++------ src/oclHashcat.c | 21 +++++-- src/shared.c | 39 ++++++++----- 6 files changed, 182 insertions(+), 47 deletions(-) diff --git a/include/ext_nvml.h b/include/ext_nvml.h index 8f89f1c..102e072 100644 --- a/include/ext_nvml.h +++ b/include/ext_nvml.h @@ -12,13 +12,23 @@ typedef nvmlDevice_t HM_ADAPTER_NV; -nvmlReturn_t hc_NVML_nvmlInit (void); -nvmlReturn_t hc_NVML_nvmlShutdown (void); -nvmlReturn_t hc_NVML_nvmlDeviceGetName (nvmlDevice_t device, char *name, unsigned int length); -nvmlReturn_t hc_NVML_nvmlDeviceGetHandleByIndex (unsigned int index, nvmlDevice_t *device); -nvmlReturn_t hc_NVML_nvmlDeviceGetTemperature (nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp); -nvmlReturn_t hc_NVML_nvmlDeviceGetFanSpeed (nvmlDevice_t device, unsigned int *speed); -nvmlReturn_t hc_NVML_nvmlDeviceGetPowerUsage (nvmlDevice_t device, unsigned int *power); -nvmlReturn_t hc_NVML_nvmlDeviceGetUtilizationRates (nvmlDevice_t device, nvmlUtilization_t *utilization); +typedef const char * (*NVML_ERROR_STRING) (nvmlReturn_t); +typedef int (*NVML_INIT) (); +typedef int (*NVML_SHUTDOWN) (); +typedef nvmlReturn_t (*NVML_DEVICE_GET_NAME) (nvmlDevice_t, char *, unsigned int); +typedef nvmlReturn_t (*NVML_DEVICE_GET_HANDLE_BY_INDEX) (unsigned int, nvmlDevice_t *); +typedef nvmlReturn_t (*NVML_DEVICE_GET_TEMPERATURE) (nvmlDevice_t, nvmlTemperatureSensors_t, unsigned int *); +typedef nvmlReturn_t (*NVML_DEVICE_GET_FAN_SPEED) (nvmlDevice_t, unsigned int *); +typedef nvmlReturn_t (*NVML_DEVICE_GET_POWER_USAGE) (nvmlDevice_t, unsigned int *); +typedef nvmlReturn_t (*NVML_DEVICE_GET_UTILIZATION_RATES) (nvmlDevice_t, nvmlUtilization_t *); + +nvmlReturn_t hc_NVML_nvmlInit (HM_LIB hDLL); +nvmlReturn_t hc_NVML_nvmlShutdown (HM_LIB hDLL); +nvmlReturn_t hc_NVML_nvmlDeviceGetName (HM_LIB hDLL, nvmlDevice_t device, char *name, unsigned int length); +nvmlReturn_t hc_NVML_nvmlDeviceGetHandleByIndex (HM_LIB hDLL, int, unsigned int index, nvmlDevice_t *device); +nvmlReturn_t hc_NVML_nvmlDeviceGetTemperature (HM_LIB hDLL, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp); +nvmlReturn_t hc_NVML_nvmlDeviceGetFanSpeed (HM_LIB hDLL, int, nvmlDevice_t device, unsigned int *speed); +nvmlReturn_t hc_NVML_nvmlDeviceGetPowerUsage (HM_LIB hDLL, nvmlDevice_t device, unsigned int *power); +nvmlReturn_t hc_NVML_nvmlDeviceGetUtilizationRates (HM_LIB hDLL, nvmlDevice_t device, nvmlUtilization_t *utilization); #endif diff --git a/include/shared.h b/include/shared.h index 2e4124c..57488cb 100644 --- a/include/shared.h +++ b/include/shared.h @@ -2026,9 +2026,9 @@ uint32_t *hm_get_list_valid_adl_adapters (int iNumberAdapters, int *num_adl_adap int hm_get_overdrive_version (HM_LIB hm_dll, hm_attrs_t *hm_device, uint32_t *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo); int hm_check_fanspeed_control (HM_LIB hm_dll, hm_attrs_t *hm_device, uint32_t *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo); -void hm_close_amd (HM_LIB hm_dll); +void hm_close (HM_LIB hm_dll); -HM_LIB hm_init_amd (); +HM_LIB hm_init (); int hm_get_temperature_with_device_id (const uint device_id); int hm_get_fanspeed_with_device_id (const uint device_id); diff --git a/src/Makefile b/src/Makefile index c1c0e43..1b4a9bd 100644 --- a/src/Makefile +++ b/src/Makefile @@ -98,7 +98,7 @@ CFLAGS_WIN := -D_WIN -DWIN -D__MSVCRT__ -D__USE_MINGW_ANSI_STDIO CFLAGS_LINUX += -I$(OCL)/include/ -I$(ADL)/include/ -I$(GDK)/usr/include/nvidia/gdk/ CFLAGS_WIN += -I$(OCL)/include/ -I$(ADL)/include/ -I$(NVAPI)/ -LFLAGS_LINUX := -lpthread -lOpenCL -ldl -lnvidia-ml +LFLAGS_LINUX := -lpthread -lOpenCL -ldl LFLAGS_WIN := -lpsapi -L./lib ## diff --git a/src/ext_nvml.c b/src/ext_nvml.c index 7a3eb73..8a06ee7 100644 --- a/src/ext_nvml.c +++ b/src/ext_nvml.c @@ -5,13 +5,43 @@ #include -nvmlReturn_t hc_NVML_nvmlInit (void) +//#ifdef _POSIX // implied + +void *GetLibFunction (void *pLibrary, const char *name) +{ + return dlsym (pLibrary, name); +} + +const char * hc_NVML_nvmlErrorString (HM_LIB hDLL, nvmlReturn_t nvml_rc) +{ + NVML_ERROR_STRING nvmlErrorString = (NVML_ERROR_STRING) GetLibFunction (hDLL, "nvmlErrorString"); + + if (nvmlErrorString == NULL) + { + log_error ("ERROR: %s\n", "nvmlErrorString() is missing"); + + exit (-1); + } + + return nvmlErrorString (nvml_rc); +} + +nvmlReturn_t hc_NVML_nvmlInit (HM_LIB hDLL) { + NVML_INIT nvmlInit = (NVML_INIT) GetLibFunction (hDLL, "nvmlInit"); + + if (nvmlInit == NULL) + { + log_error ("ERROR: %s\n", "nvmlInit() is missing"); + + exit (-1); + } + nvmlReturn_t nvml_rc = nvmlInit (); if (nvml_rc != NVML_SUCCESS) { - const char *string = nvmlErrorString (nvml_rc); + const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc); log_info ("WARN: %s %d %s\n", "nvmlInit()", nvml_rc, string); } @@ -19,13 +49,22 @@ nvmlReturn_t hc_NVML_nvmlInit (void) return nvml_rc; } -nvmlReturn_t hc_NVML_nvmlShutdown (void) +nvmlReturn_t hc_NVML_nvmlShutdown (HM_LIB hDLL) { + NVML_SHUTDOWN nvmlShutdown = (NVML_SHUTDOWN) GetLibFunction (hDLL, "nvmlShutdown"); + + if (nvmlShutdown == NULL) + { + log_error ("ERROR: %s\n", "nvmlShutdown() is missing"); + + exit (-1); + } + nvmlReturn_t nvml_rc = nvmlShutdown (); if (nvml_rc != NVML_SUCCESS) { - const char *string = nvmlErrorString (nvml_rc); + const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc); log_info ("WARN: %s %d %s\n", "nvmlShutdown()", nvml_rc, string); } @@ -33,13 +72,22 @@ nvmlReturn_t hc_NVML_nvmlShutdown (void) return nvml_rc; } -nvmlReturn_t hc_NVML_nvmlDeviceGetName (nvmlDevice_t device, char *name, unsigned int length) +nvmlReturn_t hc_NVML_nvmlDeviceGetName (HM_LIB hDLL, nvmlDevice_t device, char *name, unsigned int length) { + NVML_DEVICE_GET_NAME nvmlDeviceGetName = (NVML_DEVICE_GET_NAME) GetLibFunction (hDLL, "nvmlDeviceGetName"); + + if (nvmlDeviceGetName == NULL) + { + log_error ("ERROR: %s\n", "nvmlDeviceGetName() is missing"); + + exit (-1); + } + nvmlReturn_t nvml_rc = nvmlDeviceGetName (device, name, length); if (nvml_rc != NVML_SUCCESS) { - const char *string = nvmlErrorString (nvml_rc); + const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc); log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string); } @@ -47,29 +95,50 @@ nvmlReturn_t hc_NVML_nvmlDeviceGetName (nvmlDevice_t device, char *name, unsigne return nvml_rc; } -nvmlReturn_t hc_NVML_nvmlDeviceGetHandleByIndex (unsigned int index, nvmlDevice_t *device) +nvmlReturn_t hc_NVML_nvmlDeviceGetHandleByIndex (HM_LIB hDLL, int skip_warnings, unsigned int index, nvmlDevice_t *device) { + NVML_DEVICE_GET_HANDLE_BY_INDEX nvmlDeviceGetHandleByIndex = (NVML_DEVICE_GET_HANDLE_BY_INDEX) GetLibFunction (hDLL, "nvmlDeviceGetHandleByIndex"); + + if (nvmlDeviceGetHandleByIndex == NULL) + { + log_error ("ERROR: %s\n", "nvmlDeviceGetHandleByIndex() is missing"); + + exit (-1); + } + nvmlReturn_t nvml_rc = nvmlDeviceGetHandleByIndex (index, device); if (nvml_rc != NVML_SUCCESS) { - const char *string = nvmlErrorString (nvml_rc); + if (skip_warnings == 0) + { + const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc); - log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string); + log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string); + } } return nvml_rc; } -nvmlReturn_t hc_NVML_nvmlDeviceGetTemperature (nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp) +nvmlReturn_t hc_NVML_nvmlDeviceGetTemperature (HM_LIB hDLL, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp) { + NVML_DEVICE_GET_TEMPERATURE nvmlDeviceGetTemperature = (NVML_DEVICE_GET_TEMPERATURE) GetLibFunction (hDLL, "nvmlDeviceGetTemperature"); + + if (nvmlDeviceGetTemperature == NULL) + { + log_error ("ERROR: %s\n", "nvmlDeviceGetTemperature() is missing"); + + exit (-1); + } + nvmlReturn_t nvml_rc = nvmlDeviceGetTemperature (device, sensorType, temp); if (nvml_rc != NVML_SUCCESS) { *temp = -1; - //const char *string = nvmlErrorString (nvml_rc); + //const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc); //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string); } @@ -77,17 +146,29 @@ nvmlReturn_t hc_NVML_nvmlDeviceGetTemperature (nvmlDevice_t device, nvmlTemperat return nvml_rc; } -nvmlReturn_t hc_NVML_nvmlDeviceGetFanSpeed (nvmlDevice_t device, unsigned int *speed) +nvmlReturn_t hc_NVML_nvmlDeviceGetFanSpeed (HM_LIB hDLL, int skip_warnings, nvmlDevice_t device, unsigned int *speed) { + NVML_DEVICE_GET_FAN_SPEED nvmlDeviceGetFanSpeed = (NVML_DEVICE_GET_FAN_SPEED) GetLibFunction (hDLL, "nvmlDeviceGetFanSpeed"); + + if (nvmlDeviceGetFanSpeed == NULL) + { + log_error ("ERROR: %s\n", "nvmlDeviceGetFanSpeed() is missing"); + + exit (-1); + } + nvmlReturn_t nvml_rc = nvmlDeviceGetFanSpeed (device, speed); if (nvml_rc != NVML_SUCCESS) { *speed = -1; - //const char *string = nvmlErrorString (nvml_rc); + if (skip_warnings == 0) + { + const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc); - //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string); + log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string); + } } return nvml_rc; @@ -95,15 +176,24 @@ nvmlReturn_t hc_NVML_nvmlDeviceGetFanSpeed (nvmlDevice_t device, unsigned int *s /* only tesla following */ -nvmlReturn_t hc_NVML_nvmlDeviceGetPowerUsage (nvmlDevice_t device, unsigned int *power) +nvmlReturn_t hc_NVML_nvmlDeviceGetPowerUsage (HM_LIB hDLL, nvmlDevice_t device, unsigned int *power) { + NVML_DEVICE_GET_POWER_USAGE nvmlDeviceGetPowerUsage = (NVML_DEVICE_GET_POWER_USAGE) GetLibFunction (hDLL, "nvmlDeviceGetPowerUsage"); + + if (nvmlDeviceGetPowerUsage == NULL) + { + log_error ("ERROR: %s\n", "nvmlDeviceGetPowerUsage() is missing"); + + exit (-1); + } + nvmlReturn_t nvml_rc = nvmlDeviceGetPowerUsage (device, power); if (nvml_rc != NVML_SUCCESS) { *power = -1; - //const char *string = nvmlErrorString (nvml_rc); + //const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc); //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string); } @@ -111,8 +201,17 @@ nvmlReturn_t hc_NVML_nvmlDeviceGetPowerUsage (nvmlDevice_t device, unsigned int return nvml_rc; } -nvmlReturn_t hc_NVML_nvmlDeviceGetUtilizationRates (nvmlDevice_t device, nvmlUtilization_t *utilization) +nvmlReturn_t hc_NVML_nvmlDeviceGetUtilizationRates (HM_LIB hDLL, nvmlDevice_t device, nvmlUtilization_t *utilization) { + NVML_DEVICE_GET_UTILIZATION_RATES nvmlDeviceGetUtilizationRates = (NVML_DEVICE_GET_UTILIZATION_RATES) GetLibFunction (hDLL, "nvmlDeviceGetUtilizationRates"); + + if (nvmlDeviceGetUtilizationRates == NULL) + { + log_error ("ERROR: %s\n", "nvmlDeviceGetUtilizationRates() is missing"); + + exit (-1); + } + nvmlReturn_t nvml_rc = nvmlDeviceGetUtilizationRates (device, utilization); if (nvml_rc != NVML_SUCCESS) @@ -120,10 +219,12 @@ nvmlReturn_t hc_NVML_nvmlDeviceGetUtilizationRates (nvmlDevice_t device, nvmlUti utilization->gpu = -1; utilization->memory = -1; - //const char *string = nvmlErrorString (nvml_rc); + //const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc); //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string); } return nvml_rc; } + +//#endif diff --git a/src/oclHashcat.c b/src/oclHashcat.c index 2bdc17c..51301c8 100644 --- a/src/oclHashcat.c +++ b/src/oclHashcat.c @@ -12252,7 +12252,11 @@ int main (int argc, char **argv) if (vendor_id == VENDOR_ID_NV) { #ifdef LINUX - if (hc_NVML_nvmlInit () == NVML_SUCCESS) + HM_LIB hm_dll = hm_init (); + + data.hm_dll = hm_dll; + + if (hc_NVML_nvmlInit (hm_dll) == NVML_SUCCESS) { HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX]; @@ -12271,7 +12275,7 @@ int main (int argc, char **argv) { unsigned int speed; - if (nvmlDeviceGetFanSpeed (hm_adapter_all[i].adapter_index.nv, &speed) != NVML_ERROR_NOT_SUPPORTED) hm_adapter_all[i].fan_supported = 1; + if (hc_NVML_nvmlDeviceGetFanSpeed (hm_dll, 1, hm_adapter_all[i].adapter_index.nv, &speed) != NVML_ERROR_NOT_SUPPORTED) hm_adapter_all[i].fan_supported = 1; } } #endif @@ -12304,7 +12308,7 @@ int main (int argc, char **argv) if (vendor_id == VENDOR_ID_AMD) { - HM_LIB hm_dll = hm_init_amd (); + HM_LIB hm_dll = hm_init (); data.hm_dll = hm_dll; @@ -16048,7 +16052,7 @@ int main (int argc, char **argv) if (vendor_id == VENDOR_ID_NV) { #ifdef LINUX - hc_NVML_nvmlShutdown (); + hc_NVML_nvmlShutdown (data.hm_dll); #endif #ifdef WIN @@ -16060,8 +16064,15 @@ int main (int argc, char **argv) { hc_ADL_Main_Control_Destroy (data.hm_dll); - hm_close_amd (data.hm_dll); + hm_close (data.hm_dll); + } + + #ifdef LINUX + if (vendor_id == VENDOR_ID_NV) + { + hm_close (data.hm_dll); } + #endif } // free memory diff --git a/src/shared.c b/src/shared.c index 10b1963..5917569 100644 --- a/src/shared.c +++ b/src/shared.c @@ -2658,12 +2658,11 @@ int hm_get_adapter_index_nv (HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX]) for (uint i = 0; i < DEVICES_MAX; i++) { - // do not use wrapper function to omit warning message - if (nvmlDeviceGetHandleByIndex (i, &nvGPUHandle[i]) != NVML_SUCCESS) break; + if (hc_NVML_nvmlDeviceGetHandleByIndex (data.hm_dll, 1, i, &nvGPUHandle[i]) != NVML_SUCCESS) break; //can be used to determine if the device by index matches the cuda device by index //char name[100]; memset (name, 0, sizeof (name)); - //hc_NVML_nvmlDeviceGetName (nvGPUHandle[i], name, sizeof (name) - 1); + //hc_NVML_nvmlDeviceGetName (data.hm_dll, nvGPUHandle[i], name, sizeof (name) - 1); pGpuCount++; } @@ -2679,7 +2678,7 @@ int hm_get_adapter_index_nv (HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX]) } #endif -void hm_close_amd (HM_LIB hm_dll) +void hm_close (HM_LIB hm_dll) { #ifdef _POSIX dlclose (hm_dll); @@ -2690,17 +2689,31 @@ void hm_close_amd (HM_LIB hm_dll) #endif } -HM_LIB hm_init_amd () +HM_LIB hm_init () { - #ifdef _POSIX - HM_LIB hm_dll = dlopen ("libatiadlxx.so", RTLD_LAZY | RTLD_GLOBAL); + HM_LIB hm_dll = NULL; - #elif _WIN - HM_LIB hm_dll = LoadLibrary ("atiadlxx.dll"); + if (data.vendor_id == VENDOR_ID_AMD) + { + #ifdef _POSIX + hm_dll = dlopen ("libatiadlxx.so", RTLD_LAZY | RTLD_GLOBAL); + + #elif _WIN + hm_dll = LoadLibrary ("atiadlxx.dll"); - if (hm_dll == NULL) + if (hm_dll == NULL) + { hm_dll = LoadLibrary ("atiadlxy.dll"); + } + #endif + } + + #ifdef LINUX + if (data.vendor_id == VENDOR_ID_NV) + { + hm_dll = dlopen ("libnvidia-ml.so", RTLD_LAZY | RTLD_GLOBAL); + } #endif return hm_dll; @@ -3077,7 +3090,7 @@ int hm_get_temperature_with_device_id (const uint device_id) #ifdef LINUX int temperature = 0; - hc_NVML_nvmlDeviceGetTemperature (data.hm_device[device_id].adapter_index.nv, NVML_TEMPERATURE_GPU, (unsigned int *) &temperature); + hc_NVML_nvmlDeviceGetTemperature (data.hm_dll, data.hm_device[device_id].adapter_index.nv, NVML_TEMPERATURE_GPU, (unsigned int *) &temperature); return temperature; #endif @@ -3139,7 +3152,7 @@ int hm_get_fanspeed_with_device_id (const uint device_id) #ifdef LINUX int speed = 0; - hc_NVML_nvmlDeviceGetFanSpeed (data.hm_device[device_id].adapter_index.nv, (unsigned int *) &speed); + hc_NVML_nvmlDeviceGetFanSpeed (data.hm_dll, 1, data.hm_device[device_id].adapter_index.nv, (unsigned int *) &speed); return speed; #endif @@ -3178,7 +3191,7 @@ int hm_get_utilization_with_device_id (const uint device_id) #ifdef LINUX nvmlUtilization_t utilization; - hc_NVML_nvmlDeviceGetUtilizationRates (data.hm_device[device_id].adapter_index.nv, &utilization); + hc_NVML_nvmlDeviceGetUtilizationRates (data.hm_dll, data.hm_device[device_id].adapter_index.nv, &utilization); return utilization.gpu; #endif -- 2.43.0