-
Notifications
You must be signed in to change notification settings - Fork 106
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
LuyaoZhong
wants to merge
5
commits into
kubewharf:main
Choose a base branch
from
LuyaoZhong:memory_pressure_eviction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e29f0e
feat:add enhanced-numa-level memory pressure eviction plugin
LuyaoZhong 9e1aa76
bug fix: fix container numa metrics of cgroup v2
LuyaoZhong 4acb9a2
refine the condition of numa memory pressure
LuyaoZhong 48d2f6a
refine
LuyaoZhong ccca804
fix typo
LuyaoZhong 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
314 changes: 314 additions & 0 deletions
314
pkg/agent/evictionmanager/plugin/memory/enhanced_numa_pressure.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,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 { | ||
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 | ||
} |
Oops, something went wrong.
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.
when
podList
get larger, this function will be costly, is it really necessary?