open-nomad/client/fingerprint/cpu.go

54 lines
1.3 KiB
Go
Raw Normal View History

2015-08-26 21:27:44 +00:00
package fingerprint
import (
2015-08-27 14:19:53 +00:00
"fmt"
2015-08-26 21:27:44 +00:00
"log"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/helper/stats"
2015-08-26 21:27:44 +00:00
"github.com/hashicorp/nomad/nomad/structs"
)
// CPUFingerprint is used to fingerprint the CPU
type CPUFingerprint struct {
2015-11-05 21:46:02 +00:00
StaticFingerprinter
2015-08-26 21:27:44 +00:00
logger *log.Logger
}
// NewCPUFingerprint is used to create a CPU fingerprint
func NewCPUFingerprint(logger *log.Logger) Fingerprint {
f := &CPUFingerprint{logger: logger}
2015-08-26 21:27:44 +00:00
return f
}
func (f *CPUFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
if err := stats.Init(); err != nil {
f.logger.Printf("[FATAL] fingerprint.cpu: unable to obtain CPU information: %v", err)
2015-08-26 21:27:44 +00:00
return false, err
}
modelName := stats.CPUModelName()
if modelName != "" {
node.Attributes["cpu.modelname"] = modelName
}
mhz := stats.CPUMHzPerCore()
2016-06-22 22:09:39 +00:00
node.Attributes["cpu.frequency"] = fmt.Sprintf("%.0f", mhz)
f.logger.Printf("[DEBUG] fingerprint.cpu: frequency: %.0f MHz", mhz)
2015-08-27 14:19:53 +00:00
numCores := stats.CPUNumCores()
node.Attributes["cpu.numcores"] = fmt.Sprintf("%d", numCores)
f.logger.Printf("[DEBUG] fingerprint.cpu: core count: %d", numCores)
2015-08-27 19:15:56 +00:00
tt := stats.TotalTicksAvailable()
2016-06-22 22:09:39 +00:00
node.Attributes["cpu.totalcompute"] = fmt.Sprintf("%.0f", tt)
2015-08-27 19:15:56 +00:00
if node.Resources == nil {
node.Resources = &structs.Resources{}
2015-08-26 21:27:44 +00:00
}
2015-08-27 14:19:53 +00:00
node.Resources.CPU = int(tt)
2015-08-26 21:27:44 +00:00
return true, nil
}