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"
|
2016-06-17 19:13:53 +00:00
|
|
|
"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 {
|
2015-08-27 00:16:34 +00:00
|
|
|
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) {
|
2017-03-14 19:56:31 +00:00
|
|
|
setResources := func(totalCompute int) {
|
|
|
|
if node.Resources == nil {
|
|
|
|
node.Resources = &structs.Resources{}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Resources.CPU = totalCompute
|
|
|
|
}
|
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
if err := stats.Init(); err != nil {
|
2017-03-14 19:56:31 +00:00
|
|
|
err := fmt.Errorf("Unable to obtain CPU information: %v", err)
|
|
|
|
|
|
|
|
if cfg.CpuCompute != 0 {
|
|
|
|
f.logger.Printf("[DEBUG] fingerprint.cpu: %v. Using specified cpu compute %d", err, cfg.CpuCompute)
|
|
|
|
setResources(cfg.CpuCompute)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.logger.Printf("[ERR] fingerprint.cpu: %v", err)
|
|
|
|
f.logger.Printf("[INFO] fingerprint.cpu: cpu compute may be set manually"+
|
|
|
|
" using the client config option %q on machines where cpu information"+
|
2017-03-14 21:15:49 +00:00
|
|
|
" can not be automatically detected.", "cpu_total_compute")
|
2017-03-14 19:56:31 +00:00
|
|
|
|
|
|
|
return false, err
|
2015-08-26 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
modelName := stats.CPUModelName()
|
|
|
|
if modelName != "" {
|
|
|
|
node.Attributes["cpu.modelname"] = modelName
|
2016-05-09 19:22:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
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
|
|
|
|
2016-06-17 19:13: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
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
tt := stats.TotalTicksAvailable()
|
2015-08-27 19:15:56 +00:00
|
|
|
|
2017-03-14 19:56:31 +00:00
|
|
|
node.Attributes["cpu.totalcompute"] = fmt.Sprintf("%.0f", tt)
|
2015-08-26 21:27:44 +00:00
|
|
|
|
2017-03-14 19:56:31 +00:00
|
|
|
setResources(int(tt))
|
2015-08-26 21:27:44 +00:00
|
|
|
return true, nil
|
|
|
|
}
|