Skip to content

Commit b72000b

Browse files
authored
Merge pull request #1505 from AhmedGrati/chore-refactor-topology-discover
chore: combine cpu count and thread_siblings functions into discover topology function
2 parents 7cfcd3b + f962698 commit b72000b

File tree

1 file changed

+21
-44
lines changed

1 file changed

+21
-44
lines changed

source/cpu/cpu.go

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -271,67 +271,44 @@ func getCPUModel() map[string]string {
271271
func discoverTopology() map[string]string {
272272
features := make(map[string]string)
273273

274-
if ht, err := haveThreadSiblings(); err != nil {
275-
klog.ErrorS(err, "failed to detect hyper-threading")
276-
} else {
277-
features["hardware_multithreading"] = strconv.FormatBool(ht)
278-
}
279-
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-
286-
return features
287-
}
288-
289-
func getCPUSocketCount() (int64, error) {
290274
files, err := os.ReadDir(hostpath.SysfsDir.Path("bus/cpu/devices"))
291275
if err != nil {
292-
return 0, err
276+
klog.ErrorS(err, "failed to read devices folder")
277+
return features
293278
}
294279

280+
ht := false
295281
uniquePhysicalIDs := sets.NewString()
296282

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-
312-
// Check if any (online) CPUs have thread siblings
313-
func haveThreadSiblings() (bool, error) {
314-
315-
files, err := os.ReadDir(hostpath.SysfsDir.Path("bus/cpu/devices"))
316-
if err != nil {
317-
return false, err
318-
}
319-
320283
for _, file := range files {
321284
// Try to read siblings from topology
322285
siblings, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
323286
if err != nil {
324-
return false, err
287+
klog.ErrorS(err, "error while reading thread_sigblings_list file")
288+
return map[string]string{}
325289
}
326290
for _, char := range siblings {
327291
// If list separator found, we determine that there are multiple siblings
328292
if char == ',' || char == '-' {
329-
return true, nil
293+
ht = true
294+
break
330295
}
331296
}
297+
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+
klog.ErrorS(err, "error while reading physical_package_id file")
302+
return map[string]string{}
303+
}
304+
id := strings.TrimSpace(string(physicalID))
305+
uniquePhysicalIDs.Insert(id)
332306
}
333-
// No siblings were found
334-
return false, nil
307+
308+
features["hardware_multithreading"] = strconv.FormatBool(ht)
309+
features["socket_count"] = strconv.FormatInt(int64(uniquePhysicalIDs.Len()), 10)
310+
311+
return features
335312
}
336313

337314
func (s *cpuSource) initCpuidFilter() {

0 commit comments

Comments
 (0)