From adc506d2b32cbea5cd5845489068ecbc15dea0f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 20 Dec 2023 19:09:18 +0800 Subject: [PATCH] GPU: fix clock frequency detection and overall tidy --- src/detection/cpu/cpu.h | 4 ++-- src/detection/gpu/gpu.h | 4 ++-- src/detection/gpu/gpu_driver_specific.h | 2 +- src/detection/gpu/gpu_intel.c | 14 ++++++++------ src/detection/gpu/gpu_linux.c | 4 ++-- src/detection/gpu/gpu_nvidia.c | 6 +++++- src/detection/gpu/gpu_windows.c | 5 +++-- src/detection/gpu/gpu_wsl.cpp | 4 ++-- src/modules/gpu/gpu.c | 8 ++++---- 9 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/detection/cpu/cpu.h b/src/detection/cpu/cpu.h index 9ae364808..9586e686e 100644 --- a/src/detection/cpu/cpu.h +++ b/src/detection/cpu/cpu.h @@ -13,8 +13,8 @@ typedef struct FFCPUResult uint16_t coresLogical; uint16_t coresOnline; - double frequencyMin; - double frequencyMax; + double frequencyMin; // GHz + double frequencyMax; // GHz double temperature; } FFCPUResult; diff --git a/src/detection/gpu/gpu.h b/src/detection/gpu/gpu.h index c8efcd66c..23ebcc7f2 100644 --- a/src/detection/gpu/gpu.h +++ b/src/detection/gpu/gpu.h @@ -5,7 +5,7 @@ #define FF_GPU_TEMP_UNSET (0/0.0) #define FF_GPU_CORE_COUNT_UNSET -1 #define FF_GPU_VMEM_SIZE_UNSET ((uint64_t)-1) -#define FF_GPU_FREQUENCY_UNSET -1u +#define FF_GPU_FREQUENCY_UNSET (0/0.0) extern const char* FF_GPU_VENDOR_NAME_APPLE; extern const char* FF_GPU_VENDOR_NAME_AMD; @@ -31,7 +31,7 @@ typedef struct FFGPUResult FFstrbuf driver; double temperature; int32_t coreCount; - uint32_t frequency; + double frequency; // Real time clock frequency in GHz FFGPUMemory dedicated; FFGPUMemory shared; uint64_t deviceId; // Used internally, may be uninitialized diff --git a/src/detection/gpu/gpu_driver_specific.h b/src/detection/gpu/gpu_driver_specific.h index 3049958ba..9205c0954 100644 --- a/src/detection/gpu/gpu_driver_specific.h +++ b/src/detection/gpu/gpu_driver_specific.h @@ -41,7 +41,7 @@ typedef struct FFGpuDriverResult FFGPUMemory* memory; uint32_t* coreCount; FFGPUType* type; - uint32_t* frequency; + double* frequency; } FFGpuDriverResult; const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResult result, const char* soName); diff --git a/src/detection/gpu/gpu_intel.c b/src/detection/gpu/gpu_intel.c index 6b131c557..4b67c6eb3 100644 --- a/src/detection/gpu/gpu_intel.c +++ b/src/detection/gpu/gpu_intel.c @@ -2,6 +2,7 @@ #include "3rdparty/igcl/igcl_api.h" #include "common/library.h" +#include "util/mallocHelper.h" struct FFIgclData { FF_LIBRARY_SYMBOL(ctlClose) @@ -62,9 +63,10 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe uint32_t deviceCount = 0; if (igclData.ffctlEnumerateDevices(igclData.apiHandle, &deviceCount, NULL)) return "ctlEnumerateDevices(NULL) failed"; - ctl_device_adapter_handle_t devices[16] = {}; - if (deviceCount == 0 || deviceCount > sizeof(devices) / sizeof(devices[0])) - return "Invalid device count"; + if (deviceCount == 0) + return "No Intel graphics adapter found"; + + FF_AUTO_FREE ctl_device_adapter_handle_t* devices = malloc(deviceCount * sizeof(*devices)); if (igclData.ffctlEnumerateDevices(igclData.apiHandle, &deviceCount, devices)) return "ctlEnumerateDevices(devices) failed"; @@ -127,7 +129,7 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe { ctl_mem_handle_t memoryModules[16]; uint32_t memoryCount = sizeof(memoryModules) / sizeof(memoryModules[0]); - if (igclData.ffctlEnumMemoryModules(device, &memoryCount, memoryModules) == CTL_RESULT_SUCCESS) + if (igclData.ffctlEnumMemoryModules(device, &memoryCount, memoryModules) == CTL_RESULT_SUCCESS && memoryCount > 0) { result.memory->used = 0; result.memory->total = 0; @@ -156,7 +158,7 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe { ctl_temp_handle_t sensors[16]; uint32_t sensorCount = sizeof(sensors) / sizeof(sensors[0]); - if (igclData.ffctlEnumTemperatureSensors(device, &sensorCount, sensors) == CTL_RESULT_SUCCESS) + if (igclData.ffctlEnumTemperatureSensors(device, &sensorCount, sensors) == CTL_RESULT_SUCCESS && sensorCount > 0) { double sumValue = 0; uint32_t availableCount = 0; @@ -174,7 +176,7 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe } if (result.frequency) - *result.frequency = properties.Frequency; + *result.frequency = properties.Frequency / 1000.; return NULL; } diff --git a/src/detection/gpu/gpu_linux.c b/src/detection/gpu/gpu_linux.c index 630f12ead..c21465f0e 100644 --- a/src/detection/gpu/gpu_linux.c +++ b/src/detection/gpu/gpu_linux.c @@ -255,8 +255,8 @@ static void pciHandleDevice(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* .temp = options->temp ? &gpu->temperature : NULL, .memory = options->driverSpecific ? &gpu->dedicated : NULL, .coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL, - .type = options->driverSpecific ? &gpu->type : NULL, - .frequency = options->driverSpecific ? &gpu->frequency : NULL, + .type = &gpu->type, + .frequency = &gpu->frequency, }, "libnvidia-ml.so"); if (gpu->dedicated.total != FF_GPU_VMEM_SIZE_UNSET) diff --git a/src/detection/gpu/gpu_nvidia.c b/src/detection/gpu/gpu_nvidia.c index 1bbddd2c7..717d6389b 100644 --- a/src/detection/gpu/gpu_nvidia.c +++ b/src/detection/gpu/gpu_nvidia.c @@ -100,7 +100,11 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR nvmlData.ffnvmlDeviceGetNumGpuCores(device, result.coreCount); if (result.frequency) - nvmlData.ffnvmlDeviceGetClockInfo(device, NVML_CLOCK_GRAPHICS, result.frequency); + { + uint32_t clockMHz; + if (nvmlData.ffnvmlDeviceGetClockInfo(device, NVML_CLOCK_GRAPHICS, &clockMHz) == NVML_SUCCESS) + *result.frequency = clockMHz / 1000.; + } return NULL; } diff --git a/src/detection/gpu/gpu_windows.c b/src/detection/gpu/gpu_windows.c index 3d27a7ba3..fd0ba1046 100644 --- a/src/detection/gpu/gpu_windows.c +++ b/src/detection/gpu/gpu_windows.c @@ -106,12 +106,13 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* .subSystemId = subSystemId, .revId = revId, }, + .luid = gpu->deviceId, }, (FFGpuDriverResult) { .temp = options->temp ? &gpu->temperature : NULL, .memory = options->driverSpecific ? &gpu->dedicated : NULL, .coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL, - .type = options->driverSpecific ? &gpu->type : NULL, - .frequency = options->driverSpecific ? &gpu->frequency : NULL, + .type = &gpu->type, + .frequency = &gpu->frequency, }, gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA ? "nvml.dll" : #ifdef _WIN64 "ControlLib.dll" diff --git a/src/detection/gpu/gpu_wsl.cpp b/src/detection/gpu/gpu_wsl.cpp index 117eb2cee..1fe09fb1a 100644 --- a/src/detection/gpu/gpu_wsl.cpp +++ b/src/detection/gpu/gpu_wsl.cpp @@ -113,8 +113,8 @@ const char* ffGPUDetectByDirectX(FF_MAYBE_UNUSED const FFGPUOptions* options, FF .temp = options->temp ? &gpu->temperature : NULL, .memory = options->driverSpecific ? &gpu->dedicated : NULL, .coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL, - .type = options->driverSpecific ? &gpu->type : NULL, - .frequency = options->driverSpecific ? &gpu->frequency : NULL, + .type = &gpu->type, + .frequency = &gpu->frequency, }, "/usr/lib/wsl/lib/libnvidia-ml.so"); } } diff --git a/src/modules/gpu/gpu.c b/src/modules/gpu/gpu.c index a159764ef..a3360ad96 100644 --- a/src/modules/gpu/gpu.c +++ b/src/modules/gpu/gpu.c @@ -38,8 +38,8 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu if(gpu->coreCount != FF_GPU_CORE_COUNT_UNSET) ffStrbufAppendF(&output, " (%d)", gpu->coreCount); - if(gpu->frequency != FF_GPU_FREQUENCY_UNSET) - ffStrbufAppendF(&output, " @ %.2f MHz", gpu->frequency / 1e6); + if(gpu->frequency == gpu->frequency && gpu->frequency > 0 /* Inactive? */) + ffStrbufAppendF(&output, " @ %.2f GHz", gpu->frequency); if(gpu->temperature == gpu->temperature) //FF_GPU_TEMP_UNSET { @@ -83,7 +83,7 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu {FF_FORMAT_ARG_TYPE_UINT64, &gpu->dedicated.used}, {FF_FORMAT_ARG_TYPE_UINT64, &gpu->shared.total}, {FF_FORMAT_ARG_TYPE_UINT64, &gpu->shared.used}, - {FF_FORMAT_ARG_TYPE_UINT, &gpu->frequency}, + {FF_FORMAT_ARG_TYPE_DOUBLE, &gpu->frequency}, }); } } @@ -312,7 +312,7 @@ void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_ if (gpu->frequency == FF_GPU_FREQUENCY_UNSET) yyjson_mut_obj_add_null(doc, obj, "frequency"); else - yyjson_mut_obj_add_uint(doc, obj, "frequency", gpu->frequency); + yyjson_mut_obj_add_real(doc, obj, "frequency", gpu->frequency); } FF_LIST_FOR_EACH(FFGPUResult, gpu, gpus)