Skip to content

Commit 8b2c66b

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 135bff6 commit 8b2c66b

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
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 := -1
@@ -233,8 +233,8 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) {
233233
return cpuinfo, nil
234234
}
235235

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

239239
firstLine := firstNonEmptyLine(scanner)
240240
if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") {
@@ -301,8 +301,8 @@ func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
301301
return cpuinfo, nil
302302
}
303303

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

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

cpuinfo_test.go

Lines changed: 8 additions & 5 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
cpuinfoArm7 = `Processor : ARMv7 Processor rev 5 (v7l)
@@ -160,7 +163,7 @@ func TestCPUInfoX86(t *testing.T) {
160163
}
161164

162165
func TestCPUInfoParseARM(t *testing.T) {
163-
cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7))
166+
cpuinfo, err := parseCPUInfoARM(strings.NewReader(cpuinfoArm7))
164167
if err != nil || cpuinfo == nil {
165168
t.Fatalf("unable to parse arm cpu info: %v", err)
166169
}
@@ -176,7 +179,7 @@ func TestCPUInfoParseARM(t *testing.T) {
176179
}
177180

178181
func TestCPUInfoParseARMNew(t *testing.T) {
179-
cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7New))
182+
cpuinfo, err := parseCPUInfoARM(strings.NewReader(cpuinfoArm7New))
180183
if err != nil || cpuinfo == nil {
181184
t.Fatalf("unable to parse arm cpu info: %v", err)
182185
}
@@ -191,7 +194,7 @@ func TestCPUInfoParseARMNew(t *testing.T) {
191194
}
192195
}
193196
func TestCPUInfoParseS390X(t *testing.T) {
194-
cpuinfo, err := parseCPUInfoS390X([]byte(cpuinfoS390x))
197+
cpuinfo, err := parseCPUInfoS390X(strings.NewReader(cpuinfoS390x))
195198
if err != nil || cpuinfo == nil {
196199
t.Fatalf("unable to parse s390x cpu info: %v", err)
197200
}
@@ -210,7 +213,7 @@ func TestCPUInfoParseS390X(t *testing.T) {
210213
}
211214

212215
func TestCPUInfoParsePPC(t *testing.T) {
213-
cpuinfo, err := parseCPUInfoPPC([]byte(cpuinfoPpc64))
216+
cpuinfo, err := parseCPUInfoPPC(strings.NewReader(cpuinfoPpc64))
214217
if err != nil || cpuinfo == nil {
215218
t.Fatalf("unable to parse ppc cpu info: %v", err)
216219
}

0 commit comments

Comments
 (0)