-
Notifications
You must be signed in to change notification settings - Fork 315
/
cpuset.go
211 lines (181 loc) · 7.51 KB
/
cpuset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
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 cpuset
import (
"fmt"
"sync"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
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"
sysutil "github.com/koordinator-sh/koordinator/pkg/koordlet/util/system"
rmconfig "github.com/koordinator-sh/koordinator/pkg/runtimeproxy/config"
"github.com/koordinator-sh/koordinator/pkg/util"
)
const (
name = "CPUSetAllocator"
description = "set cpuset value by pod allocation"
)
type cpusetPlugin struct {
rule *cpusetRule
ruleRWMutex sync.RWMutex
executor resourceexecutor.ResourceUpdateExecutor
}
var (
cpusetPodQOSConditions = []string{string(apiext.QoSLSE), string(apiext.QoSLSR)}
cpusharePodQOSConditions = []string{string(apiext.QoSLS), string(apiext.QoSBE), string(apiext.QoSSystem), string(apiext.QoSNone)}
)
func (p *cpusetPlugin) Register(op hooks.Options) {
klog.V(5).Infof("register hook %v", name)
hooks.Register(rmconfig.PreCreateContainer, name, description, p.SetContainerCPUSetAndUnsetCFS)
hooks.Register(rmconfig.PreUpdateContainerResources, name, description, p.SetContainerCPUSetAndUnsetCFS)
hooks.Register(rmconfig.PreRunPodSandbox, name, "unset pod cpu quota if needed", UnsetPodCPUQuota)
rule.Register(name, description,
rule.WithParseFunc(statesinformer.RegisterTypeNodeTopology, p.parseRule),
rule.WithUpdateCallback(p.ruleUpdateCb))
reconciler.RegisterCgroupReconciler(reconciler.ContainerLevel, sysutil.CPUSet,
"set container cpuset and unset container cpu quota if needed for cpuset pod",
p.SetContainerCPUSetAndUnsetCFS, reconciler.PodQOSFilter(), cpusetPodQOSConditions...)
reconciler.RegisterCgroupReconciler(reconciler.SandboxLevel, sysutil.CPUSet,
"set sandbox container cpuset and unset container cpu quota if needed for cpuset pod",
p.SetContainerCPUSetAndUnsetCFS, reconciler.PodQOSFilter(), cpusetPodQOSConditions...)
reconciler.RegisterCgroupReconciler(reconciler.PodLevel, sysutil.CPUCFSQuota,
"unset pod cpu quota if needed for cpuset pod", UnsetPodCPUQuota,
reconciler.PodQOSFilter(), cpusetPodQOSConditions...)
reconciler.RegisterCgroupReconciler(reconciler.ContainerLevel, sysutil.CPUSet,
"set container cpuset for cpushare pod is specified",
p.SetContainerCPUSet, reconciler.PodQOSFilter(), cpusharePodQOSConditions...)
reconciler.RegisterCgroupReconciler(reconciler.SandboxLevel, sysutil.CPUSet,
"set sandbox container cpuset for cpushare pod is specified",
p.SetContainerCPUSet, reconciler.PodQOSFilter(), cpusharePodQOSConditions...)
reconciler.RegisterHostAppReconciler(sysutil.CPUSet, "set host application cpuset",
p.SetHostAppCPUSet, &reconciler.ReconcilerOption{})
p.executor = op.Executor
}
var singleton *cpusetPlugin
func Object() *cpusetPlugin {
if singleton == nil {
singleton = &cpusetPlugin{}
}
return singleton
}
func (p *cpusetPlugin) SetContainerCPUSetAndUnsetCFS(proto protocol.HooksProtocol) error {
// set container-level cpuset.cpus
err := p.SetContainerCPUSet(proto)
if err != nil {
return err
}
// unset container-level cpu.cfs_quota_us if needed
return UnsetContainerCPUQuota(proto)
}
func (p *cpusetPlugin) SetContainerCPUSet(proto protocol.HooksProtocol) error {
containerCtx, _ := proto.(*protocol.ContainerContext)
if containerCtx == nil {
return fmt.Errorf("container protocol is nil for plugin %v", name)
}
containerReq := containerCtx.Request
klog.V(5).Infof("getting container cpuset for %v/%v", containerReq.PodMeta.String(), containerReq.ContainerMeta.Name)
// cpuset from pod annotation (LSE, LSR)
if cpusetVal, err := util.GetCPUSetFromPod(containerReq.PodAnnotations); err != nil {
return err
} else if cpusetVal != "" {
containerCtx.Response.Resources.CPUSet = pointer.String(cpusetVal)
klog.V(5).Infof("get cpuset %v for container %v/%v from pod annotation", cpusetVal,
containerCtx.Request.PodMeta.String(), containerCtx.Request.ContainerMeta.Name)
return nil
}
r := p.getRule()
if r == nil {
klog.V(5).Infof("hook plugin rule is nil, nothing to do for plugin %v", name)
return nil
}
// cpuset from rule according to pod QoS
cpusetValue, err := r.getContainerCPUSet(&containerReq)
if err != nil {
return err
}
if cpusetValue != nil {
klog.V(5).Infof("get cpuset %v for container %v/%v from rule", *cpusetValue,
containerCtx.Request.PodMeta.String(), containerCtx.Request.ContainerMeta.Name)
} else {
klog.V(5).Infof("get empty cpuset for container %v/%v from rule",
containerCtx.Request.PodMeta.String(), containerCtx.Request.ContainerMeta.Name)
}
containerCtx.Response.Resources.CPUSet = cpusetValue
return nil
}
func (p *cpusetPlugin) SetHostAppCPUSet(proto protocol.HooksProtocol) error {
hostAppCtx, _ := proto.(*protocol.HostAppContext)
if hostAppCtx == nil {
return fmt.Errorf("host application protocol is nil for plugin %v", name)
}
r := p.getRule()
if r == nil {
klog.V(5).Infof("hook plugin rule is nil, do nothing for plugin %v", name)
return nil
}
hostAppReq := hostAppCtx.Request
cpusetValue, err := r.getHostAppCpuset(&hostAppReq)
if err != nil {
return err
}
if cpusetValue != nil {
klog.V(5).Infof("get cpuset %v for host application %v from rule", *cpusetValue,
hostAppReq.Name)
} else {
klog.V(5).Infof("get empty cpuset for host application %v from rule", hostAppReq.Name)
}
hostAppCtx.Response.Resources.CPUSet = cpusetValue
return nil
}
func UnsetPodCPUQuota(proto protocol.HooksProtocol) error {
podCtx := proto.(*protocol.PodContext)
if podCtx == nil {
return fmt.Errorf("pod protocol is nil for plugin %v", name)
}
req := podCtx.Request
// cpuset from pod annotation (LSE, LSR)
// NOTE: unset cfs quota for cpuset pods to avoid unexpected throttles.
// https://github.com/koordinator-sh/koordinator/issues/489
if needUnset, err := util.IsPodCfsQuotaNeedUnset(req.Annotations); err != nil {
return err
} else if needUnset {
podCtx.Response.Resources.CFSQuota = pointer.Int64(-1)
return nil
}
// do nothing for cpushare pod
return nil
}
func UnsetContainerCPUQuota(proto protocol.HooksProtocol) error {
containerCtx := proto.(*protocol.ContainerContext)
if containerCtx == nil {
return fmt.Errorf("container protocol is nil for plugin %v", name)
}
containerReq := containerCtx.Request
// cpuset from pod annotation (LSE, LSR)
// NOTE: unset cfs quota for cpuset pods to avoid unexpected throttles.
// https://github.com/koordinator-sh/koordinator/issues/489
if needUnset, err := util.IsPodCfsQuotaNeedUnset(containerReq.PodAnnotations); err != nil {
return err
} else if needUnset {
containerCtx.Response.Resources.CFSQuota = pointer.Int64(-1)
return nil
}
// do nothing for cpushare pod
return nil
}