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

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2022 The Katalyst Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package machine
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/google/cadvisor/fs"
    26  	info "github.com/google/cadvisor/info/v1"
    27  	"github.com/google/cadvisor/machine"
    28  	"github.com/google/cadvisor/utils/sysfs"
    29  
    30  	"github.com/kubewharf/katalyst-core/pkg/config/agent/global"
    31  )
    32  
    33  // GetKatalystMachineInfo returns KatalystMachineInfo by collecting machine info
    34  // actually, this function should be only called in initial processes
    35  func GetKatalystMachineInfo(conf *global.MachineInfoConfiguration) (*KatalystMachineInfo, error) {
    36  	machineInfo, err := getMachineInfo()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	cpuTopology, memoryTopology, err := Discover(machineInfo)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	extraCPUInfo, err := GetExtraCPUInfo()
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	extraNetworkInfo, err := GetExtraNetworkInfo(conf)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	extraTopologyInfo, err := GetExtraTopologyInfo(conf)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return &KatalystMachineInfo{
    62  		MachineInfo:       machineInfo,
    63  		CPUTopology:       cpuTopology,
    64  		MemoryTopology:    memoryTopology,
    65  		ExtraCPUInfo:      extraCPUInfo,
    66  		ExtraNetworkInfo:  extraNetworkInfo,
    67  		ExtraTopologyInfo: extraTopologyInfo,
    68  	}, nil
    69  }
    70  
    71  // getMachineInfo is used to construct info.MachineInfo in cadvisor
    72  func getMachineInfo() (*info.MachineInfo, error) {
    73  	fsInfo, err := fs.NewFsInfo(fs.Context{})
    74  	if err != nil {
    75  		return nil, fmt.Errorf("NewFsInfo failed with error: %v", err)
    76  	}
    77  	return machine.Info(sysfs.NewRealSysFs(), fsInfo, true)
    78  }