Skip to content

Commit 3ea2a38

Browse files
authored
Merge pull request #1497 from AhmedGrati/feat-add-cpu-socket-number
feat: add cpu socket count in `cpu.topology`
2 parents b47b1a6 + ebb0836 commit 3ea2a38

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

docs/usage/customization-guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ The following features are available for matching:
940940
| | | **`bf.enabled`** | bool | `true` if Intel SST-BF (Intel Speed Select Technology - Base frequency) has been enabled, otherwise does not exist |
941941
| **`cpu.topology`** | attribute | | | CPU topology related features |
942942
| | | **`hardware_multithreading`** | bool | Hardware multithreading, such as Intel HTT, is enabled |
943+
| | | **`socket_count`** | int | Number of CPU Sockets |
943944
| **`cpu.coprocessor`** | attribute | | | CPU Coprocessor related features |
944945
| | | **`nx_gzip`** | bool | Nest Accelerator GZIP support is enabled |
945946
| **`kernel.config`** | attribute | | | Kernel configuration options |

source/cpu/cpu.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222
"strconv"
23+
"strings"
2324

2425
"k8s.io/apimachinery/pkg/util/sets"
2526
"k8s.io/klog/v2"
@@ -276,9 +277,38 @@ func discoverTopology() map[string]string {
276277
features["hardware_multithreading"] = strconv.FormatBool(ht)
277278
}
278279

280+
if socketCount, err := getCPUSocketCount(); err != nil {
281+
klog.ErrorS(err, "failed to get sockets count")
282+
} else {
283+
features["socket_count"] = strconv.FormatInt(socketCount, 10)
284+
}
285+
279286
return features
280287
}
281288

289+
func getCPUSocketCount() (int64, error) {
290+
files, err := os.ReadDir(hostpath.SysfsDir.Path("bus/cpu/devices"))
291+
if err != nil {
292+
return 0, err
293+
}
294+
295+
uniquePhysicalIDs := sets.NewString()
296+
297+
for _, file := range files {
298+
// Try to read physical_package_id from topology
299+
physicalID, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/physical_package_id"))
300+
if err != nil {
301+
return 0, err
302+
}
303+
id := strings.TrimSpace(string(physicalID))
304+
if err != nil {
305+
return 0, err
306+
}
307+
uniquePhysicalIDs.Insert(id)
308+
}
309+
return int64(uniquePhysicalIDs.Len()), nil
310+
}
311+
282312
// Check if any (online) CPUs have thread siblings
283313
func haveThreadSiblings() (bool, error) {
284314

0 commit comments

Comments
 (0)