-
Notifications
You must be signed in to change notification settings - Fork 39
/
cpuinfo.go
57 lines (44 loc) · 1.08 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
// Contains a helper function for getting named properties out of the /dev/cpuinfo file.
// The file is only opened once. Properties are stored per processor. Processor 0 should
// be guaranteed to be present.
package hwio
import (
"bufio"
"fmt"
"os"
"strings"
)
// maps processor:property to value.
var cpuInfo map[string]string
// Look up property for a CPU. CPU's start at 0.
func CpuInfo(cpu int, property string) string {
if cpuInfo == nil {
loadCpuInfo()
}
return cpuInfo[fmt.Sprintf("%d:%s", cpu, property)]
}
func loadCpuInfo() {
cpuInfo = make(map[string]string)
file, e := os.Open("/proc/cpuinfo")
if e != nil {
return
}
currentCpu := ""
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// split on the first colon, and trim both sides
i := strings.Index(line, ":")
if i >= 0 {
name := strings.Trim(line[0:i], " \t")
value := strings.Trim(line[i+1:], " \t")
if name == "processor" {
currentCpu = value
}
cpuInfo[currentCpu+":"+name] = value
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
}