-
Notifications
You must be signed in to change notification settings - Fork 122
feat(advisor): apply cpu headroom discount when power level is present #1034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
h-w-chen
wants to merge
5
commits into
kubewharf:main
Choose a base branch
from
h-w-chen:dev/pap-discounted-cpu-headroom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
687b7f9
pap: cpu headrrom discount
h-w-chen 49af741
pap: headroom loss metric
h-w-chen 5a31102
add qos-aware cpu sub resource advisor Init to ensure discount assmbl…
h-w-chen cf4b24d
arg: --cpu-headroom-power-discount-enable default to true
h-w-chen 3f50bee
minor: irregular discount value explicit treatment
h-w-chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ysadvisor/plugin/qosaware/resource/cpu/assembler/headroomassembler/decorator/decorator.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| 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 decorator | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache" | ||
| "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/qosaware/resource/cpu/assembler/headroomassembler" | ||
| "github.com/kubewharf/katalyst-core/pkg/config" | ||
| "github.com/kubewharf/katalyst-core/pkg/metaserver" | ||
| "github.com/kubewharf/katalyst-core/pkg/metrics" | ||
| ) | ||
|
|
||
| var ( | ||
| initializers sync.Map | ||
| enabledDecorator string | ||
| lock sync.RWMutex | ||
| ) | ||
|
|
||
| type InitFunc func(inner headroomassembler.HeadroomAssembler, | ||
| conf *config.Configuration, extraConf interface{}, | ||
| metaReader metacache.MetaReader, metaServer *metaserver.MetaServer, emitter metrics.MetricEmitter, | ||
| ) headroomassembler.HeadroomAssembler | ||
|
|
||
| func RegisterInitializer(name string, initFunc InitFunc) { | ||
| initializers.Store(name, initFunc) | ||
| } | ||
|
|
||
| func GetRegisteredInitializers() map[string]InitFunc { | ||
| res := make(map[string]InitFunc) | ||
| initializers.Range(func(key, value interface{}) bool { | ||
| res[key.(string)] = value.(InitFunc) | ||
| return true | ||
| }) | ||
| return res | ||
| } | ||
|
|
||
| func EnablePlugin(name string) { | ||
| lock.Lock() | ||
| defer lock.Unlock() | ||
| enabledDecorator = name | ||
| } | ||
|
|
||
| func GetEnabledPlugin() string { | ||
| lock.RLock() | ||
| defer lock.RUnlock() | ||
|
|
||
| return enabledDecorator | ||
| } |
148 changes: 148 additions & 0 deletions
148
.../plugin/qosaware/resource/cpu/assembler/headroomassembler/decorator/decorator_discount.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| 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 decorator | ||
|
|
||
| import ( | ||
| "context" | ||
| "strconv" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "k8s.io/apimachinery/pkg/api/resource" | ||
| "k8s.io/klog/v2" | ||
|
|
||
| "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache" | ||
| "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/poweraware/spec" | ||
| "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/qosaware/resource/cpu/assembler/headroomassembler" | ||
| "github.com/kubewharf/katalyst-core/pkg/config" | ||
| "github.com/kubewharf/katalyst-core/pkg/consts" | ||
| "github.com/kubewharf/katalyst-core/pkg/metaserver" | ||
| "github.com/kubewharf/katalyst-core/pkg/metrics" | ||
| "github.com/kubewharf/katalyst-core/pkg/util/general" | ||
| ) | ||
|
|
||
| func NewAssemblerDiscountDecorator(inner headroomassembler.HeadroomAssembler, | ||
| conf *config.Configuration, extraConf interface{}, | ||
| metaReader metacache.MetaReader, metaServer *metaserver.MetaServer, emitter metrics.MetricEmitter, | ||
| ) headroomassembler.HeadroomAssembler { | ||
| discounts := map[spec.PowerAlert]float64{ | ||
| spec.PowerAlertP1: conf.CPUHeadroomPowerDiscountP1, | ||
| spec.PowerAlertP2: conf.CPUHeadroomPowerDiscountP2, | ||
| spec.PowerAlertP3: conf.CPUHeadroomPowerDiscountP3, | ||
| } | ||
|
|
||
| return &discountDecorator{ | ||
| inner: inner, | ||
| discounter: &nodeAnnotationDiscountGetter{ | ||
| specFetcher: spec.NewFetcher(metaServer.NodeFetcher, conf.PowerAwarePluginConfiguration.AnnotationKeyPrefix), | ||
| conf: conf, | ||
| discounts: discounts, | ||
| }, | ||
| emitter: emitter, | ||
| } | ||
| } | ||
|
|
||
| type DiscountGetter interface { | ||
| GetDiscount() (float64, error) | ||
| } | ||
|
|
||
| type nodeAnnotationDiscountGetter struct { | ||
| specFetcher spec.SpecFetcher | ||
| conf *config.Configuration | ||
| discounts map[spec.PowerAlert]float64 | ||
| } | ||
|
|
||
| func getDiscountByLevel(level spec.PowerAlert, discounts map[spec.PowerAlert]float64) float64 { | ||
| if len(level) == 0 { | ||
| return 1.0 | ||
| } | ||
|
|
||
| if value, ok := discounts[level]; ok { | ||
| return value | ||
| } | ||
|
|
||
| switch level { | ||
| case spec.PowerAlertS0, spec.PowerAlertP0: | ||
| return 0.0 | ||
| default: | ||
| return 1.0 | ||
| } | ||
| } | ||
|
|
||
| func (d *nodeAnnotationDiscountGetter) GetDiscount() (float64, error) { | ||
| powerSpec, err := d.specFetcher.GetPowerSpec(context.Background()) | ||
| if err != nil { | ||
| return 1.0, errors.Wrap(err, "failed to get discount") | ||
| } | ||
|
|
||
| level := powerSpec.Alert | ||
| return getDiscountByLevel(level, d.discounts), nil | ||
| } | ||
|
|
||
| type discountDecorator struct { | ||
| inner headroomassembler.HeadroomAssembler | ||
| discounter DiscountGetter | ||
| emitter metrics.MetricEmitter | ||
| } | ||
|
|
||
| func applyDiscount(cpuQuantity resource.Quantity, discount float64) resource.Quantity { | ||
| milliValue := cpuQuantity.MilliValue() | ||
| newMilliValue := int64(float64(milliValue) * discount) | ||
| newQtyViaMilli := resource.NewMilliQuantity(newMilliValue, resource.DecimalSI) | ||
| return *newQtyViaMilli | ||
| } | ||
|
|
||
| func (d *discountDecorator) GetHeadroom() (resource.Quantity, map[int]resource.Quantity, error) { | ||
| currentDiscount, err := d.discounter.GetDiscount() | ||
| if err != nil { | ||
| general.Warningf("unable to determine current discount; apply no discount instead: %s", err) | ||
| return d.inner.GetHeadroom() | ||
| } | ||
| if currentDiscount >= 1.0 || currentDiscount < 0 { | ||
| general.Warningf("discount %f apply no discount", currentDiscount) | ||
| return d.inner.GetHeadroom() | ||
| } | ||
|
|
||
| headroom, numaHeadrooms, err := d.inner.GetHeadroom() | ||
| if err != nil { | ||
| return headroom, numaHeadrooms, err | ||
| } | ||
|
|
||
| discountHeadroom := applyDiscount(headroom, currentDiscount) | ||
| discountNumaHeadrooms := make(map[int]resource.Quantity) | ||
| for numa, numaHeadroom := range numaHeadrooms { | ||
| discountNumaHeadrooms[numa] = applyDiscount(numaHeadroom, currentDiscount) | ||
| } | ||
|
|
||
| if klog.V(6).Enabled() { | ||
| general.Infof("headroom discount=%f", currentDiscount) | ||
| general.Infof("headroom original %v, %v", headroom, numaHeadrooms) | ||
| general.Infof("headroom discount %v, %v", discountHeadroom, discountNumaHeadrooms) | ||
| } | ||
|
|
||
| // calc headroom loss and emit metrics | ||
| lossHeadroomMilli := headroom.MilliValue() - discountHeadroom.MilliValue() | ||
| _ = d.emitter.StoreInt64(consts.MetricsNodeCPUHeadroomLoss, lossHeadroomMilli, metrics.MetricTypeNameRaw) | ||
| for numa, numaHeadroom := range numaHeadrooms { | ||
| discountNumaHeadroom := discountNumaHeadrooms[numa] | ||
| lossNumaHeadroomMilli := numaHeadroom.MilliValue() - discountNumaHeadroom.MilliValue() | ||
| _ = d.emitter.StoreInt64(consts.MetricsNumaCPUHeadroomLoss, lossNumaHeadroomMilli, metrics.MetricTypeNameRaw, | ||
| metrics.MetricTag{Key: "numa", Val: strconv.Itoa(numa)}, | ||
| ) | ||
| } | ||
|
|
||
| return discountHeadroom, discountNumaHeadrooms, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.