/
state.go
391 lines (327 loc) · 11.7 KB
/
state.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
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
/*
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 (
"encoding/json"
"fmt"
info "github.com/google/cadvisor/info/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"
"github.com/kubewharf/katalyst-api/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/commonstate"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/util"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)
type AllocationInfo 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"`
RampUp bool `json:"ramp_up,omitempty"`
PodRole string `json:"pod_role,omitempty"`
PodType string `json:"pod_type,omitempty"`
AggregatedQuantity uint64 `json:"aggregated_quantity"`
NumaAllocationResult machine.CPUSet `json:"numa_allocation_result,omitempty"`
// keyed by numa node id, value is assignment for the pod in corresponding NUMA node
TopologyAwareAllocations map[int]uint64 `json:"topology_aware_allocations"`
// keyed by control knob names referred in memoryadvisor package
ExtraControlKnobInfo map[string]commonstate.ControlKnobInfo `json:"extra_control_knob_info"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
QoSLevel string `json:"qosLevel"`
}
type ContainerEntries map[string]*AllocationInfo // Keyed by container name
type PodEntries map[string]ContainerEntries // Keyed by pod UID
type PodResourceEntries map[v1.ResourceName]PodEntries // Keyed by resource name
// NUMANodeState records the amount of memory per numa node (in bytes)
type NUMANodeState struct {
TotalMemSize uint64 `json:"total"`
SystemReserved uint64 `json:"systemReserved"`
Allocatable uint64 `json:"allocatable"`
Allocated uint64 `json:"Allocated"`
Free uint64 `json:"free"`
PodEntries PodEntries `json:"pod_entries"`
}
type NUMANodeMap map[int]*NUMANodeState // keyed by numa node id
type NUMANodeResourcesMap map[v1.ResourceName]NUMANodeMap // keyed by resource name
func (ai *AllocationInfo) String() string {
if ai == nil {
return ""
}
contentBytes, err := json.Marshal(ai)
if err != nil {
klog.Errorf("[AllocationInfo.String] marshal AllocationInfo failed with error: %v", err)
return ""
}
return string(contentBytes)
}
func (ai *AllocationInfo) Clone() *AllocationInfo {
if ai == nil {
return nil
}
clone := &AllocationInfo{
PodUid: ai.PodUid,
PodNamespace: ai.PodNamespace,
PodName: ai.PodName,
ContainerName: ai.ContainerName,
ContainerType: ai.ContainerType,
ContainerIndex: ai.ContainerIndex,
RampUp: ai.RampUp,
PodRole: ai.PodRole,
PodType: ai.PodType,
AggregatedQuantity: ai.AggregatedQuantity,
NumaAllocationResult: ai.NumaAllocationResult.Clone(),
QoSLevel: ai.QoSLevel,
Labels: general.DeepCopyMap(ai.Labels),
Annotations: general.DeepCopyMap(ai.Annotations),
}
if ai.TopologyAwareAllocations != nil {
clone.TopologyAwareAllocations = make(map[int]uint64)
for node, quantity := range ai.TopologyAwareAllocations {
clone.TopologyAwareAllocations[node] = quantity
}
}
if ai.ExtraControlKnobInfo != nil {
clone.ExtraControlKnobInfo = make(map[string]commonstate.ControlKnobInfo)
for name := range ai.ExtraControlKnobInfo {
clone.ExtraControlKnobInfo[name] = ai.ExtraControlKnobInfo[name]
}
}
return clone
}
// CheckNumaBinding returns true if the AllocationInfo is for pod with
// dedicated-qos and numa-binding enhancement
func (ai *AllocationInfo) CheckNumaBinding() bool {
return ai.QoSLevel == consts.PodAnnotationQoSLevelDedicatedCores &&
ai.Annotations[consts.PodAnnotationMemoryEnhancementNumaBinding] == consts.PodAnnotationMemoryEnhancementNumaBindingEnable
}
// CheckMainContainer returns true if the AllocationInfo is for main container
func (ai *AllocationInfo) CheckMainContainer() bool {
return ai.ContainerType == pluginapi.ContainerType_MAIN.String()
}
// CheckSideCar returns true if the AllocationInfo is for side-car container
func (ai *AllocationInfo) CheckSideCar() bool {
return ai.ContainerType == pluginapi.ContainerType_SIDECAR.String()
}
// GetResourceAllocation transforms resource allocation information into *pluginapi.ResourceAllocation
func (ai *AllocationInfo) GetResourceAllocation() (*pluginapi.ResourceAllocation, error) {
if ai == nil {
return nil, fmt.Errorf("GetResourceAllocation of nil AllocationInfo")
}
// deal with main resource
resourceAllocation := &pluginapi.ResourceAllocation{
ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
string(v1.ResourceMemory): {
OciPropertyName: util.OCIPropertyNameCPUSetMems,
IsNodeResource: false,
IsScalarResource: true,
AllocatedQuantity: float64(ai.AggregatedQuantity),
AllocationResult: ai.NumaAllocationResult.String(),
},
},
}
// deal with accompanying resources
for name, entry := range ai.ExtraControlKnobInfo {
if entry.OciPropertyName == "" {
continue
}
if resourceAllocation.ResourceAllocation[name] != nil {
return nil, fmt.Errorf("name: %s meets conflict", name)
}
resourceAllocation.ResourceAllocation[name] = &pluginapi.ResourceAllocationInfo{
OciPropertyName: entry.OciPropertyName,
AllocationResult: entry.ControlKnobValue,
}
}
return resourceAllocation, nil
}
func (pe PodEntries) Clone() PodEntries {
if pe == nil {
return nil
}
clone := make(PodEntries)
for podUID, containerEntries := range pe {
if containerEntries == nil {
continue
}
clone[podUID] = make(ContainerEntries)
for containerName, allocationInfo := range containerEntries {
clone[podUID][containerName] = allocationInfo.Clone()
}
}
return clone
}
// GetMainContainerAllocation returns AllocationInfo that belongs
// the main container for this pod
func (pe PodEntries) GetMainContainerAllocation(podUID string) (*AllocationInfo, bool) {
for _, allocationInfo := range pe[podUID] {
if allocationInfo.CheckMainContainer() {
return allocationInfo, true
}
}
return nil, false
}
func (pre PodResourceEntries) String() string {
if pre == nil {
return ""
}
contentBytes, err := json.Marshal(pre)
if err != nil {
klog.Errorf("[PodResourceEntries.String] marshal PodResourceEntries failed with error: %v", err)
return ""
}
return string(contentBytes)
}
func (pre PodResourceEntries) Clone() PodResourceEntries {
if pre == nil {
return nil
}
clone := make(PodResourceEntries)
for resourceName, podEntries := range pre {
clone[resourceName] = podEntries.Clone()
}
return clone
}
func (ns *NUMANodeState) String() string {
if ns == nil {
return ""
}
contentBytes, err := json.Marshal(ns)
if err != nil {
klog.Errorf("[NUMANodeState.String] marshal NUMANodeState failed with error: %v", err)
return ""
}
return string(contentBytes)
}
func (ns *NUMANodeState) Clone() *NUMANodeState {
if ns == nil {
return nil
}
return &NUMANodeState{
TotalMemSize: ns.TotalMemSize,
SystemReserved: ns.SystemReserved,
Allocatable: ns.Allocatable,
Allocated: ns.Allocated,
Free: ns.Free,
PodEntries: ns.PodEntries.Clone(),
}
}
// HasNUMABindingPods returns true if any AllocationInfo in this NUMANodeState is for numa-binding
func (ns *NUMANodeState) HasNUMABindingPods() bool {
if ns == nil {
return false
}
for _, containerEntries := range ns.PodEntries {
for _, allocationInfo := range containerEntries {
if allocationInfo != nil && allocationInfo.CheckNumaBinding() {
return true
}
}
}
return false
}
// SetAllocationInfo adds a new AllocationInfo (for pod/container pairs) into the given NUMANodeState
func (ns *NUMANodeState) SetAllocationInfo(podUID string, containerName string, allocationInfo *AllocationInfo) {
if ns == nil {
return
}
if ns.PodEntries == nil {
ns.PodEntries = make(PodEntries)
}
if _, ok := ns.PodEntries[podUID]; !ok {
ns.PodEntries[podUID] = make(ContainerEntries)
}
ns.PodEntries[podUID][containerName] = allocationInfo.Clone()
}
func (nm NUMANodeMap) Clone() NUMANodeMap {
clone := make(NUMANodeMap)
for node, ns := range nm {
clone[node] = ns.Clone()
}
return clone
}
// BytesPerNUMA is a helper function to parse memory capacity at per numa level
func (nm NUMANodeMap) BytesPerNUMA() (uint64, error) {
if len(nm) == 0 {
return 0, fmt.Errorf("getBytesPerNUMAFromMachineState got nil numaMap")
}
var maxNUMAAllocatable uint64
for _, numaState := range nm {
if numaState != nil {
maxNUMAAllocatable = general.MaxUInt64(maxNUMAAllocatable, numaState.Allocatable)
}
}
if maxNUMAAllocatable > 0 {
return maxNUMAAllocatable, nil
}
return 0, fmt.Errorf("getBytesPerNUMAFromMachineState doesn't get valid numaState")
}
// GetNUMANodesWithoutNUMABindingPods returns a set of numa nodes; for
// those numa nodes, they all don't contain numa-binding pods
func (nm NUMANodeMap) GetNUMANodesWithoutNUMABindingPods() machine.CPUSet {
res := machine.NewCPUSet()
for numaId, numaNodeState := range nm {
if numaNodeState != nil && !numaNodeState.HasNUMABindingPods() {
res = res.Union(machine.NewCPUSet(numaId))
}
}
return res
}
func (nrm NUMANodeResourcesMap) String() string {
if nrm == nil {
return ""
}
contentBytes, err := json.Marshal(nrm)
if err != nil {
klog.Errorf("[NUMANodeResourcesMap.String] marshal NUMANodeResourcesMap failed with error: %v", err)
return ""
}
return string(contentBytes)
}
func (nrm NUMANodeResourcesMap) Clone() NUMANodeResourcesMap {
clone := make(NUMANodeResourcesMap)
for resourceName, nm := range nrm {
clone[resourceName] = nm.Clone()
}
return clone
}
// reader is used to get information from local states
type reader interface {
GetMachineState() NUMANodeResourcesMap
GetPodResourceEntries() PodResourceEntries
GetAllocationInfo(resourceName v1.ResourceName, podUID, containerName string) *AllocationInfo
}
// writer is used to store information into local states,
// and it also provides functionality to maintain the local files
type writer interface {
SetMachineState(numaNodeResourcesMap NUMANodeResourcesMap)
SetPodResourceEntries(podResourceEntries PodResourceEntries)
SetAllocationInfo(resourceName v1.ResourceName, podUID, containerName string, allocationInfo *AllocationInfo)
Delete(resourceName v1.ResourceName, podUID, containerName string)
ClearState()
}
// ReadonlyState interface only provides methods for tracking pod assignments
type ReadonlyState interface {
reader
GetMachineInfo() *info.MachineInfo
GetReservedMemory() map[v1.ResourceName]map[int]uint64
}
// State interface provides methods for tracking and setting pod assignments
type State interface {
writer
ReadonlyState
}