-
Notifications
You must be signed in to change notification settings - Fork 56
/
rdt.go
378 lines (315 loc) · 10.5 KB
/
rdt.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2019 Intel Corporation. All Rights Reserved.
//
// 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 rdt
import (
"fmt"
corev1 "k8s.io/api/core/v1"
pkgcfg "github.com/intel/cri-resource-manager/pkg/config"
"github.com/intel/cri-resource-manager/pkg/cri/client"
"github.com/intel/cri-resource-manager/pkg/cri/resource-manager/cache"
"github.com/intel/cri-resource-manager/pkg/cri/resource-manager/control"
logger "github.com/intel/cri-resource-manager/pkg/log"
"github.com/intel/cri-resource-manager/pkg/metrics"
"github.com/intel/goresctrl/pkg/rdt"
)
const (
// ConfigModuleName is the configuration section for RDT
ConfigModuleName = "rdt"
// RDTController is the name of the RDT controller.
RDTController = cache.RDT
resctrlGroupPrefix = "cri-resmgr."
)
// rdtctl encapsulates the runtime state of our RTD enforcement/controller.
type rdtctl struct {
cache cache.Cache // resource manager cache
noQoSClasses bool // true if mapping pod qos class to rdt class is disabled
mode OperatingMode // track the mode here to capture mode changes
opt *config
}
type config struct {
rdt.Config
Options struct {
rdt.Options
Mode OperatingMode `json:"mode"`
MonitoringDisabled bool `json:"monitoringDisabled"`
} `json:"options"`
}
type OperatingMode string
const (
OperatingModeDisabled OperatingMode = "Disabled"
OperatingModeDiscovery OperatingMode = "Discovery"
OperatingModeFull OperatingMode = "Full"
)
// Our logger instance.
var log logger.Logger = logger.NewLogger(RDTController)
// our RDT controller singleton instance.
var singleton *rdtctl
// getRDTController returns our singleton RDT controller instance.
func getRDTController() *rdtctl {
if singleton == nil {
singleton = &rdtctl{}
singleton.opt = singleton.defaultOptions().(*config)
}
return singleton
}
// Start initializes the controller for enforcing decisions.
func (ctl *rdtctl) Start(cache cache.Cache, client client.Client) error {
if err := rdt.Initialize(resctrlGroupPrefix); err != nil {
return rdtError("failed to initialize RDT controls: %v", err)
}
ctl.cache = cache
if err := ctl.configure(); err != nil {
// Just print an error. A config update later on may be valid.
log.Error("failed apply initial configuration: %v", err)
}
rdt.RegisterCustomPrometheusLabels("pod_name", "container_name")
err := metrics.RegisterCollector("rdt", rdt.NewCollector)
if err != nil {
log.Error("failed register rdt collector: %v", err)
}
pkgcfg.GetModule(ConfigModuleName).AddNotify(getRDTController().configNotify)
return nil
}
// Stop shuts down the controller.
func (ctl *rdtctl) Stop() {
}
// PreCreateHook is the RDT controller pre-create hook.
func (ctl *rdtctl) PreCreateHook(c cache.Container) error {
return nil
}
// PreStartHook is the RDT controller pre-start hook.
func (ctl *rdtctl) PreStartHook(c cache.Container) error {
return nil
}
// PostStartHook is the RDT controller post-start hook.
func (ctl *rdtctl) PostStartHook(c cache.Container) error {
if !c.HasPending(RDTController) {
return nil
}
if err := ctl.assign(c); err != nil {
return err
}
c.ClearPending(RDTController)
return nil
}
// PostUpdateHook is the RDT controller post-update hook.
func (ctl *rdtctl) PostUpdateHook(c cache.Container) error {
if !c.HasPending(RDTController) {
return nil
}
if err := ctl.assign(c); err != nil {
return err
}
c.ClearPending(RDTController)
return nil
}
// PostStop is the RDT controller post-stop hook.
func (ctl *rdtctl) PostStopHook(c cache.Container) error {
if err := ctl.stopMonitor(c); err != nil {
return rdtError("%q: failed to remove monitoring group: %v", c.PrettyName(), err)
}
return nil
}
// assign assigns all processes/threads in a container to the correct class
func (ctl *rdtctl) assign(c cache.Container) error {
if ctl.opt.Options.Mode == OperatingModeDisabled {
return nil
}
class := c.GetRDTClass()
switch class {
case "":
class = rdt.RootClassName
case cache.RDTClassPodQoS:
if ctl.noQoSClasses {
class = rdt.RootClassName
} else {
class = string(c.GetQOSClass())
}
}
err := ctl.assignClass(c, class)
if err != nil && class != rdt.RootClassName {
log.Warn("%v; falling back to system root class", err)
return ctl.assignClass(c, rdt.RootClassName)
}
return err
}
// assignClass assigns all processes/threads in a container to the specified class
func (ctl *rdtctl) assignClass(c cache.Container, class string) error {
cls, ok := rdt.GetClass(class)
if !ok {
return rdtError("%q: unknown RDT class %q", c.PrettyName(), class)
}
pod, ok := c.GetPod()
if !ok {
return rdtError("%q: failed to get pod", c.PrettyName())
}
pids, err := c.GetProcesses()
if err != nil {
return rdtError("%q: failed to get process list: %v", c.PrettyName(), err)
}
if err := cls.AddPids(pids...); err != nil {
return rdtError("%q: failed to assign to class %q: %v", c.PrettyName(), class, err)
}
pretty := c.PrettyName()
if _, ok := cls.GetMonGroup(pretty); !ok || ctl.monitoringDisabled() {
ctl.stopMonitor(c)
}
if !ctl.monitoringDisabled() {
pname, name, id := pod.GetName(), c.GetName(), c.GetID()
if err := ctl.monitor(cls, pname, name, id, pretty, pids); err != nil {
return err
}
}
log.Info("%q: assigned to class %q", pretty, class)
return nil
}
// monitor starts monitoring a container.
func (ctl *rdtctl) monitor(cls rdt.CtrlGroup, pod, name, id, pretty string, pids []string) error {
if !rdt.MonSupported() {
return nil
}
annotations := map[string]string{"pod_name": pod, "container_name": name}
if mg, err := cls.CreateMonGroup(id, annotations); err != nil {
log.Warn("%q: failed to create monitoring group: %v", pretty, err)
} else {
if err := mg.AddPids(pids...); err != nil {
return rdtError("%q: failed to assign to monitoring group %q: %v",
pretty, cls.Name()+"/"+mg.Name(), err)
}
log.Info("%q: assigned to monitoring group %q", pretty, cls.Name()+"/"+mg.Name())
}
return nil
}
// stopMonitor stops monitoring a container.
func (ctl *rdtctl) stopMonitor(c cache.Container) error {
name := c.PrettyName()
for _, cls := range rdt.GetClasses() {
if mg, ok := cls.GetMonGroup(name); ok {
if err := cls.DeleteMonGroup(name); err != nil {
return err
}
log.Info("%q: removed monitoring group %q",
c.PrettyName(), cls.Name()+"/"+mg.Name())
}
}
return nil
}
// stopMonitorAll removes all monitoring groups
func (ctl *rdtctl) stopMonitorAll() error {
for _, cls := range rdt.GetClasses() {
if err := cls.DeleteMonGroups(); err != nil {
return err
}
}
return nil
}
func (ctl *rdtctl) assignAll(forceClass string) {
// Assign all containers
for _, c := range ctl.cache.GetContainers() {
var err error
if forceClass != "" {
err = ctl.assignClass(c, forceClass)
} else {
err = ctl.assign(c)
}
if err != nil {
log.Warn("failed to assign rdt class of %q: %v", c.PrettyName(), err)
}
}
}
func (ctl *rdtctl) monitoringDisabled() bool {
return ctl.mode == OperatingModeDisabled || ctl.opt.Options.MonitoringDisabled
}
func (ctl *rdtctl) configure() error {
// Apply RDT configuration, depending on the operating mode
switch ctl.opt.Options.Mode {
case OperatingModeDisabled:
if ctl.mode != ctl.opt.Options.Mode {
ctl.stopMonitorAll()
// Drop all cri-resctrl specific groups by applying an empty config
if err := rdt.SetConfig(&rdt.Config{}, true); err != nil {
return rdtError("failed apply empty rdt config: %v", err)
}
ctl.noQoSClasses = true
ctl.mode = ctl.opt.Options.Mode
ctl.assignAll(rdt.RootClassName)
}
case OperatingModeDiscovery:
if ctl.mode != ctl.opt.Options.Mode {
ctl.stopMonitorAll()
// Drop all cri-resctrl specific groups by applying an empty config
if err := rdt.SetConfig(&rdt.Config{}, true); err != nil {
return rdtError("failed apply empty rdt config: %v", err)
}
}
// Run Initialize with empty prefix to discover existing resctrl groups
if err := rdt.DiscoverClasses(""); err != nil {
return rdtError("failed to discover classes from fs: %v", err)
}
// Disable mapping from Pod QoS to RDT class if no Pod QoS class equivalents exist
ctl.noQoSClasses = true
cs := []corev1.PodQOSClass{corev1.PodQOSBestEffort, corev1.PodQOSBurstable, corev1.PodQOSGuaranteed}
for _, c := range cs {
if _, ok := rdt.GetClass(string(c)); ok {
ctl.noQoSClasses = false
break
}
}
ctl.mode = ctl.opt.Options.Mode
ctl.assignAll("")
case OperatingModeFull:
if ctl.mode != ctl.opt.Options.Mode {
ctl.stopMonitorAll()
}
// Copy goresctrl specific part from our extended options
ctl.opt.Config.Options = ctl.opt.Options.Options
if err := rdt.SetConfig(&ctl.opt.Config, true); err != nil {
return err
}
// Disable mapping from Pod QoS to RDT class if no classes have been defined
ctl.noQoSClasses = len(rdt.GetClasses()) <= 1
ctl.mode = ctl.opt.Options.Mode
ctl.assignAll("")
default:
return rdtError("invalid mode %q", ctl.opt.Options.Mode)
}
log.Debug("rdt controller operating mode set to %q", ctl.mode)
if ctl.opt.Options.Mode != OperatingModeDisabled {
log.Debug("rdt monitoring %s", map[bool]string{true: "disabled", false: "enabled"}[ctl.monitoringDisabled()])
}
return nil
}
// configNotify is our runtime configuration notification callback.
func (ctl *rdtctl) configNotify(event pkgcfg.Event, source pkgcfg.Source) error {
log.Info("configuration update, applying new config")
return ctl.configure()
}
func (ctl *rdtctl) defaultOptions() interface{} {
c := &config{}
c.Options.Mode = OperatingModeFull
return c
}
// GetClasses returns all available RDT classes
func GetClasses() []rdt.CtrlGroup {
return rdt.GetClasses()
}
// rdtError creates an RDT-controller-specific formatted error message.
func rdtError(format string, args ...interface{}) error {
return fmt.Errorf("rdt: "+format, args...)
}
// Register us as a controller.
func init() {
control.Register(RDTController, "RDT controller", getRDTController())
pkgcfg.Register(ConfigModuleName, "RDT control", getRDTController().opt, getRDTController().defaultOptions)
}