diff --git a/examples/frequency-probe/main.go b/examples/frequency-probe/main.go new file mode 100644 index 0000000..42d5262 --- /dev/null +++ b/examples/frequency-probe/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "fmt" + "log" + + "github.com/NVIDIA/go-nvml/pkg/nvml" +) + +func main() { + // Initialize NVML + ret := nvml.Init() + if ret != nvml.SUCCESS { + log.Fatalf("Failed to initialize NVML: %v", nvml.ErrorString(ret)) + } + defer nvml.Shutdown() + + // Get the first device + device, ret := nvml.DeviceGetHandleByIndex(0) + if ret != nvml.SUCCESS { + log.Fatalf("Failed to get device handle: %v", nvml.ErrorString(ret)) + } + + // Get device name for reference + name, ret := device.GetName() + if ret != nvml.SUCCESS { + log.Printf("Warning: Failed to get device name: %v", nvml.ErrorString(ret)) + name = "Unknown GPU" + } + + fmt.Printf("GPU: %s\n", name) + fmt.Println("Supported Clock Frequencies:") + fmt.Println("===========================") + + // Get supported memory clocks + memCount, memClocks, ret := device.GetSupportedMemoryClocks() + if ret != nvml.SUCCESS { + log.Fatalf("Failed to get supported memory clocks: %v", nvml.ErrorString(ret)) + } + + fmt.Printf("Found %d supported memory clock speeds\n", memCount) + + // Iterate over each memory clock + for _, memClock := range memClocks { + fmt.Printf("\nMemory Clock: %d MHz\n", memClock) + fmt.Println("Graphics Clocks (MHz):") + + // Get supported graphics clocks for this memory clock + graphicsCount, graphicsClocks, ret := device.GetSupportedGraphicsClocks(int(memClock)) + if ret != nvml.SUCCESS { + log.Printf("Warning: Failed to get graphics clocks for memory clock %d MHz: %v", memClock, nvml.ErrorString(ret)) + continue + } + + // Print each graphics clock + for _, graphicsClock := range graphicsClocks { + fmt.Printf(" %d MHz\n", graphicsClock) + } + fmt.Printf("Total graphics clocks for this memory speed: %d\n", graphicsCount) + } + + // Get current clocks + currentGraphicsClock, ret := device.GetClockInfo(nvml.CLOCK_GRAPHICS) + if ret == nvml.SUCCESS { + fmt.Printf("\nCurrent Graphics Clock: %d MHz\n", currentGraphicsClock) + } else { + log.Printf("Warning: Failed to get current graphics clock: %v", nvml.ErrorString(ret)) + } + + currentMemClock, ret := device.GetClockInfo(nvml.CLOCK_MEM) + if ret == nvml.SUCCESS { + fmt.Printf("Current Memory Clock: %d MHz\n", currentMemClock) + } else { + log.Printf("Warning: Failed to get current memory clock: %v", nvml.ErrorString(ret)) + } +} diff --git a/pkg/nvml/device.go b/pkg/nvml/device.go index ac778e5..c0678da 100644 --- a/pkg/nvml/device.go +++ b/pkg/nvml/device.go @@ -557,24 +557,34 @@ func (device nvmlDevice) GetMaxCustomerBoostClock(clockType ClockType) (uint32, } // nvml.DeviceGetSupportedMemoryClocks() -func (l *library) DeviceGetSupportedMemoryClocks(device Device) (int, uint32, Return) { +func (l *library) DeviceGetSupportedMemoryClocks(device Device) (int, []uint32, Return) { return device.GetSupportedMemoryClocks() } -func (device nvmlDevice) GetSupportedMemoryClocks() (int, uint32, Return) { - var count, clocksMHz uint32 - ret := nvmlDeviceGetSupportedMemoryClocks(device, &count, &clocksMHz) +func (device nvmlDevice) GetSupportedMemoryClocks() (int, []uint32, Return) { + var count uint32 + ret := nvmlDeviceGetSupportedMemoryClocks(device, &count, nil) + + clocksMHz := make([]uint32, count) + + ret = nvmlDeviceGetSupportedMemoryClocks(device, &count, &clocksMHz[0]) + return int(count), clocksMHz, ret } // nvml.DeviceGetSupportedGraphicsClocks() -func (l *library) DeviceGetSupportedGraphicsClocks(device Device, memoryClockMHz int) (int, uint32, Return) { +func (l *library) DeviceGetSupportedGraphicsClocks(device Device, memoryClockMHz int) (int, []uint32, Return) { return device.GetSupportedGraphicsClocks(memoryClockMHz) } -func (device nvmlDevice) GetSupportedGraphicsClocks(memoryClockMHz int) (int, uint32, Return) { - var count, clocksMHz uint32 - ret := nvmlDeviceGetSupportedGraphicsClocks(device, uint32(memoryClockMHz), &count, &clocksMHz) +func (device nvmlDevice) GetSupportedGraphicsClocks(memoryClockMHz int) (int, []uint32, Return) { + var count uint32 + ret := nvmlDeviceGetSupportedGraphicsClocks(device, uint32(memoryClockMHz), &count, nil) + + clocksMHz := make([]uint32, count) + + ret = nvmlDeviceGetSupportedGraphicsClocks(device, uint32(memoryClockMHz), &count, &clocksMHz[0]) + return int(count), clocksMHz, ret } diff --git a/pkg/nvml/zz_generated.api.go b/pkg/nvml/zz_generated.api.go index c1ecb2d..6d2c1bb 100644 --- a/pkg/nvml/zz_generated.api.go +++ b/pkg/nvml/zz_generated.api.go @@ -508,8 +508,8 @@ type Interface interface { DeviceGetSupportedClocksEventReasons(Device) (uint64, Return) DeviceGetSupportedClocksThrottleReasons(Device) (uint64, Return) DeviceGetSupportedEventTypes(Device) (uint64, Return) - DeviceGetSupportedGraphicsClocks(Device, int) (int, uint32, Return) - DeviceGetSupportedMemoryClocks(Device) (int, uint32, Return) + DeviceGetSupportedGraphicsClocks(Device, int) (int, []uint32, Return) + DeviceGetSupportedMemoryClocks(Device) (int, []uint32, Return) DeviceGetSupportedPerformanceStates(Device) ([]Pstates, Return) DeviceGetSupportedVgpus(Device) ([]VgpuTypeId, Return) DeviceGetTargetFanSpeed(Device, int) (int, Return) @@ -827,8 +827,8 @@ type Device interface { GetSupportedClocksEventReasons() (uint64, Return) GetSupportedClocksThrottleReasons() (uint64, Return) GetSupportedEventTypes() (uint64, Return) - GetSupportedGraphicsClocks(int) (int, uint32, Return) - GetSupportedMemoryClocks() (int, uint32, Return) + GetSupportedGraphicsClocks(int) (int, []uint32, Return) + GetSupportedMemoryClocks() (int, []uint32, Return) GetSupportedPerformanceStates() ([]Pstates, Return) GetSupportedVgpus() ([]VgpuTypeId, Return) GetTargetFanSpeed(int) (int, Return)