github.com/intel/goresctrl@v0.5.0/pkg/utils/msr.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 utils 18 19 import ( 20 "encoding/binary" 21 "fmt" 22 "os" 23 24 goresctrlpath "github.com/intel/goresctrl/pkg/path" 25 ) 26 27 func ReadMSR(cpu ID, msr int64) (uint64, error) { 28 path := goresctrlpath.Path("dev/cpu", fmt.Sprintf("%d", cpu), "msr") 29 file, err := os.Open(path) 30 if err != nil { 31 return 0, err 32 } 33 34 defer file.Close() 35 36 // Whence is the point of reference for offset 37 // 0 = Beginning of file 38 // 1 = Current position 39 // 2 = End of file 40 var whence int = 0 41 _, err = file.Seek(msr, whence) 42 if err != nil { 43 return 0, err 44 } 45 46 data := make([]byte, 8) 47 48 _, err = file.Read(data) 49 if err != nil { 50 return 0, err 51 } 52 53 return binary.LittleEndian.Uint64(data), nil 54 }