From ceff1f0abbed5773ed9275802d7a675d1c86266f Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Mon, 20 Dec 2021 16:08:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=95=E7=9D=80=E8=8E=B7=E5=8F=96cpu=20seria?= =?UTF-8?q?l?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- computer/computer.go | 18 +++++++++++ computer/computer_test.go | 14 +++++++++ cpu/cpu.go | 43 ++++++++++++++++++++++++++ example/example.go | 64 +++++++++++++++++++++++++++++++++++++++ go.mod | 13 ++++++++ 5 files changed, 152 insertions(+) create mode 100644 computer/computer.go create mode 100644 computer/computer_test.go create mode 100644 cpu/cpu.go create mode 100644 example/example.go diff --git a/computer/computer.go b/computer/computer.go new file mode 100644 index 0000000..3e06627 --- /dev/null +++ b/computer/computer.go @@ -0,0 +1,18 @@ +package computer + +import ( + "os/exec" + "regexp" +) + +func CpuID() (*string, error) { + cmd := exec.Command("wmic", "cpu", "get", "ProcessorID") + out, err := cmd.CombinedOutput() + if err != nil { + return nil, err + } + reg := regexp.MustCompile(`\s+`) + str := reg.ReplaceAllString(string(out), "") + id := str[11:] + return &id, nil +} diff --git a/computer/computer_test.go b/computer/computer_test.go new file mode 100644 index 0000000..fc1a4fd --- /dev/null +++ b/computer/computer_test.go @@ -0,0 +1,14 @@ +package computer + +import ( + "fmt" + "testing" +) + +func TestCpuID(t *testing.T) { + id, err := CpuID() + if err != nil { + t.Fatal(err) + } + fmt.Println(id) +} diff --git a/cpu/cpu.go b/cpu/cpu.go new file mode 100644 index 0000000..5085c9c --- /dev/null +++ b/cpu/cpu.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "math" + "strings" + + "github.com/jaypipes/ghw" +) + +func main() { + cpu, err := ghw.CPU() + if err != nil { + fmt.Printf("Error getting CPU info: %v", err) + } + + fmt.Printf("%v\n", cpu) + + for _, proc := range cpu.Processors { + fmt.Printf(" %v\n", proc) + for _, core := range proc.Cores { + fmt.Printf(" %v\n", core) + } + if len(proc.Capabilities) > 0 { + // pretty-print the (large) block of capability strings into rows + // of 6 capability strings + rows := int(math.Ceil(float64(len(proc.Capabilities)) / float64(6))) + for row := 1; row < rows; row = row + 1 { + rowStart := (row * 6) - 1 + rowEnd := int(math.Min(float64(rowStart+6), float64(len(proc.Capabilities)))) + rowElems := proc.Capabilities[rowStart:rowEnd] + capStr := strings.Join(rowElems, " ") + if row == 1 { + fmt.Printf(" capabilities: [%s\n", capStr) + } else if rowEnd < len(proc.Capabilities) { + fmt.Printf(" %s\n", capStr) + } else { + fmt.Printf(" %s]\n", capStr) + } + } + } + } +} diff --git a/example/example.go b/example/example.go new file mode 100644 index 0000000..906cdee --- /dev/null +++ b/example/example.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + + "github.com/intel-go/cpuid" +) + +func main() { + fmt.Printf("VendorString: %s\n", cpuid.VendorIdentificatorString) + fmt.Printf("ProcessorBrandString: %s\n", cpuid.ProcessorBrandString) + fmt.Printf("SteppingId: %d\n", cpuid.SteppingId) + fmt.Printf("ProcessorType: %d\n", cpuid.ProcessorType) + fmt.Printf("DisplayFamily: %d\n", cpuid.DisplayFamily) + fmt.Printf("DisplayModel: %d\n", cpuid.DisplayModel) + fmt.Printf("CacheLineSize: %d\n", cpuid.CacheLineSize) + fmt.Printf("MaxLogicalCPUId:%d\n", cpuid.MaxLogicalCPUId) + fmt.Printf("InitialAPICId: %d\n", cpuid.InitialAPICId) + fmt.Printf("Smallest monitor-line size in bytes: %d\n", cpuid.MonLineSizeMin) + fmt.Printf("Largest monitor-line size in bytes: %d\n", cpuid.MonLineSizeMax) + fmt.Printf("Monitor Interrupt break-event is supported: %v\n", cpuid.MonitorIBE) + fmt.Printf("MONITOR/MWAIT extensions are supported: %v\n", cpuid.MonitorEMX) + fmt.Printf("AVX state: %v\n", cpuid.EnabledAVX) + fmt.Printf("AVX-512 state: %v\n", cpuid.EnabledAVX512) + fmt.Printf("Interrupt thresholds in digital thermal sensor: %v\n", cpuid.ThermalSensorInterruptThresholds) + + fmt.Printf("Features: ") + for i := uint64(0); i < 64; i++ { + if cpuid.HasFeature(1 << i) { + fmt.Printf("%s ", cpuid.FeatureNames[1<