From 65b27688b98c471a6e753703ca16c3325d20ef80 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Fri, 18 Mar 2022 08:29:57 -0700 Subject: [PATCH] Add CPU level test (#96) Example: `cpuid -check-level=4` --- cmd/cpuid/main.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/cpuid/main.go b/cmd/cpuid/main.go index abd5c5e..5869a4b 100644 --- a/cmd/cpuid/main.go +++ b/cmd/cpuid/main.go @@ -14,6 +14,7 @@ import ( "encoding/json" "flag" "fmt" + "log" "os" "strings" @@ -21,9 +22,22 @@ import ( ) var js = flag.Bool("json", false, "Output as JSON") +var level = flag.Int("check-level", 0, "Check microarchitecture level. Exit code will be 0 if supported") func main() { flag.Parse() + if level != nil && *level > 0 { + if *level < 1 || *level > 4 { + log.Fatalln("Supply CPU level 1-4 to test as argument") + } + log.Println(cpuid.CPU.BrandName) + if cpuid.CPU.X64Level() < *level { + // Does os.Exit(1) + log.Fatalf("Microarchitecture level %d not supported. Max level is %d.", *level, cpuid.CPU.X64Level()) + } + log.Printf("Microarchitecture level %d is supported. Max level is %d.", *level, cpuid.CPU.X64Level()) + os.Exit(0) + } if *js { info := struct { cpuid.CPUInfo