Skip to content

Commit

Permalink
Add CPUInfo parsing for RISCV
Browse files Browse the repository at this point in the history
Currently only 64-bit RISCV is supported (GOARCH=riscv64) by the Go
compiler, but the cpuinfo format would be the same for 32-bit RISCV.

Signed-off-by: Tobias Klauser <[email protected]>
  • Loading branch information
tklauser committed Jul 22, 2020
1 parent 869cfc7 commit 116503e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
40 changes: 40 additions & 0 deletions cpuinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,46 @@ func parseCPUInfoPPC(info []byte) ([]CPUInfo, error) {
return cpuinfo, nil
}

func parseCPUInfoRISCV(info []byte) ([]CPUInfo, error) {
scanner := bufio.NewScanner(bytes.NewReader(info))

firstLine := firstNonEmptyLine(scanner)
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
return nil, errors.New("invalid cpuinfo file: " + firstLine)
}
field := strings.SplitN(firstLine, ": ", 2)
v, err := strconv.ParseUint(field[1], 0, 32)
if err != nil {
return nil, err
}
firstcpu := CPUInfo{Processor: uint(v)}
cpuinfo := []CPUInfo{firstcpu}
i := 0

for scanner.Scan() {
line := scanner.Text()
if !strings.Contains(line, ":") {
continue
}
field := strings.SplitN(line, ": ", 2)
switch strings.TrimSpace(field[0]) {
case "processor":
v, err := strconv.ParseUint(field[1], 0, 32)
if err != nil {
return nil, err
}
i = int(v)
cpuinfo = append(cpuinfo, CPUInfo{}) // start of the next processor
cpuinfo[i].Processor = uint(v)
case "hart":
cpuinfo[i].CoreID = field[1]
case "isa":
cpuinfo[i].ModelName = field[1]
}
}
return cpuinfo, nil
}

func parseCPUInfoDummy(_ []byte) ([]CPUInfo, error) { // nolint:unused,deadcode
return nil, errors.New("not implemented")
}
Expand Down
2 changes: 1 addition & 1 deletion cpuinfo_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// limitations under the License.

// +build linux
// +build !386,!amd64,!arm,!arm64,!mips,!mips64,!mips64le,!mipsle,!ppc64,!ppc64le,!s390x
// +build !386,!amd64,!arm,!arm64,!mips,!mips64,!mips64le,!mipsle,!ppc64,!ppc64le,!riscv64,!s390x

package procfs

Expand Down
28 changes: 28 additions & 0 deletions cpuinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ timebase : 512000000
platform : pSeries
model : IBM,8233-E8B
machine : CHRP IBM,8233-E8B
`

cpuinfoRiscv64 = `
processor : 0
hart : 0
isa : rv64imafdcsu
mmu : sv48
processor : 1
hart : 1
isa : rv64imafdcsu
mmu : sv48
`
)

Expand Down Expand Up @@ -345,3 +357,19 @@ func TestCPUInfoParsePPC(t *testing.T) {
t.Errorf("want cpu mhz %v, have %v", want, have)
}
}

func TestCPUInfoParseRISCV64(t *testing.T) {
cpuinfo, err := parseCPUInfoRISCV([]byte(cpuinfoRiscv64))
if err != nil || cpuinfo == nil {
t.Fatalf("unable to parse ppc cpu info: %v", err)
}
if want, have := 2, len(cpuinfo); want != have {
t.Errorf("want number of processors %v, have %v", want, have)
}
if want, have := "1", cpuinfo[1].CoreID; want != have {
t.Errorf("want CoreId %v, have %v", want, have)
}
if want, have := "rv64imafdcsu", cpuinfo[1].ModelName; want != have {
t.Errorf("want ModelName %v, have %v", want, have)
}
}

0 comments on commit 116503e

Please sign in to comment.