Skip to content

Commit

Permalink
use shirou/gopsutil for get cpuinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur1 committed Feb 21, 2024
1 parent f41be78 commit 988a5b2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/Songmu/timeout v0.4.0
github.com/agext/levenshtein v1.2.3
github.com/fatih/color v1.16.0
github.com/google/go-cmp v0.6.0
github.com/mackerelio/go-osstat v0.2.4
github.com/mackerelio/golib v1.2.1
github.com/mackerelio/mackerel-client-go v0.29.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
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
31 changes: 30 additions & 1 deletion spec/linux/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ package linux

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

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

// CPUGenerator collects CPU specs
Expand Down Expand Up @@ -70,12 +74,37 @@ func (g *CPUGenerator) generate(file io.Reader) (interface{}, error) {

// Generate cpu specs
func (g *CPUGenerator) Generate() (interface{}, error) {
infoStats, err := cpu.Info()
if err != nil {
cpuLogger.Errorf("Failed (skip this spec): %s", err)
return nil, err
}
results := make(mackerel.CPU, 0, len(infoStats))
for _, infoStat := range infoStats {
result := map[string]any{
"vendor_id": infoStat.VendorID,
"model": infoStat.Model,
"stepping": 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": infoStat.Cores,
"mhz": strconv.FormatFloat(infoStat.Mhz, 'f', -1, 64),
}
results = append(results, result)
}

// for debug
file, err := os.Open("/proc/cpuinfo")
if err != nil {
cpuLogger.Errorf("Failed (skip this spec): %s", err)
return nil, err
}
defer file.Close()
results2, _ := g.generate(file)
cpuLogger.Infof(cmp.Diff(results, results2))

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

0 comments on commit 988a5b2

Please sign in to comment.