diff --git a/tauri-app/src/components/overlay/GpuSection.tsx b/tauri-app/src/components/overlay/GpuSection.tsx index c7f5d44..868e475 100644 --- a/tauri-app/src/components/overlay/GpuSection.tsx +++ b/tauri-app/src/components/overlay/GpuSection.tsx @@ -4,6 +4,7 @@ import { ProgressBar } from "./ProgressBar"; import { useSettingsStore } from "@/stores/settings-store"; import { SensorType } from "@/lib/types"; import type { Sensor } from "@/lib/types"; +import { readGpuPower } from "@/lib/gpu"; import { findSensorById, formatClockLabel, formatValue, formatTemperature, sensorDisplayName } from "@/lib/utils"; interface GpuSectionProps { @@ -39,7 +40,9 @@ export function GpuSection({ isHorizontal }: GpuSectionProps) { const gpuTempVal = findSensorById(sensors, gpuTemp.customReadingId)?.value ?? 0; const gpuUsageVal = findSensorById(sensors, gpuUsage.customReadingId)?.value ?? 0; - const gpuPowerVal = findSensorById(sensors, gpuConsumption.customReadingId)?.value ?? 0; + const gpuPowerVal = readGpuPower( + findSensorById(sensors, gpuConsumption.customReadingId), settings.selectedGpuId, + ); // LibreHardwareMonitor's "GPU Memory *" readings are SmallData (MB); a // Data-typed sensor the user picked manually is already in GB. @@ -147,7 +150,7 @@ export function GpuSection({ isHorizontal }: GpuSectionProps) { {gpuConsumption.isEnabled && (
- {formatValue(gpuPowerVal)} + {gpuPowerVal === null ? "N/A" : formatValue(gpuPowerVal)} W
diff --git a/tauri-app/src/lib/gpu-power.test.ts b/tauri-app/src/lib/gpu-power.test.ts new file mode 100644 index 0000000..832c182 --- /dev/null +++ b/tauri-app/src/lib/gpu-power.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it, vi } from "vitest"; +import { renderToStaticMarkup } from "react-dom/server"; +import { createElement } from "react"; +import { readGpuPower } from "./gpu"; +import { SensorType, DEFAULT_SETTINGS } from "./types"; +import { GpuSection } from "../components/overlay/GpuSection"; +import type { HardwareMonitorData, OverlaySettings } from "./types"; + +const fixture = vi.hoisted(() => ({ state: {} as { settings: OverlaySettings; sensorData: HardwareMonitorData } })); +vi.mock("@/stores/settings-store", () => ({ + useSettingsStore: (selector: (state: typeof fixture.state) => unknown) => selector(fixture.state), +})); + +const power = { name: "GPU Package", identifier: "/gpu-nvidia/0/power/0", hardwareIdentifier: "/gpu-nvidia/0", sensorType: SensorType.Power, value: 65413 }; + +describe("GPU wattage", () => { + it.each([65413, -123, NaN, Infinity, -Infinity])("rejects invalid sample %s", value => { + expect(readGpuPower({ ...power, value }, power.hardwareIdentifier)).toBeNull(); + }); + it.each([0, 65.413, 600, 1200])("preserves valid watts %s without rescaling", value => { + expect(readGpuPower({ ...power, value }, power.hardwareIdentifier)).toBe(value); + }); + it("rejects missing, non-power and other-GPU readings", () => { + expect(readGpuPower(undefined, power.hardwareIdentifier)).toBeNull(); + expect(readGpuPower({ ...power, value: 65, sensorType: SensorType.Load }, power.hardwareIdentifier)).toBeNull(); + expect(readGpuPower({ ...power, value: 65 }, "/gpu-amd/0")).toBeNull(); + }); + it("rejects power readings until a GPU is selected", () => { + expect(readGpuPower({ ...power, value: 65 }, "")).toBeNull(); + expect(readGpuPower({ ...power, value: 65, hardwareIdentifier: "/cpu/0" }, "")).toBeNull(); + }); + it("renders N/A for the reported sample and recovers on the next valid sample", () => { + fixture.state = { settings: { + ...DEFAULT_SETTINGS, selectedGpuId: power.hardwareIdentifier, + sensors: { ...DEFAULT_SETTINGS.sensors, gpuConsumption: { ...DEFAULT_SETTINGS.sensors.gpuConsumption, isEnabled: true, customReadingId: power.identifier } }, + }, sensorData: { hardwares: [], sensors: [power], lastPollTime: 0 } }; + const render = () => renderToStaticMarkup(createElement(GpuSection, { isHorizontal: true })); + expect(render()).toContain("N/A"); + expect(render()).not.toContain("65413"); + fixture.state.sensorData = { hardwares: [], sensors: [{ ...power, value: 65 }], lastPollTime: 0 }; + expect(render()).not.toContain("N/A"); + expect(render()).toContain(">65<"); + }); +}); diff --git a/tauri-app/src/lib/gpu.ts b/tauri-app/src/lib/gpu.ts index e5ffd4d..9bb3fdb 100644 --- a/tauri-app/src/lib/gpu.ts +++ b/tauri-app/src/lib/gpu.ts @@ -36,6 +36,17 @@ export function sensorsOnGpu(sensors: Sensor[], gpuId: string): Sensor[] { return sensors.filter((s) => s.hardwareIdentifier === gpuId); } +/** Power sensors arrive in watts. Never guess a unit conversion for a corrupt + * driver sample (the reported laptop showed 65,413 W), or substitute another + * GPU's reading. The generous 2 kW ceiling is a corruption guard, not a TDP cap. + */ +export function readGpuPower(sensor: Sensor | undefined, gpuId: string): number | null { + if (!sensor || sensor.sensorType !== SensorType.Power) return null; + if (!gpuId || sensor.hardwareIdentifier !== gpuId) return null; + const watts = sensor.value; + return Number.isFinite(watts) && watts >= 0 && watts <= 2000 ? watts : null; +} + /** * Dedicated video memory a GPU reports, in whatever unit the sensor uses. * Only ever compared against another GPU's, so the unit does not matter.