forked from kubewharf/katalyst-core
/
util.go
213 lines (180 loc) · 8.27 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package state
import (
"fmt"
"strings"
"sync"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
advisorapi "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/cpuadvisor"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/util"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)
// notice that pool-name may not have direct mapping relations with qos-level, for instance
// - both isolated_shared_cores and dedicated_cores fall into PoolNameDedicated
const (
PoolNameShare = "share"
PoolNameReclaim = "reclaim"
PoolNameDedicated = "dedicated"
PoolNameReserve = "reserve"
PoolNameIsolation = "isolation"
// PoolNameFallback is not a real pool, and is a union of
// all none-reclaimed pools to put pod should have been isolated
PoolNameFallback = "fallback"
)
var (
// StaticPools are generated by cpu plugin statically,
// and they will be ignored when reading cpu advisor list and watch response.
StaticPools = sets.NewString(
PoolNameReserve,
)
// ResidentPools are guaranteed existing in state,
// and they are usually used to ensure stability.
ResidentPools = sets.NewString(
PoolNameReclaim,
).Union(StaticPools)
)
var containerRequestedCoresLock sync.RWMutex
var containerRequestedCores func(allocationInfo *AllocationInfo) int
func GetContainerRequestedCores() func(allocationInfo *AllocationInfo) int {
containerRequestedCoresLock.RLock()
defer containerRequestedCoresLock.RUnlock()
return containerRequestedCores
}
func SetContainerRequestedCores(f func(allocationInfo *AllocationInfo) int) {
containerRequestedCoresLock.Lock()
defer containerRequestedCoresLock.Unlock()
containerRequestedCores = f
}
// GetIsolatedQuantityMapFromPodEntries returns a map to indicates isolation info,
// and the map is formatted as pod -> container -> isolated-quantity
func GetIsolatedQuantityMapFromPodEntries(podEntries PodEntries, ignoreAllocationInfos []*AllocationInfo) map[string]map[string]int {
ret := make(map[string]map[string]int)
for podUID, entries := range podEntries {
if entries.IsPoolEntry() {
continue
}
containerLoop:
for containerName, allocationInfo := range entries {
// only filter dedicated_cores without numa_binding
if allocationInfo == nil || CheckDedicatedNUMABinding(allocationInfo) || !CheckDedicated(allocationInfo) {
continue
}
for _, ignoreAllocationInfo := range ignoreAllocationInfos {
if allocationInfo.PodUid == ignoreAllocationInfo.PodUid && allocationInfo.ContainerName == ignoreAllocationInfo.ContainerName {
continue containerLoop
}
}
// if there is no more cores to allocate, we will put dedicated_cores without numa_binding
// to pool rather than isolation. calling this function means we will start to adjust allocation,
// and we will try to isolate those containers, so we will treat them as containers to be isolated.
var quantity int
if allocationInfo.OwnerPoolName != PoolNameDedicated {
quantity = GetContainerRequestedCores()(allocationInfo)
} else {
quantity = allocationInfo.AllocationResult.Size()
}
if quantity == 0 {
klog.Warningf("[GetIsolatedQuantityMapFromPodEntries] isolated pod: %s/%s container: %s get zero quantity",
allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName)
continue
}
if ret[podUID] == nil {
ret[podUID] = make(map[string]int)
}
ret[podUID][containerName] = quantity
}
}
return ret
}
// GetSharedQuantityMapFromPodEntries returns a map to indicates quantity info for each shared pool,
// and the map is formatted as pool -> quantity
func GetSharedQuantityMapFromPodEntries(podEntries PodEntries, ignoreAllocationInfos []*AllocationInfo) map[string]int {
ret := make(map[string]int)
for _, entries := range podEntries {
if entries.IsPoolEntry() {
continue
}
containerLoop:
for _, allocationInfo := range entries {
// only count shared_cores not isolated.
// if there is no more cores to allocate, we will put dedicated_cores without numa_binding to pool rather than isolation.
// calling this function means we will start to adjust allocation, and we will try to isolate those containers,
// so we will treat them as containers to be isolated.
if allocationInfo == nil || !CheckShared(allocationInfo) {
continue
}
for _, ignoreAllocationInfo := range ignoreAllocationInfos {
if allocationInfo.PodUid == ignoreAllocationInfo.PodUid && allocationInfo.ContainerName == ignoreAllocationInfo.ContainerName {
continue containerLoop
}
}
if poolName := allocationInfo.GetOwnerPoolName(); poolName != advisorapi.EmptyOwnerPoolName {
ret[poolName] += GetContainerRequestedCores()(allocationInfo)
}
}
}
return ret
}
// GenerateMachineStateFromPodEntries returns NUMANodeMap for given resource based on
// machine info and reserved resources along with existed pod entries
func GenerateMachineStateFromPodEntries(topology *machine.CPUTopology, podEntries PodEntries, policyName string) (NUMANodeMap, error) {
if topology == nil {
return nil, fmt.Errorf("GenerateMachineStateFromPodEntries got nil topology")
}
machineState := make(NUMANodeMap)
for _, numaNode := range topology.CPUDetails.NUMANodes().ToSliceInt64() {
numaNodeState := &NUMANodeState{}
numaNodeAllCPUs := topology.CPUDetails.CPUsInNUMANodes(int(numaNode)).Clone()
allocatedCPUsInNumaNode := machine.NewCPUSet()
for podUID, containerEntries := range podEntries {
for containerName, allocationInfo := range containerEntries {
if containerName != advisorapi.FakedContainerName && allocationInfo != nil {
// the container hasn't cpuset assignment in the current NUMA node
if allocationInfo.OriginalTopologyAwareAssignments[int(numaNode)].Size() == 0 &&
allocationInfo.TopologyAwareAssignments[int(numaNode)].Size() == 0 {
continue
}
switch policyName {
case util.CPUResourcePluginPolicyNameDynamic:
// only modify allocated and default properties in NUMA node state if the policy is dynamic and the QoS class is dedicated_cores with NUMA binding
if CheckDedicatedNUMABinding(allocationInfo) {
allocatedCPUsInNumaNode = allocatedCPUsInNumaNode.Union(allocationInfo.OriginalTopologyAwareAssignments[int(numaNode)])
}
case util.CPUResourcePluginPolicyNameNative:
// only modify allocated and default properties in NUMA node state if the policy is native and the QoS class is Guaranteed
if CheckDedicatedPool(allocationInfo) {
allocatedCPUsInNumaNode = allocatedCPUsInNumaNode.Union(allocationInfo.OriginalTopologyAwareAssignments[int(numaNode)])
}
}
topologyAwareAssignments, _ := machine.GetNumaAwareAssignments(topology, allocationInfo.AllocationResult.Intersection(numaNodeAllCPUs))
originalTopologyAwareAssignments, _ := machine.GetNumaAwareAssignments(topology, allocationInfo.OriginalAllocationResult.Intersection(numaNodeAllCPUs))
numaNodeAllocationInfo := allocationInfo.Clone()
numaNodeAllocationInfo.AllocationResult = allocationInfo.AllocationResult.Intersection(numaNodeAllCPUs)
numaNodeAllocationInfo.OriginalAllocationResult = allocationInfo.OriginalAllocationResult.Intersection(numaNodeAllCPUs)
numaNodeAllocationInfo.TopologyAwareAssignments = topologyAwareAssignments
numaNodeAllocationInfo.OriginalTopologyAwareAssignments = originalTopologyAwareAssignments
numaNodeState.SetAllocationInfo(podUID, containerName, numaNodeAllocationInfo)
}
}
}
numaNodeState.AllocatedCPUSet = allocatedCPUsInNumaNode.Clone()
numaNodeState.DefaultCPUSet = numaNodeAllCPUs.Difference(numaNodeState.AllocatedCPUSet)
machineState[int(numaNode)] = numaNodeState
}
return machineState, nil
}
func IsIsolationPool(poolName string) bool {
return strings.HasPrefix(poolName, PoolNameIsolation)
}