...

Source file src/github.com/prometheus/procfs/sysfs/class_thermal.go

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2018 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  //go:build linux
    15  // +build linux
    16  
    17  package sysfs
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"syscall"
    25  
    26  	"github.com/prometheus/procfs/internal/util"
    27  )
    28  
    29  // ClassThermalZoneStats contains info from files in /sys/class/thermal/thermal_zone<zone>
    30  // for a single <zone>.
    31  // https://www.kernel.org/doc/Documentation/thermal/sysfs-api.txt
    32  type ClassThermalZoneStats struct {
    33  	Name    string  // The name of the zone from the directory structure.
    34  	Type    string  // The type of thermal zone.
    35  	Temp    int64   // Temperature in millidegree Celsius.
    36  	Policy  string  // One of the various thermal governors used for a particular zone.
    37  	Mode    *bool   // Optional: One of the predefined values in [enabled, disabled].
    38  	Passive *uint64 // Optional: millidegrees Celsius. (0 for disabled, > 1000 for enabled+value)
    39  }
    40  
    41  // ClassThermalZoneStats returns Thermal Zone metrics for all zones.
    42  func (fs FS) ClassThermalZoneStats() ([]ClassThermalZoneStats, error) {
    43  	zones, err := filepath.Glob(fs.sys.Path("class/thermal/thermal_zone[0-9]*"))
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	stats := make([]ClassThermalZoneStats, 0, len(zones))
    49  	for _, zone := range zones {
    50  		zoneStats, err := parseClassThermalZone(zone)
    51  		if err != nil {
    52  			if errors.Is(err, syscall.ENODATA) {
    53  				continue
    54  			}
    55  			return nil, err
    56  		}
    57  		zoneStats.Name = strings.TrimPrefix(filepath.Base(zone), "thermal_zone")
    58  		stats = append(stats, zoneStats)
    59  	}
    60  	return stats, nil
    61  }
    62  
    63  func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {
    64  	// Required attributes.
    65  	zoneType, err := util.SysReadFile(filepath.Join(zone, "type"))
    66  	if err != nil {
    67  		return ClassThermalZoneStats{}, err
    68  	}
    69  	zonePolicy, err := util.SysReadFile(filepath.Join(zone, "policy"))
    70  	if err != nil {
    71  		return ClassThermalZoneStats{}, err
    72  	}
    73  	zoneTemp, err := util.ReadIntFromFile(filepath.Join(zone, "temp"))
    74  	if err != nil {
    75  		return ClassThermalZoneStats{}, err
    76  	}
    77  
    78  	// Optional attributes.
    79  	mode, err := util.SysReadFile(filepath.Join(zone, "mode"))
    80  	if err != nil && !os.IsNotExist(err) && !os.IsPermission(err) {
    81  		return ClassThermalZoneStats{}, err
    82  	}
    83  	zoneMode := util.ParseBool(mode)
    84  
    85  	var zonePassive *uint64
    86  	passive, err := util.ReadUintFromFile(filepath.Join(zone, "passive"))
    87  	if os.IsNotExist(err) || os.IsPermission(err) {
    88  		zonePassive = nil
    89  	} else if err != nil {
    90  		return ClassThermalZoneStats{}, err
    91  	} else {
    92  		zonePassive = &passive
    93  	}
    94  
    95  	return ClassThermalZoneStats{
    96  		Type:    zoneType,
    97  		Policy:  zonePolicy,
    98  		Temp:    zoneTemp,
    99  		Mode:    zoneMode,
   100  		Passive: zonePassive,
   101  	}, nil
   102  }
   103  

View as plain text