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

koordlet: Add resctrl runtime hook for pod level #2123

Draft
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions pkg/koordlet/qosmanager/plugins/resctrl/resctrl_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ func (r *resctrlReconcile) reconcileResctrlGroups(qosStrategy *slov1alpha1.Resou
podsMeta := r.statesInformer.GetAllPods()
for _, podMeta := range podsMeta {
pod := podMeta.Pod
// only QoS class level pod are considered
if _, ok := pod.Annotations[extension.AnnotationResctrl]; ok {
continue
}

// only Running and Pending pods are considered
if pod.Status.Phase != corev1.PodRunning && pod.Status.Phase != corev1.PodPending {
continue
Expand Down
9 changes: 9 additions & 0 deletions pkg/koordlet/runtimehooks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks/cpuset"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks/gpu"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks/groupidentity"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks/resctrl"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks/terwayqos"
"github.com/koordinator-sh/koordinator/pkg/koordlet/util/system"
)
Expand Down Expand Up @@ -81,6 +82,12 @@ const (
// owner: @l1b0k
// alpha: v1.5
TerwayQoS featuregate.Feature = "TerwayQoS"

// Resctrl adjusts LLC/MB value for pod.
//
// owner: @kangclzjc @saintube @zwzhang0107
// alpha: v1.5
Resctrl featuregate.Feature = "Resctrl"
)

var (
Expand All @@ -92,6 +99,7 @@ var (
CPUNormalization: {Default: false, PreRelease: featuregate.Alpha},
CoreSched: {Default: false, PreRelease: featuregate.Alpha},
TerwayQoS: {Default: false, PreRelease: featuregate.Alpha},
Resctrl: {Default: false, PreRelease: featuregate.Alpha},
}

runtimeHookPlugins = map[featuregate.Feature]HookPlugin{
Expand All @@ -102,6 +110,7 @@ var (
CPUNormalization: cpunormalization.Object(),
CoreSched: coresched.Object(),
TerwayQoS: terwayqos.Object(),
Resctrl: resctrl.Object(),
}
)

Expand Down
6 changes: 4 additions & 2 deletions pkg/koordlet/runtimehooks/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/koordinator-sh/koordinator/pkg/koordlet/metrics"
"github.com/koordinator-sh/koordinator/pkg/koordlet/resourceexecutor"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/protocol"
"github.com/koordinator-sh/koordinator/pkg/koordlet/statesinformer"
rmconfig "github.com/koordinator-sh/koordinator/pkg/runtimeproxy/config"
)

Expand All @@ -36,8 +37,9 @@ type Hook struct {
}

type Options struct {
Reader resourceexecutor.CgroupReader
Executor resourceexecutor.ResourceUpdateExecutor
Reader resourceexecutor.CgroupReader
Executor resourceexecutor.ResourceUpdateExecutor
StatesInformer statesinformer.StatesInformer
}

type HookFn func(protocol.HooksProtocol) error
Expand Down
209 changes: 209 additions & 0 deletions pkg/koordlet/runtimehooks/hooks/resctrl/resctrl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
Copyright 2022 The Koordinator 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 resctrl

import (
"fmt"
"strings"

"k8s.io/klog/v2"

apiext "github.com/koordinator-sh/koordinator/apis/extension"
"github.com/koordinator-sh/koordinator/pkg/koordlet/resourceexecutor"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/protocol"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/reconciler"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/rule"
"github.com/koordinator-sh/koordinator/pkg/koordlet/statesinformer"
util "github.com/koordinator-sh/koordinator/pkg/koordlet/util/resctrl"
"github.com/koordinator-sh/koordinator/pkg/koordlet/util/system"
sysutil "github.com/koordinator-sh/koordinator/pkg/koordlet/util/system"
rmconfig "github.com/koordinator-sh/koordinator/pkg/runtimeproxy/config"
)

const (
name = "Resctrl"
description = "set resctrl for pod"
ruleNameForAllPods = name + " (AllPods)"
)

type plugin struct {
rule *Rule
engine util.ResctrlEngine
executor resourceexecutor.ResourceUpdateExecutor
statesInformer statesinformer.StatesInformer
}

var singleton *plugin

func Object() *plugin {
if singleton == nil {
singleton = newPlugin()
}
return singleton
}

func newPlugin() *plugin {
return &plugin{
rule: newRule(),
}
}

func (p *plugin) Register(op hooks.Options) {
// skip if host not support resctrl
if support, err := system.IsSupportResctrl(); err != nil {
klog.Warningf("check support resctrl failed, err: %s", err)
return
} else if !support {
klog.V(5).Infof("resctrl runtime hook skipped, cpu not support CAT/MBA")
return
}

if vendorID, err := sysutil.GetVendorIDByCPUInfo(sysutil.GetCPUInfoPath()); err == nil {
p.engine, err = util.NewRDTEngine(vendorID)
if err != nil {
klog.Errorf("New RDT Engine failed, error is %v", err)
return
}
}
p.executor = op.Executor
p.statesInformer = op.StatesInformer
p.engine.Rebuild()

rule.Register(ruleNameForAllPods, description,
rule.WithParseFunc(statesinformer.RegisterTypeAllPods, p.parseRuleForAllPods),
rule.WithUpdateCallback(p.ruleUpdateCbForAllPods))

hooks.Register(rmconfig.PreRunPodSandbox, name, description+" (pod)", p.SetPodResctrlResourcesForHooks)
hooks.Register(rmconfig.PreCreateContainer, name, description+" (pod)", p.SetContainerResctrlResources)
hooks.Register(rmconfig.PreRemoveRunPodSandbox, name, description+" (pod)", p.RemovePodResctrlResources)

reconciler.RegisterCgroupReconciler(reconciler.PodLevel, system.ResctrlSchemata, description+" (pod resctrl schema)", p.SetPodResctrlResourcesForReconciler, reconciler.NoneFilter())
reconciler.RegisterCgroupReconciler(reconciler.PodLevel, system.ResctrlTasks, description+" (pod resctrl tasks)", p.UpdatePodTaskIds, reconciler.NoneFilter())
reconciler.RegisterCgroupReconciler4AllPods(reconciler.AllPodsLevel, system.ResctrlRoot, description+" (pod resctl schema)", p.RemoveUnusedResctrlPath, reconciler.PodAnnotationResctrlFilter(), "resctrl")

}

func (p *plugin) SetPodResctrlResourcesForHooks(proto protocol.HooksProtocol) error {
return p.setPodResctrlResources(proto, true)
}

func (p *plugin) SetPodResctrlResourcesForReconciler(proto protocol.HooksProtocol) error {
return p.setPodResctrlResources(proto, false)
}

func (p *plugin) setPodResctrlResources(proto protocol.HooksProtocol, fromNRI bool) error {
podCtx, ok := proto.(*protocol.PodContext)
if !ok {
return fmt.Errorf("pod protocol is nil for plugin %v", name)
}

if v, ok := podCtx.Request.Annotations[apiext.AnnotationResctrl]; ok {
app, ok := p.engine.GetApp(podCtx.Request.PodMeta.UID)
if ok && app.Annotation == v {
return nil
}
updater := NewCreateResctrlProtocolUpdater(proto)
err := p.engine.RegisterApp(podCtx.Request.PodMeta.UID, v, fromNRI, updater)
if err != nil {
return err
}
}

return nil
}

func (p *plugin) RemoveUnusedResctrlPath(protos []protocol.HooksProtocol) error {
currentPods := make(map[string]protocol.HooksProtocol)

for _, proto := range protos {
podCtx, ok := proto.(*protocol.PodContext)
if !ok {
return fmt.Errorf("pod protocol is nil for plugin %v", name)
}

if _, ok := podCtx.Request.Annotations[apiext.AnnotationResctrl]; ok {
group := podCtx.Request.PodMeta.UID
currentPods[group] = podCtx
}
}

apps := p.engine.GetApps()
for k, v := range apps {
if _, ok := currentPods[k]; !ok {
updater := NewRemoveResctrlUpdater(v.Closid)
p.engine.UnRegisterApp(strings.TrimPrefix(v.Closid, util.ClosdIdPrefix), false, updater)
}
}
return nil
}

func (p *plugin) UpdatePodTaskIds(proto protocol.HooksProtocol) error {
podCtx, ok := proto.(*protocol.PodContext)
if !ok {
return fmt.Errorf("pod protocol is nil for plugin %v", name)
}

if _, ok := podCtx.Request.Annotations[apiext.AnnotationResctrl]; ok {
curTaskMaps := map[string]map[int32]struct{}{}
var err error
group := podCtx.Request.PodMeta.UID
curTaskMaps[group], err = system.ReadResctrlTasksMap(util.ClosdIdPrefix + group)
if err != nil {
klog.Warningf("failed to read Cat L3 tasks for resctrl group %s, err: %s", group, err)
}

newTaskIds := util.GetPodCgroupNewTaskIdsFromPodCtx(podCtx, curTaskMaps[group])
resctrlInfo := &protocol.Resctrl{
Closid: util.ClosdIdPrefix + group,
NewTaskIds: make([]int32, 0),
}
resctrlInfo.NewTaskIds = newTaskIds
podCtx.Response.Resources.Resctrl = resctrlInfo
}
return nil
}

func (p *plugin) SetContainerResctrlResources(proto protocol.HooksProtocol) error {
containerCtx, ok := proto.(*protocol.ContainerContext)
if !ok {
return fmt.Errorf("container protocol is nil for plugin %v", name)
}

if _, ok := containerCtx.Request.PodAnnotations[apiext.AnnotationResctrl]; ok {
containerCtx.Response.Resources.Resctrl = &protocol.Resctrl{
Schemata: "",
Closid: util.ClosdIdPrefix + containerCtx.Request.PodMeta.UID,
NewTaskIds: make([]int32, 0),
}
}

return nil
}

func (p *plugin) RemovePodResctrlResources(proto protocol.HooksProtocol) error {
podCtx, ok := proto.(*protocol.PodContext)
if !ok {
return fmt.Errorf("pod protocol is nil for plugin %v", name)
}

if _, ok := podCtx.Request.Annotations[apiext.AnnotationResctrl]; ok {
updater := NewRemoveResctrlProtocolUpdater(proto)
p.engine.UnRegisterApp(podCtx.Request.PodMeta.UID, true, updater)
}
return nil
}
72 changes: 72 additions & 0 deletions pkg/koordlet/runtimehooks/hooks/resctrl/rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2022 The Koordinator 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 resctrl

import (
"strings"
"sync"

corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"

apiext "github.com/koordinator-sh/koordinator/apis/extension"
"github.com/koordinator-sh/koordinator/pkg/koordlet/statesinformer"
util "github.com/koordinator-sh/koordinator/pkg/koordlet/util/resctrl"
)

type Rule struct {
lock sync.RWMutex
}

func newRule() *Rule {
return &Rule{}
}

func (p *plugin) parseRuleForAllPods(allPods interface{}) (bool, error) {
return true, nil
}

func (p *plugin) ruleUpdateCbForAllPods(target *statesinformer.CallbackTarget) error {
if target == nil {
klog.Warningf("callback target is nil")
return nil
}

if p.rule == nil {
klog.V(5).Infof("hook plugin rule is nil, nothing to do for plugin %v", ruleNameForAllPods)
return nil
}

apps := p.engine.GetApps()

currentPods := make(map[string]*corev1.Pod)
for _, podMeta := range target.Pods {
pod := podMeta.Pod
if _, ok := podMeta.Pod.Annotations[apiext.AnnotationResctrl]; ok {
group := string(podMeta.Pod.UID)
currentPods[group] = pod
}
}

for k, v := range apps {
if _, ok := currentPods[k]; !ok {
updater := NewRemoveResctrlUpdater(v.Closid)
p.engine.UnRegisterApp(strings.TrimPrefix(v.Closid, util.ClosdIdPrefix), false, updater)
}
}
return nil
}
Loading