github.com/smithx10/nomad@v0.9.1-rc1/client/fingerprint/cpu.go (about) 1 package fingerprint 2 3 import ( 4 "fmt" 5 6 log "github.com/hashicorp/go-hclog" 7 "github.com/hashicorp/nomad/helper/stats" 8 "github.com/hashicorp/nomad/nomad/structs" 9 ) 10 11 // CPUFingerprint is used to fingerprint the CPU 12 type CPUFingerprint struct { 13 StaticFingerprinter 14 logger log.Logger 15 } 16 17 // NewCPUFingerprint is used to create a CPU fingerprint 18 func NewCPUFingerprint(logger log.Logger) Fingerprint { 19 f := &CPUFingerprint{logger: logger.Named("cpu")} 20 return f 21 } 22 23 func (f *CPUFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error { 24 cfg := req.Config 25 setResourcesCPU := func(totalCompute int) { 26 // COMPAT(0.10): Remove in 0.10 27 resp.Resources = &structs.Resources{ 28 CPU: totalCompute, 29 } 30 31 resp.NodeResources = &structs.NodeResources{ 32 Cpu: structs.NodeCpuResources{ 33 CpuShares: int64(totalCompute), 34 }, 35 } 36 } 37 38 if err := stats.Init(); err != nil { 39 f.logger.Warn("failed initializing stats collector", "error", err) 40 } 41 42 if cfg.CpuCompute != 0 { 43 setResourcesCPU(cfg.CpuCompute) 44 return nil 45 } 46 47 if modelName := stats.CPUModelName(); modelName != "" { 48 resp.AddAttribute("cpu.modelname", modelName) 49 } 50 51 if mhz := stats.CPUMHzPerCore(); mhz > 0 { 52 resp.AddAttribute("cpu.frequency", fmt.Sprintf("%.0f", mhz)) 53 f.logger.Debug("detected cpu frequency", "MHz", log.Fmt("%.0f", mhz)) 54 } 55 56 if numCores := stats.CPUNumCores(); numCores > 0 { 57 resp.AddAttribute("cpu.numcores", fmt.Sprintf("%d", numCores)) 58 f.logger.Debug("detected core count", "cores", numCores) 59 } 60 61 tt := int(stats.TotalTicksAvailable()) 62 if cfg.CpuCompute > 0 { 63 f.logger.Debug("using user specified cpu compute", "cpu_compute", cfg.CpuCompute) 64 tt = cfg.CpuCompute 65 } 66 67 // Return an error if no cpu was detected or explicitly set as this 68 // node would be unable to receive any allocations. 69 if tt == 0 { 70 return fmt.Errorf("cannot detect cpu total compute. "+ 71 "CPU compute must be set manually using the client config option %q", 72 "cpu_total_compute") 73 } 74 75 resp.AddAttribute("cpu.totalcompute", fmt.Sprintf("%d", tt)) 76 setResourcesCPU(tt) 77 resp.Detected = true 78 79 return nil 80 }