-
Notifications
You must be signed in to change notification settings - Fork 245
/
cpuinfo.go
273 lines (239 loc) · 8.28 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
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Apache License 2.0.
* See the file "LICENSE" for details.
*/
package host
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/prometheus/procfs"
"github.com/prometheus/procfs/sysfs"
log "github.com/sirupsen/logrus"
)
const (
// Keys we get from procfs
keyCPUVendorID = "vendor"
keyCPUModel = "model"
keyCPUModelName = "model-name"
keyCPUStepping = "stepping"
keyCPUFlags = "flags"
keyCPUBugs = "bugs"
keyCPUMaxMhz = "clock/max-mhz"
keyCPUMinMhz = "clock/min-mhz"
keyCPUScalingCurFreqMhz = "clock/scaling-cur-freq-mhz"
keyCPUScalingDriver = "clock/scaling-driver"
keyCPUScalingGovernor = "clock/scaling-governor"
// Keys from CPUID
keyCPUThreadsPerCore = "threads-per-core"
keyCPUCoresPerSocket = "cores-per-socket"
keyCPUNumCPUs = "cpus"
keyCPUCacheL1d = "cache/L1d-kbytes"
keyCPUCacheL1i = "cache/L1i-kbytes"
keyCPUCacheL2 = "cache/L2-kbytes"
keyCPUCacheL3 = "cache/L3-kbytes"
// Parsed from Kernel file
keyCPUOnline = "online"
keyPrefixCPU = "host:cpu"
// measures
kiloMemDiv = 1024
megaHzDiv = 1000
)
// We use CPUInfo from prometheus/procfs to fetch all data about all CPUs and group them as we want
// and get the caches (missing in procfs) from klauspost/cpuid, locking goroutines to CPUs
// A map of "host:cpu:<suffix>" keys to a map of socketIDs to values
type cpuInfo map[string]map[int]string
func key(suffix string) string {
return fmt.Sprintf("%s/%s", keyPrefixCPU, suffix)
}
func (ci cpuInfo) add(socketID int, suffix, value string) {
if value == "" {
return
}
key := key(suffix)
if _, ok := ci[key]; !ok {
ci[key] = map[int]string{}
}
ci[key][socketID] = value
}
func (ci cpuInfo) addMany(socketID int, values map[string]string) {
for suffix, value := range values {
ci.add(socketID, suffix, value)
}
}
// readCPUInfo will return a map with data about CPUs by reading from 3 sources:
// /proc/cpuinfo, /sys/device/system/cpu and the CPUID instruction.
func readCPUInfo() (cpuInfo, error) {
info := cpuInfo{}
sysDeviceCPUs, sysDeviceCPUFreqs, err := fetchCPUSysFs()
if err != nil {
return nil, err
}
cpuProcInfos, err := fetchCPUProcInfo()
if err != nil {
return nil, fmt.Errorf("error reading /proc/cpuinfo: %v", err)
}
// Online CPUs list will be used during the per-coreID iteration
// to match the coreIDs that are online
onlineCPUs, err := ParseCPUCoreIDs(CPUOnlinePath)
if err != nil {
return nil, fmt.Errorf("error reading online CPUs: %v", err)
}
// Iterate over all the logical cores and get their topology,
// in order to group them per physical socket.
// We expect all the 3 slices fetched via sysfs and procfs to have the same
// number of entries, as they are fetched from the kernel, so we iterate
// and fetch items from them using the slice index.
for deviceID, cpu := range sysDeviceCPUs {
// We need the topology to map logical cores onto physical sockets.
topology, err := cpu.Topology()
if err != nil {
continue
}
socketID, err := strconv.Atoi(topology.PhysicalPackageID)
// An error here should never happen but we want to log it in case it happens
if err != nil {
log.Errorf("Unable to convert socketID %s to integer: %v",
topology.PhysicalPackageID, err)
continue
}
// Checks if the deviceID is available in the online CPUs, using siblings
siblings := topology.CoreSiblingsList
info.add(socketID, keyCPUOnline, onlineCPUsFor(siblings, onlineCPUs))
addCPU(info, &cpuProcInfos[deviceID], socketID)
addCPUFrequencies(info, &sysDeviceCPUFreqs[deviceID], socketID)
// CPUID data are stored in a map with key the logical coreID,
// so we will use that instead of the index of the previous slices.
coreID, err := strconv.Atoi(topology.CoreID)
if err != nil {
log.Errorf("Unable to convert coreID %s to integer: %v", topology.CoreID, err)
continue
}
addCPUID(info, socketID, coreID)
}
return info, nil
}
func addCPUFrequencies(info cpuInfo, freqs *sysfs.SystemCPUCpufreqStats, socketID int) {
// We want MegaHertz and the value is originally in KiloHertz
if freqs.CpuinfoMaximumFrequency != nil {
maxVal := *freqs.CpuinfoMaximumFrequency
info.add(socketID, keyCPUMaxMhz, strconv.Itoa(int(maxVal)/megaHzDiv))
}
if freqs.CpuinfoMinimumFrequency != nil {
minVal := *freqs.CpuinfoMinimumFrequency
info.add(socketID, keyCPUMinMhz, strconv.Itoa(int(minVal)/megaHzDiv))
}
if freqs.ScalingCurrentFrequency != nil {
scaling := *freqs.ScalingCurrentFrequency
info.add(socketID, keyCPUScalingCurFreqMhz, strconv.Itoa(int(scaling)/megaHzDiv))
}
info.addMany(socketID, map[string]string{
keyCPUScalingGovernor: freqs.Governor,
keyCPUScalingDriver: freqs.Driver,
})
}
func addCPU(info cpuInfo, cpuProcInfos *procfs.CPUInfo, socketID int) {
// We want a comma-separated, sorted list of flags and bugs, so we sort them here
sort.Strings(cpuProcInfos.Flags)
sort.Strings(cpuProcInfos.Bugs)
info.addMany(socketID, map[string]string{
keyCPUVendorID: cpuProcInfos.VendorID,
keyCPUModel: cpuProcInfos.Model,
keyCPUModelName: cpuProcInfos.ModelName,
keyCPUStepping: cpuProcInfos.Stepping,
keyCPUFlags: strings.Join(cpuProcInfos.Flags, ","),
keyCPUBugs: strings.Join(cpuProcInfos.Bugs, ","),
})
}
func addCPUID(info cpuInfo, socketID, cpuID int) {
cpuData, ok := _CPUIDs[cpuID]
if ok {
info.addMany(socketID, map[string]string{
keyCPUThreadsPerCore: strconv.Itoa(cpuData.ThreadsPerCore),
keyCPUCoresPerSocket: strconv.Itoa(cpuData.PhysicalCores),
// We want KiloBytes and the value is originally in bytes
keyCPUCacheL1i: strconv.Itoa(cpuData.Cache.L1I / kiloMemDiv),
keyCPUCacheL1d: strconv.Itoa(cpuData.Cache.L1D / kiloMemDiv),
keyCPUCacheL2: strconv.Itoa(cpuData.Cache.L2 / kiloMemDiv),
keyCPUCacheL3: strconv.Itoa(cpuData.Cache.L3 / kiloMemDiv),
})
if cpuData.LogicalCores == 0 {
// cpuData.LogicalCores returns the number of physical cores times the
// number of threads that can run on each core. Architectures like KVM does
// not have physical cores. Therefore we assume the number of threads per
// core are on a single CPU.
info.add(socketID, keyCPUNumCPUs, strconv.Itoa(cpuData.ThreadsPerCore))
} else {
info.add(socketID, keyCPUNumCPUs, strconv.Itoa(cpuData.LogicalCores))
}
} else {
// If the map lookup changed, we populate the entries with an error string.
errorString := "ERR"
info.addMany(socketID, map[string]string{
keyCPUThreadsPerCore: errorString,
keyCPUCoresPerSocket: errorString,
keyCPUNumCPUs: errorString,
keyCPUCacheL1i: errorString,
keyCPUCacheL1d: errorString,
keyCPUCacheL2: errorString,
keyCPUCacheL3: errorString,
})
}
}
func fetchCPUProcInfo() ([]procfs.CPUInfo, error) {
fs, err := procfs.NewDefaultFS()
if err != nil {
return nil, fmt.Errorf("failed to read /proc: %v", err)
}
return fs.CPUInfo()
}
func fetchCPUSysFs() ([]sysfs.CPU, []sysfs.SystemCPUCpufreqStats, error) {
sys, err := sysfs.NewDefaultFS()
if err != nil {
return nil, nil, fmt.Errorf("failed to read /sys filesystem: %v", err)
}
cpus, err := sys.CPUs()
if err != nil {
return nil, nil, fmt.Errorf("failed to read CPUS from /sys/device/system/cpu: %v", err)
}
freqs, err := sys.SystemCpufreq()
if err != nil {
return nil, nil,
fmt.Errorf("failed to read frequencies from /sys/device/system/cpu: %v", err)
}
return cpus, freqs, nil
}
func onlineCPUsFor(siblingsList string, onlineCoreIDs []int) string {
siblings, err := readCPURange(siblingsList)
if err != nil {
log.Errorf("Could not parse CPU siblings: %v", err)
}
sort.Ints(siblings)
var onlines []int
for _, c := range onlineCoreIDs {
if x := sort.SearchInts(siblings, c); x < len(siblings) &&
siblings[x] == c {
onlines = append(onlines, c)
}
}
return writeCPURange(onlines)
}
func writeCPURange(listOf []int) string {
sort.Ints(listOf)
var ret string
for i := range listOf {
if ret == "" {
ret = strconv.Itoa(listOf[i])
continue
}
if listOf[i] == listOf[i-1]+1 {
ret = strings.TrimSuffix(ret, "-"+strconv.Itoa(listOf[i-1]))
ret += "-" + strconv.Itoa(listOf[i])
} else {
ret += "," + strconv.Itoa(listOf[i])
}
}
return ret
}