Skip to content

Commit

Permalink
Merge pull request #970 from mackerelio/arm-model-name
Browse files Browse the repository at this point in the history
Use shirou/gopsutil for getting CPU info on Linux
  • Loading branch information
Arthur1 authored Mar 1, 2024
2 parents 3e1f795 + 7f379b3 commit a8580d9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 358 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
Expand Down
79 changes: 22 additions & 57 deletions spec/linux/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
package linux

import (
"bufio"
"io"
"os"
"strings"
"fmt"
"strconv"

"github.com/mackerelio/golib/logging"
"github.com/mackerelio/mackerel-client-go"
"github.com/shirou/gopsutil/v3/cpu"
)

// CPUGenerator collects CPU specs
Expand All @@ -19,63 +18,29 @@ type CPUGenerator struct {

var cpuLogger = logging.GetLogger("spec.cpu")

func (g *CPUGenerator) generate(file io.Reader) (any, error) {
scanner := bufio.NewScanner(file)

var results mackerel.CPU
var cur map[string]any
var modelName string

for scanner.Scan() {
line := scanner.Text()
kv := strings.SplitN(line, ":", 2)
if len(kv) < 2 {
continue
}
key := strings.TrimSpace(kv[0])
val := strings.TrimSpace(kv[1])
switch key {
case "processor":
cur = make(map[string]any)
if modelName != "" {
cur["model_name"] = modelName
}
results = append(results, cur)
case "Processor", "system type":
modelName = val
case "vendor_id", "model", "stepping", "physical id", "core id", "model name", "cache size":
cur[strings.Replace(key, " ", "_", -1)] = val
case "cpu family":
cur["family"] = val
case "cpu cores":
cur["cores"] = val
case "cpu MHz":
cur["mhz"] = val
}
}
if err := scanner.Err(); err != nil {
cpuLogger.Errorf("Failed (skip this spec): %s", err)
return nil, err
}

// Old kernels with CONFIG_SMP disabled has no "processor: " line
if len(results) == 0 && modelName != "" {
cur = make(map[string]any)
cur["model_name"] = modelName
results = append(results, cur)
}

return results, nil
}

// Generate cpu specs
func (g *CPUGenerator) Generate() (any, error) {
file, err := os.Open("/proc/cpuinfo")
func (g *CPUGenerator) Generate() (interface{}, error) {
infoStats, err := cpu.Info()
if err != nil {
cpuLogger.Errorf("Failed (skip this spec): %s", err)
return nil, err
}
defer file.Close()
results := make(mackerel.CPU, 0, len(infoStats))
for _, infoStat := range infoStats {
result := map[string]any{
"vendor_id": infoStat.VendorID,
"model": infoStat.Model,
"stepping": strconv.Itoa(int(infoStat.Stepping)),
"physical_id": infoStat.PhysicalID,
"core_id": infoStat.CoreID,
"cache_size": fmt.Sprintf("%d KB", infoStat.CacheSize),
"model_name": infoStat.ModelName,
"family": infoStat.Family,
"cores": strconv.Itoa(int(infoStat.Cores)),
"mhz": strconv.FormatFloat(infoStat.Mhz, 'f', -1, 64),
}
results = append(results, result)
}

return g.generate(file)
return results, nil
}
Loading

0 comments on commit a8580d9

Please sign in to comment.