-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(qrm): adding support to cpu.weight
Signed-off-by: Robin Lu <[email protected]>
- Loading branch information
1 parent
3269ac6
commit 0a5790f
Showing
8 changed files
with
361 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,21 @@ | ||
/* | ||
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 cpuweight | ||
|
||
const ( | ||
metricNameCPUWeight = "async_handler_cpu_weight" | ||
) |
93 changes: 93 additions & 0 deletions
93
pkg/agent/qrm-plugins/cpu/handlers/cpuweight/cpuweight_linux.go
This file contains 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,93 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
/* | ||
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 cpuweight | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/util/errors" | ||
|
||
cpuconsts "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/consts" | ||
coreconfig "github.com/kubewharf/katalyst-core/pkg/config" | ||
dynamicconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic" | ||
"github.com/kubewharf/katalyst-core/pkg/metaserver" | ||
"github.com/kubewharf/katalyst-core/pkg/metrics" | ||
cgroupcm "github.com/kubewharf/katalyst-core/pkg/util/cgroup/common" | ||
cgroupmgr "github.com/kubewharf/katalyst-core/pkg/util/cgroup/manager" | ||
"github.com/kubewharf/katalyst-core/pkg/util/general" | ||
) | ||
|
||
func applyCPUWeightCgroupLevelConfig(conf *coreconfig.Configuration, emitter metrics.MetricEmitter) { | ||
if conf.CPUWeightConfigFile == "" { | ||
general.Errorf("CPUWeightConfigFile isn't configured") | ||
return | ||
} | ||
|
||
cpuWightCgroupLevelConfigs := make(map[string]uint64) | ||
err := general.LoadJsonConfig(conf.CPUWeightConfigFile, &cpuWightCgroupLevelConfigs) | ||
if err != nil { | ||
general.Errorf("load CPUWeightCgroupLevelConfig failed with error: %v", err) | ||
return | ||
} | ||
|
||
for relativeCgPath, weight := range cpuWightCgroupLevelConfigs { | ||
err := cgroupmgr.ApplyCPUWithRelativePath(relativeCgPath, &cgroupcm.CPUData{ | ||
Shares: weight, | ||
}) | ||
if err != nil { | ||
general.Errorf("ApplyCPUWeightWithRelativePath in relativeCgPath: %s failed with error: %v", | ||
relativeCgPath, err) | ||
} else { | ||
general.Infof("ApplyCPUWeightWithRelativePath weight: %d in relativeCgPath: %s successfully", | ||
weight, relativeCgPath) | ||
_ = emitter.StoreInt64(metricNameCPUWeight, int64(weight), metrics.MetricTypeNameRaw, | ||
metrics.ConvertMapToTags(map[string]string{ | ||
"cgPath": relativeCgPath, | ||
})...) | ||
|
||
} | ||
} | ||
} | ||
|
||
func SetCPUWeight(conf *coreconfig.Configuration, | ||
_ interface{}, _ *dynamicconfig.DynamicAgentConfiguration, | ||
emitter metrics.MetricEmitter, metaServer *metaserver.MetaServer, | ||
) { | ||
general.Infof("called") | ||
|
||
var errList []error | ||
defer func() { | ||
_ = general.UpdateHealthzStateByError(cpuconsts.SetCPUWeight, errors.NewAggregate(errList)) | ||
}() | ||
|
||
if conf == nil || emitter == nil || metaServer == nil { | ||
general.Errorf("nil input, conf:%v, emitter:%v, metaServer:%v", conf, emitter, metaServer) | ||
return | ||
} | ||
|
||
// SettingCPUWeight featuregate. | ||
if !conf.EnableSettingCPUWeight { | ||
general.Infof("SetCPUWeight disabled") | ||
return | ||
} | ||
|
||
// checking cgroup-level cpu.weight configuration. | ||
if len(conf.CPUWeightConfigFile) > 0 { | ||
applyCPUWeightCgroupLevelConfig(conf, emitter) | ||
} | ||
} |
180 changes: 180 additions & 0 deletions
180
pkg/agent/qrm-plugins/cpu/handlers/cpuweight/cpuweight_linux_test.go
This file contains 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,180 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
/* | ||
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 cpuweight | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
|
||
coreconfig "github.com/kubewharf/katalyst-core/pkg/config" | ||
"github.com/kubewharf/katalyst-core/pkg/config/agent" | ||
configagent "github.com/kubewharf/katalyst-core/pkg/config/agent" | ||
dynamicconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic" | ||
"github.com/kubewharf/katalyst-core/pkg/config/agent/qrm" | ||
"github.com/kubewharf/katalyst-core/pkg/metaserver" | ||
metaagent "github.com/kubewharf/katalyst-core/pkg/metaserver/agent" | ||
"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric" | ||
"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod" | ||
"github.com/kubewharf/katalyst-core/pkg/metrics" | ||
"github.com/kubewharf/katalyst-core/pkg/util/machine" | ||
) | ||
|
||
func makeMetaServer() (*metaserver.MetaServer, error) { | ||
server := &metaserver.MetaServer{ | ||
MetaAgent: &metaagent.MetaAgent{}, | ||
} | ||
|
||
cpuTopology, err := machine.GenerateDummyCPUTopology(16, 1, 2) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
server.KatalystMachineInfo = &machine.KatalystMachineInfo{ | ||
CPUTopology: cpuTopology, | ||
} | ||
server.MetricsFetcher = metric.NewFakeMetricsFetcher(metrics.DummyMetrics{}) | ||
return server, nil | ||
} | ||
|
||
func TestSetCPUWeight(t *testing.T) { | ||
t.Parallel() | ||
|
||
SetCPUWeight(nil, | ||
nil, &dynamicconfig.DynamicAgentConfiguration{}, nil, nil) | ||
|
||
SetCPUWeight(&coreconfig.Configuration{ | ||
AgentConfiguration: &agent.AgentConfiguration{ | ||
StaticAgentConfiguration: &configagent.StaticAgentConfiguration{ | ||
QRMPluginsConfiguration: &qrm.QRMPluginsConfiguration{ | ||
CPUQRMPluginConfig: &qrm.CPUQRMPluginConfig{ | ||
CPUWeightOptions: qrm.CPUWeightOptions{ | ||
EnableSettingCPUWeight: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, nil, &dynamicconfig.DynamicAgentConfiguration{}, nil, nil) | ||
|
||
SetCPUWeight(&coreconfig.Configuration{ | ||
AgentConfiguration: &agent.AgentConfiguration{ | ||
StaticAgentConfiguration: &configagent.StaticAgentConfiguration{ | ||
QRMPluginsConfiguration: &qrm.QRMPluginsConfiguration{ | ||
CPUQRMPluginConfig: &qrm.CPUQRMPluginConfig{ | ||
CPUWeightOptions: qrm.CPUWeightOptions{ | ||
EnableSettingCPUWeight: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, nil, &dynamicconfig.DynamicAgentConfiguration{}, nil, nil) | ||
|
||
metaServer, err := makeMetaServer() | ||
assert.NoError(t, err) | ||
metaServer.PodFetcher = &pod.PodFetcherStub{PodList: []*v1.Pod{}} | ||
SetCPUWeight(&coreconfig.Configuration{ | ||
AgentConfiguration: &agent.AgentConfiguration{ | ||
StaticAgentConfiguration: &configagent.StaticAgentConfiguration{ | ||
QRMPluginsConfiguration: &qrm.QRMPluginsConfiguration{ | ||
CPUQRMPluginConfig: &qrm.CPUQRMPluginConfig{ | ||
CPUWeightOptions: qrm.CPUWeightOptions{ | ||
EnableSettingCPUWeight: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, metrics.DummyMetrics{}, &dynamicconfig.DynamicAgentConfiguration{}, metrics.DummyMetrics{}, metaServer) | ||
|
||
SetCPUWeight(&coreconfig.Configuration{ | ||
AgentConfiguration: &agent.AgentConfiguration{ | ||
StaticAgentConfiguration: &configagent.StaticAgentConfiguration{ | ||
QRMPluginsConfiguration: &qrm.QRMPluginsConfiguration{ | ||
CPUQRMPluginConfig: &qrm.CPUQRMPluginConfig{ | ||
CPUWeightOptions: qrm.CPUWeightOptions{ | ||
EnableSettingCPUWeight: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, nil, &dynamicconfig.DynamicAgentConfiguration{}, metrics.DummyMetrics{}, metaServer) | ||
|
||
applyCPUWeightCgroupLevelConfig(&coreconfig.Configuration{ | ||
AgentConfiguration: &agent.AgentConfiguration{ | ||
StaticAgentConfiguration: &configagent.StaticAgentConfiguration{ | ||
QRMPluginsConfiguration: &qrm.QRMPluginsConfiguration{ | ||
CPUQRMPluginConfig: &qrm.CPUQRMPluginConfig{ | ||
CPUWeightOptions: qrm.CPUWeightOptions{ | ||
EnableSettingCPUWeight: true, | ||
CPUWeightConfigFile: "fake", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, metrics.DummyMetrics{}) | ||
|
||
// Create a temporary file | ||
tempFile, err := ioutil.TempFile("", "test.json") | ||
if err != nil { | ||
fmt.Println("Error creating temporary file:", err) | ||
return | ||
} | ||
defer os.Remove(tempFile.Name()) // Defer removing the temporary file | ||
|
||
// Write the JSON content to the temporary file | ||
jsonContent := `{ | ||
"fake": 200, | ||
"fake2": 400 | ||
}` | ||
|
||
if _, err := tempFile.WriteString(jsonContent); err != nil { | ||
fmt.Println("Error writing to temporary file:", err) | ||
return | ||
} | ||
absPathCgroup, err := filepath.Abs(tempFile.Name()) | ||
if err != nil { | ||
fmt.Println("Error obtaining absolute path:", err) | ||
return | ||
} | ||
|
||
applyCPUWeightCgroupLevelConfig(&coreconfig.Configuration{ | ||
AgentConfiguration: &agent.AgentConfiguration{ | ||
StaticAgentConfiguration: &configagent.StaticAgentConfiguration{ | ||
QRMPluginsConfiguration: &qrm.QRMPluginsConfiguration{ | ||
CPUQRMPluginConfig: &qrm.CPUQRMPluginConfig{ | ||
CPUWeightOptions: qrm.CPUWeightOptions{ | ||
EnableSettingCPUWeight: true, | ||
CPUWeightConfigFile: absPathCgroup, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, metrics.DummyMetrics{}) | ||
} |
Oops, something went wrong.