github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/cpu/dynamicpolicy/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 dynamicpolicy 18 19 import ( 20 "fmt" 21 "math" 22 23 pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1" 24 25 apiconsts "github.com/kubewharf/katalyst-api/pkg/consts" 26 cpuconsts "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/consts" 27 "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state" 28 "github.com/kubewharf/katalyst-core/pkg/util/general" 29 "github.com/kubewharf/katalyst-core/pkg/util/machine" 30 ) 31 32 func getProportionalSize(oldPoolSize, oldTotalSize, newTotalSize int, ceil bool) int { 33 if ceil { 34 return int(math.Ceil(float64(newTotalSize) * (float64(oldPoolSize) / float64(oldTotalSize)))) 35 } else { 36 return int(float64(newTotalSize) * (float64(oldPoolSize) / float64(oldTotalSize))) 37 } 38 } 39 40 func generateMachineStateFromPodEntries(topology *machine.CPUTopology, podEntries state.PodEntries) (state.NUMANodeMap, error) { 41 return state.GenerateMachineStateFromPodEntries(topology, podEntries, cpuconsts.CPUResourcePluginPolicyNameDynamic) 42 } 43 44 // updateAllocationInfoByReq updates allocationInfo by latest req when admitting active pod, 45 // because qos level and annotations will change after we support customized updater of enhancements and qos level 46 func updateAllocationInfoByReq(req *pluginapi.ResourceRequest, allocationInfo *state.AllocationInfo) error { 47 if req == nil { 48 return fmt.Errorf("updateAllocationInfoByReq got ni l req") 49 } else if allocationInfo == nil { 50 return nil 51 } 52 53 if req.Annotations[apiconsts.PodAnnotationQoSLevelKey] != "" && 54 req.Annotations[apiconsts.PodAnnotationQoSLevelKey] != allocationInfo.QoSLevel { 55 general.Infof("update allocationInfo QoS level from %s to %s", 56 allocationInfo.QoSLevel, req.Annotations[apiconsts.PodAnnotationQoSLevelKey]) 57 allocationInfo.QoSLevel = req.Annotations[apiconsts.PodAnnotationQoSLevelKey] 58 } 59 60 allocationInfo.Annotations = general.DeepCopyMap(req.Annotations) 61 return nil 62 }