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 "fmt" 21 "os" 22 "path/filepath" 23 "strconv" 24 "strings" 25 26 "github.com/prometheus/procfs/internal/util" 27 ) 28 29 // RaplZone stores the information for one RAPL power zone. 30 type RaplZone struct { 31 Name string // name of RAPL zone from file "name" 32 Index int // index (different value for duplicate names) 33 Path string // filesystem path of RaplZone 34 MaxMicrojoules uint64 // max RAPL microjoule value 35 } 36 37 // GetRaplZones returns a slice of RaplZones. When RAPL files are not present, 38 // returns nil with error. 39 // - https://www.kernel.org/doc/Documentation/power/powercap/powercap.txt 40 func GetRaplZones(fs FS) ([]RaplZone, error) { 41 raplDir := fs.sys.Path("class/powercap") 42 43 files, err := os.ReadDir(raplDir) 44 if err != nil { 45 return nil, fmt.Errorf("unable to read class/powercap: %w", err) 46 } 47 48 var zones []RaplZone 49 50 // Count name usages to avoid duplicates (label them with an index). 51 countNameUsages := make(map[string]int) 52 53 // Loop through directory files searching for file "name" from subdirs. 54 for _, f := range files { 55 nameFile := filepath.Join(raplDir, f.Name(), "/name") 56 nameBytes, err := os.ReadFile(nameFile) 57 if err == nil { 58 // Add new rapl zone since name file was found. 59 name := strings.TrimSpace(string(nameBytes)) 60 61 // get a pair of index and final name 62 index, name := getIndexAndName(countNameUsages, 63 name) 64 65 maxMicrojouleFilename := filepath.Join(raplDir, f.Name(), 66 "/max_energy_range_uj") 67 maxMicrojoules, err := util.ReadUintFromFile(maxMicrojouleFilename) 68 if err != nil { 69 return nil, err 70 } 71 72 zone := RaplZone{ 73 Name: name, 74 Index: index, 75 Path: filepath.Join(raplDir, f.Name()), 76 MaxMicrojoules: maxMicrojoules, 77 } 78 79 zones = append(zones, zone) 80 81 // Store into map how many times this name has been used. There can 82 // be e.g. multiple "dram" instances without any index postfix. The 83 // count is then used for indexing 84 countNameUsages[name] = index + 1 85 } 86 } 87 88 return zones, nil 89 } 90 91 // GetEnergyMicrojoules returns the current microjoule value from the zone energy counter 92 // https://www.kernel.org/doc/Documentation/power/powercap/powercap.txt 93 func (rz RaplZone) GetEnergyMicrojoules() (uint64, error) { 94 return util.ReadUintFromFile(filepath.Join(rz.Path, "/energy_uj")) 95 } 96 97 // getIndexAndName returns a pair of (index, name) for a given name and name 98 // counting map. Some RAPL-names have an index at the end, some have duplicates 99 // without an index at the end. When the index is embedded in the name, it is 100 // provided back as an integer, and stripped from the returned name. Usage 101 // count is used when the index value is absent from the name. 102 func getIndexAndName(countNameUsages map[string]int, name string) (int, string) { 103 s := strings.Split(name, "-") 104 if len(s) == 2 { 105 index, err := strconv.Atoi(s[1]) 106 if err == nil { 107 return index, s[0] 108 } 109 } 110 // return count as the index, since name didn't have an index at the end 111 return countNameUsages[name], name 112 } 113