Skip to content

Commit 8161e6e

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

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

docs/usage/customization-guide.md

+1
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ The following features are available for matching:
921921
| | | **`family`** | int | CPU family |
922922
| | | **`vendor_id`** | string | CPU vendor ID |
923923
| | | **`id`** | int | CPU model ID |
924+
| | | **`hypervisor`** | string | Hypervisor type information from `/proc/sysinfo` (s390x-only feature) |
924925
| **`cpu.pstate`** | attribute | | | State of the Intel pstate driver. Does not exist if the driver is not enabled. |
925926
| | | **`status`** | string | Status of the driver, possible values are 'active' and 'passive' |
926927
| | | **`turbo`** | bool | 'true' if turbo frequencies are enabled, otherwise 'false' |

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/k8stopologyawareschedwg/podfingerprint v0.2.2
1414
github.com/klauspost/cpuid/v2 v2.2.10
1515
github.com/onsi/ginkgo/v2 v2.23.3
16-
github.com/onsi/gomega v1.36.3
16+
github.com/onsi/gomega v1.36.2
1717
github.com/opencontainers/image-spec v1.1.1
1818
github.com/opencontainers/runc v1.2.6
1919
github.com/prometheus/client_golang v1.21.1
@@ -156,7 +156,7 @@ require (
156156
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
157157
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 // indirect
158158
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
159-
google.golang.org/protobuf v1.36.5 // indirect
159+
google.golang.org/protobuf v1.36.4 // indirect
160160
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
161161
gopkg.in/inf.v0 v0.9.1 // indirect
162162
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J
206206
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
207207
github.com/onsi/ginkgo/v2 v2.23.3 h1:edHxnszytJ4lD9D5Jjc4tiDkPBZ3siDeJJkUZJJVkp0=
208208
github.com/onsi/ginkgo/v2 v2.23.3/go.mod h1:zXTP6xIp3U8aVuXN8ENK9IXRaTjFnpVB9mGmaSRvxnM=
209-
github.com/onsi/gomega v1.36.3 h1:hID7cr8t3Wp26+cYnfcjR6HpJ00fdogN6dqZ1t6IylU=
210-
github.com/onsi/gomega v1.36.3/go.mod h1:8D9+Txp43QWKhM24yyOBEdpkzN8FvJyAwecBgsU4KU0=
209+
github.com/onsi/gomega v1.36.2 h1:koNYke6TVk6ZmnyHrCXba/T/MoLBXFjeC1PtvYgw0A8=
210+
github.com/onsi/gomega v1.36.2/go.mod h1:DdwyADRjrc825LhMEkD76cHR5+pUnjhUN8GlHlRPHzY=
211211
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
212212
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
213213
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
@@ -378,8 +378,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f h1:
378378
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:+2Yz8+CLJbIfL9z73EW45avw8Lmge3xVElCP9zEKi50=
379379
google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg=
380380
google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec=
381-
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
382-
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
381+
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
382+
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
383383
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
384384
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
385385
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

source/cpu/cpu.go

+37
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cpu
1919
import (
2020
"fmt"
2121
"os"
22+
"regexp"
2223
"strconv"
2324
"strings"
2425

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

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

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

0 commit comments

Comments
 (0)