2018-07-17 09:26:11 +00:00
|
|
|
/*
|
2021-02-23 08:05:13 +00:00
|
|
|
Copyright 2018-2021 The Kubernetes Authors.
|
2018-07-17 09:26:11 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cpu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2021-03-02 13:17:33 +00:00
|
|
|
"strconv"
|
2021-02-23 08:05:13 +00:00
|
|
|
|
|
|
|
"k8s.io/klog/v2"
|
2018-07-17 09:26:11 +00:00
|
|
|
|
2022-03-28 09:51:23 +00:00
|
|
|
"github.com/klauspost/cpuid/v2"
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
2018-07-17 09:26:11 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
|
|
|
)
|
|
|
|
|
2022-01-19 07:12:06 +00:00
|
|
|
// Name of this feature source
|
2021-05-12 13:27:29 +00:00
|
|
|
const Name = "cpu"
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
const (
|
|
|
|
CpuidFeature = "cpuid"
|
2022-03-28 09:51:23 +00:00
|
|
|
Cpumodel = "model"
|
2021-03-02 13:17:33 +00:00
|
|
|
CstateFeature = "cstate"
|
|
|
|
PstateFeature = "pstate"
|
|
|
|
RdtFeature = "rdt"
|
2022-03-18 13:40:03 +00:00
|
|
|
SeFeature = "se"
|
2021-11-12 11:25:19 +00:00
|
|
|
SgxFeature = "sgx"
|
2021-03-02 13:17:33 +00:00
|
|
|
SstFeature = "sst"
|
|
|
|
TopologyFeature = "topology"
|
|
|
|
)
|
|
|
|
|
2019-05-13 14:10:55 +00:00
|
|
|
// Configuration file options
|
|
|
|
type cpuidConfig struct {
|
|
|
|
AttributeBlacklist []string `json:"attributeBlacklist,omitempty"`
|
|
|
|
AttributeWhitelist []string `json:"attributeWhitelist,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// Config holds configuration for the cpu source.
|
2020-04-21 19:03:37 +00:00
|
|
|
type Config struct {
|
2019-05-13 14:10:55 +00:00
|
|
|
Cpuid cpuidConfig `json:"cpuid,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
// newDefaultConfig returns a new config with pre-populated defaults
|
|
|
|
func newDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
cpuidConfig{
|
|
|
|
AttributeBlacklist: []string{
|
|
|
|
"BMI1",
|
|
|
|
"BMI2",
|
|
|
|
"CLMUL",
|
|
|
|
"CMOV",
|
|
|
|
"CX16",
|
|
|
|
"ERMS",
|
|
|
|
"F16C",
|
|
|
|
"HTT",
|
|
|
|
"LZCNT",
|
|
|
|
"MMX",
|
|
|
|
"MMXEXT",
|
|
|
|
"NX",
|
|
|
|
"POPCNT",
|
|
|
|
"RDRAND",
|
|
|
|
"RDSEED",
|
|
|
|
"RDTSCP",
|
|
|
|
"SGX",
|
|
|
|
"SGXLC",
|
|
|
|
"SSE",
|
|
|
|
"SSE2",
|
|
|
|
"SSE3",
|
2021-07-06 08:54:55 +00:00
|
|
|
"SSE4",
|
|
|
|
"SSE42",
|
2020-04-21 19:03:37 +00:00
|
|
|
"SSSE3",
|
|
|
|
},
|
|
|
|
AttributeWhitelist: []string{},
|
2019-05-13 14:10:55 +00:00
|
|
|
},
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
2019-05-13 14:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter for cpuid labels
|
|
|
|
type keyFilter struct {
|
|
|
|
keys map[string]struct{}
|
|
|
|
whitelist bool
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// cpuSource implements the FeatureSource, LabelSource and ConfigurableSource interfaces.
|
2021-03-01 07:02:22 +00:00
|
|
|
type cpuSource struct {
|
2020-04-21 19:03:37 +00:00
|
|
|
config *Config
|
|
|
|
cpuidFilter *keyFilter
|
2021-03-02 13:17:33 +00:00
|
|
|
features *feature.DomainFeatures
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
2018-07-17 09:26:11 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Singleton source instance
|
|
|
|
var (
|
2021-03-02 13:17:33 +00:00
|
|
|
src = cpuSource{config: newDefaultConfig(), cpuidFilter: &keyFilter{}}
|
|
|
|
_ source.FeatureSource = &src
|
2021-03-01 07:02:22 +00:00
|
|
|
_ source.LabelSource = &src
|
|
|
|
_ source.ConfigurableSource = &src
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *cpuSource) Name() string { return Name }
|
2018-07-17 09:26:11 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// NewConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *cpuSource) NewConfig() source.Config { return newDefaultConfig() }
|
2018-07-17 09:26:11 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// GetConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *cpuSource) GetConfig() source.Config { return s.config }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// SetConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *cpuSource) SetConfig(conf source.Config) {
|
2020-04-21 19:03:37 +00:00
|
|
|
switch v := conf.(type) {
|
|
|
|
case *Config:
|
|
|
|
s.config = v
|
|
|
|
s.initCpuidFilter()
|
|
|
|
default:
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Fatalf("invalid config type: %T", conf)
|
2019-05-13 14:10:55 +00:00
|
|
|
}
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Priority method of the LabelSource interface
|
|
|
|
func (s *cpuSource) Priority() int { return 0 }
|
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// GetLabels method of the LabelSource interface
|
|
|
|
func (s *cpuSource) GetLabels() (source.FeatureLabels, error) {
|
2021-03-02 13:17:33 +00:00
|
|
|
labels := source.FeatureLabels{}
|
|
|
|
features := s.GetFeatures()
|
2019-05-13 14:10:55 +00:00
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// CPUID
|
|
|
|
for f := range features.Keys[CpuidFeature].Elements {
|
|
|
|
if s.cpuidFilter.unmask(f) {
|
|
|
|
labels["cpuid."+f] = true
|
|
|
|
}
|
2018-07-17 09:26:11 +00:00
|
|
|
}
|
2019-04-12 11:30:53 +00:00
|
|
|
|
2022-03-28 09:51:23 +00:00
|
|
|
// CPU model
|
|
|
|
for k, v := range features.Values[Cpumodel].Elements {
|
|
|
|
labels["model."+k] = v
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// Cstate
|
|
|
|
for k, v := range features.Values[CstateFeature].Elements {
|
|
|
|
labels["cstate."+k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pstate
|
|
|
|
for k, v := range features.Values[PstateFeature].Elements {
|
|
|
|
labels["pstate."+k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// RDT
|
|
|
|
for k := range features.Keys[RdtFeature].Elements {
|
|
|
|
labels["rdt."+k] = true
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:25:19 +00:00
|
|
|
// SGX
|
|
|
|
for k, v := range features.Values[SgxFeature].Elements {
|
|
|
|
labels["sgx."+k] = v
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:40:03 +00:00
|
|
|
// Secure Execution
|
|
|
|
for k, v := range features.Values[SeFeature].Elements {
|
|
|
|
labels["se."+k] = v
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// SST
|
|
|
|
for k, v := range features.Values[SstFeature].Elements {
|
|
|
|
labels["power.sst_"+k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hyperthreading
|
|
|
|
if v, ok := features.Values[TopologyFeature].Elements["hardware_multithreading"]; ok {
|
|
|
|
labels["hardware_multithreading"] = v
|
2019-04-12 11:30:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discover method of the FeatureSource Interface
|
|
|
|
func (s *cpuSource) Discover() error {
|
|
|
|
s.features = feature.NewDomainFeatures()
|
|
|
|
|
2019-02-19 08:52:46 +00:00
|
|
|
// Detect CPUID
|
2021-03-02 13:17:33 +00:00
|
|
|
s.features.Keys[CpuidFeature] = feature.NewKeyFeatures(getCpuidFlags()...)
|
|
|
|
|
2022-03-28 09:51:23 +00:00
|
|
|
// Detect CPU model
|
|
|
|
s.features.Values[Cpumodel] = feature.NewValueFeatures(getCPUModel())
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// Detect cstate configuration
|
|
|
|
cstate, err := detectCstate()
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed to detect cstate: %v", err)
|
|
|
|
} else {
|
|
|
|
s.features.Values[CstateFeature] = feature.NewValueFeatures(cstate)
|
2019-02-19 08:52:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 13:11:27 +00:00
|
|
|
// Detect pstate features
|
|
|
|
pstate, err := detectPstate()
|
2019-02-19 08:52:46 +00:00
|
|
|
if err != nil {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Error(err)
|
2019-02-19 08:52:46 +00:00
|
|
|
}
|
2021-03-02 13:17:33 +00:00
|
|
|
s.features.Values[PstateFeature] = feature.NewValueFeatures(pstate)
|
2019-02-19 08:52:46 +00:00
|
|
|
|
|
|
|
// Detect RDT features
|
2021-03-02 13:17:33 +00:00
|
|
|
s.features.Keys[RdtFeature] = feature.NewKeyFeatures(discoverRDT()...)
|
|
|
|
|
2021-11-12 11:25:19 +00:00
|
|
|
// Detect SGX features
|
|
|
|
s.features.Values[SgxFeature] = feature.NewValueFeatures(discoverSGX())
|
|
|
|
|
2022-03-18 13:40:03 +00:00
|
|
|
// Detect Secure Execution features
|
|
|
|
s.features.Values[SeFeature] = feature.NewValueFeatures(discoverSE())
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// Detect SST features
|
|
|
|
s.features.Values[SstFeature] = feature.NewValueFeatures(discoverSST())
|
|
|
|
|
|
|
|
// Detect hyper-threading
|
|
|
|
s.features.Values[TopologyFeature] = feature.NewValueFeatures(discoverTopology())
|
|
|
|
|
|
|
|
utils.KlogDump(3, "discovered cpu features:", " ", s.features)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeatures method of the FeatureSource Interface
|
|
|
|
func (s *cpuSource) GetFeatures() *feature.DomainFeatures {
|
|
|
|
if s.features == nil {
|
|
|
|
s.features = feature.NewDomainFeatures()
|
2019-02-19 08:52:46 +00:00
|
|
|
}
|
2021-03-02 13:17:33 +00:00
|
|
|
return s.features
|
|
|
|
}
|
2019-02-19 08:52:46 +00:00
|
|
|
|
2022-03-28 09:51:23 +00:00
|
|
|
func getCPUModel() map[string]string {
|
|
|
|
cpuModelInfo := make(map[string]string)
|
|
|
|
cpuModelInfo["vendor_id"] = cpuid.CPU.VendorID.String()
|
|
|
|
cpuModelInfo["family"] = strconv.Itoa(cpuid.CPU.Family)
|
|
|
|
cpuModelInfo["id"] = strconv.Itoa(cpuid.CPU.Model)
|
|
|
|
|
|
|
|
return cpuModelInfo
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
func discoverTopology() map[string]string {
|
|
|
|
features := make(map[string]string)
|
|
|
|
|
|
|
|
if ht, err := haveThreadSiblings(); err != nil {
|
|
|
|
klog.Errorf("failed to detect hyper-threading: %v", err)
|
|
|
|
} else {
|
|
|
|
features["hardware_multithreading"] = strconv.FormatBool(ht)
|
2021-03-02 20:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
return features
|
2018-07-17 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any (online) CPUs have thread siblings
|
|
|
|
func haveThreadSiblings() (bool, error) {
|
2020-05-20 11:32:07 +00:00
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(source.SysfsDir.Path("bus/cpu/devices"))
|
2018-07-17 09:26:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
// Try to read siblings from topology
|
2020-05-20 11:32:07 +00:00
|
|
|
siblings, err := ioutil.ReadFile(source.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
|
2018-07-17 09:26:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, char := range siblings {
|
|
|
|
// If list separator found, we determine that there are multiple siblings
|
|
|
|
if char == ',' || char == '-' {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No siblings were found
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-05-13 14:10:55 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *cpuSource) initCpuidFilter() {
|
2019-05-13 14:10:55 +00:00
|
|
|
newFilter := keyFilter{keys: map[string]struct{}{}}
|
2020-04-21 19:03:37 +00:00
|
|
|
if len(s.config.Cpuid.AttributeWhitelist) > 0 {
|
|
|
|
for _, k := range s.config.Cpuid.AttributeWhitelist {
|
2019-05-13 14:10:55 +00:00
|
|
|
newFilter.keys[k] = struct{}{}
|
|
|
|
}
|
|
|
|
newFilter.whitelist = true
|
|
|
|
} else {
|
2020-04-21 19:03:37 +00:00
|
|
|
for _, k := range s.config.Cpuid.AttributeBlacklist {
|
2019-05-13 14:10:55 +00:00
|
|
|
newFilter.keys[k] = struct{}{}
|
|
|
|
}
|
|
|
|
newFilter.whitelist = false
|
|
|
|
}
|
2020-04-21 19:03:37 +00:00
|
|
|
s.cpuidFilter = &newFilter
|
2019-05-13 14:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f keyFilter) unmask(k string) bool {
|
|
|
|
if f.whitelist {
|
|
|
|
if _, ok := f.keys[k]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := f.keys[k]; !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-03-01 07:02:22 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
source.Register(&src)
|
|
|
|
}
|