Skip to content

Commit

Permalink
add resctrl
Browse files Browse the repository at this point in the history
Signed-off-by: Zhang Kang <kang.zhang@intel.com>
  • Loading branch information
kangclzjc committed Jul 3, 2024
1 parent 8674ffa commit 1c26f62
Show file tree
Hide file tree
Showing 10 changed files with 554 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pkg/koordlet/runtimehooks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package runtimehooks

import (
"flag"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks/resctrl"
"math"
"time"

Expand Down Expand Up @@ -81,6 +82,13 @@ 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 +100,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 +111,7 @@ var (
CPUNormalization: cpunormalization.Object(),
CoreSched: coresched.Object(),
TerwayQoS: terwayqos.Object(),
Resctrl: resctrl.Object(),
}
)

Expand Down
2 changes: 2 additions & 0 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 @@ -38,6 +39,7 @@ type Hook struct {
type Options struct {
Reader resourceexecutor.CgroupReader

Check failure on line 40 in pkg/koordlet/runtimehooks/hooks/hooks.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)
Executor resourceexecutor.ResourceUpdateExecutor
StatesInformer statesinformer.StatesInformer
}

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

0 comments on commit 1c26f62

Please sign in to comment.