From abe8933c8d549cc88016f7dcb8918234a183f89c Mon Sep 17 00:00:00 2001 From: apyfz Date: Mon, 19 Oct 2026 22:42:45 +0700 Subject: [PATCH 1/2] Reject invalid GPU power readings in overlay --- .../src/components/overlay/GpuSection.tsx | 7 +++- tauri-app/src/lib/gpu-power.test.ts | 40 +++++++++++++++++++ tauri-app/src/lib/gpu.ts | 11 +++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tauri-app/src/lib/gpu-power.test.ts 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..99eab19 --- /dev/null +++ b/tauri-app/src/lib/gpu-power.test.ts @@ -0,0 +1,40 @@ +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("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..4346d2d 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. From e82d174592f97fa8c70e731ad80090b1229a0612 Mon Sep 17 00:00:00 2001 From: apyfz Date: Tue, 20 Oct 2026 02:02:30 +0700 Subject: [PATCH 2/2] Reject GPU wattage until a GPU is selected --- tauri-app/src/lib/gpu-power.test.ts | 4 ++++ tauri-app/src/lib/gpu.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tauri-app/src/lib/gpu-power.test.ts b/tauri-app/src/lib/gpu-power.test.ts index 99eab19..832c182 100644 --- a/tauri-app/src/lib/gpu-power.test.ts +++ b/tauri-app/src/lib/gpu-power.test.ts @@ -25,6 +25,10 @@ describe("GPU wattage", () => { 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, diff --git a/tauri-app/src/lib/gpu.ts b/tauri-app/src/lib/gpu.ts index 4346d2d..9bb3fdb 100644 --- a/tauri-app/src/lib/gpu.ts +++ b/tauri-app/src/lib/gpu.ts @@ -42,7 +42,7 @@ export function sensorsOnGpu(sensors: Sensor[], gpuId: string): Sensor[] { */ 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; + if (!gpuId || sensor.hardwareIdentifier !== gpuId) return null; const watts = sensor.value; return Number.isFinite(watts) && watts >= 0 && watts <= 2000 ? watts : null; }