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

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package machine
    18  
    19  import (
    20  	"io/ioutil"
    21  	"regexp"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	"k8s.io/apimachinery/pkg/util/sets"
    26  )
    27  
    28  const cpuInfoPath = "/proc/cpuinfo"
    29  
    30  var (
    31  	avx2RegExp   = regexp.MustCompile(`^flags\s*:.* (avx2 .*|avx2)$`)
    32  	avx512RegExp = regexp.MustCompile(`^flags\s*:.* (avx512 .*|avx512)$`)
    33  )
    34  
    35  type ExtraCPUInfo struct {
    36  	// SupportInstructionSet instructions all cpus support.
    37  	SupportInstructionSet sets.String
    38  }
    39  
    40  // GetExtraCPUInfo get extend cpu info from proc system
    41  func GetExtraCPUInfo() (*ExtraCPUInfo, error) {
    42  	cpuInfo, err := ioutil.ReadFile(cpuInfoPath)
    43  	if err != nil {
    44  		return nil, errors.Wrapf(err, "could not read file %s", cpuInfoPath)
    45  	}
    46  
    47  	return &ExtraCPUInfo{
    48  		SupportInstructionSet: getCPUInstructionInfo(string(cpuInfo)),
    49  	}, nil
    50  }
    51  
    52  // getCPUInstructionInfo get cpu instruction info by parsing flags with "avx2", "avx512".
    53  func getCPUInstructionInfo(cpuInfo string) sets.String {
    54  	supportInstructionSet := make(sets.String)
    55  
    56  	for _, line := range strings.Split(cpuInfo, "\n") {
    57  		if line == "" {
    58  			continue
    59  		}
    60  
    61  		// whether flags match avx2, if matched we think this
    62  		// machine is support avx2 instruction
    63  		if avx2RegExp.MatchString(line) {
    64  			supportInstructionSet.Insert("avx2")
    65  		}
    66  
    67  		// whether flags match avx2, if matched we think this
    68  		// machine is support avx512 instruction
    69  		if avx512RegExp.MatchString(line) {
    70  			supportInstructionSet.Insert("avx512")
    71  		}
    72  	}
    73  
    74  	return supportInstructionSet
    75  }
    76  
    77  // GetCoreNumReservedForReclaim generates per numa reserved for reclaim resource value map.
    78  // per numa reserved resource is taken in a fair way with even step, e.g.
    79  // 4 -> 1 1 1 1; 2 -> 1 0 1 0
    80  func GetCoreNumReservedForReclaim(numReservedCores, numNumaNodes int) map[int]int {
    81  	reservedPerNuma := numReservedCores / numNumaNodes
    82  	step := numNumaNodes / numReservedCores
    83  
    84  	if reservedPerNuma < 1 {
    85  		reservedPerNuma = 1
    86  	}
    87  	if step < 1 {
    88  		step = 1
    89  	}
    90  
    91  	reservedForReclaim := make(map[int]int)
    92  	for id := 0; id < numNumaNodes; id++ {
    93  		if id%step == 0 {
    94  			reservedForReclaim[id] = reservedPerNuma
    95  		} else {
    96  			reservedForReclaim[id] = 0
    97  		}
    98  	}
    99  
   100  	return reservedForReclaim
   101  }