From 34436ac26153c62d8b231246f645c4f6ce999bfc Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Wed, 6 Sep 2023 10:09:53 +0200 Subject: [PATCH] Read file line by line --- pkg/ccTopology/ccTopology.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pkg/ccTopology/ccTopology.go b/pkg/ccTopology/ccTopology.go index 4da5774..63ae9b0 100644 --- a/pkg/ccTopology/ccTopology.go +++ b/pkg/ccTopology/ccTopology.go @@ -39,11 +39,9 @@ func fileToInt(path string) int { return int(id) } -// Get list of CPU socket IDs +// SocketList gets the list of CPU socket IDs func SocketList() []int { - packs := make([]int, 0) - file, err := os.Open(string(PROCFS_CPUINFO)) if err != nil { log.Print(err) @@ -51,6 +49,7 @@ func SocketList() []int { } defer file.Close() + packs := make([]int, 0) scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() @@ -71,23 +70,25 @@ func SocketList() []int { // Get list of hardware thread IDs in the order of listing in /proc/cpuinfo func HwthreadList() []int { - buffer, err := os.ReadFile(string(PROCFS_CPUINFO)) + file, err := os.Open(string(PROCFS_CPUINFO)) if err != nil { log.Print(err) return nil } - ll := strings.Split(string(buffer), "\n") + defer file.Close() + cpulist := make([]int, 0) - for _, line := range ll { + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() if strings.HasPrefix(line, "processor") { lv := strings.Fields(line) id, err := strconv.ParseInt(lv[2], 10, 32) if err != nil { log.Print(err) - return cpulist + return nil } - found := slices.Contains(cpulist, int(id)) - if !found { + if found := slices.Contains(cpulist, int(id)); !found { cpulist = append(cpulist, int(id)) } }