mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-05 08:17:04 +00:00
chore: combine cpu count and thread_siblings functions into discover topology function
Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>
This commit is contained in:
parent
7ae25167fe
commit
f962698c14
1 changed files with 21 additions and 44 deletions
|
@ -271,67 +271,44 @@ func getCPUModel() map[string]string {
|
||||||
func discoverTopology() map[string]string {
|
func discoverTopology() map[string]string {
|
||||||
features := make(map[string]string)
|
features := make(map[string]string)
|
||||||
|
|
||||||
if ht, err := haveThreadSiblings(); err != nil {
|
files, err := os.ReadDir(hostpath.SysfsDir.Path("bus/cpu/devices"))
|
||||||
klog.ErrorS(err, "failed to detect hyper-threading")
|
if err != nil {
|
||||||
} else {
|
klog.ErrorS(err, "failed to read devices folder")
|
||||||
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
|
return features
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCPUSocketCount() (int64, error) {
|
ht := false
|
||||||
files, err := os.ReadDir(hostpath.SysfsDir.Path("bus/cpu/devices"))
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
uniquePhysicalIDs := sets.NewString()
|
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) {
|
|
||||||
|
|
||||||
files, err := os.ReadDir(hostpath.SysfsDir.Path("bus/cpu/devices"))
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
// Try to read siblings from topology
|
// Try to read siblings from topology
|
||||||
siblings, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
|
siblings, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
klog.ErrorS(err, "error while reading thread_sigblings_list file")
|
||||||
|
return map[string]string{}
|
||||||
}
|
}
|
||||||
for _, char := range siblings {
|
for _, char := range siblings {
|
||||||
// If list separator found, we determine that there are multiple siblings
|
// If list separator found, we determine that there are multiple siblings
|
||||||
if char == ',' || char == '-' {
|
if char == ',' || char == '-' {
|
||||||
return true, nil
|
ht = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
klog.ErrorS(err, "error while reading physical_package_id file")
|
||||||
|
return map[string]string{}
|
||||||
}
|
}
|
||||||
// No siblings were found
|
id := strings.TrimSpace(string(physicalID))
|
||||||
return false, nil
|
uniquePhysicalIDs.Insert(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
features["hardware_multithreading"] = strconv.FormatBool(ht)
|
||||||
|
features["socket_count"] = strconv.FormatInt(int64(uniquePhysicalIDs.Len()), 10)
|
||||||
|
|
||||||
|
return features
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *cpuSource) initCpuidFilter() {
|
func (s *cpuSource) initCpuidFilter() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue