Skip to content

Commit

Permalink
Merge pull request #113788 from PiotrProkop/fix-discovering-numa-dist…
Browse files Browse the repository at this point in the history
…ance

Fix discovering numa distance when node ids are not starting from 0 or it's ids are not sequential
  • Loading branch information
k8s-ci-robot committed Nov 9, 2022
2 parents 8eddcac + 540b5bd commit 623376b
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 307 deletions.
2 changes: 1 addition & 1 deletion pkg/kubelet/cm/cpumanager/policy_options_test.go
Expand Up @@ -162,7 +162,7 @@ func TestValidateStaticPolicyOptions(t *testing.T) {
t.Run(testCase.description, func(t *testing.T) {
topoMgrPolicy := topologymanager.NewNonePolicy()
if testCase.topoMgrPolicy == topologymanager.PolicySingleNumaNode {
topoMgrPolicy, _ = topologymanager.NewSingleNumaNodePolicy(&topologymanager.NUMAInfo{}, map[string]string{})
topoMgrPolicy = topologymanager.NewSingleNumaNodePolicy(&topologymanager.NUMAInfo{}, topologymanager.PolicyOptions{})

}
topoMgrStore := topologymanager.NewFakeManagerWithPolicy(topoMgrPolicy)
Expand Down
59 changes: 9 additions & 50 deletions pkg/kubelet/cm/topologymanager/numa_info.go
Expand Up @@ -18,46 +18,30 @@ package topologymanager

import (
"fmt"
"io/ioutil"
"strconv"
"strings"

cadvisorapi "github.com/google/cadvisor/info/v1"
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask"
)

const (
defaultNodeDir = "/sys/devices/system/node/"
)

type NUMADistances [][]uint64
type NUMADistances map[int][]uint64

type NUMAInfo struct {
Nodes []int
NUMADistances NUMADistances
}

type NUMASysFs struct {
nodeDir string
}

func NewNUMAInfo(topology []cadvisorapi.Node) (*NUMAInfo, error) {
return newNUMAInfo(topology, &NUMASysFs{nodeDir: defaultNodeDir})
}

func newNUMAInfo(topology []cadvisorapi.Node, sysFs *NUMASysFs) (*NUMAInfo, error) {
func NewNUMAInfo(topology []cadvisorapi.Node, opts PolicyOptions) (*NUMAInfo, error) {
var numaNodes []int
distances := make([][]uint64, len(topology))
distances := map[int][]uint64{}
for _, node := range topology {
numaNodes = append(numaNodes, node.Id)

// Populate the NUMA distances
// For now we need to retrieve this information from sysfs.
// TODO: Update this as follows once a version of cadvisor with this commit is vendored in https://github.com/google/cadvisor/commit/24dd1de08a72cfee661f6178454db995900c0fee
// distances[node.Id] = node.Distances[:]
nodeDistance, err := sysFs.GetDistances(node.Id)
if err != nil {
return nil, fmt.Errorf("error getting NUMA distances from sysfs: %w", err)
var nodeDistance []uint64
if opts.PreferClosestNUMA {
nodeDistance = node.Distances
if nodeDistance == nil {
return nil, fmt.Errorf("error getting NUMA distances from cadvisor")
}
}
distances[node.Id] = nodeDistance
}
Expand Down Expand Up @@ -123,28 +107,3 @@ func (d NUMADistances) CalculateAverageFor(bm bitmask.BitMask) float64 {

return sum / count
}

func (s NUMASysFs) GetDistances(nodeId int) ([]uint64, error) {
distancePath := fmt.Sprintf("%s/node%d/distance", s.nodeDir, nodeId)
distance, err := ioutil.ReadFile(distancePath)
if err != nil {
return nil, fmt.Errorf("problem reading %s: %w", distancePath, err)
}

rawDistances := strings.TrimSpace(string(distance))

return splitDistances(rawDistances)
}

func splitDistances(rawDistances string) ([]uint64, error) {
distances := []uint64{}
for _, distance := range strings.Split(rawDistances, " ") {
distanceUint, err := strconv.ParseUint(distance, 10, 64)
if err != nil {
return nil, fmt.Errorf("cannot convert %s to int", distance)
}
distances = append(distances, distanceUint)
}

return distances, nil
}

0 comments on commit 623376b

Please sign in to comment.