...

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

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2019 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  	"path/filepath"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/prometheus/procfs/internal/util"
    25  )
    26  
    27  // ClassCoolingDeviceStats contains info from files in /sys/class/thermal/cooling_device[0-9]*
    28  // for a single device.
    29  // https://www.kernel.org/doc/Documentation/thermal/sysfs-api.txt
    30  type ClassCoolingDeviceStats struct {
    31  	Name     string // The name of the cooling device.
    32  	Type     string // Type of the cooling device(processor/fan/...)
    33  	MaxState int64  // Maximum cooling state of the cooling device
    34  	CurState int64  // Current cooling state of the cooling device
    35  }
    36  
    37  func (fs FS) ClassCoolingDeviceStats() ([]ClassCoolingDeviceStats, error) {
    38  	cds, err := filepath.Glob(fs.sys.Path("class/thermal/cooling_device[0-9]*"))
    39  	if err != nil {
    40  		return []ClassCoolingDeviceStats{}, err
    41  	}
    42  
    43  	var coolingDeviceStats = ClassCoolingDeviceStats{}
    44  	stats := make([]ClassCoolingDeviceStats, len(cds))
    45  	for i, cd := range cds {
    46  		cdName := strings.TrimPrefix(filepath.Base(cd), "cooling_device")
    47  
    48  		coolingDeviceStats, err = parseCoolingDeviceStats(cd)
    49  		if err != nil {
    50  			return []ClassCoolingDeviceStats{}, err
    51  		}
    52  
    53  		coolingDeviceStats.Name = cdName
    54  		stats[i] = coolingDeviceStats
    55  	}
    56  	return stats, nil
    57  }
    58  
    59  func parseCoolingDeviceStats(cd string) (ClassCoolingDeviceStats, error) {
    60  	cdType, err := util.SysReadFile(filepath.Join(cd, "type"))
    61  	if err != nil {
    62  		return ClassCoolingDeviceStats{}, err
    63  	}
    64  
    65  	cdMaxStateString, err := util.SysReadFile(filepath.Join(cd, "max_state"))
    66  	if err != nil {
    67  		return ClassCoolingDeviceStats{}, err
    68  	}
    69  	cdMaxStateInt, err := strconv.ParseInt(cdMaxStateString, 10, 64)
    70  	if err != nil {
    71  		return ClassCoolingDeviceStats{}, err
    72  	}
    73  
    74  	// cur_state can be -1, eg intel powerclamp
    75  	// https://www.kernel.org/doc/Documentation/thermal/intel_powerclamp.txt
    76  	cdCurStateString, err := util.SysReadFile(filepath.Join(cd, "cur_state"))
    77  	if err != nil {
    78  		return ClassCoolingDeviceStats{}, err
    79  	}
    80  
    81  	cdCurStateInt, err := strconv.ParseInt(cdCurStateString, 10, 64)
    82  	if err != nil {
    83  		return ClassCoolingDeviceStats{}, err
    84  	}
    85  
    86  	return ClassCoolingDeviceStats{
    87  		Type:     cdType,
    88  		MaxState: cdMaxStateInt,
    89  		CurState: cdCurStateInt,
    90  	}, nil
    91  }
    92  

View as plain text