github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/machine/util.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 "fmt" 21 22 "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask" 23 ) 24 25 // TransformCPUAssignmentFormat transforms cpu assignment string format to cpuset format 26 func TransformCPUAssignmentFormat(assignment map[uint64]string) map[int]CPUSet { 27 res := make(map[int]CPUSet) 28 for k, v := range assignment { 29 res[int(k)] = MustParse(v) 30 } 31 return res 32 } 33 34 // ParseCPUAssignmentFormat parses the given assignments into string format 35 func ParseCPUAssignmentFormat(assignments map[int]CPUSet) map[uint64]string { 36 if assignments == nil { 37 return nil 38 } 39 40 res := make(map[uint64]string) 41 for id, cset := range assignments { 42 res[uint64(id)] = cset.String() 43 } 44 return res 45 } 46 47 // CountCPUAssignmentCPUs returns sum of cpus among all numas in assignment 48 func CountCPUAssignmentCPUs(assignment map[int]CPUSet) int { 49 res := 0 50 for _, v := range assignment { 51 res += v.Size() 52 } 53 return res 54 } 55 56 // ParseCPUAssignmentQuantityMap is used to generate cpu resource counting map 57 // based on the given CPUSet map 58 func ParseCPUAssignmentQuantityMap(csetMap map[string]CPUSet) map[string]int { 59 ret := make(map[string]int) 60 61 for name, cset := range csetMap { 62 ret[name] = cset.Size() 63 } 64 65 return ret 66 } 67 68 // DeepcopyCPUAssignment returns a deep-copied assignments for the given one 69 func DeepcopyCPUAssignment(assignment map[int]CPUSet) map[int]CPUSet { 70 if assignment == nil { 71 return nil 72 } 73 74 copied := make(map[int]CPUSet) 75 for numaNode, cset := range assignment { 76 copied[numaNode] = cset.Clone() 77 } 78 return copied 79 } 80 81 // GetCPUAssignmentNUMAs returns memset for cpuset 82 func GetCPUAssignmentNUMAs(assignment map[int]CPUSet) CPUSet { 83 memset := NewCPUSet() 84 for numaID, cpuset := range assignment { 85 if cpuset.Size() > 0 { 86 memset.Add(numaID) 87 } 88 } 89 return memset 90 } 91 92 // MaskToUInt64Array transforms bit mask to uint slices 93 func MaskToUInt64Array(mask bitmask.BitMask) []uint64 { 94 maskBits := mask.GetBits() 95 96 maskBitsUint64 := make([]uint64, 0, len(maskBits)) 97 for _, numaNode := range maskBits { 98 maskBitsUint64 = append(maskBitsUint64, uint64(numaNode)) 99 } 100 101 return maskBitsUint64 102 } 103 104 // GetSiblingNUMAs returns numa IDs that lays in the socket with the given numa 105 func GetSiblingNUMAs(numaID int, topology *CPUTopology) (CPUSet, error) { 106 if topology == nil { 107 return NewCPUSet(), fmt.Errorf("getSiblingNUMAs got nil topology") 108 } 109 110 socketSet := topology.CPUDetails.SocketsInNUMANodes(numaID) 111 if socketSet.Size() != 1 { 112 return NewCPUSet(), fmt.Errorf("get invalid socketSet: %s from NUMA: %d", 113 socketSet.String(), numaID) 114 } 115 116 numaSet := topology.CPUDetails.NUMANodesInSockets(socketSet.ToSliceNoSortInt()...) 117 if numaSet.IsEmpty() { 118 return NewCPUSet(), fmt.Errorf("get empty numaSet from socketSet: %s", 119 socketSet.String()) 120 } 121 122 return numaSet, nil 123 }