Skip to content

Commit f61a330

Browse files
authored
Use SysReadFile for thermals and skip failed zones (#712)
Switches all calls in the thermal zones processing to use SysReadFile so that unavailable zones won't stall forever waiting for them to become available. Also continues processing in the case that one zone fails with EAGAIN, as is the case for temporarily unavailable zones. Addresses #698 Signed-off-by: Sean Swehla <[email protected]>
1 parent 67f32ef commit f61a330

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

internal/util/sysreadfile.go

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package util
2020
import (
2121
"bytes"
2222
"os"
23+
"strconv"
24+
"strings"
2325
"syscall"
2426
)
2527

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

4951
return string(bytes.TrimSpace(b[:n])), nil
5052
}
53+
54+
// SysReadUintFromFile reads a file using SysReadFile and attempts to parse a uint64 from it.
55+
func SysReadUintFromFile(path string) (uint64, error) {
56+
data, err := SysReadFile(path)
57+
if err != nil {
58+
return 0, err
59+
}
60+
return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
61+
}
62+
63+
// SysReadIntFromFile reads a file using SysReadFile and attempts to parse a int64 from it.
64+
func SysReadIntFromFile(path string) (int64, error) {
65+
data, err := SysReadFile(path)
66+
if err != nil {
67+
return 0, err
68+
}
69+
return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
70+
}

sysfs/class_thermal.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (fs FS) ClassThermalZoneStats() ([]ClassThermalZoneStats, error) {
5151
for _, zone := range zones {
5252
zoneStats, err := parseClassThermalZone(zone)
5353
if err != nil {
54-
if errors.Is(err, syscall.ENODATA) || errors.As(err, new(*fsp.PathError)) {
54+
if errors.Is(err, syscall.ENODATA) || errors.As(err, new(*fsp.PathError)) || errors.Is(err, syscall.EAGAIN) {
5555
continue
5656
}
5757
return nil, err
@@ -72,7 +72,7 @@ func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {
7272
if err != nil {
7373
return ClassThermalZoneStats{}, err
7474
}
75-
zoneTemp, err := util.ReadIntFromFile(filepath.Join(zone, "temp"))
75+
zoneTemp, err := util.SysReadIntFromFile(filepath.Join(zone, "temp"))
7676
if err != nil {
7777
return ClassThermalZoneStats{}, err
7878
}
@@ -85,7 +85,7 @@ func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {
8585
zoneMode := util.ParseBool(mode)
8686

8787
var zonePassive *uint64
88-
passive, err := util.ReadUintFromFile(filepath.Join(zone, "passive"))
88+
passive, err := util.SysReadUintFromFile(filepath.Join(zone, "passive"))
8989
if os.IsNotExist(err) || os.IsPermission(err) {
9090
zonePassive = nil
9191
} else if err != nil {

0 commit comments

Comments
 (0)