-
Notifications
You must be signed in to change notification settings - Fork 91
/
regulator.go
172 lines (135 loc) · 5.51 KB
/
regulator.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
/*
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 regulator
import (
"math"
"time"
"k8s.io/klog/v2"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/types"
)
// Regulator gets raw requirement data from policy and generates real requirement
// for a certain region with fine-grained strategies to be robust
type Regulator interface {
// SetEssentials updates some essential parameters to restrict requirement
SetEssentials(essentials types.ResourceEssentials)
// SetLatestRequirement overwrites the latest regulated requirement
SetLatestRequirement(latestRequirement int)
// Regulate runs an episode of regulation to restrict raw requirement and store the result
// as the latest requirement value
Regulate(requirement float64)
// GetRequirement returns the latest regulated requirement
GetRequirement() int
}
// DummyRegulator always get requirement without regulate
type DummyRegulator struct {
latestRequirement int
}
func NewDummyRegulator() Regulator {
return &DummyRegulator{}
}
var _ Regulator = &DummyRegulator{}
func (d *DummyRegulator) SetEssentials(_ types.ResourceEssentials) {
}
func (d *DummyRegulator) SetLatestRequirement(latestRequirement int) {
d.latestRequirement = latestRequirement
}
func (d *DummyRegulator) Regulate(requirement float64) {
d.SetLatestRequirement(int(requirement))
}
func (d *DummyRegulator) GetRequirement() int {
return d.latestRequirement
}
// CPURegulator gets raw cpu requirement data from policy and generates real cpu requirement
// for a certain region with fine-grained strategies to be robust
type CPURegulator struct {
types.ResourceEssentials
// maxRampUpStep is the max cpu cores can be increased during each cpu requirement update
maxRampUpStep int
// maxRampDownStep is the max cpu cores can be decreased during each cpu requirement update
maxRampDownStep int
// minRampDownPeriod is the min time gap between two consecutive cpu requirement ramp down
minRampDownPeriod time.Duration
// latestCPURequirement is the latest updated cpu requirement value
latestCPURequirement int
// latestRampDownTime is the latest ramp down timestamp
latestRampDownTime time.Time
}
// NewCPURegulator returns a cpu regulator instance with immutable parameters
func NewCPURegulator() Regulator {
c := &CPURegulator{
maxRampUpStep: types.MaxRampUpStep,
maxRampDownStep: types.MaxRampDownStep,
minRampDownPeriod: types.MinRampDownPeriod,
latestRampDownTime: time.Now().Add(-types.MinRampDownPeriod),
}
return c
}
// SetEssentials updates some essential parameters to restrict cpu requirement
func (c *CPURegulator) SetEssentials(essentials types.ResourceEssentials) {
c.ResourceEssentials = essentials
}
// SetLatestRequirement overwrites the latest regulated cpu requirement
func (c *CPURegulator) SetLatestRequirement(latestCPURequirement int) {
c.latestCPURequirement = latestCPURequirement
}
// Regulate runs an episode of cpu regulation to restrict raw cpu requirement and store the result
// as the latest cpu requirement value
func (c *CPURegulator) Regulate(cpuRequirement float64) {
cpuRequirementReserved := cpuRequirement + c.ReservedForAllocate
cpuRequirementRound := c.round(cpuRequirementReserved)
cpuRequirementSlowdown := c.slowdown(cpuRequirementRound)
cpuRequirementClamp := c.clamp(cpuRequirementSlowdown)
klog.Infof("[qosaware-cpu] cpu requirement by policy: %.2f, with reserve: %.2f, after round: %d, after slowdown: %d, after clamp: %d",
cpuRequirement, cpuRequirementReserved, cpuRequirementRound, cpuRequirementSlowdown, cpuRequirementClamp)
if cpuRequirementClamp != c.latestCPURequirement {
c.latestCPURequirement = cpuRequirementClamp
c.latestRampDownTime = time.Now()
}
}
// GetRequirement returns the latest regulated cpu requirement
func (c *CPURegulator) GetRequirement() int {
return c.latestCPURequirement
}
func (c *CPURegulator) slowdown(cpuRequirement int) int {
now := time.Now()
// Restrict ramp down frequency
if cpuRequirement < c.latestCPURequirement && now.Before(c.latestRampDownTime.Add(c.minRampDownPeriod)) {
return c.latestCPURequirement
}
// Restrict ramp up and down step
if cpuRequirement-c.latestCPURequirement > c.maxRampUpStep {
cpuRequirement = c.latestCPURequirement + c.maxRampUpStep
} else if c.latestCPURequirement-cpuRequirement > c.maxRampDownStep {
cpuRequirement = c.latestCPURequirement - c.maxRampDownStep
}
return cpuRequirement
}
func (c *CPURegulator) round(cpuRequirement float64) int {
// Never share cores between latency-critical pods and best-effort pods
// so make latency-critical pods require at least a core's-worth of CPUs.
// This rule can be broken by clamp.
cpuRequirementRounded := int(math.Ceil(cpuRequirement))
if cpuRequirementRounded%2 == 1 {
cpuRequirementRounded += 1
}
return cpuRequirementRounded
}
func (c *CPURegulator) clamp(cpuRequirement int) int {
if cpuRequirement < int(c.ResourceLowerBound) {
return int(c.ResourceLowerBound)
} else if cpuRequirement > int(c.ResourceUpperBound) {
return int(c.ResourceUpperBound)
} else {
return cpuRequirement
}
}