Skip to content

Commit a175916

Browse files
committed
cpuinfo: read data in place
Instead of reading the data to the buffer, then making a reader and a scanner out of that buffer, let's analyze the data as we read it line by line. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent c2b7b68 commit a175916

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

cpuinfo.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ package procfs
1717

1818
import (
1919
"bufio"
20-
"bytes"
2120
"errors"
21+
"io"
22+
"os"
2223
"regexp"
2324
"strconv"
2425
"strings"
25-
26-
"github.com/prometheus/procfs/internal/util"
2726
)
2827

2928
// CPUInfo contains general information about a system CPU found in /proc/cpuinfo
@@ -64,15 +63,16 @@ var (
6463
// CPUInfo returns information about current system CPUs.
6564
// See https://www.kernel.org/doc/Documentation/filesystems/proc.txt
6665
func (fs FS) CPUInfo() ([]CPUInfo, error) {
67-
data, err := util.ReadFileNoStat(fs.proc.Path("cpuinfo"))
66+
file, err := os.Open(fs.proc.Path("cpuinfo"))
6867
if err != nil {
6968
return nil, err
7069
}
71-
return parseCPUInfo(data)
70+
defer file.Close()
71+
return parseCPUInfo(file)
7272
}
7373

74-
func parseCPUInfoX86(info []byte) ([]CPUInfo, error) {
75-
scanner := bufio.NewScanner(bytes.NewReader(info))
74+
func parseCPUInfoX86(info io.Reader) ([]CPUInfo, error) {
75+
scanner := bufio.NewScanner(info)
7676

7777
// find the first "processor" line
7878
firstLine := firstNonEmptyLine(scanner)
@@ -186,8 +186,8 @@ func parseCPUInfoX86(info []byte) ([]CPUInfo, error) {
186186
return cpuinfo, nil
187187
}
188188

189-
func parseCPUInfoARM(info []byte) ([]CPUInfo, error) {
190-
scanner := bufio.NewScanner(bytes.NewReader(info))
189+
func parseCPUInfoARM(info io.Reader) ([]CPUInfo, error) {
190+
scanner := bufio.NewScanner(info)
191191

192192
cpuinfo := []CPUInfo{{}}
193193
i := 0
@@ -236,8 +236,8 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) {
236236
return cpuinfo, nil
237237
}
238238

239-
func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
240-
scanner := bufio.NewScanner(bytes.NewReader(info))
239+
func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) {
240+
scanner := bufio.NewScanner(info)
241241

242242
firstLine := firstNonEmptyLine(scanner)
243243
if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") {
@@ -304,8 +304,8 @@ func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
304304
return cpuinfo, nil
305305
}
306306

307-
func parseCPUInfoPPC(info []byte) ([]CPUInfo, error) {
308-
scanner := bufio.NewScanner(bytes.NewReader(info))
307+
func parseCPUInfoPPC(info io.Reader) ([]CPUInfo, error) {
308+
scanner := bufio.NewScanner(info)
309309

310310
firstLine := firstNonEmptyLine(scanner)
311311
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {

cpuinfo_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
package procfs
1717

18-
import "testing"
18+
import (
19+
"strings"
20+
"testing"
21+
)
1922

2023
const (
2124
// non-SMP system before kernel v3.8, commit
@@ -177,7 +180,7 @@ func TestCPUInfoX86(t *testing.T) {
177180
}
178181

179182
func TestCPUInfoParseARM6(t *testing.T) {
180-
cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm6))
183+
cpuinfo, err := parseCPUInfoARM(strings.NewReader(cpuinfoArm6))
181184
if err != nil || cpuinfo == nil {
182185
t.Fatalf("unable to parse arm cpu info: %v", err)
183186
}
@@ -192,7 +195,7 @@ func TestCPUInfoParseARM6(t *testing.T) {
192195
}
193196
}
194197
func TestCPUInfoParseARM7old(t *testing.T) {
195-
cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7old))
198+
cpuinfo, err := parseCPUInfoARM(strings.NewReader(cpuinfoArm7old))
196199
if err != nil || cpuinfo == nil {
197200
t.Fatalf("unable to parse arm cpu info: %v", err)
198201
}
@@ -208,7 +211,7 @@ func TestCPUInfoParseARM7old(t *testing.T) {
208211
}
209212

210213
func TestCPUInfoParseARM(t *testing.T) {
211-
cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7))
214+
cpuinfo, err := parseCPUInfoARM(strings.NewReader(cpuinfoArm7))
212215
if err != nil || cpuinfo == nil {
213216
t.Fatalf("unable to parse arm cpu info: %v", err)
214217
}
@@ -223,7 +226,7 @@ func TestCPUInfoParseARM(t *testing.T) {
223226
}
224227
}
225228
func TestCPUInfoParseS390X(t *testing.T) {
226-
cpuinfo, err := parseCPUInfoS390X([]byte(cpuinfoS390x))
229+
cpuinfo, err := parseCPUInfoS390X(strings.NewReader(cpuinfoS390x))
227230
if err != nil || cpuinfo == nil {
228231
t.Fatalf("unable to parse s390x cpu info: %v", err)
229232
}
@@ -242,7 +245,7 @@ func TestCPUInfoParseS390X(t *testing.T) {
242245
}
243246

244247
func TestCPUInfoParsePPC(t *testing.T) {
245-
cpuinfo, err := parseCPUInfoPPC([]byte(cpuinfoPpc64))
248+
cpuinfo, err := parseCPUInfoPPC(strings.NewReader(cpuinfoPpc64))
246249
if err != nil || cpuinfo == nil {
247250
t.Fatalf("unable to parse ppc cpu info: %v", err)
248251
}

0 commit comments

Comments
 (0)