Skip to content

Commit

Permalink
koord-manager: support manually configured default cpu normalization …
Browse files Browse the repository at this point in the history
…settings

In some dedicated clusters, we don't need complex CPU models;
instead, we prefer manually configured default CPU normalization settings for greater flexibility.

Signed-off-by: yangfeiyu20102011 <yangfeiyu20102011@163.com>
  • Loading branch information
yangfeiyu20102011 committed Jul 12, 2024
1 parent 6861c87 commit 7fb6cc8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apis/configuration/slo_controller_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ type CPUNormalizationStrategy struct {
// Enable defines whether the cpu normalization is enabled.
// If set to false, the node cpu normalization ratio will be removed.
Enable *bool `json:"enable,omitempty"`
// DefaultRatio defines the default cpu normalization.
DefaultRatio *float64 `json:"defaultRatio,omitempty"`
// RatioModel defines the cpu normalization ratio of each CPU model.
// It maps the CPUModel of BasicInfo into the ratios.
RatioModel map[string]ModelRatioCfg `json:"ratioModel,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions apis/configuration/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ func (p *Plugin) Calculate(_ *configuration.ColocationStrategy, node *corev1.Nod
return nil, fmt.Errorf("failed to get CPUBasicInfo in cpu normalization calculation, err: info is missing")
}

ratio, err := getCPUNormalizationRatioFromModel(basicInfo, strategy)
ratio, err := getCPUNormalizationRatio(basicInfo, strategy)
if err != nil {
return nil, fmt.Errorf("failed to get ratio in cpu normalization calculation, err: %s", err)
}

if err = isCPUNormalizationRatioValid(ratio); err != nil {
return nil, fmt.Errorf("failed to validate ratio in cpu normalization calculation, ratio %v, err: %s", ratio, err)
}
Expand Down Expand Up @@ -218,6 +219,19 @@ func isCPUBasicInfoChanged(infoOld, infoNew *extension.CPUBasicInfo) (bool, stri
return false, ""
}

func getCPUNormalizationRatio(info *extension.CPUBasicInfo, strategy *configuration.CPUNormalizationStrategy) (float64, error) {
ratio, err := getCPUNormalizationRatioFromModel(info, strategy)
if err != nil {
if strategy.DefaultRatio != nil {
klog.V(6).Infof("get no cpu ratio from model, use the default ratio %v", *strategy.DefaultRatio)
ratio = *strategy.DefaultRatio
} else {
return -1, fmt.Errorf("failed to get cpu normalization ratio from model, err: %s", err)
}
}
return ratio, nil
}

func getCPUNormalizationRatioFromModel(info *extension.CPUBasicInfo, strategy *configuration.CPUNormalizationStrategy) (float64, error) {
if strategy.RatioModel == nil {
return -1, fmt.Errorf("ratio model is nil")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,90 @@ func Test_getCPUNormalizationRatioFromModel(t *testing.T) {
}
}

func Test_getCPUNormalizationRatio(t *testing.T) {
type args struct {
info *extension.CPUBasicInfo
strategy *configuration.CPUNormalizationStrategy
}
tests := []struct {
name string
args args
want float64
wantErr bool
}{
{
name: "nil model and nil default ratio",
args: args{
strategy: &configuration.CPUNormalizationStrategy{
Enable: pointer.Bool(true),
DefaultRatio: nil,
RatioModel: nil,
},
},
want: -1,
wantErr: true,
},
{
name: "nil model and valid default ratio",
args: args{
strategy: &configuration.CPUNormalizationStrategy{
Enable: pointer.Bool(true),
DefaultRatio: pointer.Float64(2.5),
RatioModel: nil,
},
},
want: 2.5,
wantErr: false,
},
{
name: "model takes precedence over the default ratio",
args: args{
info: &extension.CPUBasicInfo{
CPUModel: "CPU XXX",
},
strategy: &configuration.CPUNormalizationStrategy{
Enable: pointer.Bool(true),
DefaultRatio: pointer.Float64(3.5),
RatioModel: map[string]configuration.ModelRatioCfg{
"CPU XXX": {
BaseRatio: pointer.Float64(1.9),
TurboEnabledRatio: pointer.Float64(1.65),
HyperThreadEnabledRatio: pointer.Float64(1.0),
HyperThreadTurboEnabledRatio: pointer.Float64(1.1),
},
},
},
},
want: 1.9,
wantErr: false,
},
{
name: "use the default ratio with invalid model",
args: args{
info: &extension.CPUBasicInfo{
CPUModel: "CPU XXX",
},
strategy: &configuration.CPUNormalizationStrategy{
Enable: pointer.Bool(true),
DefaultRatio: pointer.Float64(2.2),
RatioModel: map[string]configuration.ModelRatioCfg{
"CPU XXX": {},
},
},
},
want: 2.2,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, gotErr := getCPUNormalizationRatio(tt.args.info, tt.args.strategy)
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.wantErr, gotErr != nil)
})
}
}

func testPluginCleanup() {
client = nil
cfgHandler = nil
Expand Down

0 comments on commit 7fb6cc8

Please sign in to comment.