forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.go
199 lines (165 loc) · 4.26 KB
/
cpu.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
/*
* Copyright (C) 2014 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package systeminfo
import (
"fmt"
"strconv"
"strings"
"math"
)
const (
cpuKeyDelim = ":"
cpuKeyProcessor = "processor"
cpuKeyName = "model name"
cpuKeyCPU = "cpu"
cpuKeyMHz = "CPU frequency [MHz]"
cpuKeyActive = "cpus active"
cpuKeyARMProcessor = "Processor"
cpuKeyHardware = "Hardware"
lscpuKeyMaxMHz = "CPU max MHz"
lscpuKeyModelName = "Model name"
lscpuKeyCount = "CPU(s)"
compareAllowMin = 1e-6
)
func getProcessorByLscpu(data map[string]string) (string, error) {
modelName, ok := data[lscpuKeyModelName]
if !ok {
return "", fmt.Errorf("can not find the key %q", lscpuKeyModelName)
}
cpuCountStr, ok := data[lscpuKeyCount]
if !ok {
logger.Warningf("can not find the key %q", lscpuKeyCount)
return modelName, nil
}
cpuCount, err := strconv.ParseInt(cpuCountStr, 10, 64)
if err != nil {
logger.Warning(err)
return modelName, nil
}
return fmt.Sprintf("%s x %d", modelName, cpuCount), nil
}
func getCPUMaxMHzByLscpu(data map[string]string) (float64, error) {
maxMHz, ok := data[lscpuKeyMaxMHz]
if !ok {
return 0, fmt.Errorf("can not find the key %q", lscpuKeyMaxMHz)
}
return strconv.ParseFloat(maxMHz, 64)
}
//float数比较
func isFloatEqual(f1, f2 float64) bool {
return math.Abs(f1 - f2) < compareAllowMin
}
func GetCPUInfo(file string) (string, error) {
data, err := parseInfoFile(file, cpuKeyDelim)
if err != nil {
return "", err
}
cpu := swCPUInfo(data)
if len(cpu) != 0 {
return cpu, nil
}
// huawei kirin
cpu = hwKirinCPUInfo(data)
if len(cpu) != 0 {
return cpu, nil
}
// arm
cpu, _ = getCPUInfoFromMap(cpuKeyARMProcessor, cpuKeyProcessor, data)
if len(cpu) != 0 {
return cpu, nil
}
return getCPUInfoFromMap(cpuKeyName, cpuKeyProcessor, data)
}
func swCPUInfo(data map[string]string) string {
cpu, err := getCPUName(cpuKeyCPU, data)
if err != nil {
return ""
}
hz, err := getCPUHz(cpuKeyMHz, data)
if err == nil {
cpu = fmt.Sprintf("%s %.2fGHz", cpu, hz)
}
number, _ := getCPUNumber(cpuKeyActive, data)
if number != 1 {
cpu = fmt.Sprintf("%s x %v", cpu, number)
}
return cpu
}
func hwKirinCPUInfo(data map[string]string) string {
cpu, err := getCPUName(cpuKeyHardware, data)
if err != nil {
return ""
}
number, _ := getCPUNumber(cpuKeyProcessor, data)
if number != 1 {
cpu = fmt.Sprintf("%s x %v", cpu, number+1)
}
return cpu
}
func getCPUInfoFromMap(nameKey, numKey string, data map[string]string) (string, error) {
name, err := getCPUName(nameKey, data)
if err != nil {
return "", err
}
number, _ := getCPUNumber(numKey, data)
if number != 0 {
name = fmt.Sprintf("%s x %v", name, number+1)
}
return name, nil
}
func getCPUName(key string, data map[string]string) (string, error) {
value, ok := data[key]
if !ok {
return "", fmt.Errorf("can not find the key %q", key)
}
var name string
array := strings.Split(value, " ")
for i, v := range array {
if len(v) == 0 {
continue
}
name += v
if i != len(array)-1 {
name += " "
}
}
return name, nil
}
func getCPUNumber(key string, data map[string]string) (int, error) {
value, ok := data[key]
if !ok {
return 0, fmt.Errorf("can not find the key %q", key)
}
number, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return 0, err
}
return int(number), nil
}
func getCPUHz(key string, data map[string]string) (float64, error) {
value, ok := data[key]
if !ok {
return 0, fmt.Errorf("can not find the key %q", key)
}
hz, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, err
}
return hz / 1000, nil
}