open-nomad/client/fingerprint/cpu.go

81 lines
2.1 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/go-hclog"
"github.com/hashicorp/nomad/helper/stats"
"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
logger log.Logger
2015-08-26 21:27:44 +00:00
}
// NewCPUFingerprint is used to create a CPU fingerprint
func NewCPUFingerprint(logger log.Logger) Fingerprint {
f := &CPUFingerprint{logger: logger.Named("cpu")}
2015-08-26 21:27:44 +00:00
return f
}
func (f *CPUFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
cfg := req.Config
setResourcesCPU := func(totalCompute int) {
2018-09-30 00:23:41 +00:00
// COMPAT(0.10): Remove in 0.10
resp.Resources = &structs.Resources{
CPU: totalCompute,
}
2018-09-30 00:23:41 +00:00
resp.NodeResources = &structs.NodeResources{
Cpu: structs.NodeCpuResources{
2018-10-16 22:34:32 +00:00
CpuShares: int64(totalCompute),
2018-09-30 00:23:41 +00:00
},
}
}
if err := stats.Init(); err != nil {
f.logger.Warn("failed initializing stats collector", "error", err)
2017-06-27 18:56:52 +00:00
}
2017-06-27 18:56:52 +00:00
if cfg.CpuCompute != 0 {
setResourcesCPU(cfg.CpuCompute)
return nil
2015-08-26 21:27:44 +00:00
}
2017-06-27 18:56:52 +00:00
if modelName := stats.CPUModelName(); modelName != "" {
resp.AddAttribute("cpu.modelname", modelName)
}
2017-06-27 18:56:52 +00:00
if mhz := stats.CPUMHzPerCore(); mhz > 0 {
resp.AddAttribute("cpu.frequency", fmt.Sprintf("%.0f", mhz))
f.logger.Debug("detected cpu frequency", "MHz", log.Fmt("%.0f", mhz))
2017-06-27 18:56:52 +00:00
}
if numCores := stats.CPUNumCores(); numCores > 0 {
resp.AddAttribute("cpu.numcores", fmt.Sprintf("%d", numCores))
f.logger.Debug("detected core count", "cores", numCores)
2017-06-27 18:56:52 +00:00
}
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.Debug("using user specified cpu compute", "cpu_compute", cfg.CpuCompute)
2017-06-27 18:56:52 +00:00
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 {
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
resp.AddAttribute("cpu.totalcompute", fmt.Sprintf("%d", tt))
setResourcesCPU(tt)
2018-01-31 22:03:55 +00:00
resp.Detected = true
2017-07-03 20:25:33 +00:00
return nil
2015-08-26 21:27:44 +00:00
}