Fix function getNumaDomain, it always returned 0

This commit is contained in:
Holger Obermaier 2023-09-06 11:35:45 +02:00
parent 6a2b74b0dc
commit 4b16ca4a30

View File

@ -218,69 +218,54 @@ type HwthreadEntry struct {
func CpuData() []HwthreadEntry { func CpuData() []HwthreadEntry {
// fileToInt := func(path string) int { getCore :=
// buffer, err := os.ReadFile(path) func(basePath string) int {
// if err != nil { return fileToInt(fmt.Sprintf("%s/core_id", basePath))
// log.Print(err)
// //cclogger.ComponentError("ccTopology", "Reading", path, ":", err.Error())
// return -1
// }
// sbuffer := strings.Replace(string(buffer), "\n", "", -1)
// var id int64
// //_, err = fmt.Scanf("%d", sbuffer, &id)
// id, err = strconv.ParseInt(sbuffer, 10, 32)
// if err != nil {
// cclogger.ComponentError("ccTopology", "Parsing", path, ":", sbuffer, err.Error())
// return -1
// }
// return int(id)
// }
getCore := func(basepath string) int {
return fileToInt(fmt.Sprintf("%s/core_id", basepath))
}
getSocket := func(basepath string) int {
return fileToInt(fmt.Sprintf("%s/physical_package_id", basepath))
}
getDie := func(basepath string) int {
return fileToInt(fmt.Sprintf("%s/die_id", basepath))
}
getSMT := func(cpuid int, basepath string) int {
buffer, err := os.ReadFile(fmt.Sprintf("%s/thread_siblings_list", basepath))
if err != nil {
cclogger.ComponentError("CCTopology", "CpuData:getSMT", err.Error())
} }
threadlist := make([]int, 0)
sbuffer := strings.Replace(string(buffer), "\n", "", -1) getSocket :=
for _, x := range strings.Split(sbuffer, ",") { func(basePath string) int {
id, err := strconv.ParseInt(x, 10, 32) return fileToInt(fmt.Sprintf("%s/physical_package_id", basePath))
}
getDie :=
func(basePath string) int {
return fileToInt(fmt.Sprintf("%s/die_id", basePath))
}
getSMT :=
func(cpuID int, basePath string) int {
buffer, err := os.ReadFile(fmt.Sprintf("%s/thread_siblings_list", basePath))
if err != nil { if err != nil {
cclogger.ComponentError("CCTopology", "CpuData:getSMT", err.Error()) cclogger.ComponentError("CCTopology", "CpuData:getSMT", err.Error())
} }
threadlist = append(threadlist, int(id)) threadList := make([]int, 0)
} stringBuffer := strings.TrimSpace(string(buffer))
for i, x := range threadlist { for _, x := range strings.Split(stringBuffer, ",") {
if x == cpuid { id, err := strconv.ParseInt(x, 10, 32)
if err != nil {
cclogger.ComponentError("CCTopology", "CpuData:getSMT", err.Error())
}
threadList = append(threadList, int(id))
}
if i := slices.Index(threadList, cpuID); i != -1 {
return i return i
} }
return 1
} }
return 1
}
getNumaDomain := func(basepath string) int { getNumaDomain :=
globPath := filepath.Join(basepath, "node*") func(basePath string) int {
regexPath := filepath.Join(basepath, "node(\\d+)") globPath := filepath.Join(basePath, "node*")
regex := regexp.MustCompile(regexPath) regexPath := filepath.Join(basePath, "node([[:digit:]]+)")
files, err := filepath.Glob(globPath) regex := regexp.MustCompile(regexPath)
if err != nil { files, err := filepath.Glob(globPath)
cclogger.ComponentError("CCTopology", "CpuData:getNumaDomain", err.Error()) if err != nil {
} cclogger.ComponentError("CCTopology", "CpuData:getNumaDomain", err.Error())
for _, f := range files { }
finfo, err := os.Lstat(f) for _, file := range files {
if err == nil && finfo.IsDir() { matches := regex.FindStringSubmatch(file)
matches := regex.FindStringSubmatch(f) fmt.Println(len(matches))
if len(matches) == 2 { if len(matches) == 2 {
id, err := strconv.Atoi(matches[1]) id, err := strconv.Atoi(matches[1])
if err == nil { if err == nil {
@ -288,46 +273,47 @@ func CpuData() []HwthreadEntry {
} }
} }
} }
return 0
} }
return 0
}
clist := make([]HwthreadEntry, 0) cList := make([]HwthreadEntry, 0)
for _, c := range HwthreadList() { for _, c := range HwthreadList() {
clist = append(clist, HwthreadEntry{CpuID: c}) cList = append(cList,
HwthreadEntry{
CpuID: c,
Socket: -1,
NumaDomain: -1,
Die: -1,
Core: -1,
})
} }
for i, centry := range clist { for i := range cList {
centry.Socket = -1 cEntry := &cList[i]
centry.NumaDomain = -1
centry.Die = -1
centry.Core = -1
// Set base directory for topology lookup // Set base directory for topology lookup
cpustr := fmt.Sprintf("cpu%d", centry.CpuID) cpuStr := fmt.Sprintf("cpu%d", cEntry.CpuID)
base := filepath.Join("/sys/devices/system/cpu", cpustr) base := filepath.Join("/sys/devices/system/cpu", cpuStr)
topoBase := filepath.Join(base, "topology") topoBase := filepath.Join(base, "topology")
// Lookup CPU core id // Lookup CPU core id
centry.Core = getCore(topoBase) cEntry.Core = getCore(topoBase)
// Lookup CPU socket id // Lookup CPU socket id
centry.Socket = getSocket(topoBase) cEntry.Socket = getSocket(topoBase)
// Lookup CPU die id // Lookup CPU die id
centry.Die = getDie(topoBase) cEntry.Die = getDie(topoBase)
if centry.Die < 0 { if cEntry.Die < 0 {
centry.Die = centry.Socket cEntry.Die = cEntry.Socket
} }
// Lookup SMT thread id // Lookup SMT thread id
centry.SMT = getSMT(centry.CpuID, topoBase) cEntry.SMT = getSMT(cEntry.CpuID, topoBase)
// Lookup NUMA domain id // Lookup NUMA domain id
centry.NumaDomain = getNumaDomain(base) cEntry.NumaDomain = getNumaDomain(base)
// Update values in output list
clist[i] = centry
} }
return clist return cList
} }
// Structure holding basic information about a CPU // Structure holding basic information about a CPU