Skip to content

Commit

Permalink
feat(advisor): add cpu usage ratio for pod and node custom metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-yuliang authored and csfldf committed Sep 29, 2024
1 parent 875d630 commit b808189
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ require (
)

replace (
github.com/kubewharf/katalyst-api => github.com/sun-yuliang/katalyst-api v0.0.0-20240913040056-a0a800e37eae
k8s.io/api => k8s.io/api v0.24.6
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.24.6
k8s.io/apimachinery => k8s.io/apimachinery v0.24.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubewharf/katalyst-api v0.5.1-0.20240911051124-d5c09c115a19 h1:5kz2u+tLNzW94ByL9yfsfkvE4N8n/r6C1TKDiFWK8rw=
github.com/kubewharf/katalyst-api v0.5.1-0.20240911051124-d5c09c115a19/go.mod h1:Y2IeIorxQamF2a3oa0+URztl5QCSty6Jj3zD83R8J9k=
github.com/kubewharf/kubelet v1.24.6-kubewharf.9 h1:jOTYZt7h/J7I8xQMKMUcJjKf5UFBv37jHWvNp5VRFGc=
github.com/kubewharf/kubelet v1.24.6-kubewharf.9/go.mod h1:MxbSZUx3wXztFneeelwWWlX7NAAStJ6expqq7gY2J3c=
github.com/kyoh86/exportloopref v0.1.7/go.mod h1:h1rDl2Kdj97+Kwh4gdz3ujE7XHmH51Q0lUiZ1z4NLj8=
Expand Down Expand Up @@ -878,6 +876,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/sun-yuliang/katalyst-api v0.0.0-20240913040056-a0a800e37eae h1:8CyPnE45qnXgVep/PeBy+pdMmpAEUNB7lrgGMER3VGg=
github.com/sun-yuliang/katalyst-api v0.0.0-20240913040056-a0a800e37eae/go.mod h1:Y2IeIorxQamF2a3oa0+URztl5QCSty6Jj3zD83R8J9k=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
github.com/tetafro/godot v0.4.8/go.mod h1:/7NLHhv08H1+8DNj0MElpAACw1ajsCuf3TKNQxA5S+0=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import (
// nodeRawMetricNameMapping maps the raw metricName (collected from agent.MetricsFetcher)
// to the standard metricName (used by custom-metric-api-server)
var nodeRawMetricNameMapping = map[string]string{
consts.MetricLoad1MinSystem: apimetricnode.CustomMetricNodeCPULoad1Min,
consts.MetricLoad1MinSystem: apimetricnode.CustomMetricNodeCPULoad1Min,
consts.MetricCPUUsageRatioSystem: apimetricnode.CustomMetricNodeCPUUsageRatio,

consts.MetricMemFreeSystem: apimetricnode.CustomMetricNodeMemoryFree,
consts.MetricMemAvailableSystem: apimetricnode.CustomMetricNodeMemoryAvailable,
Expand Down
7 changes: 4 additions & 3 deletions pkg/agent/sysadvisor/plugin/metric-emitter/syncer/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ const (
// podRawMetricNameMapping maps the raw metricName (collected from agent.MetricsFetcher)
// to the standard metricName (used by custom-metric-api-server)
var podRawMetricNameMapping = map[string]string{
consts.MetricLoad1MinContainer: apimetricpod.CustomMetricPodCPULoad1Min,
consts.MetricCPUUsageContainer: apimetricpod.CustomMetricPodCPUUsage,
consts.MetricCPUCPIContainer: apimetricpod.CustomMetricPodCPUCPI,
consts.MetricLoad1MinContainer: apimetricpod.CustomMetricPodCPULoad1Min,
consts.MetricCPUUsageContainer: apimetricpod.CustomMetricPodCPUUsage,
consts.MetricCPUUsageRatioContainer: apimetricpod.CustomMetricPodCPUUsageRatio,
consts.MetricCPUCPIContainer: apimetricpod.CustomMetricPodCPUCPI,

consts.MetricMemRssContainer: apimetricpod.CustomMetricPodMemoryRSS,
consts.MetricMemUsageContainer: apimetricpod.CustomMetricPodMemoryUsage,
Expand Down
11 changes: 7 additions & 4 deletions pkg/consts/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (

// System compute metrics
const (
MetricCPUUsageRatioSystem = "cpu.usage.ratio.system"

MetricLoad1MinSystem = "cpu.load.1min.system"
MetricLoad5MinSystem = "cpu.load.5min.system"
MetricLoad15MinSystem = "cpu.load.15min.system"
Expand Down Expand Up @@ -170,10 +172,11 @@ const (

// container cpu metrics
const (
MetricCPULimitContainer = "cpu.limit.container"
MetricCPUUsageContainer = "cpu.usage.container"
MetricCPUUsageUserContainer = "cpu.usage.user.container"
MetricCPUUsageSysContainer = "cpu.usage.sys.container"
MetricCPULimitContainer = "cpu.limit.container"
MetricCPUUsageContainer = "cpu.usage.container"
MetricCPUUsageUserContainer = "cpu.usage.user.container"
MetricCPUUsageSysContainer = "cpu.usage.sys.container"
MetricCPUUsageRatioContainer = "cpu.usage.ratio.container"

MetricCPUShareContainer = "cpu.share.container"
MetricCPUQuotaContainer = "cpu.quota.container"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func (m *MalachiteMetricsProvisioner) processSystemComputeData(systemComputeData
// todo, currently we only get a unified data for the whole system compute data
updateTime := time.Unix(systemComputeData.UpdateTime, 0)

globalCPU := systemComputeData.GlobalCPU
m.metricStore.SetNodeMetric(consts.MetricCPUUsageRatioSystem,
utilmetric.MetricData{Value: globalCPU.CPUUsage / 100, Time: &updateTime})

load := systemComputeData.Load
m.metricStore.SetNodeMetric(consts.MetricLoad1MinSystem,
utilmetric.MetricData{Value: load.One, Time: &updateTime})
Expand Down Expand Up @@ -742,6 +746,8 @@ func (m *MalachiteMetricsProvisioner) processContainerCPUData(podUID, containerN
utilmetric.MetricData{Value: cpu.CPUUserUsageRatio, Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUUsageSysContainer,
utilmetric.MetricData{Value: cpu.CPUSysUsageRatio, Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUUsageRatioContainer,
utilmetric.MetricData{Value: cpu.CPUUsageRatio / (float64(cpu.CfsQuotaUs) / float64(cpu.CfsPeriodUs)), Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUShareContainer,
utilmetric.MetricData{Value: float64(cpu.CPUShares), Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUQuotaContainer,
Expand Down Expand Up @@ -811,6 +817,8 @@ func (m *MalachiteMetricsProvisioner) processContainerCPUData(podUID, containerN
utilmetric.MetricData{Value: cpu.CPUUserUsageRatio, Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUUsageSysContainer,
utilmetric.MetricData{Value: cpu.CPUSysUsageRatio, Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUUsageRatioContainer,
utilmetric.MetricData{Value: cpu.CPUUsageRatio / (float64(cpu.Max) / float64(cpu.MaxPeriod)), Time: &updateTime})

m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUNrRunnableContainer,
utilmetric.MetricData{Value: float64(cpu.TaskNrRunning), Time: &updateTime})
Expand Down

0 comments on commit b808189

Please sign in to comment.