From ae44b7f82613738c9843a35c6e139cf53a9f61a4 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Wed, 6 Sep 2023 10:03:33 +0200 Subject: [PATCH] Read file line by line --- pkg/ccTopology/ccTopology.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/ccTopology/ccTopology.go b/pkg/ccTopology/ccTopology.go index 97ca9d1..4da5774 100644 --- a/pkg/ccTopology/ccTopology.go +++ b/pkg/ccTopology/ccTopology.go @@ -1,6 +1,7 @@ package ccTopology import ( + "bufio" "fmt" "log" "os" @@ -20,6 +21,7 @@ const ( ) // fileToInt reads an integer value from a file +// In case of an error -1 is returned // Used internally for sysfs file reads func fileToInt(path string) int { buffer, err := os.ReadFile(path) @@ -39,23 +41,27 @@ func fileToInt(path string) int { // Get list of CPU socket IDs func SocketList() []int { - buffer, err := os.ReadFile(string(PROCFS_CPUINFO)) + + packs := make([]int, 0) + + file, err := os.Open(string(PROCFS_CPUINFO)) if err != nil { log.Print(err) return nil } - ll := strings.Split(string(buffer), "\n") - packs := make([]int, 0) - for _, line := range ll { + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() if strings.HasPrefix(line, "physical id") { lv := strings.Fields(line) id, err := strconv.ParseInt(lv[3], 10, 32) if err != nil { log.Print(err) - return packs + return nil } - found := slices.Contains(packs, int(id)) - if !found { + if found := slices.Contains(packs, int(id)); !found { packs = append(packs, int(id)) } }