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
|
|
|
|
2021-03-25 02:09:37 +00:00
|
|
|
"github.com/hashicorp/nomad/lib/cpuset"
|
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
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
|
|
|
)
|
|
|
|
|
2020-12-09 15:01:45 +00:00
|
|
|
const (
|
|
|
|
// defaultCPUTicks is the default amount of CPU resources assumed to be
|
|
|
|
// available if the CPU performance data is unable to be detected. This is
|
|
|
|
// common on EC2 instances, where the env_aws fingerprinter will follow up,
|
|
|
|
// setting an accurate value.
|
|
|
|
defaultCPUTicks = 1000 // 1 core * 1 GHz
|
|
|
|
)
|
|
|
|
|
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
|
2018-09-16 00:48:59 +00:00
|
|
|
logger log.Logger
|
2015-08-26 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCPUFingerprint is used to create a CPU fingerprint
|
2018-09-16 00:48:59 +00:00
|
|
|
func NewCPUFingerprint(logger log.Logger) Fingerprint {
|
|
|
|
f := &CPUFingerprint{logger: logger.Named("cpu")}
|
2015-08-26 21:27:44 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
func (f *CPUFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
|
2018-01-24 14:09:53 +00:00
|
|
|
cfg := req.Config
|
2021-03-25 02:09:37 +00:00
|
|
|
setResourcesCPU := func(totalCompute int, totalCores uint16, reservableCores []uint16) {
|
2018-09-30 00:23:41 +00:00
|
|
|
// COMPAT(0.10): Remove in 0.10
|
2018-01-30 17:57:37 +00:00
|
|
|
resp.Resources = &structs.Resources{
|
|
|
|
CPU: totalCompute,
|
|
|
|
}
|
2018-09-30 00:23:41 +00:00
|
|
|
|
|
|
|
resp.NodeResources = &structs.NodeResources{
|
|
|
|
Cpu: structs.NodeCpuResources{
|
2021-03-25 02:09:37 +00:00
|
|
|
CpuShares: int64(totalCompute),
|
|
|
|
TotalCpuCores: totalCores,
|
|
|
|
ReservableCpuCores: reservableCores,
|
2018-09-30 00:23:41 +00:00
|
|
|
},
|
|
|
|
}
|
2017-03-14 19:56:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
if err := stats.Init(); err != nil {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Warn("failed initializing stats collector", "error", err)
|
2017-06-27 18:56:52 +00:00
|
|
|
}
|
2017-03-14 19:56:31 +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))
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Debug("detected cpu frequency", "MHz", log.Fmt("%.0f", mhz))
|
2017-06-27 18:56:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 02:09:37 +00:00
|
|
|
var numCores int
|
|
|
|
if numCores = stats.CPUNumCores(); numCores > 0 {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("cpu.numcores", fmt.Sprintf("%d", numCores))
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Debug("detected core count", "cores", numCores)
|
2017-06-27 18:56:52 +00:00
|
|
|
}
|
2015-08-27 14:19:53 +00:00
|
|
|
|
2021-03-25 02:09:37 +00:00
|
|
|
var reservableCores []uint16
|
2021-03-29 18:05:12 +00:00
|
|
|
if req.Config.ReservableCores != nil {
|
|
|
|
reservableCores = req.Config.ReservableCores
|
|
|
|
f.logger.Debug("reservable cores set by config", "cpuset", reservableCores)
|
2021-03-25 02:09:37 +00:00
|
|
|
} else {
|
2021-03-29 18:05:12 +00:00
|
|
|
if cores, err := f.deriveReservableCores(req); err != nil {
|
|
|
|
f.logger.Warn("failed to detect set of reservable cores", "error", err)
|
|
|
|
} else {
|
2021-04-08 05:04:47 +00:00
|
|
|
if req.Node.ReservedResources != nil {
|
|
|
|
reservableCores = cpuset.New(cores...).Difference(cpuset.New(req.Node.ReservedResources.Cpu.ReservedCpuCores...)).ToSlice()
|
|
|
|
}
|
2021-03-29 18:05:12 +00:00
|
|
|
f.logger.Debug("detected reservable cores", "cpuset", reservableCores)
|
|
|
|
}
|
2021-03-25 02:09:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 18:56:52 +00:00
|
|
|
tt := int(stats.TotalTicksAvailable())
|
|
|
|
if cfg.CpuCompute > 0 {
|
2018-09-16 00:48:59 +00:00
|
|
|
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
|
|
|
|
2020-12-09 15:01:45 +00:00
|
|
|
// If we cannot detect the cpu total compute, fallback to a very low default
|
|
|
|
// value and log a message about configuring cpu_total_compute. This happens
|
|
|
|
// on Graviton instances where CPU information is unavailable. In that case,
|
|
|
|
// the env_aws fingerprinter updates the value with correct information.
|
2017-07-03 20:25:33 +00:00
|
|
|
if tt == 0 {
|
2020-12-09 15:01:45 +00:00
|
|
|
f.logger.Info("fallback to default cpu total compute, set client config option cpu_total_compute to override")
|
|
|
|
tt = defaultCPUTicks
|
2017-06-27 18:56:52 +00:00
|
|
|
}
|
2017-07-03 20:25:33 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("cpu.totalcompute", fmt.Sprintf("%d", tt))
|
2021-03-25 02:09:37 +00:00
|
|
|
setResourcesCPU(tt, uint16(numCores), reservableCores)
|
2018-01-31 22:03:55 +00:00
|
|
|
resp.Detected = 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
|
|
|
}
|