Skip to content

Commit

Permalink
add cpuinfo for loongarch64
Browse files Browse the repository at this point in the history
Signed-off-by: zhangwenlong <zhangwenlong@loongson.cn>
  • Loading branch information
zhangwenlong8911 committed May 27, 2021
1 parent 5162bec commit 8a07a2a
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
40 changes: 40 additions & 0 deletions cpuinfo.go
Expand Up @@ -379,6 +379,46 @@ func parseCPUInfoMips(info []byte) ([]CPUInfo, error) {
return cpuinfo, nil
}

func parseCPUInfoLoongarch(info []byte) ([]CPUInfo, error) {
scanner := bufio.NewScanner(bytes.NewReader(info))
// find the first "processor" line
firstLine := firstNonEmptyLine(scanner)
if !strings.HasPrefix(firstLine, "system type") || !strings.Contains(firstLine, ":") {
return nil, errors.New("invalid cpuinfo file: " + firstLine)
}
field := strings.SplitN(firstLine, ": ", 2)
cpuinfo := []CPUInfo{}
systemType := field[1]
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)
cpuinfo[i].VendorID = systemType
case "cpu model":
cpuinfo[i].ModelName = field[1]
case "BogoMIPS":
v, err := strconv.ParseFloat(field[1], 64)
if err != nil {
return nil, err
}
cpuinfo[i].BogoMips = v
}
}
return cpuinfo, nil
}

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

Expand Down
19 changes: 19 additions & 0 deletions cpuinfo_loongarch64.go
@@ -0,0 +1,19 @@
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build linux
// +build loongarch64

package procfs

var parseCPUInfo = parseCPUInfoLoongarch
2 changes: 1 addition & 1 deletion cpuinfo_others.go
Expand Up @@ -12,7 +12,7 @@
// limitations under the License.

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

package procfs

Expand Down
83 changes: 83 additions & 0 deletions cpuinfo_test.go
Expand Up @@ -182,6 +182,77 @@ core : 1
VCED exceptions : not available
VCEI exceptions : not available
`
cpuinfoLoongarch = `
system type : generic-loongson-machine
machine : Unknown
processor : 0
package : 0
core : 0
cpu model : Loongson-64bit
model name : Loongson-3A5000
CPU Revision : 0x10
FPU Revision : 0x00
CPU MHz : 2300.00
BogoMIPS : 4592.64
tlb_entries : 2112
isa : loongarch32 loongarch64
ASEs implemented : cpucfg lamo lsx lasx lvz lbt
kscratch registers : 3
idle instruction : yes
microsecond timers : yes
hardware watchpoint : yes, iwatch count: 8, dwatch count: 8
processor : 1
package : 0
core : 1
cpu model : Loongson-64bit
model name : Loongson-3A5000
CPU Revision : 0x10
FPU Revision : 0x00
CPU MHz : 2300.00
BogoMIPS : 4592.64
tlb_entries : 2112
isa : loongarch32 loongarch64
ASEs implemented : cpucfg lamo lsx lasx lvz lbt
kscratch registers : 3
idle instruction : yes
microsecond timers : yes
hardware watchpoint : yes, iwatch count: 8, dwatch count: 8
processor : 2
package : 0
core : 2
cpu model : Loongson-64bit
model name : Loongson-3A5000
CPU Revision : 0x10
FPU Revision : 0x00
CPU MHz : 2300.00
BogoMIPS : 4592.64
tlb_entries : 2112
isa : loongarch32 loongarch64
ASEs implemented : cpucfg lamo lsx lasx lvz lbt
kscratch registers : 3
idle instruction : yes
microsecond timers : yes
hardware watchpoint : yes, iwatch count: 8, dwatch count: 8
processor : 3
package : 0
core : 3
cpu model : Loongson-64bit
model name : Loongson-3A5000
CPU Revision : 0x10
FPU Revision : 0x00
CPU MHz : 2300.00
BogoMIPS : 4592.64
tlb_entries : 2112
isa : loongarch32 loongarch64
ASEs implemented : cpucfg lamo lsx lasx lvz lbt
kscratch registers : 3
idle instruction : yes
microsecond timers : yes
hardware watchpoint : yes, iwatch count: 8, dwatch count: 8
`

cpuinfoPpc64 = `
Expand Down Expand Up @@ -372,6 +443,18 @@ func TestCPUInfoParseMips(t *testing.T) {
t.Errorf("want ModelName '%v', have '%v'", want, have)
}
}
func TestCPUInfoParseLoongarch(t *testing.T) {
cpuinfo, err := parseCPUInfoLoongarch([]byte(cpuinfoLoongarch))
if err != nil || cpuinfo == nil {
t.Fatalf("unable to parse mips cpu info: %v", err)
}
if want, have := 4, len(cpuinfo); want != have {
t.Errorf("want number of processors %v, have %v", want, have)
}
if want, have := "Loongson-64bit", cpuinfo[1].ModelName; want != have {
t.Errorf("want ModelName '%v', have '%v'", want, have)
}
}

func TestCPUInfoParsePPC(t *testing.T) {
cpuinfo, err := parseCPUInfoPPC([]byte(cpuinfoPpc64))
Expand Down

0 comments on commit 8a07a2a

Please sign in to comment.