nvidia-oc/backend/test_controller.py
2026-01-14 12:30:45 -08:00

77 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Test the ClockController.get_clock_ranges() method."""
import sys
import pynvml
from dataclasses import dataclass
from nvidia_oc.core.clock import ClockController
@dataclass
class MockDevice:
"""Mock device for testing."""
handle: any
index: int
def test_controller():
"""Test ClockController.get_clock_ranges() with fallback logic."""
try:
pynvml.nvmlInit()
print("✓ NVML initialized\n")
controller = ClockController()
device_count = pynvml.nvmlDeviceGetCount()
print(f"Found {device_count} GPU(s)\n")
for i in range(device_count):
print(f"=== GPU {i} ===")
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
name = pynvml.nvmlDeviceGetName(handle)
if isinstance(name, bytes):
name = name.decode('utf-8')
device = MockDevice(handle=handle, index=i)
# Test get_clock_ranges with fallback
ranges = controller.get_clock_ranges(device)
current_clock = pynvml.nvmlDeviceGetClockInfo(
handle, pynvml.NVML_CLOCK_GRAPHICS
)
print(f"Name: {name}")
print(f"✓ Base core clock: {ranges['base_core_clock']} MHz")
print(f"✓ Max core clock: {ranges['max_core_clock']} MHz")
print(f"✓ Boost enabled: {ranges['boost_enabled']}")
print(f" Current clock: {current_clock} MHz")
# Analyze boost status
base = ranges['base_core_clock']
max_boost = ranges['max_core_clock']
if base < current_clock <= max_boost:
# Clock is in the boost range (between base and max)
percent = ((current_clock - base) / (max_boost - base)) * 100
print(f" Status: BOOSTED ({percent:.1f}% into boost range)")
elif current_clock <= base:
print(f" Status: AT OR BELOW BASE (not boosted)")
else:
# Clock exceeds boost maximum (overclocked beyond spec)
print(f" Status: OVER-BOOSTED (exceeds max boost by {current_clock - max_boost} MHz)")
print()
pynvml.nvmlShutdown()
print("✓ All tests passed!")
return 0
except Exception as e:
print(f"\n✗ Error: {e}", file=sys.stderr)
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(test_controller())