Skip to content

Commit

Permalink
fix: Gather CPU information
Browse files Browse the repository at this point in the history
  • Loading branch information
kichik committed Jan 4, 2022
1 parent 052c08d commit efff51c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Cloud-Z gathers information and perform benchmarks on cloud instances in multiple cloud providers.

- [x] Cloud type, instance id, and type
- [ ] CPU information including type, number of available cores, and cache sizes
- [x] CPU information including type, number of available cores, and cache sizes
- [ ] RAM information
- [ ] Benchmark CPU
- [ ] Storage devices information
- [ ] Benchmark storage
Expand Down
27 changes: 18 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,43 @@ var rootCmd = &cobra.Command{
Short: "Cloud-Z gathers information on cloud instances",
Version: fmt.Sprintf("%s, commit %s, built at %s by %s", version, commit, date, builtBy),
Run: func(cmd *cobra.Command, args []string) {
allProviders := []providers.Provider{
allCloudProviders := []providers.CloudProvider{
&providers.AwsProvider{},
&providers.GcpProvider{},
&providers.AzureProvider{},
}

for _, provider := range allProviders {
detectedCloud := false
for _, provider := range allCloudProviders {
// TODO detect faster with goroutines?
if provider.Detect() {
data, err := provider.GetData()
if err != nil {
log.Fatalln(err)
}

table := tablewriter.NewWriter(os.Stdout)
for _, v := range data {
table.Append(v)
}
table.Render()
printTable(data)

return
detectedCloud = true
}
}

println("Unable to detect cloud provider")
if !detectedCloud {
println("Unable to detect cloud provider")
}

printTable(providers.GetCPUInfo())
},
}

func printTable(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
for _, v := range data {
table.Append(v)
}
table.Render()
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module cloud-z
go 1.17

require (
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743
github.com/klauspost/cpuid/v2 v2.0.9
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.3.0
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743 h1:X3Xxno5Ji8idrNiUoFc7QyXpqhSYlDRYQmc7mlpMBzU=
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743/go.mod h1:KrtyD5PFj++GKkFS/7/RRrfnRhAMGQwy75GLCHWrCNs=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand All @@ -233,6 +235,8 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
Expand Down
33 changes: 33 additions & 0 deletions providers/cpuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package providers

import (
"fmt"
"github.com/inhies/go-bytesize"
"github.com/klauspost/cpuid/v2"
"strings"
)

func bytes(b int) string {
//return bytesize.New(float64(b)).Format("%d", "", false)
return bytesize.New(float64(b)).String()
}

func GetCPUInfo() [][]string {
return [][]string{
{"CPU", cpuid.CPU.BrandName},
{"Vendor", cpuid.CPU.VendorString},
{"Vendor ID", fmt.Sprintf("%v", cpuid.CPU.VendorID.String())},
{"Family", fmt.Sprintf("%v", cpuid.CPU.Family)},
{"MHz", fmt.Sprintf("%v", cpuid.CPU.Hz/1_000_000)},
{"Logical cores", fmt.Sprintf("%v", cpuid.CPU.LogicalCores)},
{"Physical cores", fmt.Sprintf("%v", cpuid.CPU.PhysicalCores)},
{"Thread per core", fmt.Sprintf("%v", cpuid.CPU.ThreadsPerCore)},
{"Boost frequency", fmt.Sprintf("%v", cpuid.CPU.BoostFreq)},
{"L1 Cache", fmt.Sprintf("%v instruction, %v data", bytes(cpuid.CPU.Cache.L1I), bytes(cpuid.CPU.Cache.L1D))},
{"L2 Cache", bytes(cpuid.CPU.Cache.L2)},
{"L2 Cache", bytes(cpuid.CPU.Cache.L2)},
{"L3 Cache", bytes(cpuid.CPU.Cache.L3)},
{"Cache line", fmt.Sprintf("%v", cpuid.CPU.CacheLine)},
{"Features", strings.Join(cpuid.CPU.FeatureSet(), ", ")},
}
}
2 changes: 1 addition & 1 deletion providers/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package providers

type Provider interface {
type CloudProvider interface {
Detect() bool
GetData() ([][]string, error)
}

0 comments on commit efff51c

Please sign in to comment.