Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions pkg/agent/qrm-plugins/cpu/dynamicpolicy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,15 +685,22 @@ func (p *DynamicPolicy) GetResourcesAllocation(_ context.Context,
if podResources[podUID].ContainerResources == nil {
podResources[podUID].ContainerResources = make(map[string]*pluginapi.ResourceAllocation)
}

topologyAssignments := make(map[uint64]uint64)
for numaID, cset := range allocationInfo.TopologyAwareAssignments {
topologyAssignments[uint64(numaID)] = uint64(cset.Size())
}

podResources[podUID].ContainerResources[containerName] = &pluginapi.ResourceAllocation{
ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
string(v1.ResourceCPU): {
OciPropertyName: util.OCIPropertyNameCPUSetCPUs,
IsNodeResource: false,
IsScalarResource: true,
AllocatedQuantity: float64(allocationInfo.AllocationResult.Size()),
AllocationResult: allocationInfo.AllocationResult.String(),
Annotations: general.DeepCopyMap(allocationInfo.Annotations),
OciPropertyName: util.OCIPropertyNameCPUSetCPUs,
IsNodeResource: false,
IsScalarResource: true,
AllocatedQuantity: float64(allocationInfo.AllocationResult.Size()),
AllocationResult: allocationInfo.AllocationResult.String(),
TopologyAssignments: topologyAssignments,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use of TopologyAssignments?

Annotations: general.DeepCopyMap(allocationInfo.Annotations),
},
},
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/agent/qrm-plugins/cpu/dynamicpolicy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5776,6 +5776,12 @@ func TestGetResourcesAllocation(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 10,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
0: 3,
1: 3,
2: 2,
3: 2,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
},
Expand Down Expand Up @@ -5803,6 +5809,12 @@ func TestGetResourcesAllocation(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 10,
AllocationResult: machine.NewCPUSet(1, 3, 4, 5, 6, 7, 8, 9, 10, 11).String(),
TopologyAssignments: map[uint64]uint64{
0: 3,
1: 3,
2: 2,
3: 2,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
},
Expand Down Expand Up @@ -5843,6 +5855,10 @@ func TestGetResourcesAllocation(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 4,
AllocationResult: machine.NewCPUSet(12, 13, 14, 15).String(),
TopologyAssignments: map[uint64]uint64{
uint64(2): 2,
uint64(3): 2,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelReclaimedCores,
},
Expand Down Expand Up @@ -5909,6 +5925,12 @@ func TestGetResourcesAllocation(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 14,
AllocationResult: machine.NewCPUSet(1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 3,
uint64(1): 3,
uint64(2): 4,
uint64(3): 4,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
},
Expand Down
72 changes: 72 additions & 0 deletions pkg/agent/qrm-plugins/cpu/dynamicpolicy/resize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func TestSNBVPA(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 3, // 分配到numa0 (cpu0 -> reserved, cpu1,cpu8,cpu9 for snb)
AllocationResult: "1,8-9",
TopologyAssignments: map[uint64]uint64{
uint64(0): 3,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable,
Expand Down Expand Up @@ -216,6 +219,9 @@ func TestSNBVPA(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 3, // 分配到numa0 (cpu0 -> reserved, cpu1,cpu8,cpu9 for snb)
AllocationResult: "1,8-9",
TopologyAssignments: map[uint64]uint64{
uint64(0): 3,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable,
Expand Down Expand Up @@ -374,6 +380,9 @@ func TestSNBInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 11, // 分配到numa0 (cpu0 -> reserved, cpu1~cpu5,cpu24~cpu29 for snb)
AllocationResult: "1-5,24-29",
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable,
Expand Down Expand Up @@ -404,6 +413,9 @@ func TestSNBInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 11, // 分配到numa0 (cpu0 -> reserved, cpu1~cpu5,cpu24~cpu29 for snb)
AllocationResult: "1-5,24-29",
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable,
Expand Down Expand Up @@ -487,6 +499,9 @@ func TestSNBInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 11, // 分配到numa0 (cpu0 -> reserved, cpu1~cpu5,cpu24~cpu29 for snb)
AllocationResult: "1-5,24-29",
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable,
Expand All @@ -506,6 +521,9 @@ func TestSNBInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 11, // 分配到numa0 (cpu0 -> reserved, cpu1~cpu5,cpu24~cpu29 for snb)
AllocationResult: "1-5,24-29",
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable,
Expand Down Expand Up @@ -639,6 +657,9 @@ func TestSNBInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 11, // 分配到numa0 (cpu0 -> reserved, cpu1~cpu5,cpu24~cpu29 for snb)
AllocationResult: "1-5,24-29",
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable,
Expand All @@ -658,6 +679,9 @@ func TestSNBInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 11, // 分配到numa0 (cpu0 -> reserved, cpu1~cpu5,cpu24~cpu29 for snb)
AllocationResult: "1-5,24-29",
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable,
Expand Down Expand Up @@ -757,6 +781,12 @@ func TestNonBindingShareCoresInplaceUpdateResize(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 10,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 3,
uint64(1): 3,
uint64(2): 2,
uint64(3): 2,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
},
Expand Down Expand Up @@ -829,6 +859,12 @@ func TestNonBindingShareCoresInplaceUpdateResize(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 10,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 3,
uint64(1): 3,
uint64(2): 2,
uint64(3): 2,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationInplaceUpdateResizingKey: "true",
Expand Down Expand Up @@ -915,6 +951,12 @@ func TestNonBindingShareCoresInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 42,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
uint64(1): 11,
uint64(2): 10,
uint64(3): 10,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationAggregatedRequestsKey: "{\"cpu\":\"3\"}",
Expand Down Expand Up @@ -968,6 +1010,12 @@ func TestNonBindingShareCoresInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 42,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
uint64(1): 11,
uint64(2): 10,
uint64(3): 10,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationAggregatedRequestsKey: "{\"cpu\":\"3\"}",
Expand Down Expand Up @@ -1030,6 +1078,12 @@ func TestNonBindingShareCoresInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 42,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
uint64(1): 11,
uint64(2): 10,
uint64(3): 10,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationInplaceUpdateResizingKey: "true",
Expand All @@ -1046,6 +1100,12 @@ func TestNonBindingShareCoresInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 42,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
uint64(1): 11,
uint64(2): 10,
uint64(3): 10,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationInplaceUpdateResizingKey: "true",
Expand Down Expand Up @@ -1117,6 +1177,12 @@ func TestNonBindingShareCoresInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 42,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
uint64(1): 11,
uint64(2): 10,
uint64(3): 10,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationInplaceUpdateResizingKey: "true",
Expand All @@ -1133,6 +1199,12 @@ func TestNonBindingShareCoresInplaceUpdateResizeWithSidecar(t *testing.T) {
IsScalarResource: true,
AllocatedQuantity: 42,
AllocationResult: cpuTopology.CPUDetails.CPUs().Difference(dynamicPolicy.reservedCPUs).Difference(reclaim.AllocationResult).String(),
TopologyAssignments: map[uint64]uint64{
uint64(0): 11,
uint64(1): 11,
uint64(2): 10,
uint64(3): 10,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores,
consts.PodAnnotationInplaceUpdateResizingKey: "true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strconv"
"time"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/samber/lo"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -394,43 +393,6 @@ func (p *DynamicPolicy) handleAdvisorResp(advisorResp *advisorsvc.ListAndWatchRe
return nil
}

func (p *DynamicPolicy) handleAdvisorMemoryHigh(
_ *config.Configuration,
_ interface{},
_ *dynamicconfig.DynamicAgentConfiguration,
emitter metrics.MetricEmitter,
metaServer *metaserver.MetaServer,
entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo, podResourceEntries state.PodResourceEntries,
) error {
memoryHighStr := calculationInfo.CalculationResult.Values[string(memoryadvisor.ControlKnobKeyMemoryHigh)]
memoryHigh, err := strconv.ParseInt(memoryHighStr, 10, 64)
if err != nil {
return fmt.Errorf("parse %s: %s failed with error: %v", memoryadvisor.ControlKnobKeyMemoryHigh, memoryHighStr, err)
}

if !cgroups.IsCgroup2UnifiedMode() {
general.Infof("memory.high is not supported in cgroupv1 mode")
return nil
}

if calculationInfo.CgroupPath != "" {
if err = cgroupmgr.ApplyMemoryWithRelativePath(calculationInfo.CgroupPath, &common.MemoryData{
HighInBytes: memoryHigh,
}); err != nil {
return fmt.Errorf("apply memory.high failed with error: %v", err)
}

_ = emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorMemoryHigh, memoryHigh,
metrics.MetricTypeNameRaw, metrics.ConvertMapToTags(map[string]string{
"cgroupPath": calculationInfo.CgroupPath,
})...)
return nil
}

return nil
}

func (p *DynamicPolicy) handleAdvisorMemoryLimitInBytes(
_ *config.Configuration,
_ interface{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//go:build linux
// +build linux

/*
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 (
"fmt"
"strconv"

"github.com/opencontainers/runc/libcontainer/cgroups"

"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/advisorsvc"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/memory/dynamicpolicy/memoryadvisor"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/memory/dynamicpolicy/state"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/util"
"github.com/kubewharf/katalyst-core/pkg/config"
dynamicconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
"github.com/kubewharf/katalyst-core/pkg/metaserver"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
cgroupmgr "github.com/kubewharf/katalyst-core/pkg/util/cgroup/manager"
"github.com/kubewharf/katalyst-core/pkg/util/general"
)

func (p *DynamicPolicy) handleAdvisorMemoryHigh(
_ *config.Configuration,
_ interface{},
_ *dynamicconfig.DynamicAgentConfiguration,
emitter metrics.MetricEmitter,
metaServer *metaserver.MetaServer,
entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo, podResourceEntries state.PodResourceEntries,
) error {
memoryHighStr := calculationInfo.CalculationResult.Values[string(memoryadvisor.ControlKnobKeyMemoryHigh)]
memoryHigh, err := strconv.ParseInt(memoryHighStr, 10, 64)
if err != nil {
return fmt.Errorf("parse %s: %s failed with error: %v", memoryadvisor.ControlKnobKeyMemoryHigh, memoryHighStr, err)
}

if !cgroups.IsCgroup2UnifiedMode() {
general.Infof("memory.high is not supported in cgroupv1 mode")
return nil
}

if calculationInfo.CgroupPath != "" {
if err = cgroupmgr.ApplyMemoryWithRelativePath(calculationInfo.CgroupPath, &common.MemoryData{
HighInBytes: memoryHigh,
}); err != nil {
return fmt.Errorf("apply memory.high failed with error: %v", err)
}

_ = emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorMemoryHigh, memoryHigh,
metrics.MetricTypeNameRaw, metrics.ConvertMapToTags(map[string]string{
"cgroupPath": calculationInfo.CgroupPath,
})...)
return nil
}

return nil
}
Loading
Loading