-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sysadvisor): support numa aware memory headroom policy
calculate memory headroom by numa free + reclaimed used Signed-off-by: linzhecheng <linzhecheng@bytedance.com>
- Loading branch information
1 parent
dc08b2e
commit 8cc8805
Showing
7 changed files
with
381 additions
and
15 deletions.
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.