-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sysadvisor): support numa aware memory headroom policy #278
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
pkg/agent/sysadvisor/plugin/qosaware/resource/memory/headroompolicy/policy_numa_aware.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
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 headroompolicy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"k8s.io/apimachinery/pkg/api/resource" | ||
"k8s.io/apimachinery/pkg/util/errors" | ||
|
||
apiconsts "github.com/kubewharf/katalyst-api/pkg/consts" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/qosaware/resource/helper" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/types" | ||
"github.com/kubewharf/katalyst-core/pkg/config" | ||
"github.com/kubewharf/katalyst-core/pkg/consts" | ||
"github.com/kubewharf/katalyst-core/pkg/metaserver" | ||
"github.com/kubewharf/katalyst-core/pkg/metrics" | ||
"github.com/kubewharf/katalyst-core/pkg/util/general" | ||
"github.com/kubewharf/katalyst-core/pkg/util/machine" | ||
"github.com/kubewharf/katalyst-core/pkg/util/metric" | ||
) | ||
|
||
type PolicyNUMAAware struct { | ||
*PolicyBase | ||
|
||
// memoryHeadroom is valid to be used iff updateStatus successes | ||
memoryHeadroom float64 | ||
updateStatus types.PolicyUpdateStatus | ||
|
||
conf *config.Configuration | ||
} | ||
|
||
func NewPolicyNUMAAware(conf *config.Configuration, _ interface{}, metaReader metacache.MetaReader, | ||
metaServer *metaserver.MetaServer, _ metrics.MetricEmitter) HeadroomPolicy { | ||
p := PolicyNUMAAware{ | ||
PolicyBase: NewPolicyBase(metaReader, metaServer), | ||
updateStatus: types.PolicyUpdateFailed, | ||
conf: conf, | ||
} | ||
|
||
return &p | ||
} | ||
|
||
func (p *PolicyNUMAAware) Name() types.MemoryHeadroomPolicyName { | ||
return types.MemoryHeadroomPolicyNUMAAware | ||
} | ||
|
||
func (p *PolicyNUMAAware) reclaimedContainersFilter(ci *types.ContainerInfo) bool { | ||
return ci != nil && ci.QoSLevel == apiconsts.PodAnnotationQoSLevelReclaimedCores | ||
} | ||
|
||
func (p *PolicyNUMAAware) Update() (err error) { | ||
defer func() { | ||
if err != nil { | ||
p.updateStatus = types.PolicyUpdateFailed | ||
} else { | ||
p.updateStatus = types.PolicyUpdateSucceeded | ||
} | ||
}() | ||
|
||
var ( | ||
errList []error | ||
reclaimableMemory float64 | ||
data metric.MetricData | ||
) | ||
|
||
availNUMAs := p.metaServer.CPUDetails.NUMANodes() | ||
|
||
reclaimedCoresContainers := make([]*types.ContainerInfo, 0) | ||
p.metaReader.RangeContainer(func(podUID string, containerName string, containerInfo *types.ContainerInfo) bool { | ||
if p.reclaimedContainersFilter(containerInfo) { | ||
reclaimedCoresContainers = append(reclaimedCoresContainers, containerInfo) | ||
return true | ||
} | ||
|
||
nodeReclaim := p.conf.GetDynamicConfiguration().EnableReclaim | ||
reclaimEnable, err := helper.PodEnableReclaim(context.Background(), p.metaServer, podUID, nodeReclaim) | ||
if err != nil { | ||
errList = append(errList, err) | ||
return true | ||
} | ||
|
||
if containerInfo.IsNumaExclusive() && !reclaimEnable { | ||
memset := machine.GetCPUAssignmentNUMAs(containerInfo.TopologyAwareAssignments) | ||
if memset.IsEmpty() { | ||
errList = append(errList, fmt.Errorf("container(%v/%v) TopologyAwareAssignments is empty", containerInfo.PodName, containerName)) | ||
return true | ||
} | ||
availNUMAs = availNUMAs.Difference(memset) | ||
} | ||
return true | ||
}) | ||
|
||
err = errors.NewAggregate(errList) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, numaID := range availNUMAs.ToSliceInt() { | ||
data, err = p.metaServer.GetNumaMetric(numaID, consts.MetricMemFreeNuma) | ||
if err != nil { | ||
return err | ||
} | ||
numaFree := data.Value | ||
|
||
reclaimedCoresUsed := 0. | ||
for _, container := range reclaimedCoresContainers { | ||
data, err = p.metaServer.GetContainerNumaMetric(container.PodUID, container.ContainerName, strconv.Itoa(numaID), consts.MetricsMemTotalPerNumaContainer) | ||
if err != nil { | ||
general.ErrorS(err, "failed to get metric", "name", consts.MetricsMemTotalPerNumaContainer, | ||
"podName", container.PodName, "ContainerName", container.ContainerName, "numaID", numaID) | ||
return err | ||
} | ||
reclaimedCoresUsed += data.Value | ||
general.InfoS("container memory", "podName", container.PodName, "containerName", container.ContainerName, "numaID", numaID, "memory used", general.FormatMemoryQuantity(data.Value)) | ||
} | ||
|
||
reclaimableMemory += numaFree + reclaimedCoresUsed | ||
|
||
general.InfoS("memory reclaimable", "numaID", numaID, "numaFree", general.FormatMemoryQuantity(numaFree), "reclaimable", general.FormatMemoryQuantity(numaFree+reclaimedCoresUsed)) | ||
} | ||
|
||
general.InfoS("total memory reclaimable", | ||
"reclaimableMemory", general.FormatMemoryQuantity(reclaimableMemory), | ||
"ReservedForAllocate", general.FormatMemoryQuantity(p.essentials.ReservedForAllocate), | ||
"ResourceUpperBound", general.FormatMemoryQuantity(p.essentials.ResourceUpperBound)) | ||
p.memoryHeadroom = general.Clamp(reclaimableMemory-p.essentials.ReservedForAllocate, 0, p.essentials.ResourceUpperBound) | ||
|
||
return nil | ||
} | ||
|
||
func (p *PolicyNUMAAware) GetHeadroom() (resource.Quantity, error) { | ||
if p.updateStatus != types.PolicyUpdateSucceeded { | ||
return resource.Quantity{}, fmt.Errorf("last update failed") | ||
} | ||
|
||
return *resource.NewQuantity(int64(p.memoryHeadroom), resource.BinarySI), nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it safer to substract shared/dedicated cores usage buffer, i.e. usage * 10%?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe reclaimed_cores potential usage(the gap between memory limit and actual usage) should also be considered.