Skip to content
Closed
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: 12 additions & 8 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) {
zoneStats, err := parseClassThermalZone(zone)
if err != nil {
if errors.Is(err, syscall.ENODATA) || errors.As(err, new(*fsp.PathError)) || errors.Is(err, syscall.EAGAIN) ||
errors.Is(err, syscall.EINVAL) {
errors.Is(err, syscall.EINVAL) || errors.Is(err, os.ErrProcessDone) {
continue
}
return nil, err
Expand All @@ -63,6 +63,17 @@ func (fs FS) ClassThermalZoneStats() ([]ClassThermalZoneStats, error) {
}

func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {
// Optional attributes read first.
// Check mode before reading temp: disabled zones return EINVAL on temp reads.
mode, err := util.SysReadFile(filepath.Join(zone, "mode"))
if err != nil && !os.IsNotExist(err) && !os.IsPermission(err) {
return ClassThermalZoneStats{}, err
}
if mode == "disabled" {
return ClassThermalZoneStats{}, os.ErrProcessDone
}
zoneMode := util.ParseBool(mode)

// Required attributes.
zoneType, err := util.SysReadFile(filepath.Join(zone, "type"))
if err != nil {
Expand All @@ -77,13 +88,6 @@ func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {
return ClassThermalZoneStats{}, err
}

// Optional attributes.
mode, err := util.SysReadFile(filepath.Join(zone, "mode"))
if err != nil && !os.IsNotExist(err) && !os.IsPermission(err) {
return ClassThermalZoneStats{}, err
}
zoneMode := util.ParseBool(mode)

var zonePassive *uint64
passive, err := util.SysReadUintFromFile(filepath.Join(zone, "passive"))
switch {
Expand Down
6 changes: 6 additions & 0 deletions sysfs/class_thermal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ func TestClassThermalZoneStats(t *testing.T) {
if diff := cmp.Diff(classThermalZoneStats, thermalTest); diff != "" {
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
}

// Verify that the disabled thermal_zone2 is skipped and does not appear in results.
// A disabled zone has no temp file and would previously cause the collector to fail.
if len(thermalTest) != 2 {
t.Fatalf("expected 2 thermal zones (disabled zone should be skipped), got %d", len(thermalTest))
}
}
Loading