Read file line by line

This commit is contained in:
Holger Obermaier 2023-09-06 10:15:17 +02:00
parent 34436ac261
commit 99ccc04933

View File

@ -68,7 +68,7 @@ func SocketList() []int {
return packs return packs
} }
// Get list of hardware thread IDs in the order of listing in /proc/cpuinfo // HwthreadList gets the list of hardware thread IDs in the order of listing in /proc/cpuinfo
func HwthreadList() []int { func HwthreadList() []int {
file, err := os.Open(string(PROCFS_CPUINFO)) file, err := os.Open(string(PROCFS_CPUINFO))
if err != nil { if err != nil {
@ -77,7 +77,7 @@ func HwthreadList() []int {
} }
defer file.Close() defer file.Close()
cpulist := make([]int, 0) cpuList := make([]int, 0)
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
@ -88,12 +88,12 @@ func HwthreadList() []int {
log.Print(err) log.Print(err)
return nil return nil
} }
if found := slices.Contains(cpulist, int(id)); !found { if found := slices.Contains(cpuList, int(id)); !found {
cpulist = append(cpulist, int(id)) cpuList = append(cpuList, int(id))
} }
} }
} }
return cpulist return cpuList
} }
// Get list of hardware thread IDs in the order of listing in /proc/cpuinfo // Get list of hardware thread IDs in the order of listing in /proc/cpuinfo
@ -102,30 +102,32 @@ func CpuList() []int {
return HwthreadList() return HwthreadList()
} }
// Get list of CPU core IDs in the order of listing in /proc/cpuinfo // CoreList gets the list of CPU core IDs in the order of listing in /proc/cpuinfo
func CoreList() []int { func CoreList() []int {
buffer, err := os.ReadFile(string(PROCFS_CPUINFO)) file, err := os.Open(string(PROCFS_CPUINFO))
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return nil return nil
} }
ll := strings.Split(string(buffer), "\n") defer file.Close()
corelist := make([]int, 0)
for _, line := range ll { coreList := make([]int, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "core id") { if strings.HasPrefix(line, "core id") {
lv := strings.Fields(line) lv := strings.Fields(line)
id, err := strconv.ParseInt(lv[3], 10, 32) id, err := strconv.ParseInt(lv[3], 10, 32)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return corelist return nil
} }
found := slices.Contains(corelist, int(id)) if found := slices.Contains(coreList, int(id)); !found {
if !found { coreList = append(coreList, int(id))
corelist = append(corelist, int(id))
} }
} }
} }
return corelist return coreList
} }
// Get list of NUMA node IDs // Get list of NUMA node IDs