-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathstate.go
More file actions
399 lines (329 loc) · 13.4 KB
/
state.go
File metadata and controls
399 lines (329 loc) · 13.4 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
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 commonstate
import (
"fmt"
pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"
"github.com/kubewharf/katalyst-api/pkg/consts"
cpuconsts "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/consts"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)
type AllocationMeta struct {
PodUid string `json:"pod_uid,omitempty"`
PodNamespace string `json:"pod_namespace,omitempty"`
PodName string `json:"pod_name,omitempty"`
ContainerName string `json:"container_name,omitempty"`
ContainerType string `json:"container_type,omitempty"`
ContainerIndex uint64 `json:"container_index,omitempty"`
OwnerPoolName string `json:"owner_pool_name,omitempty"`
PodRole string `json:"pod_role,omitempty"`
PodType string `json:"pod_type,omitempty"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
QoSLevel string `json:"qosLevel"`
}
func (am *AllocationMeta) GetPodUid() string {
return am.PodUid
}
func (am *AllocationMeta) GetPodNamespace() string {
return am.PodNamespace
}
func (am *AllocationMeta) GetPodName() string {
return am.PodName
}
func (am *AllocationMeta) GetContainerName() string {
return am.ContainerName
}
func (am *AllocationMeta) GetContainerType() string {
return am.ContainerType
}
func (am *AllocationMeta) GetContainerIndex() uint64 {
return am.ContainerIndex
}
func (am *AllocationMeta) GetPodRole() string {
return am.PodRole
}
func (am *AllocationMeta) GetPodType() string {
return am.PodType
}
func (am *AllocationMeta) GetLabels() map[string]string {
return am.Labels
}
func (am *AllocationMeta) GetAnnotations() map[string]string {
return am.Annotations
}
func (am *AllocationMeta) GetQoSLevel() string {
return am.QoSLevel
}
func (am *AllocationMeta) Clone() *AllocationMeta {
clone := &AllocationMeta{
PodUid: am.PodUid,
PodNamespace: am.PodNamespace,
PodName: am.PodName,
ContainerName: am.ContainerName,
ContainerType: am.ContainerType,
ContainerIndex: am.ContainerIndex,
OwnerPoolName: am.OwnerPoolName,
PodRole: am.PodRole,
PodType: am.PodType,
QoSLevel: am.QoSLevel,
Labels: general.DeepCopyMap(am.Labels),
Annotations: general.DeepCopyMap(am.Annotations),
}
return clone
}
// GetPoolName parses the owner pool name for AllocationInfo
// if owner exists, just return; otherwise, parse from qos-level
func (am *AllocationMeta) GetPoolName() string {
if am == nil {
return EmptyOwnerPoolName
}
if ownerPoolName := am.GetOwnerPoolName(); ownerPoolName != EmptyOwnerPoolName {
return ownerPoolName
}
return am.GetSpecifiedPoolName()
}
// GetOwnerPoolName parses the owner pool name for AllocationInfo
func (am *AllocationMeta) GetOwnerPoolName() string {
if am == nil {
return EmptyOwnerPoolName
}
return am.OwnerPoolName
}
// GetSpecifiedPoolName parses the owner pool name for AllocationInfo from qos-level
func (am *AllocationMeta) GetSpecifiedPoolName() string {
if am == nil {
return EmptyOwnerPoolName
}
return GetSpecifiedPoolName(am.QoSLevel, am.Annotations[consts.PodAnnotationCPUEnhancementCPUSet])
}
// GetSpecifiedNUMAID parses the numa id for AllocationInfo
func (am *AllocationMeta) GetSpecifiedNUMAID() (int, error) {
if am == nil {
return FakedNUMAID, fmt.Errorf("empty am")
}
return GetSpecifiedNUMAID(am.Annotations)
}
// SetSpecifiedNUMAID set the numa id for AllocationInfo
func (am *AllocationMeta) SetSpecifiedNUMAID(numaID uint64) {
if am == nil {
return
}
if am.Annotations == nil {
am.Annotations = make(map[string]string)
}
am.Annotations[cpuconsts.CPUStateAnnotationKeyNUMAHint] = machine.NewCPUSet(int(numaID)).String()
}
// GetSpecifiedNUMAPoolName get numa pool name
// for numa_binding or numa_affinity shared_cores according to enhancements and NUMA hint
func (am *AllocationMeta) GetSpecifiedNUMAPoolName() (string, error) {
return GetSpecifiedNUMAPoolName(am.QoSLevel, am.Annotations)
}
func GetNUMAPoolName(candidateSpecifiedPoolName, poolInfix string, targetNUMANode int) string {
return fmt.Sprintf("%s%s%d", candidateSpecifiedPoolName, poolInfix, targetNUMANode)
}
// CheckMainContainer returns true if the AllocationInfo is for main container
func (am *AllocationMeta) CheckMainContainer() bool {
if am == nil {
return false
}
return am.ContainerType == pluginapi.ContainerType_MAIN.String()
}
// CheckSideCar returns true if the AllocationInfo is for side-car container
func (am *AllocationMeta) CheckSideCar() bool {
if am == nil {
return false
}
return am.ContainerType == pluginapi.ContainerType_SIDECAR.String()
}
func (am *AllocationMeta) GetSpecifiedSystemPoolName() (string, error) {
if !am.CheckSystem() {
return EmptyOwnerPoolName, fmt.Errorf("GetSpecifiedSystemPoolName only for system_cores")
}
specifiedPoolName := am.GetSpecifiedPoolName()
if specifiedPoolName == EmptyOwnerPoolName {
return specifiedPoolName, nil
}
return fmt.Sprintf("%s%s%s", PoolNamePrefixSystem, "-", specifiedPoolName), nil
}
func (am *AllocationMeta) CheckSystem() bool {
if am == nil {
return false
}
return am.QoSLevel == consts.PodAnnotationQoSLevelSystemCores
}
// CheckDedicated returns true if the AllocationInfo is for pod with dedicated-qos
func (am *AllocationMeta) CheckDedicated() bool {
if am == nil {
return false
}
return am.QoSLevel == consts.PodAnnotationQoSLevelDedicatedCores
}
// CheckShared returns true if the AllocationInfo is for pod with shared-qos
func (am *AllocationMeta) CheckShared() bool {
if am == nil {
return false
}
return am.QoSLevel == consts.PodAnnotationQoSLevelSharedCores
}
// CheckReclaimed returns true if the AllocationInfo is for pod with reclaimed-qos
func (am *AllocationMeta) CheckReclaimed() bool {
if am == nil {
return false
}
return am.QoSLevel == consts.PodAnnotationQoSLevelReclaimedCores
}
// CheckNUMABinding returns true if the AllocationInfo is for pod with numa-binding enhancement
func (am *AllocationMeta) CheckNUMABinding() bool {
if am == nil {
return false
}
return am.Annotations[consts.PodAnnotationMemoryEnhancementNumaBinding] ==
consts.PodAnnotationMemoryEnhancementNumaBindingEnable
}
func (am *AllocationMeta) CheckNUMANotShare() bool {
if am == nil {
return false
}
return am.Annotations[consts.PodAnnotationCPUEnhancementNUMAShare] ==
consts.PodAnnotationCPUEnhancementNUMAShareDisable
}
// CheckActualNUMABinding returns true if the AllocationInfo is for pod actual numa-binding
func (am *AllocationMeta) CheckActualNUMABinding() bool {
if am == nil {
return false
}
return am.Annotations[cpuconsts.CPUStateAnnotationKeyNUMAHint] != ""
}
// CheckDedicatedNUMABinding returns true if the AllocationInfo is for pod with
// dedicated-qos and numa-binding enhancement
func (am *AllocationMeta) CheckDedicatedNUMABinding() bool {
return am.CheckDedicated() && am.CheckNUMABinding()
}
// CheckDedicatedNUMABindingNUMAExclusive returns true if the AllocationInfo is for pod with
// dedicated-qos and numa-binding and numa-exclusive enhancement
func (am *AllocationMeta) CheckDedicatedNUMABindingNUMAExclusive() bool {
return am.CheckDedicatedNUMABinding() && am.CheckNumaExclusive()
}
// CheckSharedNUMABinding returns true if the AllocationInfo is for pod with
// shared-qos and numa-binding enhancement
func (am *AllocationMeta) CheckSharedNUMABinding() bool {
return am.CheckShared() && am.CheckNUMABinding()
}
// CheckSharedOrDedicatedNUMABinding returns true if the AllocationInfo is for pod with
// shared-qos or dedicated-qos and numa-binding enhancement
func (am *AllocationMeta) CheckSharedOrDedicatedNUMABinding() bool {
return am.CheckSharedNUMABinding() || am.CheckDedicatedNUMABinding()
}
// CheckReclaimedNUMABinding returns true if the AllocationInfo is for pod with
// reclaimed-qos and numa-binding enhancement
func (am *AllocationMeta) CheckReclaimedNUMABinding() bool {
return am.CheckReclaimed() && am.CheckNUMABinding()
}
// CheckReclaimedActualNUMABinding returns true if the AllocationInfo is for pod with
// reclaimed-qos and numa-binding enhancement and numa hint is not empty, which means
// the container is allocated on a specific NUMA node
func (am *AllocationMeta) CheckReclaimedActualNUMABinding() bool {
return am.CheckReclaimedNUMABinding() && am.CheckActualNUMABinding()
}
// CheckReclaimedNonActualNUMABinding returns true if the AllocationInfo is for pod with
// reclaimed-qos and without binding to a specific NUMA node actually, which means
// the pod can be allocated on multi NUMA nodes
func (am *AllocationMeta) CheckReclaimedNonActualNUMABinding() bool {
return am.CheckReclaimed() && !am.CheckActualNUMABinding()
}
// CheckNumaExclusive returns true if the AllocationInfo is for pod with numa-exclusive enhancement
func (am *AllocationMeta) CheckNumaExclusive() bool {
if am == nil {
return false
}
return am.Annotations[consts.PodAnnotationMemoryEnhancementNumaExclusive] ==
consts.PodAnnotationMemoryEnhancementNumaExclusiveEnable
}
// CheckDedicatedPool returns true if the AllocationInfo is for a container in the dedicated pool
func (am *AllocationMeta) CheckDedicatedPool() bool {
if am == nil {
return false
}
return am.OwnerPoolName == PoolNameDedicated
}
// CheckNUMAAffinity returns true if the AllocationInfo is for pod with numa-affinity enhancement
func (am *AllocationMeta) CheckNUMAAffinity() bool {
if am == nil {
return false
}
return am.Annotations[consts.PodAnnotationCPUEnhancementNumaAffinity] ==
consts.PodAnnotationCPUEnhancementNumaAffinityEnable || am.CheckNUMABinding()
}
// CheckDedicatedNUMAAffinity returns true if the AllocationInfo is for pod with
// dedicated-qos and numa-affinity enhancement
func (am *AllocationMeta) CheckDedicatedNUMAAffinity() bool {
return am.CheckDedicated() && am.CheckNUMAAffinity()
}
// CheckSharedNUMAAffinity returns true if the AllocationInfo is for pod with
// shared-qos and numa-affinity enhancement
func (am *AllocationMeta) CheckSharedNUMAAffinity() bool {
return am.CheckShared() && am.CheckNUMAAffinity()
}
// CheckSharedOrDedicatedNUMAAffinity returns true if the AllocationInfo is for pod with
// shared-qos or dedicated-qos and numa-affinity enhancement
func (am *AllocationMeta) CheckSharedOrDedicatedNUMAAffinity() bool {
return am.CheckSharedNUMAAffinity() || am.CheckDedicatedNUMAAffinity()
}
// CheckNonBindingNUMAAffinity returns true if the AllocationInfo is for pod with
// numa-affinity enhancement and without numa-binding enhancement
func (am *AllocationMeta) CheckNonBindingNUMAAffinity() bool {
if am == nil {
return false
}
return am.Annotations[consts.PodAnnotationCPUEnhancementNumaAffinity] ==
consts.PodAnnotationCPUEnhancementNumaAffinityEnable && !am.CheckNUMABinding()
}
// CheckNonBindingSharedNUMAAffinity returns true if the AllocationInfo is for pod with
// shared-qos and numa-affinity enhancement and without numa-binding enhancement
func (am *AllocationMeta) CheckNonBindingSharedNUMAAffinity() bool {
return am.CheckShared() && am.CheckNonBindingNUMAAffinity()
}
// CheckNonBindingDedicatedNUMAAffinity returns true if the AllocationInfo is for pod with
// dedicated-qos and numa-affinity enhancement and without numa-binding enhancement
func (am *AllocationMeta) CheckNonBindingDedicatedNUMAAffinity() bool {
return am.CheckDedicated() && am.CheckNonBindingNUMAAffinity()
}
// CheckNonBindingSharedOrDedicatedNUMAAffinity returns true if the AllocationInfo is for pod with
// shared-qos or dedicated-qos and numa-affinity enhancement and without numa-binding enhancement
func (am *AllocationMeta) CheckNonBindingSharedOrDedicatedNUMAAffinity() bool {
return am.CheckNonBindingSharedNUMAAffinity() || am.CheckNonBindingDedicatedNUMAAffinity()
}
// CheckSharedNumaBindingOrDedicatedNUMAAffinity returns true if the AllocationInfo is for pod with
// shared numa-bind or dedicated numa-affinity.
func (am *AllocationMeta) CheckSharedNumaBindingOrDedicatedNUMAAffinity() bool {
return am.CheckSharedNUMABinding() || am.CheckDedicatedNUMAAffinity()
}
// CheckReclaimedNUMAAffinity returns true if the AllocationInfo is for pod with
// reclaimed numa-bind or dedicated numa-affinity.
func (am *AllocationMeta) CheckReclaimedNUMAAffinity() bool {
return am.CheckReclaimed() && am.CheckNUMAAffinity()
}
// CheckReclaimedActualNUMAAffinity returns true if the AllocationInfo is for pod with
// reclaimed-qos and numa-affinity enhancement and numa hint is not empty, which means
// the container is allocated on a specific NUMA node
func (am *AllocationMeta) CheckReclaimedActualNUMAAffinity() bool {
return am.CheckReclaimedNUMAAffinity() && am.CheckActualNUMABinding()
}
// CheckReclaimedNonActualNUMAAffinity returns true if the AllocationInfo is for pod with
// reclaimed-qos and numa-affinity enhancement and numa hint is empty, which means
// the container can be allocated on multi NUMA nodes
func (am *AllocationMeta) CheckReclaimedNonActualNUMAAffinity() bool {
return am.CheckReclaimed() && !am.CheckActualNUMABinding()
}