github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/machine/topology_linux.go (about)

     1  //go:build linux
     2  
     3  /*
     4  Copyright 2022 The Katalyst Authors.
     5  
     6  Licensed under the Apache License, Version 2.0 (the "License");
     7  you may not use this file except in compliance with the License.
     8  You may obtain a copy of the License at
     9  
    10      http://www.apache.org/licenses/LICENSE-2.0
    11  
    12  Unless required by applicable law or agreed to in writing, software
    13  distributed under the License is distributed on an "AS IS" BASIS,
    14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  See the License for the specific language governing permissions and
    16  limitations under the License.
    17  */
    18  
    19  package machine
    20  
    21  import (
    22  	"fmt"
    23  	"io/ioutil"
    24  	"path/filepath"
    25  	"strconv"
    26  	"strings"
    27  
    28  	"github.com/kubewharf/katalyst-core/pkg/config/agent/global"
    29  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    30  )
    31  
    32  const (
    33  	sysNodeDirectory = "/sys/devices/system/node"
    34  )
    35  
    36  func GetExtraTopologyInfo(conf *global.MachineInfoConfiguration) (*ExtraTopologyInfo, error) {
    37  	fInfos, err := ioutil.ReadDir(sysNodeDirectory)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("faield to ReadDir /sys/devices/system/node, err %s", err)
    40  	}
    41  
    42  	numaDistanceArray := make(map[int][]NumaDistanceInfo)
    43  	for _, fi := range fInfos {
    44  		if !fi.IsDir() {
    45  			continue
    46  		}
    47  		if !strings.HasPrefix(fi.Name(), "node") {
    48  			continue
    49  		}
    50  
    51  		nodeID, err := strconv.Atoi(fi.Name()[len("node"):])
    52  		if err != nil {
    53  			general.Infof("/sys/devices/system/node/%s is not a node directory", fi.Name())
    54  			continue
    55  		}
    56  
    57  		b, err := ioutil.ReadFile(filepath.Join("/sys/devices/system/node", fi.Name(), "distance"))
    58  		if err != nil {
    59  			return nil, err
    60  		}
    61  		s := strings.TrimSpace(strings.TrimRight(string(b), "\n"))
    62  		distances := strings.Fields(s)
    63  		var distanceArray []NumaDistanceInfo
    64  		for id, distanceStr := range distances {
    65  			distance, err := strconv.Atoi(distanceStr)
    66  			if err != nil {
    67  				return nil, err
    68  			}
    69  			distanceArray = append(distanceArray, NumaDistanceInfo{
    70  				NumaID:   id,
    71  				Distance: distance,
    72  			})
    73  		}
    74  		numaDistanceArray[nodeID] = distanceArray
    75  	}
    76  
    77  	return &ExtraTopologyInfo{
    78  		NumaDistanceMap: numaDistanceArray,
    79  		SiblingNumaInfo: GetSiblingNumaInfo(conf, numaDistanceArray),
    80  	}, nil
    81  }