Skip to content
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(eviction): add enhanced numa-level memory pressure eviction plugin #420

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package eviction

import (
"time"

"k8s.io/apimachinery/pkg/api/resource"
cliflag "k8s.io/component-base/cli/flag"

Expand All @@ -27,6 +29,8 @@ import (
type MemoryPressureEvictionOptions struct {
EnableNumaLevelEviction bool
EnableSystemLevelEviction bool
EnableEnhancedNumaLevelEviction bool
MemoryPressureEvictionInterval time.Duration
NumaVictimMinimumUtilizationThreshold float64
NumaFreeBelowWatermarkTimesThreshold int
SystemFreeMemoryThresholdMinimum string
Expand All @@ -44,6 +48,8 @@ func NewMemoryPressureEvictionOptions() *MemoryPressureEvictionOptions {
return &MemoryPressureEvictionOptions{
EnableNumaLevelEviction: eviction.DefaultEnableNumaLevelEviction,
EnableSystemLevelEviction: eviction.DefaultEnableSystemLevelEviction,
EnableEnhancedNumaLevelEviction: eviction.DefaultEnableEnhancedNumaLevelEviction,
MemoryPressureEvictionInterval: eviction.DefaultMemoryPressureEvictionInterval,
NumaVictimMinimumUtilizationThreshold: eviction.DefaultNumaVictimMinimumUtilizationThreshold,
NumaFreeBelowWatermarkTimesThreshold: eviction.DefaultNumaFreeBelowWatermarkTimesThreshold,
SystemFreeMemoryThresholdMinimum: eviction.DefaultSystemFreeMemoryThresholdMinimum,
Expand All @@ -65,6 +71,10 @@ func (o *MemoryPressureEvictionOptions) AddFlags(fss *cliflag.NamedFlagSets) {
"whether to enable numa-level eviction")
fs.BoolVar(&o.EnableSystemLevelEviction, "eviction-enable-system-level", o.EnableSystemLevelEviction,
"whether to enable system-level eviction")
fs.BoolVar(&o.EnableEnhancedNumaLevelEviction, "eviction-enable-enhanced-numa-level", o.EnableEnhancedNumaLevelEviction,
"whether to enable enhanced numa-level eviction")
fs.DurationVar(&o.MemoryPressureEvictionInterval, "memory-pressure-eviction-interval", o.MemoryPressureEvictionInterval,
"the minimum interval between two memory evictions")
fs.Float64Var(&o.NumaVictimMinimumUtilizationThreshold, "eviction-numa-victim-minimum-utilization-threshold", o.NumaVictimMinimumUtilizationThreshold,
"the threshold for the victim's minimum memory utilization on a NUMA node")
fs.IntVar(&o.NumaFreeBelowWatermarkTimesThreshold, "eviction-numa-free-below-watermark-times-threshold", o.NumaFreeBelowWatermarkTimesThreshold,
Expand All @@ -91,6 +101,8 @@ func (o *MemoryPressureEvictionOptions) AddFlags(fss *cliflag.NamedFlagSets) {
func (o *MemoryPressureEvictionOptions) ApplyTo(c *eviction.MemoryPressureEvictionConfiguration) error {
c.EnableNumaLevelEviction = o.EnableNumaLevelEviction
c.EnableSystemLevelEviction = o.EnableSystemLevelEviction
c.EnableEnhancedNumaLevelEviction = o.EnableEnhancedNumaLevelEviction
c.MemoryPressureEvictionInterval = o.MemoryPressureEvictionInterval
c.NumaVictimMinimumUtilizationThreshold = o.NumaVictimMinimumUtilizationThreshold
c.NumaFreeBelowWatermarkTimesThreshold = o.NumaFreeBelowWatermarkTimesThreshold
quantity, err := resource.ParseQuantity(o.SystemFreeMemoryThresholdMinimum)
Expand Down
1 change: 1 addition & 0 deletions pkg/agent/evictionmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func NewInnerEvictionPluginInitializers() map[string]plugin.InitFunc {
innerEvictionPluginInitializers[resource.ReclaimedResourcesEvictionPluginName] = resource.NewReclaimedResourcesEvictionPlugin
innerEvictionPluginInitializers[memory.EvictionPluginNameNumaMemoryPressure] = memory.NewNumaMemoryPressureEvictionPlugin
innerEvictionPluginInitializers[memory.EvictionPluginNameSystemMemoryPressure] = memory.NewSystemPressureEvictionPlugin
innerEvictionPluginInitializers[memory.EvictionPluginNameEnhancedNumaMemoryPressure] = memory.NewEnhancedNumaMemoryPressureEvictionPlugin
innerEvictionPluginInitializers[memory.EvictionPluginNameRssOveruse] = memory.NewRssOveruseEvictionPlugin
innerEvictionPluginInitializers[rootfs.EvictionPluginNamePodRootfsPressure] = rootfs.NewPodRootfsPressureEvictionPlugin
return innerEvictionPluginInitializers
Expand Down
314 changes: 314 additions & 0 deletions pkg/agent/evictionmanager/plugin/memory/enhanced_numa_pressure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
/*
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 memory

import (
"context"
"fmt"
"strconv"
"time"

"github.com/alecthomas/units"

v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/events"

pluginapi "github.com/kubewharf/katalyst-api/pkg/protocol/evictionplugin/v1alpha1"
"github.com/kubewharf/katalyst-core/pkg/agent/evictionmanager/plugin"
"github.com/kubewharf/katalyst-core/pkg/client"
"github.com/kubewharf/katalyst-core/pkg/config"
"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
"github.com/kubewharf/katalyst-core/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/metaserver"
"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric/helper"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/native"
"github.com/kubewharf/katalyst-core/pkg/util/process"
)

const (
EvictionPluginNameEnhancedNumaMemoryPressure = "enhanced-numa-memory-pressure-eviction-plugin"
EvictionScopeEnhancedNumaMemory = "EnhancedNumaMemory"
)

// NewEnhancedNumaMemoryPressureEvictionPlugin returns a new MemoryPressureEvictionPlugin
func NewEnhancedNumaMemoryPressureEvictionPlugin(_ *client.GenericClientSet, _ events.EventRecorder,
metaServer *metaserver.MetaServer, emitter metrics.MetricEmitter, conf *config.Configuration) plugin.EvictionPlugin {
return &EnhancedNumaMemoryPressurePlugin{
pluginName: EvictionPluginNameEnhancedNumaMemoryPressure,
emitter: emitter,
StopControl: process.NewStopControl(time.Time{}),
metaServer: metaServer,
dynamicConfig: conf.DynamicAgentConfiguration,
reclaimedPodFilter: conf.CheckReclaimedQoSForPod,
numaActionMap: make(map[int]int),
numaPressureStatMap: make(map[int]float64),
numaKswapdStealLastCycle: make(map[int]float64),
evictionHelper: NewEvictionHelper(emitter, metaServer, conf),
}
}

// EnhancedNumaMemoryPressurePlugin implements the EvictionPlugin interface
// It triggers pod eviction based on numa node memory pressure
type EnhancedNumaMemoryPressurePlugin struct {
*process.StopControl

emitter metrics.MetricEmitter
reclaimedPodFilter func(pod *v1.Pod) (bool, error)
pluginName string
metaServer *metaserver.MetaServer
evictionHelper *EvictionHelper

dynamicConfig *dynamic.DynamicAgentConfiguration

numaActionMap map[int]int
numaPressureStatMap map[int]float64
numaKswapdStealLastCycle map[int]float64
systemKswapdStealLastCycle float64
isUnderNumaPressure bool
lastEvictTime time.Time
}

func (m *EnhancedNumaMemoryPressurePlugin) Start() {
return
}

func (m *EnhancedNumaMemoryPressurePlugin) Name() string {
if m == nil {
return ""
}

return m.pluginName
}

func (m *EnhancedNumaMemoryPressurePlugin) ThresholdMet(_ context.Context) (*pluginapi.ThresholdMetResponse, error) {
resp := &pluginapi.ThresholdMetResponse{
MetType: pluginapi.ThresholdMetType_NOT_MET,
}

// TODO: only one of enhanced-numa-level eviction numa-level eviction should be allowed to be enabled
// TODO: replace numa-level eviction with enhanced-numa-level eviction after testing
if !m.dynamicConfig.GetDynamicConfiguration().EnableEnhancedNumaLevelEviction {
return resp, nil
}

m.detectNumaPressures()
if m.isUnderNumaPressure {
resp = &pluginapi.ThresholdMetResponse{
MetType: pluginapi.ThresholdMetType_HARD_MET,
EvictionScope: EvictionScopeEnhancedNumaMemory,
}
}

return resp, nil
}

func (m *EnhancedNumaMemoryPressurePlugin) detectNumaPressures() {
m.isUnderNumaPressure = false
systemKswapdSteal, err := m.metaServer.GetNodeMetric(consts.MetricMemKswapdstealSystem)
if err != nil {
general.Errorf("failed to get kswapd steal for system, err: %v", err)
return
}
for _, numaID := range m.metaServer.CPUDetails.NUMANodes().ToSliceNoSortInt() {
m.numaActionMap[numaID] = actionNoop
numaFree, numaTotal, scaleFactor, err := helper.GetWatermarkMetrics(m.metaServer.MetricsFetcher, m.emitter, numaID)
if err != nil {
general.Errorf("failed to getWatermarkMetrics for numa %d, err: %v", numaID, err)
continue
}

numaInactiveFile, err := m.metaServer.GetNumaMetric(numaID, consts.MetricMemInactiveFileNuma)
if err != nil {
general.Errorf("failed to get inactive file for numa %d, err: %v", numaID, err)
continue
}

// TODO: get kswapd steal for numa
general.Infof("numa metrics of ID: %d, "+
"free: %+v, total: %+v, scaleFactor: %+v, inactiveFile: %+v, numaKswapdStealDelta: %+v",
numaID, numaFree, numaTotal, scaleFactor, numaInactiveFile.Value, 0)

threshold := general.Clamp(float64(10*units.GiB), 2*numaTotal*scaleFactor/10000, float64((25 * units.GiB)))
m.numaPressureStatMap[numaID] = numaFree + numaInactiveFile.Value - threshold
// TODO: use numa kswapd steal to detect numa pressure instead of system kswapd steal
if m.numaPressureStatMap[numaID] <= 0 && systemKswapdSteal.Value-m.systemKswapdStealLastCycle > 0 {
m.isUnderNumaPressure = true
m.numaActionMap[numaID] = actionEviction
_ = m.emitter.StoreInt64(metricsNameThresholdMet, 1, metrics.MetricTypeNameCount,
metrics.ConvertMapToTags(map[string]string{
metricsTagKeyEvictionScope: EvictionScopeEnhancedNumaMemory,
metricsTagKeyDetectionLevel: metricsTagValueDetectionLevelNuma,
metricsTagKeyNumaID: strconv.Itoa(numaID),
metricsTagKeyAction: metricsTagValueActionEviction,
})...)
}
}
m.systemKswapdStealLastCycle = systemKswapdSteal.Value
}

func (m *EnhancedNumaMemoryPressurePlugin) GetTopEvictionPods(_ context.Context, request *pluginapi.GetTopEvictionPodsRequest) (*pluginapi.GetTopEvictionPodsResponse, error) {
currTime := time.Now()
if currTime.Sub(m.lastEvictTime) < m.dynamicConfig.GetDynamicConfiguration().MemoryPressureEvictionInterval {
general.Infof("Skip EnhancedNumaMemoryPressurePlugin due to the short time interval")
return nil, nil
}
if request == nil {
return nil, fmt.Errorf("GetTopEvictionPods got nil request")
}

if len(request.ActivePods) == 0 {
general.Warningf("GetTopEvictionPods got empty active pods list")
return &pluginapi.GetTopEvictionPodsResponse{}, nil
}

dynamicConfig := m.dynamicConfig.GetDynamicConfiguration()
targetPods := make([]*v1.Pod, 0, len(request.ActivePods))
podToEvictList := make([]*v1.Pod, 0, len(request.ActivePods))
podToEvictMap := make(map[string]*v1.Pod)

general.Infof("GetTopEvictionPods condition, isUnderNumaPressure: %+v, m.numaActionMap: %+v",
m.isUnderNumaPressure,
m.numaActionMap)

if dynamicConfig.EnableEnhancedNumaLevelEviction && m.isUnderNumaPressure {
for numaID, action := range m.numaActionMap {
candidates := m.getCandidates(request.ActivePods, numaID, dynamicConfig.NumaVictimMinimumUtilizationThreshold)
m.evictionHelper.selectTopNPodsToEvictByMetrics(candidates, request.TopN, numaID, action,
dynamicConfig.NumaEvictionRankingMetrics, podToEvictMap)
}
}

for uid := range podToEvictMap {
podToEvictList = append(podToEvictList, podToEvictMap[uid])
}
targetPods = m.getTargets(podToEvictList, targetPods)

_ = m.emitter.StoreInt64(metricsNameNumberOfTargetPods, int64(len(targetPods)), metrics.MetricTypeNameRaw)
general.Infof("[enhanced-numa-memory-pressure-eviction-plugin] GetTopEvictionPods result, targetPods: %+v", native.GetNamespacedNameListFromSlice(targetPods))

resp := &pluginapi.GetTopEvictionPodsResponse{
TargetPods: targetPods,
}
if gracePeriod := dynamicConfig.MemoryPressureEvictionConfiguration.GracePeriod; gracePeriod > 0 {
resp.DeletionOptions = &pluginapi.DeletionOptions{
GracePeriodSeconds: gracePeriod,
}
}
if len(targetPods) > 0 {
m.lastEvictTime = time.Now()
}
return resp, nil
}

func (m *EnhancedNumaMemoryPressurePlugin) getTargets(podToEvictList []*v1.Pod, targetPods []*v1.Pod) []*v1.Pod {
predictNotUnderPressure := func(predictNumaPressureStatMap map[int]float64) bool {
for _, numaAvailMemSubtractWatermark := range predictNumaPressureStatMap {
if numaAvailMemSubtractWatermark <= 0 {
return false
}
}
return true
}
releaseNumaPressureByEvictPod := func(predictNumaPressureStatMap map[int]float64, pod *v1.Pod) {
for numaID := range predictNumaPressureStatMap {
podNumaTotal, err := helper.GetPodMetric(m.metaServer.MetricsFetcher, m.emitter, pod, consts.MetricsMemTotalPerNumaContainer, numaID)
if err != nil {
general.Infof("Failed to get pod %+v total mem usage on numa id %+v", pod.UID, numaID)
continue
}
// TODO: it's better to use sum of podNumaAnon and podNumaActiveFile to predict how much the pod can release the pressure
// since inactive file is already counted in predictNumaPressureStatMap, while cgroupv1 does not provide numa active/inactive statistics.
// So we falls back to use pod numa total.
predictNumaPressureStatMap[numaID] += podNumaTotal
}
}
getPodPermutations := func(podList []*v1.Pod) [][]*v1.Pod {
Copy link
Collaborator

Choose a reason for hiding this comment

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

when podList get larger, this function will be costly, is it really necessary?

var backtrack func([]*v1.Pod, int, *[][]*v1.Pod)
backtrack = func(podList []*v1.Pod, start int, result *[][]*v1.Pod) {
swap := func(podList []*v1.Pod, i, j int) {
podList[i], podList[j] = podList[j], podList[i]
}
if start == len(podList) {
permutation := make([]*v1.Pod, len(podList))
copy(permutation, podList)
*result = append(*result, permutation)
} else {
for i := start; i < len(podList); i++ {
swap(podList, start, i)
backtrack(podList, start+1, result)
swap(podList, start, i)
}
}
}
var result [][]*v1.Pod
backtrack(podList, 0, &result)
return result
}

for _, permutation := range getPodPermutations(podToEvictList) {
tmpPredictNumaPressureStatMap := general.DeepCopyIntFload64Map(m.numaPressureStatMap)
var tmpTargetPods []*v1.Pod
for _, pod := range permutation {
if predictNotUnderPressure(tmpPredictNumaPressureStatMap) {
break
}
releaseNumaPressureByEvictPod(tmpPredictNumaPressureStatMap, pod)
tmpTargetPods = append(tmpTargetPods, pod)
}
if len(targetPods) == 0 || len(tmpTargetPods) < len(targetPods) {
targetPods = tmpTargetPods
}
}
return targetPods
}

// getCandidates returns pods which use memory more than minimumUsageThreshold.
func (m *EnhancedNumaMemoryPressurePlugin) getCandidates(pods []*v1.Pod, numaID int, minimumUsageThreshold float64) []*v1.Pod {
result := make([]*v1.Pod, 0, len(pods))
for i := range pods {
pod := pods[i]
totalMem, totalMemErr := helper.GetNumaMetric(m.metaServer.MetricsFetcher, m.emitter,
consts.MetricMemTotalNuma, numaID)
usedMem, usedMemErr := helper.GetPodMetric(m.metaServer.MetricsFetcher, m.emitter, pod,
consts.MetricsMemTotalPerNumaContainer, numaID)
if totalMemErr != nil || usedMemErr != nil {
result = append(result, pod)
continue
}

usedMemRatio := usedMem / totalMem
if usedMemRatio < minimumUsageThreshold {
general.Infof("pod %v/%v memory usage on numa %v is %v, which is lower than threshold %v, "+
"ignore it", pod.Namespace, pod.Name, numaID, usedMemRatio, minimumUsageThreshold)
continue
}

result = append(result, pod)
}

return result
}

func (m *EnhancedNumaMemoryPressurePlugin) GetEvictPods(_ context.Context, request *pluginapi.GetEvictPodsRequest) (*pluginapi.GetEvictPodsResponse, error) {
if request == nil {
return nil, fmt.Errorf("GetEvictPods got nil request")
}

return &pluginapi.GetEvictPodsResponse{}, nil
}
Loading
Loading