-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu-unix.go
288 lines (238 loc) · 5.91 KB
/
cpu-unix.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// +build !windows
package cpu
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
)
// Info -
func Info() CPU {
count := fetchCPUInfo2("Socket(s):")
countAsInt, _ := strconv.Atoi(count)
coresPerCPUCount := fetchCPUInfo2("Core(s) per socket:")
coresPerCPUCountAsInt, _ := strconv.Atoi(coresPerCPUCount)
threadsPerCoreCount := fetchCPUInfo2("Thread(s) per core:")
threadsPerCoreCountAsInt, _ := strconv.Atoi(threadsPerCoreCount)
cpuInfo := CPU{
Count: countAsInt,
CoresPerCPU: coresPerCPUCountAsInt,
ThreadsPerCore: threadsPerCoreCountAsInt,
TotalThreads: runtime.NumCPU(),
Architecture: getCPUArchitecture(),
Variant: CPUVariant{
Name: getCPUVariant(),
Detailed: getCPUVariantDetailed(),
},
Manufacturer: fetchCPUInfo2("Vendor ID:"),
Model: fetchCPUInfo2("Model name:"),
ByteOrder: fetchCPUInfo2("Byte Order:"),
Features: []string{
fetchCPUInfo2("Flags:"),
},
AdditionalInfo: AdditionalInfo{
"goos": runtime.GOOS,
"goarch": runtime.GOARCH,
"model name": fetchCPUInfo("model name"),
"Model name": fetchCPUInfo2("Model name:"),
"bugs": fetchCPUInfo("bugs"),
"Vulnerability": fetchCPUInfo2All("Vulnerability"),
},
}
if len(strings.TrimSpace(cpuInfo.Model)) == 0 {
cpuInfo.Model = fetchCPUInfo("model name")
}
cpuInfo.setID()
return cpuInfo
}
// On Linux
// The kernel has already detected the ABI, ISA and Features.
// We don't need to access the ARM registers to detect platform information.
// We can just parse these information from /proc/cpuinfo
func fetchCPUInfo(pattern string) string {
cpuinfo, err := os.Open("/proc/cpuinfo")
if err != nil {
return ""
}
defer cpuinfo.Close()
// Parse the Cpuinfo line by line. For SMP SoC, we parse the first core is enough.
scanner := bufio.NewScanner(cpuinfo)
for scanner.Scan() {
newline := scanner.Text()
list := strings.Split(newline, ":")
if len(list) > 1 && strings.EqualFold(strings.TrimSpace(list[0]), pattern) {
return strings.TrimSpace(list[1])
}
}
// Check whether the scanner encountered errors
err = scanner.Err()
if err != nil {
return ""
}
return ""
}
// LscpuFieldData -
type LscpuFieldData struct {
Field string `json:"field"`
Data string `json:"data"`
}
// LscpuOutput -
type LscpuOutput struct {
Lscpu []LscpuFieldData `json:"lscpu"`
}
func fetchCPUInfo2(pattern string) string {
cmd := exec.Command("lscpu", "--json")
cmd.Stdin = strings.NewReader("")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return ""
}
outputAsString := strings.TrimSpace(out.String())
// fmt.Println(outputAsString)
lscpuO := LscpuOutput{}
err = json.Unmarshal([]byte(outputAsString), &lscpuO)
if err != nil {
return ""
}
for _, v := range lscpuO.Lscpu {
if strings.Index(v.Field, pattern) != -1 {
return v.Data
}
}
return ""
}
func fetchCPUInfo2All(pattern string) []string {
cmd := exec.Command("lscpu", "--json")
cmd.Stdin = strings.NewReader("")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return []string{}
}
type lscpuFieldData struct {
field string `json:"field"`
data string `json:"data"`
}
type lscpuOutput struct {
lscpu []lscpuFieldData `json:"lscpu"`
}
outputAsString := strings.TrimSpace(out.String())
var lscpuO lscpuOutput
err = json.Unmarshal([]byte(outputAsString), &lscpuO)
if err != nil {
return []string{}
}
theList := []string{}
for _, v := range lscpuO.lscpu {
if strings.Index(v.field, pattern) != -1 {
theList = append(theList, fmt.Sprintf("%s : %s", v.field, v.data))
}
}
return theList
}
func getPhysicalCPUCount() int {
cpuinfo, err := os.Open("/proc/cpuinfo")
if err != nil {
return 0
}
defer cpuinfo.Close()
pattern := "physical id"
scanner := bufio.NewScanner(cpuinfo)
allLines := []string{}
for scanner.Scan() {
newline := scanner.Text()
if strings.Index(newline, pattern) != -1 {
list := strings.Split(newline, ":")
if len(list) > 1 && strings.EqualFold(strings.TrimSpace(list[0]), pattern) {
allLines = append(allLines, strings.TrimSpace(list[1]))
}
}
}
// Check whether the scanner encountered errors
err = scanner.Err()
if err != nil {
return 0
}
if len(allLines) > 0 {
cpuCountAsInt, err := strconv.Atoi(allLines[len(allLines)-1])
if err == nil {
return cpuCountAsInt + 1
}
}
return 0
}
func getCPUVariant() CPUArchitectureVariant {
variantAsString := fetchCPUInfo("Cpu architecture")
if variantAsString == "" {
return ""
}
switch strings.ToLower(variantAsString) {
case "8", "aarch64":
// special case:
// if running a 32-bit userspace on aarch64,
// the variant should be "v7"
if runtime.GOARCH == "arm" {
return CPUV_ARMv7
}
return CPUV_ARMv8
case "7", "7m", "?(12)", "?(13)", "?(14)", "?(15)", "?(16)", "?(17)":
return CPUV_ARMv7
case "6", "6tej":
return CPUV_ARMv6
case "5", "5t", "5te", "5tej":
return CPUV_ARMv5
case "4", "4t":
return CPUV_ARMv4
case "3":
return CPUV_ARMv3
}
return CPUV_Uknown
}
func getCPUVariantDetailed() CPUArchitectureVariantDetailed {
variantAsString := fetchCPUInfo("Cpu architecture")
if variantAsString == "" {
return CPUVD_Uknown
}
switch strings.ToLower(variantAsString) {
case "7m":
return CPUVD_ARMv7_M
case "?(12)", "?(13)", "?(14)", "?(15)", "?(16)", "?(17)":
return CPUV_ARMv7
case "6tej":
return CPUVD_ARMv6TEJ
case "5t":
return CPUVD_ARMv5T
case "5te":
return CPUVD_ARMv5TE
case "5tej":
return CPUVD_ARMv5TEJ
case "4t":
return CPUVD_ARMv4T
}
return CPUVD_Uknown
}
func getKernelVersion() string {
cmd := exec.Command("uname", "-r")
cmd.Stdin = strings.NewReader("")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return ""
}
return strings.TrimSpace(out.String())
}