From 4e5dcf5c43d70f33346fb8147ed3dcdb9009aff5 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Thu, 27 Aug 2015 09:19:53 -0500 Subject: [PATCH] Add cpu.frequency, cpu.totalcompute --- client/fingerprint/cpu.go | 24 ++++++++++++++++++------ client/fingerprint/cpu_test.go | 7 +++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/client/fingerprint/cpu.go b/client/fingerprint/cpu.go index e928f129d..15c54c887 100644 --- a/client/fingerprint/cpu.go +++ b/client/fingerprint/cpu.go @@ -1,8 +1,8 @@ package fingerprint import ( + "fmt" "log" - "strconv" "github.com/hashicorp/nomad/client/config" "github.com/hashicorp/nomad/nomad/structs" @@ -28,21 +28,33 @@ func (f *CPUFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bo } var numCores int32 - numCores = 0 + var mhz float64 var modelName string + // Assume all CPUs found have same Model. Log if not. // If CPUInfo() returns nil above, this loop is still safe - for i, c := range cpuInfo { - f.logger.Printf("(%d) Vendor: %s", i, c.VendorID) + for _, c := range cpuInfo { numCores += c.Cores + mhz += c.Mhz + if modelName != "" && modelName != c.ModelName { f.logger.Println("[WARN] Found different model names in the same CPU information. Recording last found") } modelName = c.ModelName } - if numCores > 0 { - node.Attributes["cpu.numcores"] = strconv.FormatInt(int64(numCores), 10) + + if mhz > 0 { + node.Attributes["cpu.frequency"] = fmt.Sprintf("%.6f", mhz) } + + if numCores > 0 { + node.Attributes["cpu.numcores"] = fmt.Sprintf("%d", numCores) + } + + if mhz > 0 && numCores > 0 { + node.Attributes["cpu.totalcompute"] = fmt.Sprintf("%.6f", float64(numCores)*mhz) + } + if modelName != "" { node.Attributes["cpu.modelname"] = modelName } diff --git a/client/fingerprint/cpu_test.go b/client/fingerprint/cpu_test.go index 5f2acbcb7..f65c344dc 100644 --- a/client/fingerprint/cpu_test.go +++ b/client/fingerprint/cpu_test.go @@ -28,4 +28,11 @@ func TestCPUFingerprint(t *testing.T) { t.Fatalf("Missing Model Name") } + if node.Attributes["cpu.frequency"] == "" { + t.Fatalf("Missing CPU Frequency") + } + if node.Attributes["cpu.totalcompute"] == "" { + t.Fatalf("Missing CPU Total Compute") + } + }