-
Notifications
You must be signed in to change notification settings - Fork 184
/
node.go
293 lines (251 loc) · 6.58 KB
/
node.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
289
290
291
292
293
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package node
import (
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
cpuidv2 "github.com/klauspost/cpuid/v2"
"gopkg.in/yaml.v3"
"k8s.io/klog/v2"
"github.com/sustainable-computing-io/kepler/pkg/config"
)
const (
cpuModelDataPath = "/var/lib/kepler/data/cpus.yaml"
cpuPmuNamePath = "/sys/devices/cpu/caps/pmu_name"
)
type nodeInfo struct {
name string
cpuArchitecture string
metadataFeatureNames []string
metadataFeatureValues []string
}
type Node interface {
Name() string
CPUArchitecture() string
MetadataFeatureNames() []string
MetadataFeatureValues() []string
}
type cpuModelData struct {
uarch string `yaml:"uarch"`
family string `yaml:"family"`
model string `yaml:"model"`
stepping string `yaml:"stepping"`
}
type cpuInfo struct {
cpusInfo []cpuModelData
}
func Name() string {
return nodeName()
}
func CPUArchitecture() string {
return cpuArch()
}
func MetadataFeatureNames() []string {
return []string{"cpu_architecture"}
}
func MetadataFeatureValues() []string {
cpuArchitecture := cpuArch()
return []string{cpuArchitecture}
}
func NewNodeInfo() Node {
cpuArchitecture := cpuArch()
return &nodeInfo{
name: nodeName(),
cpuArchitecture: cpuArchitecture,
metadataFeatureNames: []string{"cpu_architecture"},
metadataFeatureValues: []string{cpuArchitecture},
}
}
func (ni *nodeInfo) Name() string {
return ni.name
}
func (ni *nodeInfo) CPUArchitecture() string {
return ni.cpuArchitecture
}
func (ni *nodeInfo) MetadataFeatureNames() []string {
return ni.metadataFeatureNames
}
func (ni *nodeInfo) MetadataFeatureValues() []string {
return ni.metadataFeatureValues
}
func nodeName() string {
if nodeName := os.Getenv("NODE_NAME"); nodeName != "" {
return nodeName
}
nodeName, err := os.Hostname()
if err != nil {
klog.Fatalf("could not get the node name: %s", err)
}
return nodeName
}
func cpuArch() string {
if arch, err := getCPUArchitecture(); err == nil {
klog.V(3).Infof("Current CPU architecture: %s", arch)
return arch
} else {
klog.Errorf("getCPUArch failure: %s", err)
return "unknown"
}
}
func x86Architecture() (string, error) {
grep := exec.Command("grep", "uarch")
output := exec.Command("cpuid", "-1")
pipe, err := output.StdoutPipe()
if err != nil {
return "", err
}
defer pipe.Close()
grep.Stdin = pipe
err = output.Start()
if err != nil {
klog.Errorf("cpuid command start failure: %s", err)
return "", err
}
res, err := grep.Output()
if err != nil {
klog.Errorf("grep cpuid command output failure: %s", err)
return "", err
}
uarchSection := strings.Split(string(res), "=")
if len(uarchSection) != 2 {
return "", fmt.Errorf("cpuid grep output is unexpected")
}
vendorUarchFamily := strings.Split(strings.TrimSpace(uarchSection[1]), ",")[0]
var vendorUarch string
if strings.Contains(vendorUarchFamily, "{") {
vendorUarch = strings.TrimSpace(strings.Split(vendorUarchFamily, "{")[0])
} else {
vendorUarch = vendorUarchFamily
}
start := strings.Index(vendorUarch, " ") + 1
uarch := vendorUarch[start:]
if err = output.Wait(); err != nil {
klog.Errorf("cpuid command is not properly completed: %s", err)
}
return uarch, err
}
func s390xArchitecture() (string, error) {
grep := exec.Command("grep", "Machine type:")
output := exec.Command("lscpu")
pipe, err := output.StdoutPipe()
if err != nil {
return "", err
}
defer pipe.Close()
grep.Stdin = pipe
err = output.Start()
if err != nil {
klog.Errorf("lscpu command start failure: %s", err)
return "", err
}
res, err := grep.Output()
if err != nil {
klog.Errorf("grep lscpu command output failure: %s", err)
return "", err
}
uarch := strings.Split(string(res), ":")
if len(uarch) != 2 {
return "", fmt.Errorf("lscpu grep output is unexpected")
}
if err = output.Wait(); err != nil {
klog.Errorf("lscpu command is not properly completed: %s", err)
}
return fmt.Sprintf("zSystems model %s", strings.TrimSpace(uarch[1])), err
}
func readCPUModelData() ([]byte, error) {
data, err := os.ReadFile(cpuModelDataPath)
if err == nil {
return data, nil
}
if !os.IsNotExist(err) {
return nil, err
}
klog.Infof("%s not found, reading local cpus.yaml", cpuModelDataPath)
return os.ReadFile("./data/cpus.yaml")
}
func cpuMicroArchitecture(family, model, stepping string) (string, error) {
yamlBytes, err := readCPUModelData()
if err != nil {
klog.Errorf("failed to read cpus.yaml: %v", err)
return "", err
}
cpus := &cpuInfo{
cpusInfo: []cpuModelData{},
}
err = yaml.Unmarshal(yamlBytes, &cpus.cpusInfo)
if err != nil {
klog.Errorf("failed to parse cpus.yaml: %v", err)
return "", err
}
for _, info := range cpus.cpusInfo {
if info.family == family {
var reModel *regexp.Regexp
reModel, err = regexp.Compile(info.model)
if err != nil {
return "", err
}
if reModel.FindString(model) == model {
if info.stepping != "" {
var reStepping *regexp.Regexp
reStepping, err = regexp.Compile(info.stepping)
if err != nil {
return "", err
}
if reStepping.FindString(stepping) == "" {
continue
}
}
return info.uarch, nil
}
}
}
klog.V(3).Infof("CPU match not found for family %s, model %s, stepping %s. Use pmu_name as uarch.", family, model, stepping)
return cpuPmuName()
}
func cpuPmuName() (string, error) {
data, err := os.ReadFile(cpuPmuNamePath)
if err != nil {
klog.V(3).Infoln(err)
return "", err
}
return string(data), nil
}
func getCPUArchitecture() (string, error) {
cpuArchOverride := config.CPUArchOverride()
if cpuArchOverride != "" {
klog.V(2).Infof("cpu arch override: %v\n", cpuArchOverride)
return cpuArchOverride, nil
}
var (
myCPUArch string
err error
)
if runtime.GOARCH == "amd64" {
myCPUArch, err = x86Architecture()
} else if runtime.GOARCH == "s390x" {
return s390xArchitecture()
} else {
myCPUArch, err = cpuPmuName()
}
if err == nil {
return myCPUArch, nil
} else {
f, m, s := strconv.Itoa(cpuidv2.CPU.Family), strconv.Itoa(cpuidv2.CPU.Model), strconv.Itoa(cpuidv2.CPU.Stepping)
return cpuMicroArchitecture(f, m, s)
}
}