-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpuinfo.go
127 lines (107 loc) · 2.7 KB
/
cpuinfo.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package proc
import (
"io/ioutil"
"regexp"
"strconv"
"strings"
)
type CPUInfo struct {
Processors []Processor `json:"processors"`
}
func (self *CPUInfo) NumCPU() int {
return len(self.Processors)
}
func (self *CPUInfo) NumCore() int {
core := make(map[string]bool)
for _, p := range self.Processors {
pid := p.PhysicalId
cid := p.CoreId
if pid == -1 {
return self.NumCPU()
} else {
// to avoid fmt import
key := strconv.FormatInt(int64(pid), 10) + ":" + strconv.FormatInt(int64(cid), 10)
core[key] = true
}
}
return len(core)
}
func (self *CPUInfo) NumPhysicalCPU() int {
pcpu := make(map[string]bool)
for _, p := range self.Processors {
pid := p.PhysicalId
if pid == -1 {
return self.NumCPU()
} else {
// to avoid fmt import
key := strconv.FormatInt(int64(pid), 10)
pcpu[key] = true
}
}
return len(pcpu)
}
type Processor struct {
Id int64 `json:"id"`
VendorId string `json:"vendor_id"`
Model int64 `json:"model"`
ModelName string `json:"model_name"`
Flags []string `json:"flags"`
Cores int64 `json:"cores"`
MHz float64 `json:"mhz"`
PhysicalId int64 `json:"physical_id"`
CoreId int64 `json:"core_id"`
}
var cpuinfoRegExp = regexp.MustCompile("([^:]*?)\\s*:\\s*(.*)$")
func ReadCPUInfo(path string) (*CPUInfo, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
content := string(b)
lines := strings.Split(content, "\n")
var cpuinfo = CPUInfo{}
var processor = &Processor{CoreId: -1, PhysicalId: -1}
for i, line := range lines {
var key string
var value string
if len(line) == 0 && i != len(lines)-1 {
// end of processor
cpuinfo.Processors = append(cpuinfo.Processors, *processor)
processor = &Processor{}
continue
} else if i == len(lines)-1 {
continue
}
submatches := cpuinfoRegExp.FindStringSubmatch(line)
key = submatches[1]
value = submatches[2]
switch key {
case "processor":
processor.Id, _ = strconv.ParseInt(value, 10, 32)
case "vendor_id":
processor.VendorId = value
case "model":
processor.Model, _ = strconv.ParseInt(value, 10, 32)
case "model name":
processor.ModelName = value
case "flags":
processor.Flags = strings.Fields(value)
case "cpu cores":
processor.Cores, _ = strconv.ParseInt(value, 10, 32)
case "cpu MHz":
processor.MHz, _ = strconv.ParseFloat(value, 64)
case "physical id":
processor.PhysicalId, _ = strconv.ParseInt(value, 10, 32)
case "core id":
processor.CoreId, _ = strconv.ParseInt(value, 10, 32)
}
/*
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 26
model name : Intel(R) Xeon(R) CPU L5520 @ 2.27GHz
*/
}
return &cpuinfo, nil
}