Skip to content

Commit

Permalink
memory headroom canonical policy support enable buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
luomingmeng committed May 19, 2023
1 parent e1ac91b commit b30f177
Show file tree
Hide file tree
Showing 12 changed files with 1,020 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 headroom

import (
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/errors"

"github.com/kubewharf/katalyst-core/pkg/config/agent/sysadvisor/qosaware/resource/memory/headroom"
)

type MemoryHeadroomPolicyOptions struct {
MemoryPolicyCanonicalOptions *MemoryPolicyCanonicalOptions
}

func NewMemoryHeadroomPolicyOptions() *MemoryHeadroomPolicyOptions {
return &MemoryHeadroomPolicyOptions{
MemoryPolicyCanonicalOptions: NewMemoryPolicyCanonicalOptions(),
}
}

func (o *MemoryHeadroomPolicyOptions) AddFlags(fs *pflag.FlagSet) {
o.MemoryPolicyCanonicalOptions.AddFlags(fs)
}

func (o *MemoryHeadroomPolicyOptions) ApplyTo(c *headroom.MemoryHeadroomPolicyConfiguration) error {
var errList []error
errList = append(errList, o.MemoryPolicyCanonicalOptions.ApplyTo(c.MemoryPolicyCanonicalConfiguration))
return errors.NewAggregate(errList)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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 headroom

import (
"github.com/spf13/pflag"

"github.com/kubewharf/katalyst-core/pkg/config/agent/sysadvisor/qosaware/resource/memory/headroom"
)

const (
defaultEnabled = false
defaultFreeBasedRatio = 0.6
defaultStaticBasedCapacity = 20 << 30 // 20GB

defaultCacheBaseRatio = 0
defaultCPUMemRatioLowerBound = 1. / 6.
defaultCPUMemRatioUpperBound = 1. / 3.5
)

type MemoryPolicyCanonicalOptions struct {
*MemoryUtilBasedOptions
}

type MemoryUtilBasedOptions struct {
Enabled bool
FreeBasedRatio float64
CacheBasedRatio float64
StaticBasedCapacity float64

CPUMemRatioLowerBound float64
CPUMemRatioUpperBound float64
}

func NewMemoryPolicyCanonicalOptions() *MemoryPolicyCanonicalOptions {
return &MemoryPolicyCanonicalOptions{
MemoryUtilBasedOptions: &MemoryUtilBasedOptions{
Enabled: defaultEnabled,
FreeBasedRatio: defaultFreeBasedRatio,
StaticBasedCapacity: defaultStaticBasedCapacity,
CacheBasedRatio: defaultCacheBaseRatio,
CPUMemRatioLowerBound: defaultCPUMemRatioLowerBound,
CPUMemRatioUpperBound: defaultCPUMemRatioUpperBound,
},
}
}

func (o *MemoryPolicyCanonicalOptions) AddFlags(fs *pflag.FlagSet) {
fs.BoolVar(&o.Enabled, "memory-headroom-policy-canonical-util-based-enabled", o.Enabled,
"the flag to enable memory buffer")
fs.Float64Var(&o.FreeBasedRatio, "memory-headroom-policy-canonical-free-based-ratio", o.FreeBasedRatio,
"the estimation of free memory utilization, which can be used as system buffer to oversold memory")
fs.Float64Var(&o.StaticBasedCapacity, "memory-headroom-policy-canonical-static-based-capacity", o.StaticBasedCapacity,
"the static oversold memory size by bytes")
fs.Float64Var(&o.CacheBasedRatio, "memory-headroom-policy-canonical-cache-based-ratio", o.CacheBasedRatio,
"the rate of cache oversold, 0 means disable cache oversold")
fs.Float64Var(&o.CPUMemRatioLowerBound, "memory-headroom-policy-canonical-cpu-mem-ratio-lower-bound", o.CPUMemRatioLowerBound,
"the upper bound of memory to cpu ratio for enabling cache oversold")
fs.Float64Var(&o.CPUMemRatioUpperBound, "memory-headroom-policy-canonical-cpu-mem-ratio-upper-bound", o.CPUMemRatioUpperBound,
"the lower bound of memory to cpu ratio for enabling cache oversold")
}

func (o *MemoryPolicyCanonicalOptions) ApplyTo(c *headroom.MemoryPolicyCanonicalConfiguration) error {
c.Enabled = o.Enabled
c.FreeBasedRatio = o.FreeBasedRatio
c.StaticBasedCapacity = o.StaticBasedCapacity
c.CacheBasedRatio = o.CacheBasedRatio
c.CPUMemRatioLowerBound = o.CPUMemRatioLowerBound
c.CPUMemRatioUpperBound = o.CPUMemRatioUpperBound
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,42 @@ package memory

import (
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/errors"

"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/options/sysadvisor/qosaware/resource/memory/headroom"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/types"
"github.com/kubewharf/katalyst-core/pkg/config/agent/sysadvisor/qosaware/resource/memory"
)

// MemoryAdvisorOptions holds the configurations for memory advisor in qos aware plugin
type MemoryAdvisorOptions struct {
MemoryHeadroomPolicyPriority []string

*headroom.MemoryHeadroomPolicyOptions
}

// NewMemoryAdvisorOptions creates a new Options with a default config
func NewMemoryAdvisorOptions() *MemoryAdvisorOptions {
return &MemoryAdvisorOptions{
MemoryHeadroomPolicyPriority: []string{string(types.MemoryHeadroomPolicyCanonical)},
MemoryHeadroomPolicyOptions: headroom.NewMemoryHeadroomPolicyOptions(),
}
}

// AddFlags adds flags to the specified FlagSet.
func (o *MemoryAdvisorOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(&o.MemoryHeadroomPolicyPriority, "memory-headroom-policy-priority", o.MemoryHeadroomPolicyPriority,
"policy memory advisor to estimate resource headroom, sorted by priority descending order, should be formatted as 'policy1,policy2'")
o.MemoryHeadroomPolicyOptions.AddFlags(fs)
}

// ApplyTo fills up config with options
func (o *MemoryAdvisorOptions) ApplyTo(c *memory.MemoryAdvisorConfiguration) error {
for _, policy := range o.MemoryHeadroomPolicyPriority {
c.MemoryHeadroomPolicies = append(c.MemoryHeadroomPolicies, types.MemoryHeadroomPolicyName(policy))
}
return nil

var errList []error
errList = append(errList, o.MemoryHeadroomPolicyOptions.ApplyTo(c.MemoryHeadroomPolicyConfiguration))
return errors.NewAggregate(errList)
}
12 changes: 0 additions & 12 deletions pkg/agent/resourcemanager/reporter/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,6 @@ func Test_managerImpl_convertReportFieldsIfNeeded(t *testing.T) {
Value: []byte("Value_a"),
},
},
testGroupVersionKindSecond: {
{
FieldType: v1alpha1.FieldType_Spec,
FieldName: "fieldName_b",
Value: []byte("Value_b"),
},
},
},
},
want: map[v1.GroupVersionKind][]*v1alpha1.ReportField{
Expand All @@ -290,11 +283,6 @@ func Test_managerImpl_convertReportFieldsIfNeeded(t *testing.T) {
FieldName: "fieldName_a",
Value: []byte("Value_a"),
},
{
FieldType: v1alpha1.FieldType_Spec,
FieldName: "fieldName_b",
Value: []byte("Value_b"),
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package provisionpolicy

import (
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"

"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (p *PolicyCanonical) estimationCPUUsage() (cpuEstimation float64, container
continue
}

containerEstimation, err := helper.EstimateContainerResourceUsage(ci, v1.ResourceCPU, p.metaReader, p.essentials.EnableReclaim)
containerEstimation, err := helper.EstimateContainerCPUUsage(ci, p.metaReader, p.essentials.EnableReclaim)
if err != nil {
return 0, 0, err
}
Expand Down
Loading

0 comments on commit b30f177

Please sign in to comment.