Skip to content

feat: add cpu temperature readings for other cpu architectures and OS's - #14

Merged
rishibaghel25 merged 1 commit into
bytestrix:mainfrom
Ryankhlifi:cpu-temperature
Oct 17, 2026
Merged

rishibaghel25 merged 1 commit into
bytestrix:mainfrom
Ryankhlifi:cpu-temperature

Conversation

@Ryankhlifi

Copy link
Copy Markdown
Contributor

Summary

cpuTempC now reads sensors through gopsutil and pins one sensor for the
lifetime of the process, so successive readings always come from the same
source.

Fixes #6

Why

gopsutil doesn't guarantee the order of the sensors it returns, so picking
temps[0] (or the first match) can return a different sensor from one call to
the next. Package and per-core temperatures can differ by several degrees, so
the displayed value could jump without any real change in temperature.

Changes

  • Sensors are matched against cpuSensorPrefixes in preference order, so
    package-level sensors win over per-core ones. The first successful call pins
    that sensor's full SensorKey.
  • Later calls read only the pinned sensor. If it is missing or reads 0, the
    function returns (0, false) rather than falling back to another sensor.
  • The pinned key is guarded by a mutex, so concurrent callers are safe.
  • Replaces the direct /sys/class/thermal read with
    github.com/shirou/gopsutil/v4/sensors (pure Go, no cgo). go.mod and
    go.sum are updated.
  • Doc comments rewritten to describe the above.

Behavior to be aware of

  • (0, false) is returned on any error from SensorsTemperatures (including
    warnings about individual unreadable sensors) and when no sensor matches.
    This is the normal result on VMs, containers, WSL, and Windows.
  • A pinned sensor that disappears for good (for example, a driver reload that
    renames it) means no temperature until restart. This is deliberately strict;
    I can add a reset after N consecutive misses if you'd prefer.
  • On Windows, gopsutil exposes only an ACPI thermal zone, and only when
    elevated. It isn't guaranteed to be the CPU, so I left it out of
    cpuSensorPrefixes. The function reports a real CPU measurement or says it
    can't.

Testing

  • Linux (fake sysfs): no hardware with sensors was available, so I used a
    fake sysfs tree via HOST_SYS containing coretemp_package_id_0 and
    coretemp_core_0. The package sensor was selected and its reading
    displayed.
  • WSL: no sensors exposed, so (0, false).
  • Windows: without elevation, gopsutil returns "Access denied". Elevated,
    it exposes one ACPI thermal zone (ACPI\ThermalZone\TZ00_0) that doesn't
    reliably map to the CPU, so cpuTempC returns (0, false) and the UI shows
    n/a.
  • go vet ./... and go test ./... pass on Linux/WSL. On Windows,
    TestKeyFilesAreNotWorldReadable and TestStateFileIsPrivateAndValid fail
    because Go reports 0666/0777 there regardless of real permissions. They
    are unrelated to this change, and I left them alone to keep the PR focused.
  • No new unit tests are included. I'm happy to add tests for the sensor
    pinning if you'd like them.
  • A check on physical Linux hardware would be welcome.

@rishibaghel25
rishibaghel25 merged commit 71de2ce into bytestrix:main Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CPU temperature on macOS and Windows

3 participants