github.com/intel/goresctrl@v0.5.0/pkg/sst/sysfs.go (about) 1 /* 2 Copyright 2021 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sst 18 19 import ( 20 "os" 21 "path/filepath" 22 "strconv" 23 "strings" 24 25 goresctrlpath "github.com/intel/goresctrl/pkg/path" 26 "github.com/intel/goresctrl/pkg/utils" 27 ) 28 29 type cpuPackageInfo struct { 30 id int 31 cpus []int 32 } 33 34 func (pkg *cpuPackageInfo) hasCpus(cpus utils.IDSet) bool { 35 return utils.NewIDSetFromIntSlice(pkg.cpus...).Has(cpus.Members()...) 36 } 37 38 func getOnlineCpuPackages() (map[int]*cpuPackageInfo, error) { 39 basePath := goresctrlpath.Path("sys/bus/cpu/devices") 40 41 files, err := os.ReadDir(basePath) 42 if err != nil { 43 return nil, err 44 } 45 46 pkgs := make(map[int]*cpuPackageInfo) 47 48 for _, file := range files { 49 // Try to read siblings from topology 50 raw, err := os.ReadFile(filepath.Join(basePath, file.Name(), "topology/physical_package_id")) 51 if os.IsNotExist(err) { 52 // Offline -> topology information does not exist 53 continue 54 } else if err != nil { 55 return nil, err 56 } 57 58 cpuId, err := strconv.Atoi(file.Name()[3:]) 59 if err != nil { 60 return nil, err 61 } 62 63 pkgId, err := strconv.Atoi(strings.TrimSpace(string(raw))) 64 if err != nil { 65 return nil, err 66 } 67 68 if _, ok := pkgs[pkgId]; !ok { 69 pkgs[pkgId] = &cpuPackageInfo{id: pkgId} 70 } 71 pkgs[pkgId].cpus = append(pkgs[pkgId].cpus, cpuId) 72 } 73 74 return pkgs, nil 75 } 76 77 func isHWPEnabled() bool { 78 status, err := utils.ReadMSR(0, MSR_PM_ENABLE) 79 if err != nil { 80 return false 81 } 82 83 return (status & 0xff) != 0 84 } 85 86 func setCPUScalingMin2CPUInfoMinFreq(cpu utils.ID) error { 87 freq, err := utils.GetCPUFreqValue(cpu, "cpuinfo_min_freq") 88 if err != nil { 89 return err 90 } 91 92 return utils.SetCPUScalingMinFreq(cpu, freq) 93 } 94 95 func setCPUScalingMin2CPUInfoMaxFreq(cpu utils.ID) error { 96 freq, err := utils.GetCPUFreqValue(cpu, "cpuinfo_max_freq") 97 if err != nil { 98 return err 99 } 100 101 return utils.SetCPUScalingMinFreq(cpu, freq) 102 }