1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00
node-feature-discovery/source/cpu/cpu.go
Markus Lehtonen 852cf4b61d source: introduce FeatureSource interface
Specify a new interface for managing "raw" feature data. This is the
first step to separate raw feature data from node labels. None of the
feature sources implement this interface, yet.

This patch unifies the data format of "raw" features by dividing them
into three different basic types.
- keys, a set of names without any associated values, e.g. CPUID flags
  or loaded kernel modules
- values, a map of key-value pairs, for features with a single value,
  e.g. kernel config flags or os version
- instances, a list of instances each of which has multiple attributes
  (key-value pairs of their own), e.g. PCI or USB devices

The new feature data types are defined in a new "pkg/api/feature"
package, catering decoupling and re-usability of code e.g. within future
extentions of the NFD gRPC API.

Rename the Discover() method of LabelSource interface to GetLabels().
2021-09-20 09:58:07 +03:00

227 lines
4.9 KiB
Go

/*
Copyright 2018-2021 The Kubernetes Authors.
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"
"k8s.io/klog/v2"
"sigs.k8s.io/node-feature-discovery/source"
"sigs.k8s.io/node-feature-discovery/source/internal/cpuidutils"
)
const Name = "cpu"
// Configuration file options
type cpuidConfig struct {
AttributeBlacklist []string `json:"attributeBlacklist,omitempty"`
AttributeWhitelist []string `json:"attributeWhitelist,omitempty"`
}
type Config struct {
Cpuid cpuidConfig `json:"cpuid,omitempty"`
}
// 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",
"SSE4",
"SSE42",
"SSSE3",
},
AttributeWhitelist: []string{},
},
}
}
// Filter for cpuid labels
type keyFilter struct {
keys map[string]struct{}
whitelist bool
}
// cpuSource implements the LabelSource and ConfigurableSource interfaces.
type cpuSource struct {
config *Config
cpuidFilter *keyFilter
}
// Singleton source instance
var (
src cpuSource
_ source.LabelSource = &src
_ source.ConfigurableSource = &src
)
func (s *cpuSource) Name() string { return Name }
// NewConfig method of the LabelSource interface
func (s *cpuSource) NewConfig() source.Config { return newDefaultConfig() }
// GetConfig method of the LabelSource interface
func (s *cpuSource) GetConfig() source.Config { return s.config }
// SetConfig method of the LabelSource interface
func (s *cpuSource) SetConfig(conf source.Config) {
switch v := conf.(type) {
case *Config:
s.config = v
s.initCpuidFilter()
default:
klog.Fatalf("invalid config type: %T", conf)
}
}
// Priority method of the LabelSource interface
func (s *cpuSource) Priority() int { return 0 }
// GetLabels method of the LabelSource interface
func (s *cpuSource) GetLabels() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
// Check if hyper-threading seems to be enabled
found, err := haveThreadSiblings()
if err != nil {
klog.Errorf("failed to detect hyper-threading: %v", err)
} else if found {
features["hardware_multithreading"] = true
}
// Check SST-BF
found, err = discoverSSTBF()
if err != nil {
klog.Errorf("failed to detect SST-BF: %v", err)
} else if found {
features["power.sst_bf.enabled"] = true
}
// Detect CPUID
cpuidFlags := cpuidutils.GetCpuidFlags()
for _, f := range cpuidFlags {
if s.cpuidFilter.unmask(f) {
features["cpuid."+f] = true
}
}
// Detect pstate features
pstate, err := detectPstate()
if err != nil {
klog.Error(err)
} else {
for k, v := range pstate {
features["pstate."+k] = v
}
}
// Detect RDT features
rdt := discoverRDT()
for _, f := range rdt {
features["rdt."+f] = true
}
// Detect cstate configuration
cstate, ok, err := detectCstate()
if err != nil {
klog.Error(err)
} else if ok {
features["cstate.enabled"] = cstate
}
return features, nil
}
// Check if any (online) CPUs have thread siblings
func haveThreadSiblings() (bool, error) {
files, err := ioutil.ReadDir(source.SysfsDir.Path("bus/cpu/devices"))
if err != nil {
return false, err
}
for _, file := range files {
// Try to read siblings from topology
siblings, err := ioutil.ReadFile(source.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
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
}
func (s *cpuSource) initCpuidFilter() {
newFilter := keyFilter{keys: map[string]struct{}{}}
if len(s.config.Cpuid.AttributeWhitelist) > 0 {
for _, k := range s.config.Cpuid.AttributeWhitelist {
newFilter.keys[k] = struct{}{}
}
newFilter.whitelist = true
} else {
for _, k := range s.config.Cpuid.AttributeBlacklist {
newFilter.keys[k] = struct{}{}
}
newFilter.whitelist = false
}
s.cpuidFilter = &newFilter
}
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
}
func init() {
source.Register(&src)
}