Skip to content

Commit 1e4e696

Browse files
committed
feat: add support to differentiate specific hypervisors on s390x
Signed-off-by: Ching Han Chen <[email protected]>
1 parent af706a8 commit 1e4e696

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

source/cpu/cpu.go

+33
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,42 @@ func getCPUModel() map[string]string {
263263
cpuModelInfo["family"] = strconv.Itoa(cpuid.CPU.Family)
264264
cpuModelInfo["id"] = strconv.Itoa(cpuid.CPU.Model)
265265

266+
hypervisor, err := getHypervisor()
267+
if err != nil {
268+
klog.ErrorS(err, "failed to detect hypervisor")
269+
} else if hypervisor != "" {
270+
cpuModelInfo["hypervisor"] = hypervisor
271+
}
272+
266273
return cpuModelInfo
267274
}
268275

276+
// getHypervisor detects the hypervisor on s390x by reading /proc/sysinfo.
277+
// If the file does not exist, it returns an empty string with no error.
278+
func getHypervisor() (string, error) {
279+
if _, err := os.Stat("/proc/sysinfo"); os.IsNotExist(err) {
280+
return "", nil
281+
}
282+
283+
data, err := os.ReadFile("/proc/sysinfo")
284+
if err != nil {
285+
return "", err
286+
}
287+
288+
hypervisor := "PR/SM"
289+
for _, line := range strings.Split(string(data), "\n") {
290+
if strings.Contains(line, "Control Program:") {
291+
parts := strings.SplitN(line, ":", 2)
292+
if len(parts) == 2 {
293+
hypervisor = strings.TrimSpace(parts[1])
294+
}
295+
break
296+
}
297+
}
298+
299+
return hypervisor, nil
300+
}
301+
269302
func discoverTopology() map[string]string {
270303
features := make(map[string]string)
271304

source/cpu/cpuid_linux_s390x.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ func getCpuidFlags() []string {
9696
return r
9797
}
9898

99-
func getCpuidAttributes() map[string]string { return nil }
99+
func getCpuidAttributes() map[string]string { return nil }

0 commit comments

Comments
 (0)