-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprocinfo.go
84 lines (78 loc) · 1.85 KB
/
procinfo.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
package androidutils
import (
"errors"
"io/ioutil"
"regexp"
"strconv"
"strings"
)
// MemoryInfo read from /proc/meminfo, unit kB
func MemoryInfo() (info map[string]int, err error) {
data, err := ioutil.ReadFile("/proc/meminfo")
if err != nil {
return
}
return parseMemoryInfo(data)
}
func parseMemoryInfo(data []byte) (info map[string]int, err error) {
re := regexp.MustCompile(`([\w_\(\)]+):\s*(\d+) kB`)
matches := re.FindAllStringSubmatch(string(data), -1)
if len(matches) == 0 {
return nil, errors.New("Invalid memory info data")
}
info = make(map[string]int)
for _, m := range matches {
var key = m[1]
val, _ := strconv.Atoi(m[2])
info[key] = val
}
return
}
type Processor struct {
Index int
BogoMIPS string
Features []string
}
// ProcessorInfo read from /proc/cpuinfo
func ProcessorInfo() (hardware string, processors []Processor, err error) {
data, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil {
return
}
return parseCpuInfo(data)
}
func parseCpuInfo(data []byte) (hardware string, processors []Processor, err error) {
re := regexp.MustCompile(`([\w ]+\w)\s*:\s*(.+)`)
ms := re.FindAllStringSubmatch(string(data), -1)
processors = make([]Processor, 0, 8)
var processor = Processor{Index: -1}
for _, m := range ms {
var key = m[1]
var val = m[2]
if key == "Hardware" { // Hardware occur at last
hardware = val
processors = append(processors, processor)
break
}
if key == "processor" {
idx, _ := strconv.Atoi(val)
if idx != processor.Index && processor.Index != -1 {
processors = append(processors, processor)
}
processor.Index = idx
continue
}
switch key {
case "BogoMIPS":
processor.BogoMIPS = val
case "Features":
processor.Features = strings.Split(val, " ")
default:
// ignore
}
}
if len(processors) == 0 {
err = errors.New("Invalid cpuinfo data")
}
return
}