diff --git a/docs/usage/customization-guide.md b/docs/usage/customization-guide.md index 8f3652052..f28d8435c 100644 --- a/docs/usage/customization-guide.md +++ b/docs/usage/customization-guide.md @@ -940,6 +940,7 @@ The following features are available for matching: | | | **`bf.enabled`** | bool | `true` if Intel SST-BF (Intel Speed Select Technology - Base frequency) has been enabled, otherwise does not exist | | **`cpu.topology`** | attribute | | | CPU topology related features | | | | **`hardware_multithreading`** | bool | Hardware multithreading, such as Intel HTT, is enabled | +| | | **`socket_count`** | int | Number of CPU Sockets | | **`cpu.coprocessor`** | attribute | | | CPU Coprocessor related features | | | | **`nx_gzip`** | bool | Nest Accelerator GZIP support is enabled | | **`kernel.config`** | attribute | | | Kernel configuration options | diff --git a/source/cpu/cpu.go b/source/cpu/cpu.go index 7b10d9349..edb35dff6 100644 --- a/source/cpu/cpu.go +++ b/source/cpu/cpu.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "strconv" + "strings" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/klog/v2" @@ -276,9 +277,38 @@ func discoverTopology() map[string]string { features["hardware_multithreading"] = strconv.FormatBool(ht) } + if socketCount, err := getCPUSocketCount(); err != nil { + klog.ErrorS(err, "failed to get sockets count") + } else { + features["socket_count"] = strconv.FormatInt(socketCount, 10) + } + return features } +func getCPUSocketCount() (int64, error) { + files, err := os.ReadDir(hostpath.SysfsDir.Path("bus/cpu/devices")) + if err != nil { + return 0, err + } + + uniquePhysicalIDs := sets.NewString() + + for _, file := range files { + // Try to read physical_package_id from topology + physicalID, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/physical_package_id")) + if err != nil { + return 0, err + } + id := strings.TrimSpace(string(physicalID)) + if err != nil { + return 0, err + } + uniquePhysicalIDs.Insert(id) + } + return int64(uniquePhysicalIDs.Len()), nil +} + // Check if any (online) CPUs have thread siblings func haveThreadSiblings() (bool, error) {