diff --git a/src/extract_gpuinfo_nvidia.c b/src/extract_gpuinfo_nvidia.c index 33670a60..d6d08078 100644 --- a/src/extract_gpuinfo_nvidia.c +++ b/src/extract_gpuinfo_nvidia.c @@ -310,11 +310,9 @@ __attribute__((constructor)) static void init_extract_gpuinfo_nvidia(void) { reg * function gpuinfo_nvidia_last_error_string. * */ -static bool gpuinfo_nvidia_init(void) { +static bool gpuinfo_nvidia_init_with_lib(const char *libname) { - libnvidia_ml_handle = dlopen("libnvidia-ml.so", RTLD_LAZY); - if (!libnvidia_ml_handle) - libnvidia_ml_handle = dlopen("libnvidia-ml.so.1", RTLD_LAZY); + libnvidia_ml_handle = dlopen(libname, RTLD_LAZY); if (!libnvidia_ml_handle) { local_error_string = dlerror(); return false; @@ -472,7 +470,8 @@ static bool gpuinfo_nvidia_init(void) { last_nvml_return_status = nvmlInit(); if (last_nvml_return_status != NVML_SUCCESS) { - return false; + local_error_string = nvmlErrorString(last_nvml_return_status); + goto init_error_clean_exit; } local_error_string = NULL; @@ -484,6 +483,26 @@ static bool gpuinfo_nvidia_init(void) { return false; } +/* + * + * Try the NVML libraries in turn. The SONAME (libnvidia-ml.so.1) comes first + * because it is the name the runtime driver always installs, while the + * unversioned libnvidia-ml.so is a development symlink that may point at a + * library unusable on this system (e.g. the native driver package installed + * inside WSL, where only the WSL-provided libnvidia-ml.so.1 can talk to the + * host GPU). + * + */ +static bool gpuinfo_nvidia_init(void) { + static const char *const nvml_libs[] = {"libnvidia-ml.so.1", "libnvidia-ml.so"}; + + for (size_t i = 0; i < sizeof(nvml_libs) / sizeof(*nvml_libs); ++i) { + if (gpuinfo_nvidia_init_with_lib(nvml_libs[i])) + return true; + } + return false; +} + static void gpuinfo_nvidia_shutdown(void) { if (libnvidia_ml_handle) { nvmlShutdown(); diff --git a/src/extract_gpuinfo_v3d.c b/src/extract_gpuinfo_v3d.c index c92757c0..cad256da 100644 --- a/src/extract_gpuinfo_v3d.c +++ b/src/extract_gpuinfo_v3d.c @@ -195,8 +195,7 @@ static void add_v3d_cards(struct nvtop_device *dev, const char *devname, struct return; const char *driver; - nvtop_device_get_driver(parent, &driver); - if (strcmp(driver, "v3d")) + if (nvtop_device_get_driver(parent, &driver) < 0 || strcmp(driver, "v3d")) return; struct gpu_info_v3d *thisGPU = &gpu_infos[v3d_gpu_count++];