forked from kubewharf/katalyst-core
/
policy_allocation_handlers.go
1050 lines (897 loc) · 46.6 KB
/
policy_allocation_handlers.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 dynamicpolicy
import (
"context"
"fmt"
"time"
v1 "k8s.io/api/core/v1"
pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"
apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/calculator"
advisorapi "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/cpuadvisor"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/util"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
qosutil "github.com/kubewharf/katalyst-core/pkg/util/qos"
)
func (p *DynamicPolicy) sharedCoresAllocationHandler(_ context.Context,
req *pluginapi.ResourceRequest) (*pluginapi.ResourceAllocationResponse, error) {
if req == nil {
return nil, fmt.Errorf("sharedCoresAllocationHandler got nil request")
}
reqInt, err := util.GetQuantityFromResourceReq(req)
if err != nil {
return nil, fmt.Errorf("getReqQuantityFromResourceReq failed with error: %v", err)
}
machineState := p.state.GetMachineState()
pooledCPUs := machineState.GetFilteredAvailableCPUSet(p.reservedCPUs,
state.CheckDedicated, state.CheckDedicatedNUMABinding)
if pooledCPUs.IsEmpty() {
general.Errorf("pod: %s/%s, container: %s get empty pooledCPUs", req.PodNamespace, req.PodName, req.ContainerName)
return nil, fmt.Errorf("get empty pooledCPUs")
}
pooledCPUsTopologyAwareAssignments, err := machine.GetNumaAwareAssignments(p.machineInfo.CPUTopology, pooledCPUs)
if err != nil {
general.Errorf("pod: %s/%s, container: %s GetTopologyAwareAssignmentsByCPUSet failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("GetTopologyAwareAssignmentsByCPUSet failed with error: %v", err)
}
needSet := true
allocationInfo := p.state.GetAllocationInfo(req.PodUid, req.ContainerName)
if allocationInfo == nil {
general.Infof("pod: %s/%s, container: %s is met firstly, do ramp up with pooled cpus: %s",
req.PodNamespace, req.PodName, req.ContainerName, pooledCPUs.String())
allocationInfo = &state.AllocationInfo{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType.String(),
ContainerIndex: req.ContainerIndex,
RampUp: true,
// fill OwnerPoolName with empty string when ramping up
OwnerPoolName: advisorapi.EmptyOwnerPoolName,
PodRole: req.PodRole,
PodType: req.PodType,
AllocationResult: pooledCPUs,
OriginalAllocationResult: pooledCPUs.Clone(),
TopologyAwareAssignments: pooledCPUsTopologyAwareAssignments,
OriginalTopologyAwareAssignments: machine.DeepcopyCPUAssignment(pooledCPUsTopologyAwareAssignments),
InitTimestamp: time.Now().Format(util.QRMTimeFormat),
Labels: general.DeepCopyMap(req.Labels),
Annotations: general.DeepCopyMap(req.Annotations),
QoSLevel: apiconsts.PodAnnotationQoSLevelSharedCores,
RequestQuantity: reqInt,
}
} else if allocationInfo.RampUp {
general.Infof("pod: %s/%s, container: %s is still in ramp up, allocate pooled cpus: %s",
req.PodNamespace, req.PodName, req.ContainerName, pooledCPUs.String())
allocationInfo.AllocationResult = pooledCPUs
allocationInfo.OriginalAllocationResult = pooledCPUs.Clone()
allocationInfo.TopologyAwareAssignments = pooledCPUsTopologyAwareAssignments
allocationInfo.OriginalTopologyAwareAssignments = machine.DeepcopyCPUAssignment(pooledCPUsTopologyAwareAssignments)
} else {
// need to adjust pools and putAllocationsAndAdjustAllocationEntries will set the allocationInfo after adjusted
err = p.putAllocationsAndAdjustAllocationEntries([]*state.AllocationInfo{allocationInfo})
if err != nil {
general.Errorf("pod: %s/%s, container: %s putContainerAndReGeneratePool failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("putContainerAndReGeneratePool failed with error: %v", err)
}
allocationInfo = p.state.GetAllocationInfo(req.PodUid, req.ContainerName)
if allocationInfo == nil {
general.Errorf("pod: %s/%s, container: %s get nil allocationInfo after putContainerAndReGeneratePool",
req.PodNamespace, req.PodName, req.ContainerName)
return nil, fmt.Errorf("putContainerAndReGeneratePool failed with error: %v", err)
}
needSet = false
}
if needSet {
// update pod entries directly.
// if one of subsequent steps is failed,
// we will delete current allocationInfo from podEntries in defer function of allocation function.
p.state.SetAllocationInfo(allocationInfo.PodUid, allocationInfo.ContainerName, allocationInfo)
podEntries := p.state.GetPodEntries()
updatedMachineState, err := generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
if err != nil {
general.Errorf("pod: %s/%s, container: %s GenerateMachineStateFromPodEntries failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("GenerateMachineStateFromPodEntries failed with error: %v", err)
}
p.state.SetMachineState(updatedMachineState)
}
resp, err := util.PackAllocationResponse(allocationInfo, string(v1.ResourceCPU), util.OCIPropertyNameCPUSetCPUs, false, true, req)
if err != nil {
general.Errorf("pod: %s/%s, container: %s packAllocationResponse failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("PackResourceAllocationResponseByAllocationInfo failed with error: %v", err)
}
return resp, nil
}
func (p *DynamicPolicy) reclaimedCoresAllocationHandler(_ context.Context,
req *pluginapi.ResourceRequest) (*pluginapi.ResourceAllocationResponse, error) {
if req == nil {
return nil, fmt.Errorf("reclaimedCoresAllocationHandler got nil request")
}
reqInt, err := util.GetQuantityFromResourceReq(req)
if err != nil {
return nil, fmt.Errorf("getReqQuantityFromResourceReq failed with error: %v", err)
}
allocationInfo := p.state.GetAllocationInfo(req.PodUid, req.ContainerName)
reclaimedAllocationInfo := p.state.GetAllocationInfo(state.PoolNameReclaim, advisorapi.FakedContainerName)
if reclaimedAllocationInfo == nil {
general.Errorf("allocation for pod: %s/%s, container: %s is failed, because pool: %s is not ready",
req.PodNamespace, req.PodName, req.ContainerName, state.PoolNameReclaim)
return nil, fmt.Errorf("pool: %s is not ready", state.PoolNameReclaim)
} else if reclaimedAllocationInfo.AllocationResult.Size() == 0 {
general.Errorf("allocation for pod: %s/%s, container: %s is failed, because pool: %s is empty",
req.PodNamespace, req.PodName, req.ContainerName, state.PoolNameReclaim)
return nil, fmt.Errorf("pool: %s is not empty", state.PoolNameReclaim)
}
if allocationInfo != nil {
general.Infof("pod: %s/%s, container: %s with old allocation result: %s, allocate by reclaimedCPUSet: %s",
req.PodNamespace, req.PodName, req.ContainerName, allocationInfo.AllocationResult.String(), reclaimedAllocationInfo.AllocationResult.String())
} else {
general.Infof("pod: %s/%s, container: %s is firstly met, allocate by reclaimedCPUSet: %s",
req.PodNamespace, req.PodName, req.ContainerName, reclaimedAllocationInfo.AllocationResult.String())
allocationInfo = &state.AllocationInfo{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType.String(),
ContainerIndex: req.ContainerIndex,
OwnerPoolName: state.PoolNameReclaim,
PodRole: req.PodRole,
PodType: req.PodType,
InitTimestamp: time.Now().Format(util.QRMTimeFormat),
Labels: general.DeepCopyMap(req.Labels),
Annotations: general.DeepCopyMap(req.Annotations),
QoSLevel: apiconsts.PodAnnotationQoSLevelReclaimedCores,
RequestQuantity: reqInt,
}
}
allocationInfo.AllocationResult = reclaimedAllocationInfo.AllocationResult.Clone()
allocationInfo.OriginalAllocationResult = reclaimedAllocationInfo.OriginalAllocationResult.Clone()
allocationInfo.TopologyAwareAssignments = machine.DeepcopyCPUAssignment(reclaimedAllocationInfo.TopologyAwareAssignments)
allocationInfo.OriginalTopologyAwareAssignments = machine.DeepcopyCPUAssignment(reclaimedAllocationInfo.OriginalTopologyAwareAssignments)
// update pod entries directly.
// if one of subsequent steps is failed, we will delete current allocationInfo from podEntries in defer function of allocation function.
p.state.SetAllocationInfo(allocationInfo.PodUid, allocationInfo.ContainerName, allocationInfo)
podEntries := p.state.GetPodEntries()
updatedMachineState, err := generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
if err != nil {
general.Errorf("pod: %s/%s, container: %s GenerateMachineStateFromPodEntries failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("GenerateMachineStateFromPodEntries failed with error: %v", err)
}
resp, err := util.PackAllocationResponse(allocationInfo, string(v1.ResourceCPU), util.OCIPropertyNameCPUSetCPUs, false, true, req)
if err != nil {
general.Errorf("pod: %s/%s, container: %s packAllocationResponse failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("PackResourceAllocationResponseByAllocationInfo failed with error: %v", err)
}
p.state.SetMachineState(updatedMachineState)
return resp, nil
}
func (p *DynamicPolicy) dedicatedCoresAllocationHandler(ctx context.Context,
req *pluginapi.ResourceRequest) (*pluginapi.ResourceAllocationResponse, error) {
if req == nil {
return nil, fmt.Errorf("dedicatedCoresAllocationHandler got nil req")
}
switch req.Annotations[apiconsts.PodAnnotationMemoryEnhancementNumaBinding] {
case apiconsts.PodAnnotationMemoryEnhancementNumaBindingEnable:
return p.dedicatedCoresWithNUMABindingAllocationHandler(ctx, req)
default:
return p.dedicatedCoresWithoutNUMABindingAllocationHandler(ctx, req)
}
}
func (p *DynamicPolicy) dedicatedCoresWithoutNUMABindingAllocationHandler(_ context.Context,
_ *pluginapi.ResourceRequest) (*pluginapi.ResourceAllocationResponse, error) {
// todo: support dedicated_cores without NUMA binding
return nil, fmt.Errorf("not support dedicated_cores without NUMA binding")
}
func (p *DynamicPolicy) dedicatedCoresWithNUMABindingAllocationHandler(ctx context.Context,
req *pluginapi.ResourceRequest) (*pluginapi.ResourceAllocationResponse, error) {
if req.ContainerType == pluginapi.ContainerType_SIDECAR {
return p.dedicatedCoresWithNUMABindingAllocationSidecarHandler(ctx, req)
}
var machineState state.NUMANodeMap
oldAllocationInfo := p.state.GetAllocationInfo(req.PodUid, req.ContainerName)
if oldAllocationInfo == nil {
machineState = p.state.GetMachineState()
} else {
p.state.Delete(req.PodUid, req.ContainerName)
podEntries := p.state.GetPodEntries()
var err error
machineState, err = generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
if err != nil {
general.Errorf("pod: %s/%s, container: %s GenerateMachineStateFromPodEntries failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("GenerateMachineStateFromPodEntries failed with error: %v", err)
}
}
reqInt, err := util.GetQuantityFromResourceReq(req)
if err != nil {
return nil, fmt.Errorf("getReqQuantityFromResourceReq failed with error: %v", err)
}
result, err := p.allocateNumaBindingCPUs(reqInt, req.Hint, machineState, req.Annotations)
if err != nil {
general.ErrorS(err, "unable to allocate CPUs",
"podNamespace", req.PodNamespace,
"podName", req.PodName,
"containerName", req.ContainerName,
"numCPUs", reqInt)
return nil, err
}
general.InfoS("allocate CPUs successfully",
"podNamespace", req.PodNamespace,
"podName", req.PodName,
"containerName", req.ContainerName,
"numCPUs", reqInt,
"result", result.String())
topologyAwareAssignments, err := machine.GetNumaAwareAssignments(p.machineInfo.CPUTopology, result)
if err != nil {
general.ErrorS(err, "unable to calculate topologyAwareAssignments",
"podNamespace", req.PodNamespace,
"podName", req.PodName,
"containerName", req.ContainerName,
"numCPUs", reqInt,
"result cpuset", result.String())
return nil, err
}
allocationInfo := &state.AllocationInfo{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType.String(),
ContainerIndex: req.ContainerIndex,
RampUp: true,
PodRole: req.PodRole,
PodType: req.PodType,
OwnerPoolName: state.PoolNameDedicated,
AllocationResult: result.Clone(),
OriginalAllocationResult: result.Clone(),
TopologyAwareAssignments: topologyAwareAssignments,
OriginalTopologyAwareAssignments: machine.DeepcopyCPUAssignment(topologyAwareAssignments),
InitTimestamp: time.Now().Format(util.QRMTimeFormat),
QoSLevel: apiconsts.PodAnnotationQoSLevelDedicatedCores,
Labels: general.DeepCopyMap(req.Labels),
Annotations: general.DeepCopyMap(req.Annotations),
RequestQuantity: reqInt,
}
// update pod entries directly.
// if one of subsequent steps is failed, we will delete current allocationInfo from podEntries in defer function of allocation function.
p.state.SetAllocationInfo(allocationInfo.PodUid, allocationInfo.ContainerName, allocationInfo)
podEntries := p.state.GetPodEntries()
updatedMachineState, err := generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
if err != nil {
general.Errorf("pod: %s/%s, container: %s GenerateMachineStateFromPodEntries failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("GenerateMachineStateFromPodEntries failed with error: %v", err)
}
p.state.SetMachineState(updatedMachineState)
err = p.adjustAllocationEntries()
if err != nil {
general.Errorf("pod: %s/%s, container: %s putContainersAndAdjustAllocationEntriesWithoutAllocation failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("adjustAllocationEntries failed with error: %v", err)
}
resp, err := util.PackAllocationResponse(allocationInfo, string(v1.ResourceCPU), util.OCIPropertyNameCPUSetCPUs, false, true, req)
if err != nil {
general.Errorf("pod: %s/%s, container: %s PackResourceAllocationResponseByAllocationInfo failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("PackResourceAllocationResponseByAllocationInfo failed with error: %v", err)
}
return resp, nil
}
// dedicatedCoresWithNUMABindingAllocationSidecarHandler currently we set cpuset of sidecar to the cpuset of its main container
func (p *DynamicPolicy) dedicatedCoresWithNUMABindingAllocationSidecarHandler(_ context.Context,
req *pluginapi.ResourceRequest) (*pluginapi.ResourceAllocationResponse, error) {
reqInt, err := util.GetQuantityFromResourceReq(req)
if err != nil {
return nil, fmt.Errorf("getReqQuantityFromResourceReq failed with error: %v", err)
}
podEntries := p.state.GetPodEntries()
if podEntries[req.PodUid] == nil {
general.Infof("there is no pod entry, pod: %s/%s, sidecar: %s, waiting next reconcile",
req.PodNamespace, req.PodName, req.ContainerName)
return &pluginapi.ResourceAllocationResponse{}, nil
}
mainContainerAllocationInfo := podEntries[req.PodUid].GetMainContainerEntry()
// todo: consider sidecar without reconcile in vpa
if mainContainerAllocationInfo == nil {
general.Infof("main container is not found for pod: %s/%s, sidecar: %s, waiting next reconcile",
req.PodNamespace, req.PodName, req.ContainerName)
return &pluginapi.ResourceAllocationResponse{}, nil
}
allocationInfo := &state.AllocationInfo{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType.String(),
ContainerIndex: req.ContainerIndex,
PodRole: req.PodRole,
PodType: req.PodType,
AllocationResult: mainContainerAllocationInfo.AllocationResult.Clone(),
OriginalAllocationResult: mainContainerAllocationInfo.OriginalAllocationResult.Clone(),
TopologyAwareAssignments: machine.DeepcopyCPUAssignment(mainContainerAllocationInfo.TopologyAwareAssignments),
OriginalTopologyAwareAssignments: machine.DeepcopyCPUAssignment(mainContainerAllocationInfo.OriginalTopologyAwareAssignments),
InitTimestamp: time.Now().Format(util.QRMTimeFormat),
QoSLevel: apiconsts.PodAnnotationQoSLevelDedicatedCores,
Labels: general.DeepCopyMap(req.Labels),
Annotations: general.DeepCopyMap(req.Annotations),
RequestQuantity: reqInt,
}
// update pod entries directly.
// if one of subsequent steps is failed, we will delete current allocationInfo from podEntries in defer function of allocation function.
p.state.SetAllocationInfo(allocationInfo.PodUid, allocationInfo.ContainerName, allocationInfo)
podEntries = p.state.GetPodEntries()
updatedMachineState, err := generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
if err != nil {
general.Errorf("pod: %s/%s, container: %s GenerateMachineStateFromPodEntries failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("GenerateMachineStateFromPodEntries failed with error: %v", err)
}
p.state.SetMachineState(updatedMachineState)
resp, err := util.PackAllocationResponse(allocationInfo, string(v1.ResourceCPU), util.OCIPropertyNameCPUSetCPUs, false, true, req)
if err != nil {
general.Errorf("pod: %s/%s, container: %s packAllocationResponse failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("PackResourceAllocationResponseByAllocationInfo failed with error: %v", err)
}
return resp, nil
}
func (p *DynamicPolicy) allocateNumaBindingCPUs(numCPUs int, hint *pluginapi.TopologyHint,
machineState state.NUMANodeMap, reqAnnotations map[string]string) (machine.CPUSet, error) {
if hint == nil {
return machine.NewCPUSet(), fmt.Errorf("hint is nil")
} else if len(hint.Nodes) == 0 {
return machine.NewCPUSet(), fmt.Errorf("hint is empty")
} else if qosutil.AnnotationsIndicateNUMABinding(reqAnnotations) &&
!qosutil.AnnotationsIndicateNUMAExclusive(reqAnnotations) &&
len(hint.Nodes) > 1 {
return machine.NewCPUSet(), fmt.Errorf("NUMA not exclusive binding container has request larger than 1 NUMA")
}
result := machine.NewCPUSet()
alignedAvailableCPUs := machine.CPUSet{}
for _, numaNode := range hint.Nodes {
alignedAvailableCPUs = alignedAvailableCPUs.Union(machineState[int(numaNode)].GetAvailableCPUSet(p.reservedCPUs))
}
var alignedCPUs machine.CPUSet
if qosutil.AnnotationsIndicateNUMAExclusive(reqAnnotations) {
// todo: currently we hack dedicated_cores with NUMA binding take up whole NUMA,
// and we will modify strategy here if assumption above breaks.
alignedCPUs = alignedAvailableCPUs.Clone()
} else {
var err error
alignedCPUs, err = calculator.TakeByTopology(p.machineInfo, alignedAvailableCPUs, numCPUs)
if err != nil {
general.ErrorS(err, "take cpu for NUMA not exclusive binding container failed",
"hints", hint.Nodes,
"alignedAvailableCPUs", alignedAvailableCPUs.String())
return machine.NewCPUSet(),
fmt.Errorf("take cpu for NUMA not exclusive binding container failed with err: %v", err)
}
}
general.InfoS("allocate by hints",
"hints", hint.Nodes,
"alignedAvailableCPUs", alignedAvailableCPUs.String(),
"alignedAllocatedCPUs", alignedCPUs)
// currently, result equals to alignedCPUs,
// maybe extend cpus not aligned to meet requirement later
result = result.Union(alignedCPUs)
leftNumCPUs := numCPUs - result.Size()
if leftNumCPUs > 0 {
general.Errorf("result cpus: %s in hint NUMA nodes: %d with size: %d can't meet cpus request: %d",
result.String(), hint.Nodes, result.Size(), numCPUs)
return machine.NewCPUSet(), fmt.Errorf("results can't meet cpus request")
}
return result, nil
}
// putAllocationsAndAdjustAllocationEntries calculates and generates the latest checkpoint
// - unlike adjustAllocationEntries, it will also consider AllocationInfo
func (p *DynamicPolicy) putAllocationsAndAdjustAllocationEntries(allocationInfos []*state.AllocationInfo) error {
if len(allocationInfos) == 0 {
return nil
}
entries := p.state.GetPodEntries()
machineState := p.state.GetMachineState()
var poolsQuantityMap map[string]int
if p.enableCPUAdvisor {
// if sys advisor is enabled, we believe the pools' ratio that sys advisor indicates
poolsQuantityMap = machine.ParseCPUAssignmentQuantityMap(entries.GetFilteredPoolsCPUSetMap(state.ResidentPools))
} else {
// else we do sum(containers req) for each pool to get pools ratio
poolsQuantityMap = state.GetSharedQuantityMapFromPodEntries(entries, allocationInfos)
}
for _, allocationInfo := range allocationInfos {
if allocationInfo == nil {
return fmt.Errorf("found nil allocationInfo in input parameter")
} else if !state.CheckShared(allocationInfo) {
return fmt.Errorf("put container with invalid qos level: %s into pool", allocationInfo.QoSLevel)
}
poolName := allocationInfo.GetSpecifiedPoolName()
if poolName == advisorapi.EmptyOwnerPoolName {
return fmt.Errorf("allocationInfo points to empty poolName")
}
reqInt := state.GetContainerRequestedCores()(allocationInfo)
poolsQuantityMap[poolName] += reqInt
}
isolatedQuantityMap := state.GetIsolatedQuantityMapFromPodEntries(entries, allocationInfos)
err := p.adjustPoolsAndIsolatedEntries(poolsQuantityMap, isolatedQuantityMap, entries, machineState)
if err != nil {
return fmt.Errorf("adjustPoolsAndIsolatedEntries failed with error: %v", err)
}
return nil
}
// adjustAllocationEntries calculates and generates the latest checkpoint
func (p *DynamicPolicy) adjustAllocationEntries() error {
entries := p.state.GetPodEntries()
machineState := p.state.GetMachineState()
// since adjustAllocationEntries will cause re-generate pools,
// if sys advisor is enabled, we believe the pools' ratio that sys advisor indicates,
// else we do sum(containers req) for each pool to get pools ratio
var poolsQuantityMap map[string]int
if p.enableCPUAdvisor {
poolsQuantityMap = machine.ParseCPUAssignmentQuantityMap(entries.GetFilteredPoolsCPUSetMap(state.ResidentPools))
} else {
poolsQuantityMap = state.GetSharedQuantityMapFromPodEntries(entries, nil)
}
isolatedQuantityMap := state.GetIsolatedQuantityMapFromPodEntries(entries, nil)
err := p.adjustPoolsAndIsolatedEntries(poolsQuantityMap, isolatedQuantityMap, entries, machineState)
if err != nil {
return fmt.Errorf("adjustPoolsAndIsolatedEntries failed with error: %v", err)
}
return nil
}
// adjustPoolsAndIsolatedEntries works for the following steps
// 1. calculate pools and isolated cpusets according to expectant quantities
// 2. make reclaimed overlap with numa-binding
// 3. apply them to local state
// 4. clean pools
func (p *DynamicPolicy) adjustPoolsAndIsolatedEntries(poolsQuantityMap map[string]int,
isolatedQuantityMap map[string]map[string]int, entries state.PodEntries, machineState state.NUMANodeMap) error {
availableCPUs := machineState.GetFilteredAvailableCPUSet(p.reservedCPUs, nil, state.CheckDedicatedNUMABinding)
poolsCPUSet, isolatedCPUSet, err := p.generatePoolsAndIsolation(poolsQuantityMap, isolatedQuantityMap, availableCPUs)
if err != nil {
return fmt.Errorf("generatePoolsAndIsolation failed with error: %v", err)
}
err = p.reclaimOverlapNUMABinding(poolsCPUSet, entries)
if err != nil {
return fmt.Errorf("reclaimOverlapNUMABinding failed with error: %v", err)
}
err = p.applyPoolsAndIsolatedInfo(poolsCPUSet, isolatedCPUSet, entries, machineState)
if err != nil {
return fmt.Errorf("applyPoolsAndIsolatedInfo failed with error: %v", err)
}
err = p.cleanPools()
if err != nil {
return fmt.Errorf("cleanPools failed with error: %v", err)
}
return nil
}
// reclaimOverlapNUMABinding unions calculated reclaim pool in empty NUMAs
// with the intersection of previous reclaim pool and non-ramp-up dedicated_cores numa_binding containers
func (p *DynamicPolicy) reclaimOverlapNUMABinding(poolsCPUSet map[string]machine.CPUSet, entries state.PodEntries) error {
// reclaimOverlapNUMABinding only works with cpu advisor and reclaim enabled
if !(p.enableCPUAdvisor && p.dynamicConfig.GetDynamicConfiguration().EnableReclaim) {
return nil
}
if entries.CheckPoolEmpty(state.PoolNameReclaim) {
return fmt.Errorf("reclaim pool misses in current entries")
}
curReclaimCPUSet := entries[state.PoolNameReclaim][advisorapi.FakedContainerName].AllocationResult.Clone()
nonOverlapReclaimCPUSet := poolsCPUSet[state.PoolNameReclaim].Clone()
general.Infof("curReclaimCPUSet: %s", curReclaimCPUSet.String())
for _, containerEntries := range entries {
if containerEntries.IsPoolEntry() {
continue
}
for _, allocationInfo := range containerEntries {
if !(allocationInfo != nil && state.CheckDedicatedNUMABinding(allocationInfo) && allocationInfo.CheckMainContainer()) {
continue
} else if allocationInfo.RampUp {
general.Infof("dedicated numa_binding pod: %s/%s container: %s is in ramp up, not to overlap reclaim pool with it",
allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName)
continue
}
poolsCPUSet[state.PoolNameReclaim] = poolsCPUSet[state.PoolNameReclaim].Union(curReclaimCPUSet.Intersection(allocationInfo.AllocationResult))
}
}
if poolsCPUSet[state.PoolNameReclaim].IsEmpty() {
return fmt.Errorf("reclaim pool is empty after overlapping with dedicated_cores numa_binding containers")
}
general.Infof("nonOverlapReclaimCPUSet: %s, finalReclaimCPUSet: %s", nonOverlapReclaimCPUSet.String(), poolsCPUSet[state.PoolNameReclaim].String())
return nil
}
// applyPoolsAndIsolatedInfo generates the latest checkpoint by pools and isolated cpusets calculation results.
// 1. construct entries for isolated containers (probably be dedicated_cores not numa_binding )
// 2. construct entries for all pools
// 3. construct entries for shared and reclaimed containers
func (p *DynamicPolicy) applyPoolsAndIsolatedInfo(poolsCPUSet map[string]machine.CPUSet,
isolatedCPUSet map[string]map[string]machine.CPUSet, curEntries state.PodEntries, machineState state.NUMANodeMap) error {
newPodEntries := make(state.PodEntries)
unionDedicatedIsolatedCPUSet := machine.NewCPUSet()
// walk through all isolated CPUSet map to store those pods/containers in pod entries
for podUID, containerEntries := range isolatedCPUSet {
for containerName, isolatedCPUs := range containerEntries {
allocationInfo := curEntries[podUID][containerName]
if allocationInfo == nil {
general.Errorf("isolated pod: %s, container: %s without entry in current checkpoint", podUID, containerName)
continue
} else if !state.CheckDedicated(allocationInfo) || state.CheckNUMABinding(allocationInfo) {
general.Errorf("isolated pod: %s, container: %s isn't dedicated_cores without NUMA binding", podUID, containerName)
continue
}
topologyAwareAssignments, err := machine.GetNumaAwareAssignments(p.machineInfo.CPUTopology, isolatedCPUs)
if err != nil {
general.ErrorS(err, "Unable to calculate topologyAwareAssignments",
"podNamespace", allocationInfo.PodNamespace,
"podName", allocationInfo.PodName,
"containerName", allocationInfo.ContainerName,
"result cpuset", isolatedCPUs.String())
continue
}
general.InfoS("isolate info",
"podNamespace", allocationInfo.PodNamespace,
"podName", allocationInfo.PodName,
"containerName", allocationInfo.ContainerName,
"result cpuset", isolatedCPUs.String(),
"result cpuset size", isolatedCPUs.Size(),
"qosLevel", allocationInfo.QoSLevel)
if newPodEntries[podUID] == nil {
newPodEntries[podUID] = make(state.ContainerEntries)
}
newPodEntries[podUID][containerName] = allocationInfo.Clone()
newPodEntries[podUID][containerName].OwnerPoolName = state.PoolNameDedicated
newPodEntries[podUID][containerName].AllocationResult = isolatedCPUs.Clone()
newPodEntries[podUID][containerName].OriginalAllocationResult = isolatedCPUs.Clone()
newPodEntries[podUID][containerName].TopologyAwareAssignments = topologyAwareAssignments
newPodEntries[podUID][containerName].OriginalTopologyAwareAssignments = machine.DeepcopyCPUAssignment(topologyAwareAssignments)
unionDedicatedIsolatedCPUSet = unionDedicatedIsolatedCPUSet.Union(isolatedCPUs)
}
}
_ = p.emitter.StoreInt64(util.MetricNameIsolatedPodNum, int64(len(newPodEntries)), metrics.MetricTypeNameRaw)
if poolsCPUSet[state.PoolNameReclaim].IsEmpty() {
return fmt.Errorf("entry: %s is empty", state.PoolNameShare)
}
// walk through all pools CPUSet map to store those pools in pod entries
for poolName, cset := range poolsCPUSet {
general.Infof("try to apply pool %s: %s", poolName, cset.String())
topologyAwareAssignments, err := machine.GetNumaAwareAssignments(p.machineInfo.CPUTopology, cset)
if err != nil {
return fmt.Errorf("unable to calculate topologyAwareAssignments for pool: %s, result cpuset: %s, error: %v",
poolName, cset.String(), err)
}
allocationInfo := curEntries[poolName][advisorapi.FakedContainerName]
if allocationInfo != nil {
general.Infof("pool: %s allocation result transform from %s to %s",
poolName, allocationInfo.AllocationResult.String(), cset.String())
}
if newPodEntries[poolName] == nil {
newPodEntries[poolName] = make(state.ContainerEntries)
}
newPodEntries[poolName][advisorapi.FakedContainerName] = &state.AllocationInfo{
PodUid: poolName,
OwnerPoolName: poolName,
AllocationResult: cset.Clone(),
OriginalAllocationResult: cset.Clone(),
TopologyAwareAssignments: topologyAwareAssignments,
OriginalTopologyAwareAssignments: machine.DeepcopyCPUAssignment(topologyAwareAssignments),
}
_ = p.emitter.StoreInt64(util.MetricNamePoolSize, int64(cset.Size()), metrics.MetricTypeNameRaw,
metrics.MetricTag{Key: "poolName", Val: poolName})
}
// rampUpCPUs includes common reclaimed pool
rampUpCPUs := machineState.GetFilteredAvailableCPUSet(p.reservedCPUs,
nil, state.CheckDedicatedNUMABinding).Difference(unionDedicatedIsolatedCPUSet)
rampUpCPUsTopologyAwareAssignments, err := machine.GetNumaAwareAssignments(p.machineInfo.CPUTopology, rampUpCPUs)
if err != nil {
return fmt.Errorf("unable to calculate topologyAwareAssignments for rampUpCPUs, result cpuset: %s, error: %v",
rampUpCPUs.String(), err)
}
// walk through current pod entries to handle container-related entries (besides pooled entries)
for podUID, containerEntries := range curEntries {
if containerEntries.IsPoolEntry() {
continue
}
containerLoop:
for containerName, allocationInfo := range containerEntries {
if allocationInfo == nil {
general.Errorf("pod: %s, container: %s has nil allocationInfo", podUID, containerName)
continue
}
reqInt := state.GetContainerRequestedCores()(allocationInfo)
if newPodEntries[podUID][containerName] != nil {
// adapt to old checkpoint without RequestQuantity property
newPodEntries[podUID][containerName].RequestQuantity = reqInt
general.Infof("pod: %s/%s, container: %s, qosLevel: %s is isolated, ignore original allocationInfo",
allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName, allocationInfo.QoSLevel)
continue
}
if newPodEntries[podUID] == nil {
newPodEntries[podUID] = make(state.ContainerEntries)
}
newPodEntries[podUID][containerName] = allocationInfo.Clone()
switch allocationInfo.QoSLevel {
case apiconsts.PodAnnotationQoSLevelDedicatedCores:
ownerPoolName := allocationInfo.GetOwnerPoolName()
if ownerPoolName == advisorapi.EmptyOwnerPoolName {
ownerPoolName = allocationInfo.GetSpecifiedPoolName()
}
newPodEntries[podUID][containerName].OwnerPoolName = ownerPoolName
// for numa_binding containers, we just clone checkpoint already exist
if state.CheckDedicatedNUMABinding(allocationInfo) {
continue containerLoop
}
// dedicated_cores with numa_binding is not isolated, we will try to isolate it in next adjustment.
general.Warningf("pod: %s/%s, container: %s isa dedicated_cores with numa_binding but not isolated, "+
"we put it into fallback pool: %s temporary",
allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName, rampUpCPUs.String())
newPodEntries[podUID][containerName].OwnerPoolName = state.PoolNameFallback
newPodEntries[podUID][containerName].AllocationResult = rampUpCPUs.Clone()
newPodEntries[podUID][containerName].OriginalAllocationResult = rampUpCPUs.Clone()
newPodEntries[podUID][containerName].TopologyAwareAssignments = machine.DeepcopyCPUAssignment(rampUpCPUsTopologyAwareAssignments)
newPodEntries[podUID][containerName].OriginalTopologyAwareAssignments = machine.DeepcopyCPUAssignment(rampUpCPUsTopologyAwareAssignments)
case apiconsts.PodAnnotationQoSLevelSharedCores, apiconsts.PodAnnotationQoSLevelReclaimedCores:
ownerPoolName := allocationInfo.GetPoolName()
if allocationInfo.RampUp {
general.Infof("pod: %s/%s container: %s is in ramp up, set its allocation result from %s to rampUpCPUs :%s",
allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName,
allocationInfo.AllocationResult.String(), rampUpCPUs.String())
newPodEntries[podUID][containerName].OwnerPoolName = advisorapi.EmptyOwnerPoolName
newPodEntries[podUID][containerName].AllocationResult = rampUpCPUs.Clone()
newPodEntries[podUID][containerName].OriginalAllocationResult = rampUpCPUs.Clone()
newPodEntries[podUID][containerName].TopologyAwareAssignments = machine.DeepcopyCPUAssignment(rampUpCPUsTopologyAwareAssignments)
newPodEntries[podUID][containerName].OriginalTopologyAwareAssignments = machine.DeepcopyCPUAssignment(rampUpCPUsTopologyAwareAssignments)
} else if newPodEntries[ownerPoolName][advisorapi.FakedContainerName] == nil {
general.Warningf("pod: %s/%s container: %s get owner pool: %s allocationInfo failed. reuse its allocation result: %s",
allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName,
ownerPoolName, allocationInfo.AllocationResult.String())
} else {
poolEntry := newPodEntries[ownerPoolName][advisorapi.FakedContainerName]
general.Infof("put pod: %s/%s container: %s to pool: %s, set its allocation result from %s to %s",
allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName,
ownerPoolName, allocationInfo.AllocationResult.String(), poolEntry.AllocationResult.String())
newPodEntries[podUID][containerName].OwnerPoolName = ownerPoolName
newPodEntries[podUID][containerName].AllocationResult = poolEntry.AllocationResult.Clone()
newPodEntries[podUID][containerName].OriginalAllocationResult = poolEntry.OriginalAllocationResult.Clone()
newPodEntries[podUID][containerName].TopologyAwareAssignments = machine.DeepcopyCPUAssignment(poolEntry.TopologyAwareAssignments)
newPodEntries[podUID][containerName].OriginalTopologyAwareAssignments = machine.DeepcopyCPUAssignment(poolEntry.TopologyAwareAssignments)
}
default:
return fmt.Errorf("invalid qosLevel: %s for pod: %s/%s container: %s",
allocationInfo.QoSLevel, allocationInfo.PodNamespace,
allocationInfo.PodName, allocationInfo.ContainerName)
}
}
}
// use pod entries generated above to generate machine state info, and store in local state
machineState, err = generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, newPodEntries)
if err != nil {
return fmt.Errorf("calculate machineState by newPodEntries failed with error: %v", err)
}
p.state.SetPodEntries(newPodEntries)
p.state.SetMachineState(machineState)
return nil
}
// generatePoolsAndIsolation is used to generate cpuset pools and isolated cpuset
// 1. allocate isolated cpuset for pod/containers, and divide total cores evenly if not possible to allocate
// 2. use the left cores to allocate among different pools
// 3. apportion to other pools if reclaimed is disabled
func (p *DynamicPolicy) generatePoolsAndIsolation(poolsQuantityMap map[string]int,
isolatedQuantityMap map[string]map[string]int, availableCPUs machine.CPUSet) (poolsCPUSet map[string]machine.CPUSet,
isolatedCPUSet map[string]map[string]machine.CPUSet, err error) {
// clear pool map with zero quantity
for poolName, quantity := range poolsQuantityMap {
if quantity == 0 {
general.Warningf("pool: %s with 0 quantity, skip generate", poolName)
delete(poolsQuantityMap, poolName)
}
}
// clear isolated map with zero quantity
for podUID, containerEntries := range isolatedQuantityMap {
for containerName, quantity := range containerEntries {
if quantity == 0 {
general.Warningf("isolated pod: %s, container: %s with 0 quantity, skip generate it", podUID, containerName)
delete(containerEntries, containerName)
}
}
if len(containerEntries) == 0 {
general.Warningf(" isolated pod: %s all container entries skipped", podUID)
delete(isolatedQuantityMap, podUID)
}
}
availableSize := availableCPUs.Size()
poolsCPUSet = make(map[string]machine.CPUSet)
poolsTotalQuantity := general.SumUpMapValues(poolsQuantityMap)
isolatedCPUSet = make(map[string]map[string]machine.CPUSet)
isolatedTotalQuantity := general.SumUpMultipleMapValues(isolatedQuantityMap)
general.Infof("isolatedTotalQuantity: %d, poolsTotalQuantity: %d, availableSize: %d",
isolatedTotalQuantity, poolsTotalQuantity, availableSize)
var tErr error
if poolsTotalQuantity+isolatedTotalQuantity <= availableSize {
general.Infof("all pools and isolated containers could be allocated")
isolatedCPUSet, availableCPUs, tErr = p.takeCPUsForContainers(isolatedQuantityMap, availableCPUs)
if tErr != nil {
err = fmt.Errorf("allocate isolated cpus for dedicated_cores failed with error: %v", tErr)
return
}
poolsCPUSet, availableCPUs, tErr = p.takeCPUsForPools(poolsQuantityMap, availableCPUs)
if tErr != nil {
err = fmt.Errorf("allocate cpus for pools failed with error: %v", tErr)
return
}
} else if poolsTotalQuantity <= availableSize {
general.Infof("all pools could be allocated, all isolated containers would be put to pools")
poolsCPUSet, availableCPUs, tErr = p.takeCPUsForPools(poolsQuantityMap, availableCPUs)
if tErr != nil {
err = fmt.Errorf("allocate cpus for pools failed with error: %v", tErr)
return
}
} else if poolsTotalQuantity > 0 {
general.Infof("can't allocate for all pools")
totalProportionalPoolsQuantity := 0
proportionalPoolsQuantityMap := make(map[string]int)
for poolName, poolQuantity := range poolsQuantityMap {
proportionalSize := general.Max(getProportionalSize(poolQuantity, poolsTotalQuantity, availableSize), 1)
proportionalPoolsQuantityMap[poolName] = proportionalSize
totalProportionalPoolsQuantity += proportionalSize
}
// corner case: after divide, the total count goes to be bigger than available total
for totalProportionalPoolsQuantity > availableSize {
curTotalProportionalPoolsQuantity := totalProportionalPoolsQuantity
for poolName, quantity := range proportionalPoolsQuantityMap {
if quantity > 1 && totalProportionalPoolsQuantity > 0 {
quantity--
totalProportionalPoolsQuantity--
proportionalPoolsQuantityMap[poolName] = quantity
}
}
// availableSize can't satisfy every pool has at least one cpu
if curTotalProportionalPoolsQuantity == totalProportionalPoolsQuantity {
break
}
}
general.Infof("poolsQuantityMap: %v, proportionalPoolsQuantityMap: %v", poolsQuantityMap, proportionalPoolsQuantityMap)
// availableSize can't satisfy every pool has at least one cpu,
// we make all pools equals to availableCPUs in this case.
if totalProportionalPoolsQuantity > availableSize {
for poolName := range poolsQuantityMap {
poolsCPUSet[poolName] = availableCPUs.Clone()
}
} else {
poolsCPUSet, availableCPUs, tErr = p.takeCPUsForPools(proportionalPoolsQuantityMap, availableCPUs)
if tErr != nil {
err = fmt.Errorf("allocate cpus for pools failed with error: %v", tErr)
return
}
}
}
if poolsCPUSet[state.PoolNameReserve].IsEmpty() {
poolsCPUSet[state.PoolNameReserve] = p.reservedCPUs.Clone()
general.Infof("set pool %s:%s", state.PoolNameReserve, poolsCPUSet[state.PoolNameReserve].String())
} else {
err = fmt.Errorf("static pool %s result: %s is generated dynamically", state.PoolNameReserve, poolsCPUSet[state.PoolNameReserve].String())
return
}
poolsCPUSet[state.PoolNameReclaim] = poolsCPUSet[state.PoolNameReclaim].Union(availableCPUs)
if poolsCPUSet[state.PoolNameReclaim].IsEmpty() {
// for reclaimed pool, we must make them exist when the node isn't in hybrid mode even if cause overlap
allAvailableCPUs := p.machineInfo.CPUDetails.CPUs().Difference(p.reservedCPUs)
reclaimedCPUSet, _, tErr := calculator.TakeByNUMABalance(p.machineInfo, allAvailableCPUs, reservedReclaimedCPUsSize)
if tErr != nil {
err = fmt.Errorf("fallback takeByNUMABalance faild in generatePoolsAndIsolation for reclaimedCPUSet with error: %v", tErr)
return
}
general.Infof("fallback takeByNUMABalance in generatePoolsAndIsolation for reclaimedCPUSet: %s", reclaimedCPUSet.String())
poolsCPUSet[state.PoolNameReclaim] = reclaimedCPUSet
}
enableReclaim := p.dynamicConfig.GetDynamicConfiguration().EnableReclaim
if !enableReclaim && poolsCPUSet[state.PoolNameReclaim].Size() > reservedReclaimedCPUsSize {
poolsCPUSet[state.PoolNameReclaim] = p.apportionReclaimedPool(poolsCPUSet, poolsCPUSet[state.PoolNameReclaim].Clone())
general.Infof("apportionReclaimedPool finished, current %s pool: %s",
state.PoolNameReclaim, poolsCPUSet[state.PoolNameReclaim].String())
}
return
}
// apportionReclaimedPool tries to allocate reclaimed cores to none-reclaimed pools.
// if we disable reclaim on current node, this could be used a down-grade strategy
// to disable reclaimed workloads in emergency
func (p *DynamicPolicy) apportionReclaimedPool(poolsCPUSet map[string]machine.CPUSet, reclaimedCPUs machine.CPUSet) machine.CPUSet {
totalSize := 0
for poolName, poolCPUs := range poolsCPUSet {
if state.ResidentPools.Has(poolName) {
continue
}
totalSize += poolCPUs.Size()
}
availableSize := reclaimedCPUs.Size() - reservedReclaimedCPUsSize
if availableSize <= 0 || totalSize == 0 {
return reclaimedCPUs
}
for poolName, poolCPUs := range poolsCPUSet {
if state.ResidentPools.Has(poolName) {
continue
}
proportionalSize := general.Max(getProportionalSize(poolCPUs.Size(), totalSize, availableSize), 1)
var err error
var cpuset machine.CPUSet
cpuset, reclaimedCPUs, err = calculator.TakeByNUMABalance(p.machineInfo, reclaimedCPUs, proportionalSize)
if err != nil {
general.Errorf("take %d cpus from reclaimedCPUs: %s, size: %d failed with error: %v",
proportionalSize, reclaimedCPUs.String(), reclaimedCPUs.Size(), err)
return reclaimedCPUs
}
poolsCPUSet[poolName] = poolCPUs.Union(cpuset)
general.Infof("take %s to %s; prev: %s, current: %s", cpuset.String(), poolName, poolCPUs.String(), poolsCPUSet[poolName].String())
if reclaimedCPUs.Size() <= reservedReclaimedCPUsSize {
break
}
}
return reclaimedCPUs
}
// takeCPUsForPools tries to allocate cpuset for each given pool,
// and it will consider the total available cpuset during calculation.
// the returned value includes cpuset pool map and remaining available cpuset.
func (p *DynamicPolicy) takeCPUsForPools(poolsQuantityMap map[string]int,