-
Notifications
You must be signed in to change notification settings - Fork 11
/
msr.go
76 lines (68 loc) · 1.38 KB
/
msr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package perf
import (
"os"
"path/filepath"
)
const (
// MSRBaseDir is the base dir for MSRs.
MSRBaseDir = "/dev/cpu"
)
// MSRPaths returns the set of MSR paths.
func MSRPaths() ([]string, error) {
msrs := []string{}
err := filepath.Walk(MSRBaseDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}
if path != MSRBaseDir {
// TODO: replace this with a real recursive walk.
msrs = append(msrs, path+"/msr")
}
return nil
})
return msrs, err
}
// MSRs attemps to return all available MSRs.
func MSRs(flag int, perm os.FileMode, onErr func(error)) []*MSR {
paths, err := MSRPaths()
if err != nil {
onErr(err)
return nil
}
msrs := []*MSR{}
for _, path := range paths {
msr, err := NewMSR(path, flag, perm)
if err != nil {
onErr(err)
continue
}
msrs = append(msrs, msr)
}
return msrs
}
// MSR represents a Model Specific Register
type MSR struct {
f *os.File
}
// NewMSR returns a MSR.
func NewMSR(path string, flag int, perm os.FileMode) (*MSR, error) {
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
return &MSR{
f: f,
}, nil
}
// Read is used to read a MSR value.
func (m *MSR) Read(off int64, buf []byte) error {
_, err := m.f.ReadAt(buf, off)
return err
}
// Close is used to close the MSR.
func (m *MSR) Close() error {
if m.f != nil {
return m.f.Close()
}
return nil
}