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"
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2016-06-17 19:13:53 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/stats"
|
2018-01-30 17:57:37 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-08-26 21:27:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
func (f *CPUFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
|
|
|
|
cfg := req.Config
|
2018-01-26 16:21:07 +00:00
|
|
|
setResourcesCPU := func(totalCompute int) {
|
2018-01-30 17:57:37 +00:00
|
|
|
resp.Resources = &structs.Resources{
|
|
|
|
CPU: totalCompute,
|
|
|
|
}
|
2017-03-14 19:56:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
if err := stats.Init(); err != nil {
|
2017-06-27 18:56:52 +00:00
|
|
|
f.logger.Printf("[WARN] fingerprint.cpu: %v", err)
|
|
|
|
}
|
2017-03-14 19:56:31 +00:00
|
|
|
|
2017-06-27 18:56:52 +00:00
|
|
|
if cfg.CpuCompute != 0 {
|
2018-01-26 16:21:07 +00:00
|
|
|
setResourcesCPU(cfg.CpuCompute)
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-08-26 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 18:56:52 +00:00
|
|
|
if modelName := stats.CPUModelName(); modelName != "" {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("cpu.modelname", modelName)
|
2016-05-09 19:22:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 18:56:52 +00:00
|
|
|
if mhz := stats.CPUMHzPerCore(); mhz > 0 {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("cpu.frequency", fmt.Sprintf("%.0f", mhz))
|
2017-06-27 18:56:52 +00:00
|
|
|
f.logger.Printf("[DEBUG] fingerprint.cpu: frequency: %.0f MHz", mhz)
|
|
|
|
}
|
|
|
|
|
|
|
|
if numCores := stats.CPUNumCores(); numCores > 0 {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("cpu.numcores", fmt.Sprintf("%d", numCores))
|
2017-06-27 18:56:52 +00:00
|
|
|
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-07-03 20:25:33 +00:00
|
|
|
// Return an error if no cpu was detected or explicitly set as this
|
|
|
|
// node would be unable to receive any allocations.
|
|
|
|
if tt == 0 {
|
2018-01-24 14:09:53 +00:00
|
|
|
return fmt.Errorf("cannot detect cpu total compute. "+
|
2017-06-27 18:56:52 +00:00
|
|
|
"CPU compute must be set manually using the client config option %q",
|
|
|
|
"cpu_total_compute")
|
|
|
|
}
|
2017-07-03 20:25:33 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("cpu.totalcompute", fmt.Sprintf("%d", tt))
|
|
|
|
setResourcesCPU(tt)
|
2018-01-30 17:57:37 +00:00
|
|
|
resp.Applicable = true
|
2017-07-03 20:25:33 +00:00
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-08-26 21:27:44 +00:00
|
|
|
}
|