Skip to content

Use SysReadFile for thermals and skip failed zones #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2025
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
20 changes: 20 additions & 0 deletions internal/util/sysreadfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package util
import (
"bytes"
"os"
"strconv"
"strings"
"syscall"
)

Expand Down Expand Up @@ -48,3 +50,21 @@ func SysReadFile(file string) (string, error) {

return string(bytes.TrimSpace(b[:n])), nil
}

// SysReadUintFromFile reads a file using SysReadFile and attempts to parse a uint64 from it.
func SysReadUintFromFile(path string) (uint64, error) {
data, err := SysReadFile(path)
if err != nil {
return 0, err
}
return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
}

// SysReadIntFromFile reads a file using SysReadFile and attempts to parse a int64 from it.
func SysReadIntFromFile(path string) (int64, error) {
data, err := SysReadFile(path)
if err != nil {
return 0, err
}
return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
}
6 changes: 3 additions & 3 deletions sysfs/class_thermal.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (fs FS) ClassThermalZoneStats() ([]ClassThermalZoneStats, error) {
for _, zone := range zones {
zoneStats, err := parseClassThermalZone(zone)
if err != nil {
if errors.Is(err, syscall.ENODATA) || errors.As(err, new(*fsp.PathError)) {
if errors.Is(err, syscall.ENODATA) || errors.As(err, new(*fsp.PathError)) || errors.Is(err, syscall.EAGAIN) {
continue
}
return nil, err
Expand All @@ -72,7 +72,7 @@ func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {
if err != nil {
return ClassThermalZoneStats{}, err
}
zoneTemp, err := util.ReadIntFromFile(filepath.Join(zone, "temp"))
zoneTemp, err := util.SysReadIntFromFile(filepath.Join(zone, "temp"))
if err != nil {
return ClassThermalZoneStats{}, err
}
Expand All @@ -85,7 +85,7 @@ func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {
zoneMode := util.ParseBool(mode)

var zonePassive *uint64
passive, err := util.ReadUintFromFile(filepath.Join(zone, "passive"))
passive, err := util.SysReadUintFromFile(filepath.Join(zone, "passive"))
if os.IsNotExist(err) || os.IsPermission(err) {
zonePassive = nil
} else if err != nil {
Expand Down
Loading