open-nomad/client/fingerprint/cpu.go

78 lines
2.0 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) {
setResources := func(totalCompute int) {
if node.Resources == nil {
node.Resources = &structs.Resources{}
}
node.Resources.CPU = totalCompute
}
if err := stats.Init(); err != nil {
2017-06-27 18:56:52 +00:00
f.logger.Printf("[WARN] fingerprint.cpu: %v", err)
}
2017-06-27 18:56:52 +00:00
if cfg.CpuCompute != 0 {
setResources(cfg.CpuCompute)
return true, nil
2015-08-26 21:27:44 +00:00
}
2017-06-27 18:56:52 +00:00
if modelName := stats.CPUModelName(); modelName != "" {
node.Attributes["cpu.modelname"] = modelName
}
2017-06-27 18:56:52 +00:00
if mhz := stats.CPUMHzPerCore(); mhz > 0 {
node.Attributes["cpu.frequency"] = fmt.Sprintf("%.0f", mhz)
f.logger.Printf("[DEBUG] fingerprint.cpu: frequency: %.0f MHz", mhz)
}
if numCores := stats.CPUNumCores(); numCores > 0 {
node.Attributes["cpu.numcores"] = fmt.Sprintf("%d", numCores)
f.logger.Printf("[DEBUG] fingerprint.cpu: core count: %d", numCores)
}
2015-08-27 14:19:53 +00:00
2017-06-27 18:56:52 +00:00
tt := int(stats.TotalTicksAvailable())
if cfg.CpuCompute > 0 {
f.logger.Printf("[DEBUG] fingerprint.cpu: Using specified cpu compute %d", cfg.CpuCompute)
tt = cfg.CpuCompute
}
2015-08-27 19:15:56 +00:00
2017-06-27 18:56:52 +00:00
// Set the node compute resources if they are detected/configured
if tt > 0 {
node.Attributes["cpu.totalcompute"] = fmt.Sprintf("%d", tt)
2015-08-27 19:15:56 +00:00
2017-06-27 18:56:52 +00:00
if node.Resources == nil {
node.Resources = &structs.Resources{}
}
2015-08-26 21:27:44 +00:00
2017-06-27 18:56:52 +00:00
node.Resources.CPU = tt
} else {
f.logger.Printf("[INFO] fingerprint.cpu: cannot detect cpu total compute. "+
"CPU compute must be set manually using the client config option %q",
"cpu_total_compute")
}
2015-08-26 21:27:44 +00:00
return true, nil
}