Skip to content

Commit

Permalink
chore: combine cpu count and thread_siblings functions into discover …
Browse files Browse the repository at this point in the history
…topology function

Signed-off-by: AhmedGrati <[email protected]>
  • Loading branch information
TessaIO committed Dec 18, 2023
1 parent 7ae2516 commit f962698
Showing 1 changed file with 21 additions and 44 deletions.
65 changes: 21 additions & 44 deletions source/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,67 +271,44 @@ func getCPUModel() map[string]string {
func discoverTopology() map[string]string {
features := make(map[string]string)

if ht, err := haveThreadSiblings(); err != nil {
klog.ErrorS(err, "failed to detect hyper-threading")
} else {
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
klog.ErrorS(err, "failed to read devices folder")
return features

Check warning on line 277 in source/cpu/cpu.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/cpu.go#L276-L277

Added lines #L276 - L277 were not covered by tests
}

ht := false

Check warning on line 280 in source/cpu/cpu.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/cpu.go#L280

Added line #L280 was not covered by tests
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 {
// Try to read siblings from topology
siblings, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
if err != nil {
return false, err
klog.ErrorS(err, "error while reading thread_sigblings_list file")
return map[string]string{}

Check warning on line 288 in source/cpu/cpu.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/cpu.go#L287-L288

Added lines #L287 - L288 were not covered by tests
}
for _, char := range siblings {
// If list separator found, we determine that there are multiple siblings
if char == ',' || char == '-' {
return true, nil
ht = true
break

Check warning on line 294 in source/cpu/cpu.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/cpu.go#L293-L294

Added lines #L293 - L294 were not covered by tests
}
}

// 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{}
}
id := strings.TrimSpace(string(physicalID))
uniquePhysicalIDs.Insert(id)

Check warning on line 305 in source/cpu/cpu.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/cpu.go#L299-L305

Added lines #L299 - L305 were not covered by tests
}
// No siblings were found
return false, nil

features["hardware_multithreading"] = strconv.FormatBool(ht)
features["socket_count"] = strconv.FormatInt(int64(uniquePhysicalIDs.Len()), 10)

return features

Check warning on line 311 in source/cpu/cpu.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/cpu.go#L308-L311

Added lines #L308 - L311 were not covered by tests
}

func (s *cpuSource) initCpuidFilter() {
Expand Down

0 comments on commit f962698

Please sign in to comment.