Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support to differentiate specific hypervisors on s390x #2055

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/usage/customization-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ The following features are available for matching:
| | | **`family`** | int | CPU family |
| | | **`vendor_id`** | string | CPU vendor ID |
| | | **`id`** | int | CPU model ID |
| | | **`hypervisor`** | string | Hypervisor type information from `/proc/sysinfo` (s390x-only feature) |
| **`cpu.pstate`** | attribute | | | State of the Intel pstate driver. Does not exist if the driver is not enabled. |
| | | **`status`** | string | Status of the driver, possible values are 'active' and 'passive' |
| | | **`turbo`** | bool | 'true' if turbo frequencies are enabled, otherwise 'false' |
Expand Down
37 changes: 37 additions & 0 deletions source/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cpu
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -263,9 +264,45 @@ func getCPUModel() map[string]string {
cpuModelInfo["family"] = strconv.Itoa(cpuid.CPU.Family)
cpuModelInfo["id"] = strconv.Itoa(cpuid.CPU.Model)

hypervisor, err := getHypervisor()
if err != nil {
klog.ErrorS(err, "failed to detect hypervisor")
} else if hypervisor != "" {
cpuModelInfo["hypervisor"] = hypervisor
}

return cpuModelInfo
}

// getHypervisor detects the hypervisor on s390x by reading /proc/sysinfo.
// If the file does not exist, it returns an empty string with no error.
func getHypervisor() (string, error) {
if _, err := os.Stat("/proc/sysinfo"); os.IsNotExist(err) {
return "", nil
}

data, err := os.ReadFile("/proc/sysinfo")
if err != nil {
return "", err
}

hypervisor := "PR/SM"
for _, line := range strings.Split(string(data), "\n") {
if strings.Contains(line, "Control Program:") {
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
hypervisor = strings.TrimSpace(parts[1])
}
break
}
}
// Replace forbidden symbols
fullRegex := regexp.MustCompile("[^-A-Za-z0-9_.]+")
hypervisor = fullRegex.ReplaceAllString(hypervisor, "_")

return hypervisor, nil
}

func discoverTopology() map[string]string {
features := make(map[string]string)

Expand Down