Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tauri-app/src/components/overlay/GpuSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -147,7 +150,7 @@ export function GpuSection({ isHorizontal }: GpuSectionProps) {
{gpuConsumption.isEnabled && (
<div className="flex items-center gap-1">
<span style={{ fontSize: valueFontSize, fontWeight: valueFontWeight, color: "var(--overlay-text)", fontFamily: "Inter", letterSpacing: "-0.02em" }} className="tabular-nums">
{formatValue(gpuPowerVal)}
{gpuPowerVal === null ? "N/A" : formatValue(gpuPowerVal)}
</span>
<span style={{ fontSize: labelFontSize, fontWeight: labelFontWeight, color: "var(--overlay-text)", fontFamily: "Inter", letterSpacing: "0.04em" }}>W</span>
</div>
Expand Down
44 changes: 44 additions & 0 deletions tauri-app/src/lib/gpu-power.test.ts
Original file line number Diff line number Diff line change
@@ -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<");
});
});
11 changes: 11 additions & 0 deletions tauri-app/src/lib/gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading